From e0483beb54b2446cf511926aa7dba5eb75229f0d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 10 Jul 2021 12:56:40 +0200 Subject: [PATCH] Fix CustomCommandListener StackOverFlowError Signed-off-by: yoyosource --- .../features/script/CustomCommandListener.java | 11 ++++++++++- .../bausystem/features/script/ScriptExecutor.java | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java index c89e1296..57df508f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java @@ -81,6 +81,11 @@ public class CustomCommandListener implements Listener { @EventHandler public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { + if (e.getMessage().startsWith("/script:")) { + e.setMessage("/" + e.getMessage().substring(8)); + return; + } + Map bookMetaMap = playerMap.get(e.getPlayer()); if (bookMetaMap == null) { return; @@ -90,7 +95,11 @@ public class CustomCommandListener implements Listener { return; } e.setCancelled(true); - new ScriptExecutor(bookMeta, e.getPlayer()); + try { + new ScriptExecutor(bookMeta, e.getPlayer()); + } catch (StackOverflowError exception) { + // Ignored + } } private boolean isNoBook(ItemStack item) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java index 0c098f8a..e23d1891 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java @@ -18,6 +18,8 @@ public final class ScriptExecutor { public static final Set SPECIAL_COMMANDS = new HashSet<>(); + private String specialCommand = null; + @Getter private final Player player; @@ -40,10 +42,18 @@ public final class ScriptExecutor { this.player = player; globalVariables = ScriptListener.getGlobalContext(player); + boolean initial = true; for (String page : bookMeta.getPages()) { for (String command : page.split("\n")) { command = command.replaceAll(" +", " "); - if (command.startsWith("#") || command.trim().isEmpty()) continue; + if (command.startsWith("#") || command.trim().isEmpty()) { + if (initial && command.startsWith("#!CMD /")) { + specialCommand = command.substring(7); + } + initial = false; + continue; + } + initial = false; if (command.startsWith(".")) { jumpPoints.put(command.substring(1), commands.size()); continue; @@ -86,7 +96,8 @@ public final class ScriptExecutor { continue; } - PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); + + PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + (command.equals(specialCommand) ? "script:" : "") + command); Bukkit.getServer().getPluginManager().callEvent(preprocessEvent); if (preprocessEvent.isCancelled()) { continue;