Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch sections (experimental feature). #6572

Draft
wants to merge 39 commits into
base: dev/feature
Choose a base branch
from

Conversation

Moderocky
Copy link
Member

@Moderocky Moderocky commented Apr 15, 2024

Summary

Adds a (switch|check) %objects% section.

As it is experimental, this requires the script to be using switch sections.

Note

I've never been a huge fan of switch-sections in general, and I've voted against their inclusion every time the issue has come up: everything you can do in a switch block you can do with an if/else tree, and I think they're quite unintuitive to non-programmers.
Unfortunately, it's practically impossible to improve performance with a switch section over simple if/else because of the fluidity of Skript conditions.

That said, I've created this as a draft PR so that, if they're wanted, I can at least make sure they're done in a sensible way.

Rules

We have consistently tried to make sure every line of Skript code is interpretable on its own, and fairly grammatically sound. This is why we have always avoided expression-only lines.
A design like case 5: or is true: will probably break this rule (as well as being nightmarish to implement with current condition patterns) so I've deliberately avoided it.

Switch Section Syntax

The switch section (switch|check) %objects% takes in an array of values and checks each against a set of switch case conditions. These cases are run (depending on the mode) if the condition passes.

Only switch cases may be used inside switch sections.

Switch Case Syntax

Switch cases look like if-statements: [case|if] %condition%

Switch Subject Syntax

A special expression (this|it) can be used to refer to the current subject of a switch section inside its cases.
This is used to prevent multiple evaluation of an expression, but it's not necessary.

Switch Design

A simple switch takes in inputs and, for each input, checks it against every case condition. If a condition matches, the case section will be run.

Example

switch player's tool:
  it is unbreakable:
    broadcast "your tool is unbreakable"
  it is a diamond shovel:
    broadcast "your tool is a diamond shovel"
  it is a shovel:
    broadcast "your tool is a shovel"

example results:

unbreakable dirt:
  your tool is unbreakable
diamond shovel:
  your tool is a diamond shovel
  your tool is a shovel
unbreakable diamond shovel:
  your tool is unbreakable
  your tool is a diamond shovel
  your tool is a shovel
shovel:
  your tool is a shovel

This means that all matching conditions will run.
(Functionally equivalent to if X, if Y, if Z.)
In the example below, input 3 will print both cases, because 3 is 3 and also a number.

switch {_number}:
    if it is 3:
        broadcast "the number was 3!"
    if it is a number:
        broadcast "the number was a number!"

Since the switch section is technically a form of loop, the exit and continue effects are available within it, so you can break from a switch when you don't want anything else to run.

Switch cases can be stacked together. Stacking multiple cases requires all conditions to run or the section won't be run.

Example

switch player's tool:
  it is a shovel
  it is unbreakable:
    broadcast "your tool is unbreakable"
  it is a diamond shovel:
    broadcast "your tool is a diamond shovel"

example results:

unbreakable dirt:
  none :(
diamond shovel:
  your tool is a diamond shovel
unbreakable diamond shovel:
  your tool is unbreakable
  your tool is a diamond shovel
shovel:
  none :(

Switch Modes

For advanced users only, alternative switch modes are provided: STRICT and FALL-THROUGH.
These are currently locked behind the annotations @strict switch mode and @fall-through switch mode, since they are quite unintuitive and difficult to explain.

Strict

A strict switch section will run ONLY the first case matched for each input.
(Functionally equivalent to if X, else if Y, else if Z.)

@strict switch mode
switch player's tool:
  it exists
  it is unbreakable:
    broadcast "your tool is unbreakable"
  it is a diamond shovel:
    broadcast "your tool is a diamond shovel"
  it is a shovel:
    broadcast "your tool is a shovel"

example results:

unbreakable dirt:
  your tool is unbreakable
diamond shovel:
  your tool is a diamond shovel
unbreakable diamond shovel:
  your tool is unbreakable
shovel:
  your tool is a shovel

Fall-Through

This mode is designed to simulate Java switch cases. It's quite unintuitive and easy to make a mistake if you don't understand what it does, which is why Sovde suggested I gate it behind an annotation.

A fall-through switch will run the first case matched, and then run every case after that, no matter whether it matches or not.

@fall-through switch mode
switch player's tool:
  it exists
  it is unbreakable:
    broadcast "your tool is unbreakable"
  it is a diamond shovel:
    broadcast "your tool is a diamond shovel"
  it is a shovel:
    broadcast "your tool is a shovel"

example results:

unbreakable dirt:
  your tool is unbreakable
  your tool is a diamond shovel
  your tool is a shovel
diamond shovel:
  your tool is a diamond shovel
  your tool is a shovel
unbreakable diamond shovel:
  your tool is unbreakable
  your tool is a diamond shovel
  your tool is a shovel
shovel:
  your tool is a shovel

This is a really weird way of checking, but it does allow for clever avoidance of code duplication.


Target Minecraft Versions: any
Requirements: none
Related Issues: #4000, requires #6552, requires #6571, transitively requires #6551

APickledWalrus and others added 30 commits April 10, 2024 21:38
Version thingy.

Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com>
Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com>
@Moderocky Moderocky added the feature Pull request adding a new feature. label Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Pull request adding a new feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants