From 02c095a946c6d50e6a519acf245201ef1e730c78 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 2 Aug 2023 12:42:55 +0200 Subject: [PATCH] Fix Error Messages Signed-off-by: Chaoscaot --- BauSystem_Main/src/BauSystem.properties | 2 +- .../features/script/ScriptRunner.java | 48 ++++++++----------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index 5ae58f17..b75ada62 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -250,7 +250,7 @@ SCRIPT_MENU_GUI_ITEM_ADD_NAME = §eInsert SCRIPT_MENU_GUI_ITEM_ADD_LORE = §7Click with a book to insert SCRIPT_MENU_GUI_ENTER_NAME = §eEnter a name -SCRIPT_DEPRECATED=§cThe function §8'§e{0}§8'§c is deprecated and will be removed in the future. Please use §8'§e{1}§8'§c instead. +SCRIPT_DEPRECATED=§cThe function §8\'§e{0}§8\'§c is deprecated and will be removed in the future. Please use §8\'§e{1}§8\'§c instead. # Shield Printing SHIELD_PRINTING_HELP_START = §8/§eshieldprinting start §8- §7Starts the shield printing 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 44b3b50b..85cc8586 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptRunner.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptRunner.java @@ -50,13 +50,7 @@ public class ScriptRunner { public static void runScript(String script, Player player) { Globals globals = SteamWarPlatform.createClickGlobals(player); - - try { - globals.load(script).call(); - } catch (Exception e) { - String[] sp = e.getMessage().split(":"); - BauSystem.MESSAGE.send("SCRIPT_ERROR_CLICK", player, String.join(":", Arrays.copyOfRange(sp, 1, sp.length))); - } + catchScript("SCRIPT_ERROR_CLICK", player, () -> globals.load(script).call()); } public static void updateGlobalScript(Player player) { @@ -72,12 +66,7 @@ public class ScriptRunner { commandRegister -> COMMAND_MAP.computeIfAbsent(player, player1 -> new HashMap<>()).put(commandRegister.getName(), commandRegister)); for (String script : scripts) { - try { - globals.load(script).call(); - } catch (Exception e) { - String[] sp = e.getMessage().split(":"); - BauSystem.MESSAGE.send("SCRIPT_ERROR_GLOBAL", player, String.join(":", Arrays.copyOfRange(sp, 1, sp.length))); - } + catchScript("SCRIPT_ERROR_GLOBAL", player, () -> globals.load(script).call()); } } @@ -119,13 +108,10 @@ public class ScriptRunner { }); } + final LuaValue finalEventValue = eventValue; + for (LuaFunction luaFunction : luaFunctions) { - try { - luaFunction.call(eventValue); - } catch (Exception e) { - String[] sp = e.getMessage().split(":"); - BauSystem.MESSAGE.send("SCRIPT_ERROR_GLOBAL", player, String.join(":", Arrays.copyOfRange(sp, 1, sp.length))); - } + catchScript("SCRIPT_ERROR_GLOBAL", player, () -> luaFunction.call(finalEventValue)); } if (wrappedEvent instanceof Cancellable) { @@ -192,22 +178,30 @@ public class ScriptRunner { } }); - try { - commandRegister.getFunction().call(args); - } catch (Exception e) { - String[] sp = e.getMessage().split(":"); - BauSystem.MESSAGE.send("SCRIPT_ERROR_GLOBAL", player, String.join(":", Arrays.copyOfRange(sp, 1, sp.length))); - } + catchScript("SCRIPT_ERROR_GLOBAL", player, () -> commandRegister.getFunction().call(args)); return true; } public static void callHotkey(int mods, int key, Player player, boolean pressed) { Hotkey hotkey = Hotkey.fromChar(key, mods); + catchScript("SCRIPT_ERROR_GLOBAL", player, () -> HOTKEY_MAP.getOrDefault(player, Collections.emptyMap()).getOrDefault(hotkey, Collections.emptyList()).forEach(luaFunction -> luaFunction.call(LuaValue.valueOf(pressed)))); + } + + public static void catchScript(String errorMsg, Player player, Runnable run) { try { - HOTKEY_MAP.getOrDefault(player, Collections.emptyMap()).getOrDefault(hotkey, Collections.emptyList()).forEach(luaFunction -> luaFunction.call(LuaValue.valueOf(pressed))); + run.run(); } catch (Exception e) { String[] sp = e.getMessage().split(":"); - BauSystem.MESSAGE.send("SCRIPT_ERROR_GLOBAL", player, String.join(":", Arrays.copyOfRange(sp, 1, sp.length))); + int index = 0; + for (int i = sp.length - 1; i >= 0; i--) { + String[] ss = sp[i].split(" "); + boolean num = ss[0].chars().mapToObj(Character::isDigit).allMatch(b -> b); + if (num && !ss[0].isEmpty()) { + index = i; + break; + } + } + BauSystem.MESSAGE.send(errorMsg, player, String.join(":", Arrays.copyOfRange(sp, index, sp.length))); } } }