forked from SteamWar/SteamWar
First impl
This commit is contained in:
+46
-26
@@ -3,50 +3,65 @@ package de.steamwar.bausystem.features.script.lua.libs;
|
||||
import de.steamwar.bausystem.features.tracer.TNTPoint;
|
||||
import de.steamwar.bausystem.features.tracer.Trace;
|
||||
import de.steamwar.bausystem.features.tracer.TraceManager;
|
||||
import de.steamwar.linkage.Linked;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.ZeroArgFunction;
|
||||
|
||||
public class TracerLib implements LuaLib{
|
||||
@Linked
|
||||
public class TracerLib implements LuaLib {
|
||||
@Override
|
||||
public String name() {
|
||||
return "tracer";
|
||||
}
|
||||
|
||||
private LuaTable convertTrace(Trace trace) {
|
||||
private static LuaTable convertTrace(Trace trace) {
|
||||
LuaTable luaTrace = new LuaTable();
|
||||
|
||||
luaTrace.set("getRecords", getter(() -> LuaValue.listOf(
|
||||
trace.getHistories()
|
||||
.stream()
|
||||
.map((history) -> LuaValue.listOf(history.stream().map(this::convertTntPoint).toArray(LuaValue[]::new)))
|
||||
.toArray(LuaValue[]::new)
|
||||
)));
|
||||
luaTrace.set("getRecords", new ZeroArgFunction() {
|
||||
@Override
|
||||
public LuaValue call() {
|
||||
return LuaValue.listOf(
|
||||
trace.getHistories()
|
||||
.stream()
|
||||
.map((history) -> LuaValue.listOf(history
|
||||
.stream()
|
||||
.map(TracerLib::convertTntPoint)
|
||||
.toArray(LuaValue[]::new)))
|
||||
.toArray(LuaValue[]::new));
|
||||
}
|
||||
});
|
||||
|
||||
luaTrace.set("getId", getter(() -> TraceManager.instance.getId(trace)));
|
||||
luaTrace.set("getId", new ZeroArgFunction() {
|
||||
@Override
|
||||
public LuaValue call() {
|
||||
return LuaValue.valueOf(trace.getUuid().toString());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return luaTrace;
|
||||
}
|
||||
|
||||
private LuaTable convertTntPoint(TNTPoint tntPoint) {
|
||||
private static LuaTable convertTntPoint(TNTPoint tntPoint) {
|
||||
Location pointPos = tntPoint.getLocation();
|
||||
LuaTable luaPos = LuaValue.listOf(new LuaValue[] {
|
||||
LuaValue.valueOf(pointPos.getX()),
|
||||
LuaValue.valueOf(pointPos.getY()),
|
||||
LuaValue.valueOf(pointPos.getZ()),
|
||||
LuaTable luaPos = LuaValue.tableOf(new LuaValue[]{
|
||||
LuaValue.valueOf("x"), LuaValue.valueOf(pointPos.getX()),
|
||||
LuaValue.valueOf("y"), LuaValue.valueOf(pointPos.getY()),
|
||||
LuaValue.valueOf("z"), LuaValue.valueOf(pointPos.getZ()),
|
||||
});
|
||||
|
||||
Vector pointVel = tntPoint.getVelocity();
|
||||
LuaTable luaVel = LuaValue.listOf(new LuaValue[] {
|
||||
LuaValue.valueOf(pointVel.getX()),
|
||||
LuaValue.valueOf(pointVel.getY()),
|
||||
LuaValue.valueOf(pointVel.getZ()),
|
||||
LuaTable luaVel = LuaValue.tableOf(new LuaValue[]{
|
||||
LuaValue.valueOf("x"), LuaValue.valueOf(pointVel.getX()),
|
||||
LuaValue.valueOf("y"), LuaValue.valueOf(pointVel.getY()),
|
||||
LuaValue.valueOf("z"), LuaValue.valueOf(pointVel.getZ()),
|
||||
});
|
||||
|
||||
return LuaValue.tableOf(new LuaValue[] {
|
||||
return LuaValue.tableOf(new LuaValue[]{
|
||||
LuaValue.valueOf("pos"), luaPos,
|
||||
LuaValue.valueOf("vel"), luaVel,
|
||||
LuaValue.valueOf("ticksSinceStart"), LuaValue.valueOf(tntPoint.getTicksSinceStart()),
|
||||
@@ -60,14 +75,19 @@ public class TracerLib implements LuaLib{
|
||||
|
||||
@Override
|
||||
public LuaTable get(Player player) {
|
||||
LuaTable rootTable = new LuaTable();
|
||||
LuaTable rootTable = LuaValue.tableOf();
|
||||
|
||||
rootTable.set("getTraces", getter(() ->
|
||||
LuaValue.listOf(TraceManager.instance.getAll()
|
||||
.stream()
|
||||
.map((trace) -> convertTrace(trace))
|
||||
.toArray(LuaValue[]::new))
|
||||
));
|
||||
rootTable.set("getTraces", new ZeroArgFunction() {
|
||||
@Override
|
||||
public LuaValue call() {
|
||||
return LuaValue.listOf(TraceManager.instance.getAll()
|
||||
.stream()
|
||||
.map(TracerLib::convertTrace)
|
||||
.toArray(LuaValue[]::new));
|
||||
}
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
return rootTable;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user