diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java index 12ee975da5c..b62c638d1c1 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -44,7 +44,10 @@ import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.attribute.Attribute; -import org.bukkit.block.*; +import org.bukkit.block.Biome; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.DoubleChest; import org.bukkit.block.data.BlockData; import org.bukkit.command.CommandSender; import org.bukkit.enchantments.Enchantment; diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java index 35ba7c8ea38..e13a7c7ec94 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java @@ -1938,19 +1938,19 @@ public RegainReason get(EntityRegainHealthEvent event) { if (Skript.classExists("com.destroystokyo.paper.event.block.BeaconEffectEvent")) { EventValues.registerEventValue(BeaconEffectEvent.class, PotionEffectType.class, new Getter() { @Override - public @Nullable PotionEffectType get(BeaconEffectEvent event) { + public PotionEffectType get(BeaconEffectEvent event) { return event.getEffect().getType(); } }, EventValues.TIME_NOW); EventValues.registerEventValue(BeaconEffectEvent.class, Player.class, new Getter() { @Override - public @Nullable Player get(BeaconEffectEvent event) { + public Player get(BeaconEffectEvent event) { return event.getPlayer(); } }, EventValues.TIME_NOW); EventValues.registerEventValue(BeaconEffectEvent.class, Boolean.class, new Getter() { @Override - public @Nullable Boolean get(BeaconEffectEvent event) { + public Boolean get(BeaconEffectEvent event) { return event.isPrimary(); } }, EventValues.TIME_NOW, "Use 'applied effect is (primary|secondary)' in beacon effect events", BeaconEffectEvent.class); @@ -1959,7 +1959,7 @@ public RegainReason get(EntityRegainHealthEvent event) { if (Skript.classExists("io.papermc.paper.event.player.PlayerChangeBeaconEffectEvent")) { EventValues.registerEventValue(PlayerChangeBeaconEffectEvent.class, Block.class, new Getter() { @Override - public @Nullable Block get(PlayerChangeBeaconEffectEvent event) { + public Block get(PlayerChangeBeaconEffectEvent event) { return event.getBeacon(); } }, EventValues.TIME_NOW); diff --git a/src/main/java/ch/njol/skript/conditions/CondAppliedEffect.java b/src/main/java/ch/njol/skript/conditions/CondAppliedEffect.java index 2590f225554..3ac7a4b6dfc 100644 --- a/src/main/java/ch/njol/skript/conditions/CondAppliedEffect.java +++ b/src/main/java/ch/njol/skript/conditions/CondAppliedEffect.java @@ -25,12 +25,12 @@ public class CondAppliedEffect extends Condition { static { Skript.registerCondition(CondAppliedEffect.class, - "applied effect is primary", - "applied effect is secondary" + "applied effect is [the] (primary|:secondary) [[potion] effect]", + "applied effect (isn't|is not) [the] (primary|:secondary) [[potion] effect]" ); } - private int pattern; + private boolean checkPrimary; @Override public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { @@ -38,7 +38,9 @@ public boolean init(Expression[] expressions, int matchedPattern, Kleenean is Skript.error("This condition can only be used in a on beacon effect event."); return false; } - pattern = matchedPattern; + checkPrimary = !parseResult.hasTag("secondary"); + if (matchedPattern == 1) + setNegated(true); return true; } @@ -46,17 +48,12 @@ public boolean init(Expression[] expressions, int matchedPattern, Kleenean is public boolean check(Event event) { BeaconEffectEvent beaconEffectEvent = (BeaconEffectEvent) event; boolean isPrimary = beaconEffectEvent.isPrimary(); - if (pattern == 0) { - return isPrimary; - } else if (pattern == 1) { - return !isPrimary; - } - return false; + return isPrimary == checkPrimary; } @Override public String toString(@Nullable Event event, boolean debug) { - return "applied effect is " + (pattern == 0 ? "primary" : "secondary"); + return "applied effect is " + (isNegated() ? "not" : "") + (checkPrimary ? "primary" : "secondary"); } } diff --git a/src/main/java/ch/njol/skript/events/EvtBeacon.java b/src/main/java/ch/njol/skript/events/EvtBeacon.java index 657df701bc5..7fd65d667e8 100644 --- a/src/main/java/ch/njol/skript/events/EvtBeacon.java +++ b/src/main/java/ch/njol/skript/events/EvtBeacon.java @@ -27,7 +27,7 @@ public class EvtBeacon extends SkriptEvent { .requiredPlugins("Paper"); } if (Skript.classExists("io.papermc.paper.event.block.BeaconActivatedEvent")) { - Skript.registerEvent("Beacon Toggle", EvtBeacon.class, new Class[] {BeaconActivatedEvent.class, BeaconDeactivatedEvent.class}, "beacon [:de]activate") + Skript.registerEvent("Beacon Toggle", EvtBeacon.class, new Class[] {BeaconActivatedEvent.class, BeaconDeactivatedEvent.class}, "beacon [:de]activat(e|ion)") .description("Called when a beacon is activated or deactivated") .examples( "on beacon activate:", @@ -38,7 +38,7 @@ public class EvtBeacon extends SkriptEvent { } if (Skript.classExists("io.papermc.paper.event.player.PlayerChangeBeaconEffectEvent")) { Skript.registerEvent("Beacon Change Effect", EvtBeacon.class, PlayerChangeBeaconEffectEvent.class, - "beacon change effect", "beacon effect change") + "beacon change effect", "beacon effect change", "player chang(e[s]|ing) [of] beacon effect") .description("Called when a player changes the effects of a beacon") .examples( "on beacon effect change:", @@ -67,11 +67,12 @@ public boolean check(Event event) { } else if (event instanceof BeaconDeactivatedEvent) { return !isActivate; } - return true; + return false; } @Override public String toString(@Nullable Event event, boolean debug) { return "beacon effect/activate/deactivate/change effect"; } + } diff --git a/src/main/java/ch/njol/skript/expressions/ExprBeaconValues.java b/src/main/java/ch/njol/skript/expressions/ExprBeaconValues.java index b805b0bda14..3af7ded206a 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprBeaconValues.java +++ b/src/main/java/ch/njol/skript/expressions/ExprBeaconValues.java @@ -43,27 +43,23 @@ enum BeaconValues { RANGE("[beacon] range"), TIER("[beacon] tier"); - private final String name; + private final String pattern; - BeaconValues(String name) { - this.name = name; + BeaconValues(String pattern) { + this.pattern = pattern; } } - private static boolean PAPER_EVENTS, PAPER_RANGE = false; + private static final boolean PAPER_EVENTS = Skript.classExists("com.destroystokyo.paper.event.block.BeaconEffectEvent"); + private static final boolean PAPER_RANGE = Skript.methodExists(Beacon.class, "getEffectRange"); private static BeaconValues[] beaconValues = BeaconValues.values(); static { - if (Skript.classExists("com.destroystokyo.paper.event.block.BeaconEffectEvent")) - PAPER_EVENTS = true; - if (Skript.methodExists(Beacon.class, "getEffectRange")) - PAPER_RANGE = true; - int size = beaconValues.length; String[] patterns = new String[size * 2]; for (BeaconValues value : beaconValues) { - patterns[2 * value.ordinal()] = "%blocks%['s] " + value.name; - patterns[2 * value.ordinal() + 1] = value.name + " [of %blocks%]"; + patterns[2 * value.ordinal()] = "%blocks%['s] " + value.pattern; + patterns[2 * value.ordinal() + 1] = value.pattern + " [of %blocks%]"; } Skript.registerExpression(ExprBeaconValues.class, Object.class, ExpressionType.PROPERTY, patterns); @@ -76,7 +72,7 @@ enum BeaconValues { public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { valueType = beaconValues[(int) floor(matchedPattern/2)]; if (valueType == BeaconValues.RANGE && !PAPER_RANGE) { - Skript.error("This can only be used on Paper."); + Skript.error(valueType.pattern + "can only be used on Paper."); return false; } if (exprs[0] != null) { @@ -147,6 +143,14 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { providedRange = (double) ((long) delta[0]); } } + switch (valueType) { + case RANGE -> {changeRange(event, providedRange, mode);} + case PRIMARY -> {changePrimary(event, providedEffect, mode);} + case SECONDARY -> {changeSecondary(event, providedEffect, mode);} + } + } + + private void changeRange(Event event, double providedRange, ChangeMode mode) { switch (mode) { case ADD -> { for (Block block : getExpr().getArray(event)) { @@ -163,45 +167,55 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { } } case DELETE, RESET -> { - if (valueType == BeaconValues.RANGE) { - for (Block block : getExpr().getArray(event)) { - Beacon beacon = (Beacon) block.getState(); - beacon.resetEffectRange(); - beacon.update(true); - } - } else if (valueType == BeaconValues.PRIMARY) { - for (Block block : getExpr().getArray(event)) { - Beacon beacon = (Beacon) block.getState(); - beacon.setPrimaryEffect(null); - beacon.update(true); - } - } else if (valueType == BeaconValues.SECONDARY) { - for (Block block : getExpr().getArray(event)) { - Beacon beacon = (Beacon) block.getState(); - beacon.setSecondaryEffect(null); - beacon.update(true); - } + for (Block block : getExpr().getArray(event)) { + Beacon beacon = (Beacon) block.getState(); + beacon.resetEffectRange(); + beacon.update(true); } } case SET -> { - if (valueType == BeaconValues.RANGE) { - for (Block block : getExpr().getArray(event)) { - Beacon beacon = (Beacon) block.getState(); - beacon.setEffectRange(providedRange); - beacon.update(true); - } - } else if (valueType == BeaconValues.PRIMARY) { - for (Block block : getExpr().getArray(event)) { - Beacon beacon = (Beacon) block.getState(); - beacon.setPrimaryEffect(providedEffect); - beacon.update(true); - } - } else if (valueType == BeaconValues.SECONDARY) { - for (Block block : getExpr().getArray(event)) { - Beacon beacon = (Beacon) block.getState(); - beacon.setSecondaryEffect(providedEffect); - beacon.update(true); - } + for (Block block : getExpr().getArray(event)) { + Beacon beacon = (Beacon) block.getState(); + beacon.setEffectRange(providedRange); + beacon.update(true); + } + } + } + } + + private void changePrimary(Event event, PotionEffectType providedEffect, ChangeMode mode) { + switch (mode) { + case SET -> { + for (Block block : getExpr().getArray(event)) { + Beacon beacon = (Beacon) block.getState(); + beacon.setPrimaryEffect(providedEffect); + beacon.update(true); + } + } + case DELETE, RESET -> { + for (Block block : getExpr().getArray(event)) { + Beacon beacon = (Beacon) block.getState(); + beacon.setPrimaryEffect(null); + beacon.update(true); + } + } + } + } + + private void changeSecondary(Event event, PotionEffectType providedEffect, ChangeMode mode) { + switch (mode) { + case SET -> { + for (Block block : getExpr().getArray(event)) { + Beacon beacon = (Beacon) block.getState(); + beacon.setSecondaryEffect(providedEffect); + beacon.update(true); + } + } + case DELETE, RESET -> { + for (Block block : getExpr().getArray(event)) { + Beacon beacon = (Beacon) block.getState(); + beacon.setSecondaryEffect(null); + beacon.update(true); } } } @@ -210,7 +224,7 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { @Override public boolean isSingle() { - return true; + return getExpr().isSingle(); } @Override @@ -220,18 +234,12 @@ public Class getReturnType() { @Override public String toString(@Nullable Event event, boolean debug) { - String result = ""; - if (isEffect) { - if (valueType == BeaconValues.PRIMARY) { - result = "primary beacon effect"; - } else { - result = "secondary beacon effect"; - } - } else if (valueType == BeaconValues.RANGE) { - result = "range"; - } else if (valueType == BeaconValues.TIER) { - result = "tier"; - } - return result + " of " + getExpr().toString(event, debug); + return switch (valueType) { + case PRIMARY -> "primary beacon effect"; + case SECONDARY -> "secondary beacon effect"; + case RANGE -> "beacon range"; + case TIER -> "beacon tier"; + } + " of " + getExpr().toString(event, debug); } + } diff --git a/src/test/skript/junit/EvtBeaconTest.sk b/src/test/skript/junit/EvtBeaconTest.sk index b9161594536..bf545b3d94e 100644 --- a/src/test/skript/junit/EvtBeaconTest.sk +++ b/src/test/skript/junit/EvtBeaconTest.sk @@ -67,4 +67,4 @@ on beacon change effect: if secondary beacon effect is luck: complete objective "change effect - got secondary effect type" for junit test {_test} if event-block is set: - complete objective "change effect - got beacon" for junit test {_test} \ No newline at end of file + complete objective "change effect - got beacon" for junit test {_test} diff --git a/src/test/skript/tests/syntaxes/expressions/ExprBeaconValues.sk b/src/test/skript/tests/syntaxes/expressions/ExprBeaconValues.sk index e4f414c078c..70ac69a73c7 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprBeaconValues.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprBeaconValues.sk @@ -1,9 +1,21 @@ on load: - set {_loc} to spawn of world "world" ~ vector(5,5,5) + set {_corner1} to spawn of world "world" ~ vector(10,1,10) + set {_corner2} to {_corner1} ~ vector(8,3,8) + set blocks within {_corner1} and {_corner2} to emerald block + set {_loc} to {_corner1} ~ vector(4,4,4) set block at {_loc} to beacon + set {-ExprBeaconValues} to block at {_loc} +on unload: + clear {-ExprBeaconValues} + set {_corner1} to spawn of world "world" ~ vector(10,1,10) + set {_corner2} to {_corner1} ~ vector(8,3,8) + set blocks within {_corner1} and {_corner2} to air + broadcast "ExprBeaconValues - Unload" on beacon activate: + broadcast "ExprBeaconValues - Beacon Activated" + event-block is {-ExprBeaconValues} set primary beacon effect to haste assert primary beacon effect is haste with "Set beacon primary effect to haste" clear primary beacon effect @@ -20,3 +32,7 @@ on beacon activate: assert beacon range is 15 with "Add 5 to beacon range" set event-block to air + clear {-ExprBeaconValues} + set {_corner1} to spawn of world "world" ~ vector(10,1,10) + set {_corner2} to {_corner1} ~ vector(8,3,8) + set blocks within {_corner1} and {_corner2} to air