From 28c8e523084a3cbe81ee991aba5e931abe601ad1 Mon Sep 17 00:00:00 2001 From: Efnilite Date: Thu, 19 Dec 2024 11:47:15 +0100 Subject: [PATCH 1/9] init commit --- .../expressions/ExprExplodedBlocks.java | 13 ++++++----- .../ch/njol/skript/lang/SkriptParser.java | 23 ++++++++++++------- .../ch/njol/skript/lang/SyntaxElement.java | 13 +++++++++++ .../skript/tests/misc/supported events.sk | 5 ++++ 4 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 src/test/skript/tests/misc/supported events.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprExplodedBlocks.java b/src/main/java/ch/njol/skript/expressions/ExprExplodedBlocks.java index aac5d1bd2a3..bbae3b3c04b 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprExplodedBlocks.java +++ b/src/main/java/ch/njol/skript/expressions/ExprExplodedBlocks.java @@ -66,16 +66,17 @@ public class ExprExplodedBlocks extends SimpleExpression { 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[] supportedEvents() { + return CollectionUtils.array(EntityExplodeEvent.class); + } + @Nullable @Override protected Block[] get(Event e) { diff --git a/src/main/java/ch/njol/skript/lang/SkriptParser.java b/src/main/java/ch/njol/skript/lang/SkriptParser.java index e713edeb750..8c283fc7c53 100644 --- a/src/main/java/ch/njol/skript/lang/SkriptParser.java +++ b/src/main/java/ch/njol/skript/lang/SkriptParser.java @@ -49,19 +49,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; @@ -243,7 +238,19 @@ public boolean hasTag(String tag) { } } T element = info.getElementClass().newInstance(); - if (element.init(parseResult.exprs, patternIndex, getParser().getHasDelayBefore(), parseResult)) { + + Class[] supportedEvents = element.supportedEvents(); + if (supportedEvents.length > 0 && !getParser().isCurrentEvent(supportedEvents)) { + Object[] names = Arrays.stream(supportedEvents) + .map(it -> it.getSimpleName().replaceAll("([A-Z])", " $1").toLowerCase().trim()) + .toArray(); + + Skript.error("'" + parseResult.expr + "' can only be used in the " + StringUtils.join(names, ", ")); + continue; + } + + boolean success = element.init(parseResult.exprs, patternIndex, getParser().getHasDelayBefore(), parseResult); + if (success) { log.printLog(); return element; } diff --git a/src/main/java/ch/njol/skript/lang/SyntaxElement.java b/src/main/java/ch/njol/skript/lang/SyntaxElement.java index 5ab3589733c..0fadac9d773 100644 --- a/src/main/java/ch/njol/skript/lang/SyntaxElement.java +++ b/src/main/java/ch/njol/skript/lang/SyntaxElement.java @@ -21,6 +21,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. @@ -47,4 +48,16 @@ default ParserInstance getParser() { return ParserInstance.get(); } + /** + * Returns all supported events for this syntax element. + *

+ * If the event is + *

+ * @return All supported + */ + default Class[] supportedEvents() { + //noinspection unchecked + return (Class[]) new Class[0]; + } + } diff --git a/src/test/skript/tests/misc/supported events.sk b/src/test/skript/tests/misc/supported events.sk new file mode 100644 index 00000000000..ff77ea57b5f --- /dev/null +++ b/src/test/skript/tests/misc/supported events.sk @@ -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" \ No newline at end of file From ec4b4987727e22e1fc3a390468b976cadf81d4f2 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Thu, 19 Dec 2024 14:25:44 +0100 Subject: [PATCH 2/9] update error message --- src/main/java/ch/njol/skript/lang/SkriptParser.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/njol/skript/lang/SkriptParser.java b/src/main/java/ch/njol/skript/lang/SkriptParser.java index 8c283fc7c53..a26b4c72b49 100644 --- a/src/main/java/ch/njol/skript/lang/SkriptParser.java +++ b/src/main/java/ch/njol/skript/lang/SkriptParser.java @@ -240,12 +240,14 @@ public boolean hasTag(String tag) { T element = info.getElementClass().newInstance(); Class[] supportedEvents = element.supportedEvents(); - if (supportedEvents.length > 0 && !getParser().isCurrentEvent(supportedEvents)) { - Object[] names = Arrays.stream(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()) - .toArray(); + .forEachOrdered(joiner::add); - Skript.error("'" + parseResult.expr + "' can only be used in the " + StringUtils.join(names, ", ")); + Skript.error("'" + parseResult.expr + "' can only be used in " + joiner); continue; } From 2e750369e1a88c87a565db684538f77dd0caa573 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Thu, 19 Dec 2024 14:39:25 +0100 Subject: [PATCH 3/9] update docs, update message --- .../ch/njol/skript/lang/SkriptParser.java | 18 ------------ .../ch/njol/skript/lang/SyntaxElement.java | 28 +++++-------------- .../skript/tests/misc/supported events.sk | 2 +- 3 files changed, 8 insertions(+), 40 deletions(-) diff --git a/src/main/java/ch/njol/skript/lang/SkriptParser.java b/src/main/java/ch/njol/skript/lang/SkriptParser.java index a26b4c72b49..ad94db29b17 100644 --- a/src/main/java/ch/njol/skript/lang/SkriptParser.java +++ b/src/main/java/ch/njol/skript/lang/SkriptParser.java @@ -1,21 +1,3 @@ -/** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.lang; import ch.njol.skript.Skript; diff --git a/src/main/java/ch/njol/skript/lang/SyntaxElement.java b/src/main/java/ch/njol/skript/lang/SyntaxElement.java index 0fadac9d773..00a8dea2e10 100644 --- a/src/main/java/ch/njol/skript/lang/SyntaxElement.java +++ b/src/main/java/ch/njol/skript/lang/SyntaxElement.java @@ -1,21 +1,3 @@ -/** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.lang; import ch.njol.skript.lang.SkriptParser.ParseResult; @@ -49,11 +31,15 @@ default ParserInstance getParser() { } /** - * Returns all supported events for this syntax element. + * Returns all supported events for this syntax element. By default, all events are accepted. *

- * If the event is + * 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. *

- * @return All supported + * + * @return All supported event classes. + * @see ch.njol.util.coll.CollectionUtils#array(Object[]) */ default Class[] supportedEvents() { //noinspection unchecked diff --git a/src/test/skript/tests/misc/supported events.sk b/src/test/skript/tests/misc/supported events.sk index ff77ea57b5f..f0cfc2f8a16 100644 --- a/src/test/skript/tests/misc/supported events.sk +++ b/src/test/skript/tests/misc/supported events.sk @@ -2,4 +2,4 @@ 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" \ No newline at end of file + 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" From 8967de522c2beea836043e3cd6532ff83cdbfc5b Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Tue, 24 Dec 2024 22:34:52 +0100 Subject: [PATCH 4/9] move to interface --- .../expressions/ExprExplodedBlocks.java | 3 ++- .../skript/lang/EventRestrictedSyntax.java | 24 +++++++++++++++++++ .../ch/njol/skript/lang/SkriptParser.java | 20 +++++++++------- .../ch/njol/skript/lang/SyntaxElement.java | 17 ------------- 4 files changed, 37 insertions(+), 27 deletions(-) create mode 100644 src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java diff --git a/src/main/java/ch/njol/skript/expressions/ExprExplodedBlocks.java b/src/main/java/ch/njol/skript/expressions/ExprExplodedBlocks.java index 551136ef1d2..d67b502f56b 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprExplodedBlocks.java +++ b/src/main/java/ch/njol/skript/expressions/ExprExplodedBlocks.java @@ -3,6 +3,7 @@ import java.util.List; import ch.njol.skript.classes.Changer.ChangeMode; +import ch.njol.skript.lang.EventRestrictedSyntax; import ch.njol.util.coll.CollectionUtils; import org.bukkit.block.Block; import org.bukkit.event.Event; @@ -43,7 +44,7 @@ "\tadd blocks above event-entity to exploded blocks"}) @Events("explode") @Since("2.5, 2.8.6 (modify blocks)") -public class ExprExplodedBlocks extends SimpleExpression { +public class ExprExplodedBlocks extends SimpleExpression implements EventRestrictedSyntax { static { Skript.registerExpression(ExprExplodedBlocks.class, Block.class, ExpressionType.COMBINED, "[the] exploded blocks"); diff --git a/src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java b/src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java new file mode 100644 index 00000000000..4de1b4c2bdb --- /dev/null +++ b/src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java @@ -0,0 +1,24 @@ +package ch.njol.skript.lang; + +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; + +public interface EventRestrictedSyntax { + + /** + * Returns all supported events for this syntax element. By default, all events are accepted. + *

+ * Before {@link SyntaxElement#init(Expression[], int, Kleenean, SkriptParser.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. + *

+ * + * @return All supported event classes. + * @see ch.njol.util.coll.CollectionUtils#array(Object[]) + */ + default Class[] supportedEvents() { + //noinspection unchecked + return (Class[]) new Class[0]; + } + +} diff --git a/src/main/java/ch/njol/skript/lang/SkriptParser.java b/src/main/java/ch/njol/skript/lang/SkriptParser.java index ad94db29b17..4853f5b24cd 100644 --- a/src/main/java/ch/njol/skript/lang/SkriptParser.java +++ b/src/main/java/ch/njol/skript/lang/SkriptParser.java @@ -221,17 +221,19 @@ public boolean hasTag(String tag) { } T element = info.getElementClass().newInstance(); - Class[] supportedEvents = element.supportedEvents(); - if (supportedEvents.length > 0 && !getParser().isCurrentEvent(supportedEvents)) { - StringJoiner joiner = new StringJoiner(", ", "the ", ""); + if (element instanceof EventRestrictedSyntax eventRestrictedSyntax) { + Class[] supportedEvents = eventRestrictedSyntax.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); + 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; - } + Skript.error("'" + parseResult.expr + "' can only be used in " + joiner); + continue; + } + } boolean success = element.init(parseResult.exprs, patternIndex, getParser().getHasDelayBefore(), parseResult); if (success) { diff --git a/src/main/java/ch/njol/skript/lang/SyntaxElement.java b/src/main/java/ch/njol/skript/lang/SyntaxElement.java index 00a8dea2e10..d0ada62cf69 100644 --- a/src/main/java/ch/njol/skript/lang/SyntaxElement.java +++ b/src/main/java/ch/njol/skript/lang/SyntaxElement.java @@ -3,7 +3,6 @@ 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. @@ -30,20 +29,4 @@ default ParserInstance getParser() { return ParserInstance.get(); } - /** - * Returns all supported events for this syntax element. By default, all events are accepted. - *

- * 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. - *

- * - * @return All supported event classes. - * @see ch.njol.util.coll.CollectionUtils#array(Object[]) - */ - default Class[] supportedEvents() { - //noinspection unchecked - return (Class[]) new Class[0]; - } - } From ddeea02ab61bbeb1ae70139211f7e5fc801f4e5d Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Wed, 25 Dec 2024 02:00:33 +0100 Subject: [PATCH 5/9] update --- .../ch/njol/skript/lang/EventRestrictedSyntax.java | 10 ++++------ src/main/java/ch/njol/skript/lang/SkriptParser.java | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java b/src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java index 4de1b4c2bdb..59cde1b4431 100644 --- a/src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java +++ b/src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java @@ -1,12 +1,13 @@ package ch.njol.skript.lang; import ch.njol.util.Kleenean; +import ch.njol.util.coll.CollectionUtils; import org.bukkit.event.Event; public interface EventRestrictedSyntax { /** - * Returns all supported events for this syntax element. By default, all events are accepted. + * Returns all supported events for this syntax element. *

* Before {@link SyntaxElement#init(Expression[], int, Kleenean, SkriptParser.ParseResult)} is called, checks * to see if the current event is supported by this syntax element. @@ -14,11 +15,8 @@ public interface EventRestrictedSyntax { *

* * @return All supported event classes. - * @see ch.njol.util.coll.CollectionUtils#array(Object[]) + * @see CollectionUtils#array(Object[]) */ - default Class[] supportedEvents() { - //noinspection unchecked - return (Class[]) new Class[0]; - } + Class[] supportedEvents(); } diff --git a/src/main/java/ch/njol/skript/lang/SkriptParser.java b/src/main/java/ch/njol/skript/lang/SkriptParser.java index 4853f5b24cd..8990701f679 100644 --- a/src/main/java/ch/njol/skript/lang/SkriptParser.java +++ b/src/main/java/ch/njol/skript/lang/SkriptParser.java @@ -223,7 +223,7 @@ public boolean hasTag(String tag) { if (element instanceof EventRestrictedSyntax eventRestrictedSyntax) { Class[] supportedEvents = eventRestrictedSyntax.supportedEvents(); - if (supportedEvents.length > 0 && !getParser().isCurrentEvent(supportedEvents)) { + if (!getParser().isCurrentEvent(supportedEvents)) { StringJoiner joiner = new StringJoiner(", ", "the ", ""); Arrays.stream(supportedEvents) From 3452bf99efeb04d1e916ed1c895b4274608a0824 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Thu, 26 Dec 2024 22:28:15 +0100 Subject: [PATCH 6/9] le fix --- src/main/java/ch/njol/skript/lang/SkriptParser.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/lang/SkriptParser.java b/src/main/java/ch/njol/skript/lang/SkriptParser.java index 8990701f679..89faec41f95 100644 --- a/src/main/java/ch/njol/skript/lang/SkriptParser.java +++ b/src/main/java/ch/njol/skript/lang/SkriptParser.java @@ -224,13 +224,16 @@ public boolean hasTag(String tag) { if (element instanceof EventRestrictedSyntax eventRestrictedSyntax) { Class[] supportedEvents = eventRestrictedSyntax.supportedEvents(); if (!getParser().isCurrentEvent(supportedEvents)) { - StringJoiner joiner = new StringJoiner(", ", "the ", ""); + Iterator iterator = Arrays.stream(supportedEvents) + .map(it -> "the " + it.getSimpleName() + .replaceAll("([A-Z])", " $1") + .toLowerCase() + .trim()) + .iterator(); - Arrays.stream(supportedEvents) - .map(it -> it.getSimpleName().replaceAll("([A-Z])", " $1").toLowerCase().trim()) - .forEachOrdered(joiner::add); + String events = StringUtils.join(iterator, ",", " or "); - Skript.error("'" + parseResult.expr + "' can only be used in " + joiner); + Skript.error("'" + parseResult.expr + "' can only be used in " + events); continue; } } From cec68eb461adc64435e4af0bbdbd0adba9e05f39 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Thu, 26 Dec 2024 22:37:08 +0100 Subject: [PATCH 7/9] update doc --- src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java b/src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java index 59cde1b4431..901c23d6ece 100644 --- a/src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java +++ b/src/main/java/ch/njol/skript/lang/EventRestrictedSyntax.java @@ -4,6 +4,9 @@ import ch.njol.util.coll.CollectionUtils; import org.bukkit.event.Event; +/** + * A syntax element that restricts the events it can be used in. + */ public interface EventRestrictedSyntax { /** From e2b8bcea793d17f8f9181b9e5c08810e915fae8d Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Thu, 26 Dec 2024 23:34:49 +0100 Subject: [PATCH 8/9] implement --- .../ch/njol/skript/expressions/ExprArgument.java | 16 ++++++++++------ .../ch/njol/skript/expressions/ExprAttacked.java | 15 +++++++++------ .../ch/njol/skript/expressions/ExprAttacker.java | 16 ++++++++++------ .../ch/njol/skript/expressions/ExprCommand.java | 15 +++++++++------ .../ch/njol/skript/expressions/ExprDrops.java | 12 +++++++----- .../skript/expressions/ExprPushedBlocks.java | 15 ++++++++------- 6 files changed, 53 insertions(+), 36 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprArgument.java b/src/main/java/ch/njol/skript/expressions/ExprArgument.java index a6c9f666525..db91407e222 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprArgument.java +++ b/src/main/java/ch/njol/skript/expressions/ExprArgument.java @@ -3,6 +3,8 @@ import java.util.List; import java.util.regex.MatchResult; +import ch.njol.skript.lang.EventRestrictedSyntax; +import ch.njol.util.coll.CollectionUtils; import org.bukkit.event.Event; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.server.ServerCommandEvent; @@ -43,7 +45,7 @@ "heal the last argument" }) @Since("1.0, 2.7 (support for command events)") -public class ExprArgument extends SimpleExpression { +public class ExprArgument extends SimpleExpression implements EventRestrictedSyntax { static { Skript.registerExpression(ExprArgument.class, Object.class, ExpressionType.SIMPLE, @@ -68,10 +70,6 @@ public class ExprArgument extends SimpleExpression { @SuppressWarnings("unchecked") public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { boolean scriptCommand = getParser().isCurrentEvent(ScriptCommandEvent.class); - if (!scriptCommand && !getParser().isCurrentEvent(PlayerCommandPreprocessEvent.class, ServerCommandEvent.class)) { - Skript.error("The 'argument' expression can only be used in a script command or command event"); - return false; - } switch (matchedPattern) { case 0: @@ -192,7 +190,13 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye return true; } - + + @Override + public Class[] supportedEvents() { + return CollectionUtils.array(ScriptCommandEvent.class, PlayerCommandPreprocessEvent.class, + ServerCommandEvent.class); + } + @Override @Nullable protected Object[] get(final Event e) { diff --git a/src/main/java/ch/njol/skript/expressions/ExprAttacked.java b/src/main/java/ch/njol/skript/expressions/ExprAttacked.java index c8f2a8dd1d3..5b2ed4e51cb 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprAttacked.java +++ b/src/main/java/ch/njol/skript/expressions/ExprAttacked.java @@ -2,6 +2,8 @@ import java.lang.reflect.Array; +import ch.njol.skript.lang.EventRestrictedSyntax; +import ch.njol.util.coll.CollectionUtils; import org.bukkit.entity.Entity; import org.bukkit.event.Event; import org.bukkit.event.entity.EntityDamageEvent; @@ -36,7 +38,7 @@ "\tdamage the attacked by 1 heart"}) @Since("1.3, 2.6.1 (projectile hit event)") @Events({"damage", "death", "projectile hit"}) -public class ExprAttacked extends SimpleExpression { +public class ExprAttacked extends SimpleExpression implements EventRestrictedSyntax { private static final boolean SUPPORT_PROJECTILE_HIT = Skript.methodExists(ProjectileHitEvent.class, "getHitEntity"); @@ -49,11 +51,6 @@ public class ExprAttacked extends SimpleExpression { @Override public boolean init(Expression[] vars, int matchedPattern, Kleenean isDelayed, ParseResult parser) { - if (!getParser().isCurrentEvent(EntityDamageEvent.class, EntityDeathEvent.class, VehicleDamageEvent.class, VehicleDestroyEvent.class, ProjectileHitEvent.class) - || !SUPPORT_PROJECTILE_HIT && getParser().isCurrentEvent(ProjectileHitEvent.class)) { - Skript.error("The expression 'victim' can only be used in a damage" + (SUPPORT_PROJECTILE_HIT ? ", death, or projectile hit" : " or death") + " event"); - return false; - } String type = parser.regexes.size() == 0 ? null : parser.regexes.get(0).group(); if (type == null) { this.type = EntityData.fromClass(Entity.class); @@ -68,6 +65,12 @@ public boolean init(Expression[] vars, int matchedPattern, Kleenean isDelayed return true; } + @Override + public Class[] supportedEvents() { + return CollectionUtils.array(EntityDamageEvent.class, EntityDeathEvent.class, + VehicleDamageEvent.class, VehicleDestroyEvent.class, ProjectileHitEvent.class); + } + @Override @Nullable protected Entity[] get(Event e) { diff --git a/src/main/java/ch/njol/skript/expressions/ExprAttacker.java b/src/main/java/ch/njol/skript/expressions/ExprAttacker.java index 435a3a33b5f..bf1e6d9f20e 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprAttacker.java +++ b/src/main/java/ch/njol/skript/expressions/ExprAttacker.java @@ -1,5 +1,7 @@ package ch.njol.skript.expressions; +import ch.njol.skript.lang.EventRestrictedSyntax; +import ch.njol.util.coll.CollectionUtils; import org.bukkit.entity.Entity; import org.bukkit.entity.Projectile; import org.bukkit.event.Event; @@ -36,7 +38,7 @@ " damage victim by 1 heart"}) @Since("1.3") @Events({"damage", "death", "destroy"}) -public class ExprAttacker extends SimpleExpression { +public class ExprAttacker extends SimpleExpression implements EventRestrictedSyntax { static { Skript.registerExpression(ExprAttacker.class, Entity.class, ExpressionType.SIMPLE, "[the] (attacker|damager)"); @@ -44,13 +46,15 @@ public class ExprAttacker extends SimpleExpression { @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { - if (!getParser().isCurrentEvent(EntityDamageEvent.class, EntityDeathEvent.class, VehicleDamageEvent.class, VehicleDestroyEvent.class)) { - Skript.error("Cannot use 'attacker' outside of a damage/death/destroy event", ErrorQuality.SEMANTIC_ERROR); - return false; - } return true; } - + + @Override + public Class[] supportedEvents() { + return CollectionUtils.array(EntityDamageEvent.class, EntityDeathEvent.class, + VehicleDamageEvent.class, VehicleDestroyEvent.class); + } + @Override protected Entity[] get(Event e) { return new Entity[] {getAttacker(e)}; diff --git a/src/main/java/ch/njol/skript/expressions/ExprCommand.java b/src/main/java/ch/njol/skript/expressions/ExprCommand.java index b2173d00cb3..8cb71f77e34 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprCommand.java +++ b/src/main/java/ch/njol/skript/expressions/ExprCommand.java @@ -1,6 +1,8 @@ package ch.njol.skript.expressions; import ch.njol.skript.command.ScriptCommandEvent; +import ch.njol.skript.lang.EventRestrictedSyntax; +import ch.njol.util.coll.CollectionUtils; import org.bukkit.event.Event; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.server.ServerCommandEvent; @@ -31,7 +33,7 @@ "\t\t\tcancel the event"}) @Since("2.0, 2.7 (support for script commands)") @Events("command") -public class ExprCommand extends SimpleExpression { +public class ExprCommand extends SimpleExpression implements EventRestrictedSyntax { static { Skript.registerExpression(ExprCommand.class, String.class, ExpressionType.SIMPLE, @@ -44,13 +46,14 @@ public class ExprCommand extends SimpleExpression { @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - if (!getParser().isCurrentEvent(PlayerCommandPreprocessEvent.class, ServerCommandEvent.class, ScriptCommandEvent.class)) { - Skript.error("The 'command' expression can only be used in a script command or command event"); - return false; - } fullCommand = matchedPattern == 0; return true; } + + @Override + public Class[] supportedEvents() { + return CollectionUtils.array(PlayerCommandPreprocessEvent.class, ServerCommandEvent.class, ScriptCommandEvent.class); + } @Override @Nullable @@ -88,5 +91,5 @@ public Class getReturnType() { public String toString(@Nullable Event e, boolean debug) { return fullCommand ? "the full command" : "the command"; } - + } diff --git a/src/main/java/ch/njol/skript/expressions/ExprDrops.java b/src/main/java/ch/njol/skript/expressions/ExprDrops.java index 0bdc73304da..02809a05502 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprDrops.java +++ b/src/main/java/ch/njol/skript/expressions/ExprDrops.java @@ -8,6 +8,7 @@ import ch.njol.skript.doc.Examples; import ch.njol.skript.doc.Name; import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.EventRestrictedSyntax; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; import ch.njol.skript.lang.SkriptParser.ParseResult; @@ -38,7 +39,7 @@ "remove 4 planks from the drops"}) @Since("1.0") @Events("death") -public class ExprDrops extends SimpleExpression { +public class ExprDrops extends SimpleExpression implements EventRestrictedSyntax { static { Skript.registerExpression(ExprDrops.class, ItemType.class, ExpressionType.SIMPLE, "[the] drops"); @@ -48,15 +49,16 @@ public class ExprDrops extends SimpleExpression { @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - if (!getParser().isCurrentEvent(EntityDeathEvent.class, BlockDropItemEvent.class)) { - Skript.error("The expression 'drops' can only be used in death events and block drop events"); - return false; - } if (getParser().isCurrentEvent(EntityDeathEvent.class)) isDeathEvent = true; return true; } + @Override + public Class[] supportedEvents() { + return CollectionUtils.array(EntityDeathEvent.class, BlockDropItemEvent.class); + } + @Override protected ItemType @Nullable [] get(Event event) { if (event instanceof EntityDeathEvent entityDeathEvent) { diff --git a/src/main/java/ch/njol/skript/expressions/ExprPushedBlocks.java b/src/main/java/ch/njol/skript/expressions/ExprPushedBlocks.java index a1b93a4d1c1..bdb6bb5a6bb 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprPushedBlocks.java +++ b/src/main/java/ch/njol/skript/expressions/ExprPushedBlocks.java @@ -1,5 +1,6 @@ package ch.njol.skript.expressions; +import ch.njol.skript.lang.EventRestrictedSyntax; import ch.njol.util.coll.CollectionUtils; import org.bukkit.block.Block; import org.bukkit.event.Event; @@ -23,7 +24,7 @@ @Description("Blocks which are moved in a piston event. Cannot be used outside of piston events.") @Examples("the moved blocks") @Since("2.2-dev27") -public class ExprPushedBlocks extends SimpleExpression { +public class ExprPushedBlocks extends SimpleExpression implements EventRestrictedSyntax { static { Skript.registerExpression(ExprPushedBlocks.class, Block.class, ExpressionType.SIMPLE, "[the] moved blocks"); @@ -31,14 +32,14 @@ public class ExprPushedBlocks extends SimpleExpression { @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - if (!getParser().isCurrentEvent(BlockPistonExtendEvent.class, BlockPistonRetractEvent.class)) { - Skript.error("The moved blocks are only usable in piston extend and retract events", ErrorQuality.SEMANTIC_ERROR); - return false; - } - return true; } - + + @Override + public Class[] supportedEvents() { + return CollectionUtils.array(BlockPistonExtendEvent.class, BlockPistonRetractEvent.class); + } + @Override @Nullable protected Block[] get(Event e) { From aae310a043cac0adaa5c6d182ba5d8cd4bec0f08 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Fri, 27 Dec 2024 11:50:42 +0100 Subject: [PATCH 9/9] oops! --- src/main/java/ch/njol/skript/lang/SkriptParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/lang/SkriptParser.java b/src/main/java/ch/njol/skript/lang/SkriptParser.java index 89faec41f95..79d94bf6ebc 100644 --- a/src/main/java/ch/njol/skript/lang/SkriptParser.java +++ b/src/main/java/ch/njol/skript/lang/SkriptParser.java @@ -231,7 +231,7 @@ public boolean hasTag(String tag) { .trim()) .iterator(); - String events = StringUtils.join(iterator, ",", " or "); + String events = StringUtils.join(iterator, ", ", " or "); Skript.error("'" + parseResult.expr + "' can only be used in " + events); continue;