From 3c30f57e08ba1bb150829d83b3441c706d442a59 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 28 Jul 2023 10:46:09 +0200 Subject: [PATCH] Hotfix Exceptions in InventoryLib Signed-off-by: yoyosource --- .../script/lua/libs/InventoryLib.java | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/InventoryLib.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/InventoryLib.java index 392580f4..685ea98a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/InventoryLib.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/InventoryLib.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.features.script.lua.libs; +import de.steamwar.bausystem.BauSystem; import de.steamwar.inventory.SWInventory; import de.steamwar.inventory.SWItem; import de.steamwar.linkage.Linked; @@ -35,6 +36,7 @@ import org.luaj.vm2.lib.VarArgFunction; import org.luaj.vm2.lib.ZeroArgFunction; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; @Linked @@ -80,7 +82,14 @@ public class InventoryLib implements LuaLib { } LuaFunction handler = args.checkfunction(args.narg()); - item.setCallback(clickType -> handler.call(LuaValue.valueOf(clickType.name()))); + item.setCallback(clickType -> { + try { + handler.call(LuaValue.valueOf(clickType.name())); + } catch (Exception e) { + String[] sp = e.getMessage().split(":"); + BauSystem.MESSAGE.send("SCRIPT_ERROR_CLICK", player, String.join(":", Arrays.copyOfRange(sp, 1, sp.length))); + } + }); inventory.setItem(slot, item); return LuaValue.NIL; @@ -94,7 +103,14 @@ public class InventoryLib implements LuaLib { Material material = SWItem.getMaterial(args.checkjstring(2)); String name = ChatColor.translateAlternateColorCodes('&', args.checkjstring(3)); LuaFunction handler = args.checkfunction(4); - SWItem item = new SWItem(material, name, clickType -> handler.call(LuaValue.valueOf(clickType.name()))); + SWItem item = new SWItem(material, name, clickType -> { + try { + handler.call(LuaValue.valueOf(clickType.name())); + } catch (Exception e) { + String[] sp = e.getMessage().split(":"); + BauSystem.MESSAGE.send("SCRIPT_ERROR_CLICK", player, String.join(":", Arrays.copyOfRange(sp, 1, sp.length))); + } + }); if (args.narg() >= 5) { LuaTable lore = args.checktable(5); @@ -122,7 +138,14 @@ public class InventoryLib implements LuaLib { @Override public LuaValue call(LuaValue arg) { LuaFunction function = arg.checkfunction(); - inventory.addCloseRunnable(function::call); + inventory.addCloseRunnable(() -> { + try { + function.call(); + } catch (Exception e) { + String[] sp = e.getMessage().split(":"); + BauSystem.MESSAGE.send("SCRIPT_ERROR_CLICK", player, String.join(":", Arrays.copyOfRange(sp, 1, sp.length))); + } + }); return LuaValue.NIL; } });