SteamWar/BauSystem2.0
Archiviert
12
0

Hotfix player lib and world edit lib
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2023-07-19 22:46:20 +02:00
Ursprung 8fe93d39a6
Commit 0b992a5663
3 geänderte Dateien mit 61 neuen und 20 gelöschten Zeilen

Datei anzeigen

@ -36,10 +36,14 @@ public interface LuaLib {
return LuaValue.NIL; return LuaValue.NIL;
} }
Class<?> clazz = value.getClass(); Class<?> clazz = value.getClass();
if (clazz == Integer.class || clazz == Long.class) { if (clazz == Integer.class) {
return LuaValue.valueOf((int) value); 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); return LuaValue.valueOf((double) value);
} else if (clazz == Float.class) {
return LuaValue.valueOf((float) value);
} else if (clazz == String.class) { } else if (clazz == String.class) {
return LuaValue.valueOf((String) value); return LuaValue.valueOf((String) value);
} else if (clazz == Boolean.class) { } else if (clazz == Boolean.class) {
@ -57,8 +61,8 @@ public interface LuaLib {
}; };
} }
default <T> LuaFunction getterAndSetter(Supplier<T> supplier, Consumer<T> consumer) { default <T> LuaFunction getterAndSetter(String name, Supplier<T> supplier, Consumer<T> consumer) {
return new GetterAndSetter<>(supplier, consumer); return new GetterAndSetter<>(name, supplier, consumer);
} }
default String varArgsToString(Varargs varargs) { default String varArgsToString(Varargs varargs) {
@ -74,6 +78,7 @@ public interface LuaLib {
@AllArgsConstructor @AllArgsConstructor
class GetterAndSetter<T> extends VarArgFunction { class GetterAndSetter<T> extends VarArgFunction {
private String name;
private Supplier<T> supplier; private Supplier<T> supplier;
private Consumer consumer; private Consumer consumer;
@ -83,21 +88,35 @@ public interface LuaLib {
return runGetter(supplier); return runGetter(supplier);
} else { } else {
if (args.narg() == 1) { if (args.narg() == 1) {
LuaValue luaValue = args.arg(0); LuaValue luaValue = args.arg(1);
try { try {
if (luaValue instanceof LuaBoolean) { try {
consumer.accept(luaValue.toboolean()); 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()); consumer.accept(luaValue.toint());
} else if (luaValue instanceof LuaDouble) { return NIL;
} catch (Exception ignored) {}
try {
consumer.accept(luaValue.todouble()); 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()); consumer.accept(luaValue.toString());
} else { return NIL;
throw new LuaError("Invalid lua type: " + luaValue.typename()); } catch (Exception ignored) {}
} throw new LuaError("Invalid lua type: " + luaValue.typename());
} catch (Throwable throwable) { } catch (Throwable throwable) {
throw new LuaError("Error in '" + checkfunction().name() + "' " + throwable.getMessage()); throw new LuaError("Error in '" + name + "' " + throwable.getMessage());
} }
} }
return NIL; return NIL;

Datei anzeigen

@ -22,9 +22,11 @@ package de.steamwar.bausystem.features.script.lua.libs;
import de.steamwar.linkage.Linked; import de.steamwar.linkage.Linked;
import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Print;
import org.luaj.vm2.Varargs; import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.VarArgFunction; import org.luaj.vm2.lib.VarArgFunction;
@ -41,15 +43,35 @@ public class PlayerLib implements LuaLib {
table.set("chat", new Print(player)); table.set("chat", new Print(player));
table.set("actionbar", new SendActionbar(player)); table.set("actionbar", new SendActionbar(player));
table.set("x", getterAndSetter(() -> player.getLocation().getX(), player.getLocation()::setX)); table.set("x", getterAndSetter("x", () -> player.getLocation().getX(), x -> {
table.set("y", getterAndSetter(() -> player.getLocation().getY(), player.getLocation()::setY)); Location location = player.getLocation();
table.set("z", getterAndSetter(() -> player.getLocation().getZ(), player.getLocation()::setZ)); location.setX(x);
table.set("yaw", getterAndSetter(() -> (double) player.getLocation().getYaw(), d -> player.getLocation().setYaw((float) (double) d))); player.teleport(location);
table.set("pitch", getterAndSetter(() -> (double) player.getLocation().getPitch(), d -> player.getLocation().setPitch((float) (double) d))); }));
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("sneaking", getter(player::isSneaking));
table.set("sprinting", getter(player::isSprinting)); 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("item", getter(() -> player.getInventory().getItemInMainHand().getType().name()));
table.set("offHandItem", getter(() -> player.getInventory().getItemInOffHand().getType().name())); table.set("offHandItem", getter(() -> player.getInventory().getItemInOffHand().getType().name()));
return table; return table;

Datei anzeigen

@ -38,7 +38,7 @@ public class WorldEditLib implements LuaLib {
@Override @Override
public LuaTable get(Player player) { public LuaTable get(Player player) {
LuaTable table = new LuaTable(); LuaTable table = new LuaTable();
table.set("selection", getterAndSetter(() -> { table.set("selection", getterAndSetter("selection", () -> {
LuaTable selection = new LuaTable(); LuaTable selection = new LuaTable();
selection.set("min", posFromVec(WorldEdit.getInstance().getSessionManager().get(new BukkitPlayer(player)).getSelectionWorld().getMinimumPoint())); 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())); selection.set("max", posFromVec(WorldEdit.getInstance().getSessionManager().get(new BukkitPlayer(player)).getSelectionWorld().getMaximumPoint()));