Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
Ursprung
8fe93d39a6
Commit
0b992a5663
@ -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 <T> LuaFunction getterAndSetter(Supplier<T> supplier, Consumer<T> consumer) {
|
||||
return new GetterAndSetter<>(supplier, consumer);
|
||||
default <T> LuaFunction getterAndSetter(String name, Supplier<T> supplier, Consumer<T> consumer) {
|
||||
return new GetterAndSetter<>(name, supplier, consumer);
|
||||
}
|
||||
|
||||
default String varArgsToString(Varargs varargs) {
|
||||
@ -74,6 +78,7 @@ public interface LuaLib {
|
||||
@AllArgsConstructor
|
||||
class GetterAndSetter<T> extends VarArgFunction {
|
||||
|
||||
private String name;
|
||||
private Supplier<T> 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;
|
||||
|
@ -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;
|
||||
|
@ -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()));
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren