From 59ceb4cbaacab0560bb0008bb427beb3c860d490 Mon Sep 17 00:00:00 2001 From: "Alex \"mcmonkey\" Goodwin" Date: Sun, 7 Aug 2022 12:12:47 -0700 Subject: [PATCH] impl tryObjectSwitch and improve damaged events --- .../denizen/paper/events/EntityPathfindScriptEvent.java | 3 +-- .../denizen/paper/events/PlayerElytraBoostScriptEvent.java | 3 +-- .../paper/events/PlayerTradesWithMerchantScriptEvent.java | 2 +- .../denizen/events/block/BlockSpreadsScriptEvent.java | 2 +- .../denizen/events/block/LiquidSpreadScriptEvent.java | 2 +- .../denizen/events/entity/EntityDamagedScriptEvent.java | 5 +++-- .../denizen/events/entity/EntityDeathScriptEvent.java | 2 +- .../events/entity/EntityFoodLevelChangeScriptEvent.java | 2 +- .../events/entity/ProjectileHitsBlockScriptEvent.java | 2 +- .../events/entity/ProjectileHitsEntityScriptEvent.java | 2 +- .../denizen/events/player/HotbarScrollScriptEvent.java | 2 +- .../denizen/events/player/PlayerClicksBlockScriptEvent.java | 2 +- .../events/player/PlayerRightClicksEntityScriptEvent.java | 2 +- .../denizen/events/player/PlayerSwapsItemsScriptEvent.java | 4 ++-- .../denizen/events/player/PlayerUsesPortalScriptEvent.java | 4 ++-- .../denizen/events/vehicle/VehicleDamagedScriptEvent.java | 2 +- .../denizen/events/world/SpawnChangeScriptEvent.java | 2 +- .../denizen/events/world/ThunderChangesScriptEvent.java | 2 +- .../denizen/utilities/packets/NetworkInterceptCodeGen.java | 1 - 19 files changed, 22 insertions(+), 24 deletions(-) diff --git a/paper/src/main/java/com/denizenscript/denizen/paper/events/EntityPathfindScriptEvent.java b/paper/src/main/java/com/denizenscript/denizen/paper/events/EntityPathfindScriptEvent.java index 929cec8c5d..c4d4bfbd6d 100644 --- a/paper/src/main/java/com/denizenscript/denizen/paper/events/EntityPathfindScriptEvent.java +++ b/paper/src/main/java/com/denizenscript/denizen/paper/events/EntityPathfindScriptEvent.java @@ -63,8 +63,7 @@ public boolean matches(ScriptPath path) { if (!runInCheck(path, event.getLoc(), "to")) { return false; } - String at = path.switches.get("at"); - if (at != null && (target == null || !target.tryAdvancedMatcher(at))) { + if (!path.tryObjectSwitch("at", target)) { return false; } return super.matches(path); diff --git a/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerElytraBoostScriptEvent.java b/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerElytraBoostScriptEvent.java index 1328ad1ed2..a81f1520ea 100644 --- a/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerElytraBoostScriptEvent.java +++ b/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerElytraBoostScriptEvent.java @@ -63,8 +63,7 @@ public boolean matches(ScriptPath path) { if (!runWithCheck(path, firework)) { return false; } - if (path.switches.containsKey("elytra") - && !new ItemTag(player.getPlayerEntity().getEquipment().getChestplate()).tryAdvancedMatcher(path.switches.get("elytra"))) { + if (!path.tryObjectSwitch("elytra", new ItemTag(player.getPlayerEntity().getEquipment().getChestplate()))) { return false; } return super.matches(path); diff --git a/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerTradesWithMerchantScriptEvent.java b/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerTradesWithMerchantScriptEvent.java index cd575bfa44..d5da0734ae 100644 --- a/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerTradesWithMerchantScriptEvent.java +++ b/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerTradesWithMerchantScriptEvent.java @@ -56,7 +56,7 @@ public boolean matches(ScriptPath path) { if (!runInCheck(path, event.getPlayer().getLocation())) { return false; } - if (path.switches.containsKey("result") && !new ItemTag(event.getTrade().getResult()).tryAdvancedMatcher(path.switches.get("result"))) { + if (!path.tryObjectSwitch("result", new ItemTag(event.getTrade().getResult()))) { return false; } return super.matches(path); diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/block/BlockSpreadsScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/block/BlockSpreadsScriptEvent.java index b33c1a0dd7..b3816fd053 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/block/BlockSpreadsScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/block/BlockSpreadsScriptEvent.java @@ -66,7 +66,7 @@ public boolean matches(ScriptPath path) { if (!material.tryAdvancedMatcher(path.eventArgLowerAt(0))) { return false; } - if (path.switches.containsKey("type") && !material.tryAdvancedMatcher(path.switches.get("type"))) { + if (!path.tryObjectSwitch("type", material)) { return false; } return super.matches(path); diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/block/LiquidSpreadScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/block/LiquidSpreadScriptEvent.java index 01e90dba71..dc8d024948 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/block/LiquidSpreadScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/block/LiquidSpreadScriptEvent.java @@ -71,7 +71,7 @@ public boolean matches(ScriptPath path) { return false; } } - if (path.switches.containsKey("type") && !material.tryAdvancedMatcher(path.switches.get("type"))) { + if (!path.tryObjectSwitch("type", material)) { return false; } if (!runInCheck(path, location) && !runInCheck(path, destination)) { diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDamagedScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDamagedScriptEvent.java index 82bdf0e9bd..0b646290cb 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDamagedScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDamagedScriptEvent.java @@ -41,6 +41,7 @@ public class EntityDamagedScriptEvent extends BukkitScriptEvent implements Liste // @Location true // // @Switch with: to only process the event when the item used to cause damage (in the damager's hand) is a specified item. + // @Switch type: to only run if the entity damaged matches the entity input. // // @Cancellable true // @@ -71,7 +72,7 @@ public EntityDamagedScriptEvent() { registerCouldMatcher(" damaged (by <'cause'>)"); registerCouldMatcher(" damaged by "); registerCouldMatcher(" damages "); - registerSwitches("with"); + registerSwitches("with", "type"); } public static EntityDamagedScriptEvent instance; @@ -123,7 +124,7 @@ public boolean matches(ScriptPath path) { } } } - if (!entity.tryAdvancedMatcher(target)) { + if (!entity.tryAdvancedMatcher(target) || !path.tryObjectSwitch("type", entity)) { return false; } if (!runInCheck(path, entity.getLocation())) { diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDeathScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDeathScriptEvent.java index 79c6d02db4..f4ffaf9acd 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDeathScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDeathScriptEvent.java @@ -86,7 +86,7 @@ public boolean matches(ScriptPath path) { if (!runInCheck(path, entity.getLocation())) { return false; } - if (path.switches.containsKey("by") && (damager == null || !damager.tryAdvancedMatcher(path.switches.get("by")))) { + if (!path.tryObjectSwitch("by", damager)) { return false; } if (!runGenericSwitchCheck(path, "cause", cause == null ? null : cause.asString())) { diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityFoodLevelChangeScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityFoodLevelChangeScriptEvent.java index 70f7e4f436..d872fa49e9 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityFoodLevelChangeScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityFoodLevelChangeScriptEvent.java @@ -60,7 +60,7 @@ public boolean matches(ScriptPath path) { if (!entity.tryAdvancedMatcher(target)) { return false; } - if (path.switches.containsKey("item") && !item.tryAdvancedMatcher(path.switches.get("item"))) { + if (!path.tryObjectSwitch("item", item)) { return false; } if (!runInCheck(path, entity.getLocation())) { diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/entity/ProjectileHitsBlockScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/entity/ProjectileHitsBlockScriptEvent.java index 6dd9e45158..57ac5b213b 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/entity/ProjectileHitsBlockScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/entity/ProjectileHitsBlockScriptEvent.java @@ -119,7 +119,7 @@ else if (cmd.equals("shoots")) { if (!pTest.isEmpty() && !pTest.equals("projectile") && !projectile.tryAdvancedMatcher(pTest)) { return false; } - if (path.switches.containsKey("with") && !projectile.tryAdvancedMatcher(path.switches.get("with"))) { + if (!path.tryObjectSwitch("with", projectile)) { return false; } if (!material.tryAdvancedMatcher(path.eventArgLowerAt(2))) { diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/entity/ProjectileHitsEntityScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/entity/ProjectileHitsEntityScriptEvent.java index 3f070d6ddf..71b8789218 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/entity/ProjectileHitsEntityScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/entity/ProjectileHitsEntityScriptEvent.java @@ -78,7 +78,7 @@ public boolean matches(ScriptPath path) { if (!hitEntity.tryAdvancedMatcher(path.eventArgLowerAt(2))) { return false; } - if (path.switches.containsKey("shooter") && (shooter == null || !shooter.tryAdvancedMatcher(path.switches.get("shooter")))) { + if (!path.tryObjectSwitch("shooter", shooter)) { return false; } if (!runInCheck(path, location)) { diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/player/HotbarScrollScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/player/HotbarScrollScriptEvent.java index 372ef114e8..2dc13d0d0b 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/player/HotbarScrollScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/player/HotbarScrollScriptEvent.java @@ -58,7 +58,7 @@ public boolean matches(ScriptPath path) { if (!runInCheck(path, event.getPlayer().getLocation())) { return false; } - if (path.switches.containsKey("item") && !new ItemTag(event.getPlayer().getInventory().getItem(event.getNewSlot())).tryAdvancedMatcher(path.switches.get("item"))) { + if (!path.tryObjectSwitch("item", new ItemTag(event.getPlayer().getInventory().getItem(event.getNewSlot())))) { return false; } return super.matches(path); diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerClicksBlockScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerClicksBlockScriptEvent.java index d224b66058..aa68702df0 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerClicksBlockScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerClicksBlockScriptEvent.java @@ -143,7 +143,7 @@ public boolean matches(ScriptPath path) { if (!runInCheck(path, location != null ? location : event.getPlayer().getLocation())) { return false; } - if (path.switches.containsKey("type") && !blockMaterial.tryAdvancedMatcher(path.switches.get("type"))) { + if (!path.tryObjectSwitch("type", blockMaterial)) { return false; } return super.matches(path); diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerRightClicksEntityScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerRightClicksEntityScriptEvent.java index 50763c9c3c..6d9a9cb9d3 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerRightClicksEntityScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerRightClicksEntityScriptEvent.java @@ -68,7 +68,7 @@ public boolean matches(ScriptPath path) { if (path.eventArgLowerAt(isAt ? 5 : 4).equals("with") && !item.tryAdvancedMatcher(path.eventArgLowerAt(isAt ? 6 : 5))) { return false; } - if (path.switches.containsKey("type") && !entity.tryAdvancedMatcher(path.switches.get("type"))) { + if (!path.tryObjectSwitch("type", entity)) { return false; } return super.matches(path); diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerSwapsItemsScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerSwapsItemsScriptEvent.java index bee3255907..8760de9c57 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerSwapsItemsScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerSwapsItemsScriptEvent.java @@ -56,10 +56,10 @@ public boolean couldMatch(ScriptPath path) { @Override public boolean matches(ScriptPath path) { - if (path.switches.containsKey("main") && !new ItemTag(event.getMainHandItem()).tryAdvancedMatcher(path.switches.get("main"))) { + if (!path.tryObjectSwitch("main", new ItemTag(event.getMainHandItem()))) { return false; } - if (path.switches.containsKey("offhand") && !new ItemTag(event.getOffHandItem()).tryAdvancedMatcher(path.switches.get("offhand"))) { + if (!path.tryObjectSwitch("offhand", new ItemTag(event.getOffHandItem()))) { return false; } return super.matches(path); diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerUsesPortalScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerUsesPortalScriptEvent.java index 5897c4c31f..7488bee83d 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerUsesPortalScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerUsesPortalScriptEvent.java @@ -63,10 +63,10 @@ public boolean matches(ScriptPath path) { if (!runInCheck(path, to) && !runInCheck(path, from)) { return false; } - if (path.switches.containsKey("from") && !from.tryAdvancedMatcher(path.switches.get("from"))) { + if (!path.tryObjectSwitch("from", from)) { return false; } - if (path.switches.containsKey("to") && (to == null || !to.tryAdvancedMatcher(path.switches.get("to")))) { + if (!path.tryObjectSwitch("to", to)) { return false; } return super.matches(path); diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/vehicle/VehicleDamagedScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/vehicle/VehicleDamagedScriptEvent.java index e0a5ac48a8..e83a586ad6 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/vehicle/VehicleDamagedScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/vehicle/VehicleDamagedScriptEvent.java @@ -60,7 +60,7 @@ public boolean couldMatch(ScriptPath path) { if (!cmd.equals("damaged") && !cmd.equals("damages")) { return false; } - if (path.eventArgLowerAt(3).equals("by")) { + if (path.eventArgLowerAt(2).equals("by")) { return false; } if (!exactMatchesVehicle(path.eventArgLowerAt(0)) && !exactMatchesVehicle(path.eventArgLowerAt(2))) { diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/world/SpawnChangeScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/world/SpawnChangeScriptEvent.java index 40d964c11e..88e606e1d6 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/world/SpawnChangeScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/world/SpawnChangeScriptEvent.java @@ -38,7 +38,7 @@ public SpawnChangeScriptEvent() { @Override public boolean matches(ScriptPath path) { - if (path.switches.containsKey("world") && !new WorldTag(event.getWorld()).tryAdvancedMatcher(path.switches.get("world"))) { + if (!path.tryObjectSwitch("world", new WorldTag(event.getWorld()))) { return false; } return super.matches(path); diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/world/ThunderChangesScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/world/ThunderChangesScriptEvent.java index f728fd34e2..d777a90e4d 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/world/ThunderChangesScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/world/ThunderChangesScriptEvent.java @@ -53,7 +53,7 @@ else if (changeType.equals("begins")) { else if (!changeType.equals("changes")) { return false; } - if (path.switches.containsKey("in") && !new WorldTag(event.getWorld()).tryAdvancedMatcher(path.switches.get("in"))) { + if (!path.tryObjectSwitch("in", new WorldTag(event.getWorld()))) { return false; } return super.matches(path); diff --git a/plugin/src/main/java/com/denizenscript/denizen/utilities/packets/NetworkInterceptCodeGen.java b/plugin/src/main/java/com/denizenscript/denizen/utilities/packets/NetworkInterceptCodeGen.java index 6565f5936f..e402d628f3 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/utilities/packets/NetworkInterceptCodeGen.java +++ b/plugin/src/main/java/com/denizenscript/denizen/utilities/packets/NetworkInterceptCodeGen.java @@ -8,7 +8,6 @@ import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.reflect.Constructor; -import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier;