From 9207d3c966996ca16b7d84842fe3f5fba9013fd5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 17 Jul 2023 20:14:38 +0200 Subject: [PATCH] Fix command stack overflow Signed-off-by: yoyosource --- .../script/event/CommandListener.java | 27 +++++++++++++++++++ .../features/script/event/EventListener.java | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/CommandListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/CommandListener.java index d02066ae..ae9fb7d2 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/CommandListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/CommandListener.java @@ -21,21 +21,48 @@ package de.steamwar.bausystem.features.script.event; import de.steamwar.bausystem.features.script.ScriptRunner; import de.steamwar.linkage.Linked; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.luaj.vm2.LuaValue; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + @Linked public class CommandListener implements Listener { + private Map> calledCommands = new HashMap<>(); + @EventHandler public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { String[] split = event.getMessage().split(" "); + if (calledCommands.getOrDefault(event.getPlayer(), new HashSet<>()).contains(split[0])) { + return; + } + LuaValue[] values = new LuaValue[split.length - 1]; for (int i = 1; i < split.length; i++) { values[i - 1] = LuaValue.valueOf(split[i]); } + + calledCommands.getOrDefault(event.getPlayer(), new HashSet<>()).add(split[0]); event.setCancelled(ScriptRunner.callCommand(event.getPlayer(), split[0].substring(1), LuaValue.listOf(values))); + calledCommands.getOrDefault(event.getPlayer(), new HashSet<>()).remove(split[0]); + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + calledCommands.put(event.getPlayer(), new HashSet<>()); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + calledCommands.remove(event.getPlayer()); } } 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 f8497822..22fd60a3 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 @@ -159,7 +159,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.TNTExplode, table, event) + ScriptRunner.callEvent(player, SteamWarGlobalLuaPlugin.EventType.TNTExplode, table, event); } } }