Skip to content

Commit 06b54ed

Browse files
NotroDevEfniliteAbsolutionism
authored
Replace the manual isCurrentEvent checks in favor of ERS (#8529)
* Rewrite the basic legacy isCurrentEvent usages in favor of ERS * Do the TODOs ERS * clean up imports/formatting * Implement ERS in EffHidePlayerFromServerList * Fix the else block formatting Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com> * Remove the check for paper event exisiting in EffHidePlayerFromServerList Co-authored-by: SirSmurfy2 <82696841+Absolutionism@users.noreply.github.com> * Remove the unused PAPER_EVENT_EXISTS from EffHidePlayerFromServerList * Remove redundant PAPER_EVENT_EXISTS checks and expand ERS usage * revert EffLoadServerIcon invalid ERS implementation * clean up event class conversions Co-authored-by: SirSmurfy2 <82696841+Absolutionism@users.noreply.github.com> * clean up event class conversions in ExprIP * Add missing blank lines * clean up imports in EffLoadServerIcon * clean up imports in ExprHoverList --------- Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com> Co-authored-by: SirSmurfy2 <82696841+Absolutionism@users.noreply.github.com>
1 parent fa0e5e1 commit 06b54ed

51 files changed

Lines changed: 414 additions & 377 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/ch/njol/skript/conditions/CondElytraBoostConsume.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import ch.njol.skript.Skript;
44
import ch.njol.skript.doc.*;
55
import ch.njol.skript.lang.Condition;
6+
import ch.njol.skript.lang.EventRestrictedSyntax;
67
import ch.njol.skript.lang.Expression;
78
import ch.njol.skript.lang.SkriptParser.ParseResult;
89
import ch.njol.util.Kleenean;
10+
import ch.njol.util.coll.CollectionUtils;
911
import com.destroystokyo.paper.event.player.PlayerElytraBoostEvent;
1012
import org.bukkit.event.Event;
1113
import org.jetbrains.annotations.Nullable;
@@ -18,7 +20,7 @@
1820
prevent the used firework from being consumed
1921
""")
2022
@Since("2.10")
21-
public class CondElytraBoostConsume extends Condition {
23+
public class CondElytraBoostConsume extends Condition implements EventRestrictedSyntax {
2224

2325
static {
2426
if (Skript.classExists("com.destroystokyo.paper.event.player.PlayerElytraBoostEvent")) {
@@ -32,14 +34,15 @@ public class CondElytraBoostConsume extends Condition {
3234

3335
@Override
3436
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
35-
if (!getParser().isCurrentEvent(PlayerElytraBoostEvent.class)) {
36-
Skript.error("This condition can only be used in an 'elytra boost' event.");
37-
return false;
38-
}
3937
checkConsume = matchedPattern == 0;
4038
return true;
4139
}
4240

41+
@Override
42+
public Class<? extends Event>[] supportedEvents() {
43+
return CollectionUtils.array(PlayerElytraBoostEvent.class);
44+
}
45+
4346
@Override
4447
public boolean check(Event event) {
4548
if (!(event instanceof PlayerElytraBoostEvent boostEvent))

src/main/java/ch/njol/skript/conditions/CondLeashWillDrop.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ch.njol.skript.conditions;
22

3+
import ch.njol.skript.lang.EventRestrictedSyntax;
4+
import ch.njol.util.coll.CollectionUtils;
35
import org.bukkit.event.Event;
46
import org.bukkit.event.entity.EntityUnleashEvent;
57
import org.jetbrains.annotations.Nullable;
@@ -23,7 +25,7 @@
2325
@Keywords("lead")
2426
@Events("Leash / Unleash")
2527
@Since("2.10")
26-
public class CondLeashWillDrop extends Condition {
28+
public class CondLeashWillDrop extends Condition implements EventRestrictedSyntax {
2729

2830
static {
2931
// TODO - remove this when Spigot support is dropped
@@ -33,14 +35,15 @@ public class CondLeashWillDrop extends Condition {
3335

3436
@Override
3537
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
36-
if (!getParser().isCurrentEvent(EntityUnleashEvent.class)) {
37-
Skript.error("The 'leash will drop' condition can only be used in an 'unleash' event");
38-
return false;
39-
}
4038
setNegated(parseResult.hasTag("not"));
4139
return true;
4240
}
4341

42+
@Override
43+
public Class<? extends Event>[] supportedEvents() {
44+
return CollectionUtils.array(EntityUnleashEvent.class);
45+
}
46+
4447
@Override
4548
public boolean check(Event event) {
4649
if (!(event instanceof EntityUnleashEvent unleashEvent))

src/main/java/ch/njol/skript/conditions/CondResourcePack.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ch.njol.skript.conditions;
22

3+
import ch.njol.skript.lang.EventRestrictedSyntax;
4+
import ch.njol.util.coll.CollectionUtils;
35
import org.bukkit.event.Event;
46
import org.bukkit.event.player.PlayerResourcePackStatusEvent;
57
import org.bukkit.event.player.PlayerResourcePackStatusEvent.Status;
@@ -25,7 +27,7 @@
2527
""")
2628
@Since("2.4")
2729
@Events("resource pack request response")
28-
public class CondResourcePack extends Condition {
30+
public class CondResourcePack extends Condition implements EventRestrictedSyntax {
2931

3032
static {
3133
Skript.registerCondition(CondResourcePack.class,
@@ -39,14 +41,15 @@ public class CondResourcePack extends Condition {
3941
@SuppressWarnings({"unchecked", "null"})
4042
@Override
4143
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
42-
if (!getParser().isCurrentEvent(PlayerResourcePackStatusEvent.class)) {
43-
Skript.error("The resource pack condition can't be used outside of a resource pack response event");
44-
return false;
45-
}
4644
states = (Expression<Status>) exprs[0];
4745
setNegated(matchedPattern == 1);
4846
return true;
4947
}
48+
49+
@Override
50+
public Class<? extends Event>[] supportedEvents() {
51+
return CollectionUtils.array(PlayerResourcePackStatusEvent.class);
52+
}
5053

5154
@Override
5255
public boolean check(Event e) {

src/main/java/ch/njol/skript/conditions/CondRespawnLocation.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import ch.njol.skript.doc.RequiredPlugins;
99
import ch.njol.skript.doc.Since;
1010
import ch.njol.skript.lang.Condition;
11+
import ch.njol.skript.lang.EventRestrictedSyntax;
1112
import ch.njol.skript.lang.Expression;
1213
import ch.njol.skript.lang.SkriptParser.ParseResult;
1314
import ch.njol.util.Kleenean;
15+
import ch.njol.util.coll.CollectionUtils;
1416
import org.bukkit.event.Event;
1517
import org.bukkit.event.player.PlayerRespawnEvent;
1618
import org.jetbrains.annotations.Nullable;
@@ -25,7 +27,7 @@
2527
@RequiredPlugins("Minecraft 1.16+")
2628
@Since("2.7")
2729
@Events("respawn")
28-
public class CondRespawnLocation extends Condition {
30+
public class CondRespawnLocation extends Condition implements EventRestrictedSyntax {
2931

3032
static {
3133
Skript.registerCondition(CondRespawnLocation.class, "[the] respawn location (was|is)[1:(n'| no)t] [a] (:bed|respawn anchor)");
@@ -35,15 +37,16 @@ public class CondRespawnLocation extends Condition {
3537

3638
@Override
3739
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
38-
if (!getParser().isCurrentEvent(PlayerRespawnEvent.class)) {
39-
Skript.error("The 'respawn location' condition may only be used in a respawn event");
40-
return false;
41-
}
4240
setNegated(parseResult.mark == 1);
4341
bedSpawn = parseResult.hasTag("bed");
4442
return true;
4543
}
4644

45+
@Override
46+
public Class<? extends Event>[] supportedEvents() {
47+
return CollectionUtils.array(PlayerRespawnEvent.class);
48+
}
49+
4750
@Override
4851
public boolean check(Event event) {
4952
if (event instanceof PlayerRespawnEvent) {

src/main/java/ch/njol/skript/conditions/CondWillHatch.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import ch.njol.skript.doc.Name;
88
import ch.njol.skript.doc.Since;
99
import ch.njol.skript.lang.Condition;
10+
import ch.njol.skript.lang.EventRestrictedSyntax;
1011
import ch.njol.skript.lang.Expression;
1112
import ch.njol.skript.lang.SkriptParser.ParseResult;
1213
import ch.njol.util.Kleenean;
14+
import ch.njol.util.coll.CollectionUtils;
1315
import org.bukkit.event.Event;
1416
import org.bukkit.event.player.PlayerEggThrowEvent;
1517
import org.jetbrains.annotations.Nullable;
@@ -23,7 +25,7 @@
2325
""")
2426
@Events("Egg Throw")
2527
@Since("2.7")
26-
public class CondWillHatch extends Condition {
28+
public class CondWillHatch extends Condition implements EventRestrictedSyntax {
2729

2830
static {
2931
Skript.registerCondition(CondWillHatch.class,
@@ -33,14 +35,15 @@ public class CondWillHatch extends Condition {
3335

3436
@Override
3537
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
36-
if (!getParser().isCurrentEvent(PlayerEggThrowEvent.class)) {
37-
Skript.error("You can't use the 'egg will hatch' condition outside of a Player Egg Throw event.");
38-
return false;
39-
}
4038
setNegated(!parseResult.hasTag("will"));
4139
return true;
4240
}
4341

42+
@Override
43+
public Class<? extends Event>[] supportedEvents() {
44+
return CollectionUtils.array(PlayerEggThrowEvent.class);
45+
}
46+
4447
@Override
4548
public boolean check(Event event) {
4649
if (!(event instanceof PlayerEggThrowEvent))

src/main/java/ch/njol/skript/effects/EffCancelCooldown.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ch.njol.skript.effects;
22

3+
import ch.njol.skript.lang.EventRestrictedSyntax;
4+
import ch.njol.util.coll.CollectionUtils;
35
import org.bukkit.event.Event;
46
import org.jetbrains.annotations.Nullable;
57

@@ -30,7 +32,7 @@
3032
set the player's display name to arg-1
3133
""")
3234
@Since("2.2-dev34")
33-
public class EffCancelCooldown extends Effect {
35+
public class EffCancelCooldown extends Effect implements EventRestrictedSyntax {
3436

3537
static {
3638
Skript.registerEffect(EffCancelCooldown.class,
@@ -42,14 +44,15 @@ public class EffCancelCooldown extends Effect {
4244

4345
@Override
4446
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
45-
if (!getParser().isCurrentEvent(ScriptCommandEvent.class)) {
46-
Skript.error("The cancel cooldown effect may only be used in a command", ErrorQuality.SEMANTIC_ERROR);
47-
return false;
48-
}
4947
cancel = matchedPattern == 0;
5048
return true;
5149
}
5250

51+
@Override
52+
public Class<? extends Event>[] supportedEvents() {
53+
return CollectionUtils.array(ScriptCommandEvent.class);
54+
}
55+
5356
@Override
5457
protected void execute(Event e) {
5558
if (!(e instanceof ScriptCommandEvent))

src/main/java/ch/njol/skript/effects/EffDropLeash.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ch.njol.skript.effects;
22

3+
import ch.njol.skript.lang.EventRestrictedSyntax;
4+
import ch.njol.util.coll.CollectionUtils;
35
import org.bukkit.event.Event;
46
import org.bukkit.event.entity.EntityUnleashEvent;
57
import org.jetbrains.annotations.Nullable;
@@ -23,7 +25,7 @@
2325
@Keywords("lead")
2426
@Events("Leash / Unleash")
2527
@Since("2.10")
26-
public class EffDropLeash extends Effect {
28+
public class EffDropLeash extends Effect implements EventRestrictedSyntax {
2729

2830
static {
2931
Skript.registerEffect(EffDropLeash.class,
@@ -36,14 +38,15 @@ public class EffDropLeash extends Effect {
3638

3739
@Override
3840
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
39-
if (!getParser().isCurrentEvent(EntityUnleashEvent.class)) {
40-
Skript.error("The 'drop leash' effect can only be used in an 'unleash' event");
41-
return false;
42-
}
4341
allowLeashDrop = matchedPattern == 0;
4442
return true;
4543
}
4644

45+
@Override
46+
public Class<? extends Event>[] supportedEvents() {
47+
return CollectionUtils.array(EntityUnleashEvent.class);
48+
}
49+
4750
@Override
4851
protected void execute(Event event) {
4952
if (!(event instanceof EntityUnleashEvent unleashEvent))

src/main/java/ch/njol/skript/effects/EffElytraBoostConsume.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import ch.njol.skript.Skript;
44
import ch.njol.skript.doc.*;
55
import ch.njol.skript.lang.Effect;
6+
import ch.njol.skript.lang.EventRestrictedSyntax;
67
import ch.njol.skript.lang.Expression;
78
import ch.njol.skript.lang.SkriptParser.ParseResult;
89
import ch.njol.util.Kleenean;
10+
import ch.njol.util.coll.CollectionUtils;
911
import com.destroystokyo.paper.event.player.PlayerElytraBoostEvent;
1012
import org.bukkit.event.Event;
1113
import org.jetbrains.annotations.Nullable;
@@ -18,7 +20,7 @@
1820
prevent the used firework from being consume
1921
""")
2022
@Since("2.10")
21-
public class EffElytraBoostConsume extends Effect {
23+
public class EffElytraBoostConsume extends Effect implements EventRestrictedSyntax {
2224

2325
static {
2426
if (Skript.classExists("com.destroystokyo.paper.event.player.PlayerElytraBoostEvent")) {
@@ -32,14 +34,15 @@ public class EffElytraBoostConsume extends Effect {
3234

3335
@Override
3436
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
35-
if (!getParser().isCurrentEvent(PlayerElytraBoostEvent.class)) {
36-
Skript.error("This effect can only be used in an 'elytra boost' event.");
37-
return false;
38-
}
3937
consume = matchedPattern == 1;
4038
return true;
4139
}
4240

41+
@Override
42+
public Class<? extends Event>[] supportedEvents() {
43+
return CollectionUtils.array(PlayerElytraBoostEvent.class);
44+
}
45+
4346
@Override
4447
protected void execute(Event event) {
4548
if (!(event instanceof PlayerElytraBoostEvent boostEvent))

src/main/java/ch/njol/skript/effects/EffHidePlayerFromServerList.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.Arrays;
44
import java.util.Iterator;
55

6+
import ch.njol.skript.lang.EventRestrictedSyntax;
7+
import ch.njol.util.coll.CollectionUtils;
68
import org.bukkit.entity.Player;
79
import org.bukkit.event.Event;
810
import org.bukkit.event.server.ServerListPingEvent;
@@ -28,35 +30,33 @@
2830
hide {vanished::*} from the server list
2931
""")
3032
@Since("2.3")
31-
public class EffHidePlayerFromServerList extends Effect {
33+
public class EffHidePlayerFromServerList extends Effect implements EventRestrictedSyntax {
3234

3335
static {
3436
Skript.registerEffect(EffHidePlayerFromServerList.class,
3537
"hide %players% (in|on|from) [the] server list",
3638
"hide %players%'[s] info[rmation] (in|on|from) [the] server list");
3739
}
3840

39-
private static final boolean PAPER_EVENT_EXISTS = Skript.classExists("com.destroystokyo.paper.event.server.PaperServerListPingEvent");
40-
4141
@SuppressWarnings("null")
4242
private Expression<Player> players;
4343

4444
@SuppressWarnings({"unchecked", "null"})
4545
@Override
4646
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
47-
boolean isServerPingEvent = getParser().isCurrentEvent(ServerListPingEvent.class) ||
48-
(PAPER_EVENT_EXISTS && getParser().isCurrentEvent(PaperServerListPingEvent.class));
49-
if (!isServerPingEvent) {
50-
Skript.error("The hide player from server list effect can't be used outside of a server list ping event");
51-
return false;
52-
} else if (isDelayed == Kleenean.TRUE) {
47+
if (isDelayed == Kleenean.TRUE) {
5348
Skript.error("Can't hide players from the server list anymore after the server list ping event has already passed");
5449
return false;
5550
}
5651
players = (Expression<Player>) exprs[0];
5752
return true;
5853
}
5954

55+
@Override
56+
public Class<? extends Event>[] supportedEvents() {
57+
return CollectionUtils.array(ServerListPingEvent.class, PaperServerListPingEvent.class);
58+
}
59+
6060
@Override
6161
@SuppressWarnings("removal")
6262
protected void execute(Event e) {

0 commit comments

Comments
 (0)