Skip to content

StructUsing Multiple #7853

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

Open
wants to merge 4 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions src/main/java/ch/njol/skript/structures/StructUsing.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.util.StringUtils;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.entry.EntryContainer;
import org.skriptlang.skript.lang.experiment.Experiment;
import org.skriptlang.skript.lang.structure.Structure;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Name("Using Experimental Feature")
@Description({
"Place at the top of a script file to enable an optional experimental feature.",
Expand All @@ -22,38 +28,49 @@
})
@Examples({
"using 1.21",
"using the experiment my-cool-addon-feature"
"using the experiment my-cool-addon-feature",
"using feature_1, feature_2"
})
@Since("2.9.0")
@Since("2.9.0, INSERT VERSION (multiple in one)")
public class StructUsing extends Structure {

public static final Priority PRIORITY = new Priority(15);

static {
Skript.registerSimpleStructure(StructUsing.class, "using [[the] experiment] <.+>");
Skript.registerSimpleStructure(StructUsing.class, "using [[the] experiment[s]] <.+>");
}

private Experiment experiment;
private Experiment[] experiments;
private String experimentCodeNames;

@Override
public boolean init(Literal<?> @NotNull [] arguments, int pattern, ParseResult result, @Nullable EntryContainer container) {
this.enableExperiment(result.regexes.get(0).group());
return true;
}

private void enableExperiment(String name) {
this.experiment = Skript.experiments().find(name.trim());
switch (experiment.phase()) {
case MAINSTREAM:
Skript.warning("The experimental feature '" + name + "' is now included by default and is no longer required.");
break;
case DEPRECATED:
Skript.warning("The experimental feature '" + name + "' is deprecated and may be removed in future versions.");
break;
case UNKNOWN:
Skript.warning("The experimental feature '" + name + "' was not found.");
private void enableExperiment(String pattern) {
String[] names = pattern.split(",");
List<Experiment> experiments = new ArrayList<>();
ParserInstance parser = getParser();
for (String name : names) {
String trimmed = name.trim();
if (trimmed.isEmpty())
continue;
Experiment experiment = Skript.experiments().find(trimmed);
experiments.add(experiment);
switch (experiment.phase()) {
case MAINSTREAM ->
Skript.warning("The experimental feature '" + name + "' is now included by default and is no longer required.");
case DEPRECATED ->
Skript.warning("The experimental feature '" + name + "' is deprecated and may be removed in future versions.");
case UNKNOWN ->
Skript.warning("The experimental feature '" + name + "' was not found.");
}
parser.addExperiment(experiment);
}
this.getParser().addExperiment(experiment);
this.experiments = experiments.toArray(Experiment[]::new);
experimentCodeNames = StringUtils.join(Arrays.stream(this.experiments).map(Experiment::codeName).toArray(), ", ");
}

@Override
Expand All @@ -68,7 +85,7 @@ public Priority getPriority() {

@Override
public String toString(@Nullable Event event, boolean debug) {
return "using " + experiment.codeName();
return "using " + experimentCodeNames;
}

}
3 changes: 1 addition & 2 deletions src/test/skript/tests/syntaxes/structures/StructUsing.sk
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ test "using before declaration":
assert the current script is using "example feature" with "using feature failed"
assert experimental only is true with "feature-conditional syntax failed"

using example feature
using fizz buzz
using example feature, fizz buzz
using deprecated feature

test "using":
Expand Down