From abc053bf8b70f3ad0153d98586cd7db299c9f3af Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 17 Jul 2023 20:02:41 +0200 Subject: [PATCH] Clean up some internal stuff Signed-off-by: yoyosource --- .../features/script/ScriptRunner.java | 35 +++++++++++++------ .../features/script/event/EventListener.java | 30 +++++++--------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptRunner.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptRunner.java index 15ce60bb..3015aba7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptRunner.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptRunner.java @@ -27,7 +27,10 @@ import de.steamwar.sql.Script; import de.steamwar.sql.SteamwarUser; import lombok.experimental.UtilityClass; import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; import org.luaj.vm2.Globals; +import org.luaj.vm2.LuaError; import org.luaj.vm2.LuaFunction; import org.luaj.vm2.LuaValue; import org.luaj.vm2.lib.OneArgFunction; @@ -87,14 +90,13 @@ public class ScriptRunner { HOTKEY_MAP.remove(player); } - public static boolean callEvent(Player player, SteamWarGlobalLuaPlugin.EventType event, LuaValue eventValue) { + public static void callEvent(Player player, SteamWarGlobalLuaPlugin.EventType event, LuaValue eventValue, Event wrappedEvent) { List luaFunctions = EVENT_MAP.getOrDefault(player, Collections.emptyMap()).getOrDefault(event, Collections.emptyList()); if (luaFunctions == null) { if(event == SteamWarGlobalLuaPlugin.EventType.DoubleSwap) { player.performCommand("gui"); } - - return false; + return; } if (eventValue == LuaValue.NIL) { @@ -103,13 +105,22 @@ public class ScriptRunner { AtomicBoolean cancelled = new AtomicBoolean(false); - eventValue.set("setCancelled", new OneArgFunction() { - @Override - public LuaValue call(LuaValue arg) { - cancelled.set(arg.checkboolean()); - return valueOf(cancelled.get()); - } - }); + if (wrappedEvent instanceof Cancellable) { + eventValue.set("setCancelled", new OneArgFunction() { + @Override + public LuaValue call(LuaValue arg) { + cancelled.set(arg.checkboolean()); + return valueOf(cancelled.get()); + } + }); + } else { + eventValue.set("setCancelled", new OneArgFunction() { + @Override + public LuaValue call(LuaValue arg) { + throw new LuaError("Event is not cancellable"); + } + }); + } for (LuaFunction luaFunction : luaFunctions) { try { @@ -120,7 +131,9 @@ public class ScriptRunner { } } - return cancelled.get(); + if (wrappedEvent instanceof Cancellable) { + ((Cancellable) wrappedEvent).setCancelled(cancelled.get()); + } } public static boolean callCommand(Player player, String command, LuaValue args) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java index 6e84fbcd..f8497822 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java @@ -62,18 +62,18 @@ public class EventListener implements Listener { @EventHandler(priority = EventPriority.MONITOR) public void onPlayerJoin(PlayerJoinEvent event) { - ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.SelfJoin, LuaValue.NIL); + ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.SelfJoin, LuaValue.NIL, event); } @EventHandler(priority = EventPriority.HIGH) public void onPlayerQuit(PlayerQuitEvent event) { - ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.SelfLeave, LuaValue.NIL); + ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.SelfLeave, LuaValue.NIL, event); } @EventHandler(priority = EventPriority.HIGH) public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { if (LAST_FS.containsKey(event.getPlayer())) { - event.setCancelled(ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.DoubleSwap, LuaValue.NIL)); + ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.DoubleSwap, LuaValue.NIL, event); } else { LAST_FS.put(event.getPlayer(), TPSUtils.currentTick.get()); } @@ -86,7 +86,7 @@ public class EventListener implements Listener { table.set("y", event.getBlock().getY()); table.set("z", event.getBlock().getZ()); table.set("type", event.getBlock().getType().name()); - event.setCancelled(ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.PlaceBlock, table)); + ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.PlaceBlock, table, event); } @EventHandler(priority = EventPriority.HIGH) @@ -96,7 +96,7 @@ public class EventListener implements Listener { table.set("y", event.getBlock().getY()); table.set("z", event.getBlock().getZ()); table.set("type", event.getBlock().getType().name()); - event.setCancelled(ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.BreakBlock, table)); + ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.BreakBlock, table, event); } private final Set ignore = new HashSet<>(); @@ -125,9 +125,9 @@ public class EventListener implements Listener { } if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) { - event.setCancelled(ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.RightClick, table)); + ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.RightClick, table, event); } else if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) { - event.setCancelled(ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.LeftClick, table)); + ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.LeftClick, table, event); } } @@ -140,7 +140,7 @@ public class EventListener implements Listener { for (Player player : Bukkit.getOnlinePlayers()) { if (tntRegion.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL)) { - ScriptRunner.callEvent(player, SteamWarGlobalLuaPlugin.EventType.TNTSpawn, LuaValue.NIL); + ScriptRunner.callEvent(player, SteamWarGlobalLuaPlugin.EventType.TNTSpawn, LuaValue.NIL, event); } } } @@ -157,17 +157,11 @@ public class EventListener implements Listener { table.set("y", event.getLocation().getY()); table.set("z", event.getLocation().getZ()); - boolean cancel = false; - for (Player player : Bukkit.getOnlinePlayers()) { - if (tntRegion.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL) && - ScriptRunner.callEvent(player, SteamWarGlobalLuaPlugin.EventType.TNTExplode, table)) { - cancel = true; - + if (tntRegion.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL)) { + ScriptRunner.callEvent(player, SteamWarGlobalLuaPlugin.EventType.TNTExplode, table, event) } } - - event.setCancelled(cancel); } @EventHandler(priority = EventPriority.HIGH) @@ -175,7 +169,7 @@ public class EventListener implements Listener { ignore.add(event.getPlayer()); LuaTable table = new LuaTable(); table.set("type", event.getItemDrop().getItemStack().getType().name()); - event.setCancelled(ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.DropItem, table)); + ScriptRunner.callEvent(event.getPlayer(), SteamWarGlobalLuaPlugin.EventType.DropItem, table, event); } @EventHandler(priority = EventPriority.HIGH) @@ -183,7 +177,7 @@ public class EventListener implements Listener { for (Player player : Bukkit.getOnlinePlayers()) { LuaTable table = new LuaTable(); table.set("type", event.getEntityType().name()); - ScriptRunner.callEvent(player, SteamWarGlobalLuaPlugin.EventType.EntityDeath, table); + ScriptRunner.callEvent(player, SteamWarGlobalLuaPlugin.EventType.EntityDeath, table, event); } } }