From 0b992a56630993d8871a0dafe44cc74ff6febf55 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 19 Jul 2023 22:46:20 +0200 Subject: [PATCH] Hotfix player lib and world edit lib Signed-off-by: yoyosource --- .../features/script/lua/libs/LuaLib.java | 45 +++++++++++++------ .../features/script/lua/libs/PlayerLib.java | 34 +++++++++++--- .../script/lua/libs/WorldEditLib.java | 2 +- 3 files changed, 61 insertions(+), 20 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/LuaLib.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/LuaLib.java index 4167c4cc..b88ee9d0 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/LuaLib.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/LuaLib.java @@ -36,10 +36,14 @@ public interface LuaLib { return LuaValue.NIL; } Class clazz = value.getClass(); - if (clazz == Integer.class || clazz == Long.class) { + if (clazz == Integer.class) { return LuaValue.valueOf((int) value); - } else if (clazz == Double.class || clazz == Float.class) { + } else if (clazz == Long.class) { + return LuaValue.valueOf((int) (long) value); + } else if (clazz == Double.class) { return LuaValue.valueOf((double) value); + } else if (clazz == Float.class) { + return LuaValue.valueOf((float) value); } else if (clazz == String.class) { return LuaValue.valueOf((String) value); } else if (clazz == Boolean.class) { @@ -57,8 +61,8 @@ public interface LuaLib { }; } - default LuaFunction getterAndSetter(Supplier supplier, Consumer consumer) { - return new GetterAndSetter<>(supplier, consumer); + default LuaFunction getterAndSetter(String name, Supplier supplier, Consumer consumer) { + return new GetterAndSetter<>(name, supplier, consumer); } default String varArgsToString(Varargs varargs) { @@ -74,6 +78,7 @@ public interface LuaLib { @AllArgsConstructor class GetterAndSetter extends VarArgFunction { + private String name; private Supplier supplier; private Consumer consumer; @@ -83,21 +88,35 @@ public interface LuaLib { return runGetter(supplier); } else { if (args.narg() == 1) { - LuaValue luaValue = args.arg(0); + LuaValue luaValue = args.arg(1); try { - if (luaValue instanceof LuaBoolean) { + try { consumer.accept(luaValue.toboolean()); - } else if (luaValue instanceof LuaInteger) { + return NIL; + } catch (Exception ingored) {} + try { + consumer.accept((long) luaValue.toint()); + return NIL; + } catch (Exception ignored) {} + try { consumer.accept(luaValue.toint()); - } else if (luaValue instanceof LuaDouble) { + return NIL; + } catch (Exception ignored) {} + try { consumer.accept(luaValue.todouble()); - } else if (luaValue instanceof LuaString) { + return NIL; + } catch (Exception ignored) {} + try { + consumer.accept((float) luaValue.todouble()); + return NIL; + } catch (Exception ignored) {} + try { consumer.accept(luaValue.toString()); - } else { - throw new LuaError("Invalid lua type: " + luaValue.typename()); - } + return NIL; + } catch (Exception ignored) {} + throw new LuaError("Invalid lua type: " + luaValue.typename()); } catch (Throwable throwable) { - throw new LuaError("Error in '" + checkfunction().name() + "' " + throwable.getMessage()); + throw new LuaError("Error in '" + name + "' " + throwable.getMessage()); } } return NIL; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/PlayerLib.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/PlayerLib.java index 6ff3cf4b..49ad977a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/PlayerLib.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/PlayerLib.java @@ -22,9 +22,11 @@ package de.steamwar.bausystem.features.script.lua.libs; import de.steamwar.linkage.Linked; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Location; import org.bukkit.entity.Player; import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaValue; +import org.luaj.vm2.Print; import org.luaj.vm2.Varargs; import org.luaj.vm2.lib.VarArgFunction; @@ -41,15 +43,35 @@ public class PlayerLib implements LuaLib { table.set("chat", new Print(player)); table.set("actionbar", new SendActionbar(player)); - table.set("x", getterAndSetter(() -> player.getLocation().getX(), player.getLocation()::setX)); - table.set("y", getterAndSetter(() -> player.getLocation().getY(), player.getLocation()::setY)); - table.set("z", getterAndSetter(() -> player.getLocation().getZ(), player.getLocation()::setZ)); - table.set("yaw", getterAndSetter(() -> (double) player.getLocation().getYaw(), d -> player.getLocation().setYaw((float) (double) d))); - table.set("pitch", getterAndSetter(() -> (double) player.getLocation().getPitch(), d -> player.getLocation().setPitch((float) (double) d))); + table.set("x", getterAndSetter("x", () -> player.getLocation().getX(), x -> { + Location location = player.getLocation(); + location.setX(x); + player.teleport(location); + })); + table.set("y", getterAndSetter("y", () -> player.getLocation().getY(), y -> { + Location location = player.getLocation(); + location.setY(y); + player.teleport(location); + })); + table.set("z", getterAndSetter("z", () -> player.getLocation().getZ(), z -> { + Location location = player.getLocation(); + location.setZ(z); + player.teleport(location); + })); + table.set("yaw", getterAndSetter("yaw", () -> player.getLocation().getYaw(), yaw -> { + Location location = player.getLocation(); + location.setYaw(yaw); + player.teleport(location); + })); + table.set("pitch", getterAndSetter("pitch", () -> player.getLocation().getPitch(), pitch -> { + Location location = player.getLocation(); + location.setPitch(pitch); + player.teleport(location); + })); table.set("sneaking", getter(player::isSneaking)); table.set("sprinting", getter(player::isSprinting)); - table.set("slot", getterAndSetter(() -> player.getInventory().getHeldItemSlot(), player.getInventory()::setHeldItemSlot)); + table.set("slot", getterAndSetter("slot", () -> player.getInventory().getHeldItemSlot(), player.getInventory()::setHeldItemSlot)); table.set("item", getter(() -> player.getInventory().getItemInMainHand().getType().name())); table.set("offHandItem", getter(() -> player.getInventory().getItemInOffHand().getType().name())); return table; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/WorldEditLib.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/WorldEditLib.java index dae8dbb8..87d1f9fb 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/WorldEditLib.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/WorldEditLib.java @@ -38,7 +38,7 @@ public class WorldEditLib implements LuaLib { @Override public LuaTable get(Player player) { LuaTable table = new LuaTable(); - table.set("selection", getterAndSetter(() -> { + table.set("selection", getterAndSetter("selection", () -> { LuaTable selection = new LuaTable(); selection.set("min", posFromVec(WorldEdit.getInstance().getSessionManager().get(new BukkitPlayer(player)).getSelectionWorld().getMinimumPoint())); selection.set("max", posFromVec(WorldEdit.getInstance().getSessionManager().get(new BukkitPlayer(player)).getSelectionWorld().getMaximumPoint()));