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

Supported events #7281

Merged
merged 14 commits into from
Dec 29, 2024
13 changes: 7 additions & 6 deletions src/main/java/ch/njol/skript/expressions/ExprExplodedBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ public class ExprExplodedBlocks extends SimpleExpression<Block> {
static {
Skript.registerExpression(ExprExplodedBlocks.class, Block.class, ExpressionType.COMBINED, "[the] exploded blocks");
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
if (!getParser().isCurrentEvent(EntityExplodeEvent.class)) {
Skript.error("Exploded blocks can only be retrieved from an explode event.");
return false;
}
return true;
}


@Override
public Class<? extends Event>[] supportedEvents() {
return CollectionUtils.array(EntityExplodeEvent.class);
}

@Nullable
@Override
protected Block[] get(Event e) {
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/ch/njol/skript/lang/SkriptParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,14 @@
import ch.njol.util.StringUtils;
import ch.njol.util.coll.CollectionUtils;
import com.google.common.primitives.Booleans;
import org.bukkit.event.Event;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.lang.script.ScriptWarning;

import java.util.ArrayList;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
Expand Down Expand Up @@ -225,7 +220,21 @@ public boolean hasTag(String tag) {
}
}
T element = info.getElementClass().newInstance();
if (element.init(parseResult.exprs, patternIndex, getParser().getHasDelayBefore(), parseResult)) {

Class<? extends Event>[] supportedEvents = element.supportedEvents();
if (supportedEvents.length > 0 && !getParser().isCurrentEvent(supportedEvents)) {
StringJoiner joiner = new StringJoiner(", ", "the ", "");

Arrays.stream(supportedEvents)
.map(it -> it.getSimpleName().replaceAll("([A-Z])", " $1").toLowerCase().trim())
.forEachOrdered(joiner::add);

Skript.error("'" + parseResult.expr + "' can only be used in " + joiner);
continue;
}

boolean success = element.init(parseResult.exprs, patternIndex, getParser().getHasDelayBefore(), parseResult);
if (success) {
log.printLog();
return element;
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/ch/njol/skript/lang/SyntaxElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;

/**
* Represents a general part of the syntax.
Expand All @@ -29,4 +30,20 @@ default ParserInstance getParser() {
return ParserInstance.get();
}

/**
* Returns all supported events for this syntax element. By default, all events are accepted.
* <p>
* Before {@link #init(Expression[], int, Kleenean, ParseResult)} is called, checks
* to see if the current event is supported by this syntax element.
* If it is not, an error will be printed and the syntax element will not be initialised.
* </p>
*
* @return All supported event classes.
* @see ch.njol.util.coll.CollectionUtils#array(Object[])
*/
default Class<? extends Event>[] supportedEvents() {
//noinspection unchecked
return (Class<? extends Event>[]) new Class[0];
}

}
5 changes: 5 additions & 0 deletions src/test/skript/tests/misc/supported events.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test "supported events":
parse:
set {_x} to the exploded blocks

assert last parse logs contain "'the exploded blocks' can only be used in the entity explode event" with "supported events message did not get sent correctly"
Loading