First impl

This commit is contained in:
D4rkr34lm
2025-03-11 22:58:03 +01:00
parent 3b41cc4ac5
commit 394591f302
2 changed files with 65 additions and 41 deletions
@@ -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.TNTPoint;
import de.steamwar.bausystem.features.tracer.Trace; import de.steamwar.bausystem.features.tracer.Trace;
import de.steamwar.bausystem.features.tracer.TraceManager; import de.steamwar.bausystem.features.tracer.TraceManager;
import de.steamwar.linkage.Linked;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.ZeroArgFunction;
public class TracerLib implements LuaLib{ @Linked
public class TracerLib implements LuaLib {
@Override @Override
public String name() { public String name() {
return "tracer"; return "tracer";
} }
private LuaTable convertTrace(Trace trace) { private static LuaTable convertTrace(Trace trace) {
LuaTable luaTrace = new LuaTable(); LuaTable luaTrace = new LuaTable();
luaTrace.set("getRecords", getter(() -> LuaValue.listOf( luaTrace.set("getRecords", new ZeroArgFunction() {
trace.getHistories() @Override
.stream() public LuaValue call() {
.map((history) -> LuaValue.listOf(history.stream().map(this::convertTntPoint).toArray(LuaValue[]::new))) return LuaValue.listOf(
.toArray(LuaValue[]::new) 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; return luaTrace;
} }
private LuaTable convertTntPoint(TNTPoint tntPoint) { private static LuaTable convertTntPoint(TNTPoint tntPoint) {
Location pointPos = tntPoint.getLocation(); Location pointPos = tntPoint.getLocation();
LuaTable luaPos = LuaValue.listOf(new LuaValue[] { LuaTable luaPos = LuaValue.tableOf(new LuaValue[]{
LuaValue.valueOf(pointPos.getX()), LuaValue.valueOf("x"), LuaValue.valueOf(pointPos.getX()),
LuaValue.valueOf(pointPos.getY()), LuaValue.valueOf("y"), LuaValue.valueOf(pointPos.getY()),
LuaValue.valueOf(pointPos.getZ()), LuaValue.valueOf("z"), LuaValue.valueOf(pointPos.getZ()),
}); });
Vector pointVel = tntPoint.getVelocity(); Vector pointVel = tntPoint.getVelocity();
LuaTable luaVel = LuaValue.listOf(new LuaValue[] { LuaTable luaVel = LuaValue.tableOf(new LuaValue[]{
LuaValue.valueOf(pointVel.getX()), LuaValue.valueOf("x"), LuaValue.valueOf(pointVel.getX()),
LuaValue.valueOf(pointVel.getY()), LuaValue.valueOf("y"), LuaValue.valueOf(pointVel.getY()),
LuaValue.valueOf(pointVel.getZ()), LuaValue.valueOf("z"), LuaValue.valueOf(pointVel.getZ()),
}); });
return LuaValue.tableOf(new LuaValue[] { return LuaValue.tableOf(new LuaValue[]{
LuaValue.valueOf("pos"), luaPos, LuaValue.valueOf("pos"), luaPos,
LuaValue.valueOf("vel"), luaVel, LuaValue.valueOf("vel"), luaVel,
LuaValue.valueOf("ticksSinceStart"), LuaValue.valueOf(tntPoint.getTicksSinceStart()), LuaValue.valueOf("ticksSinceStart"), LuaValue.valueOf(tntPoint.getTicksSinceStart()),
@@ -60,14 +75,19 @@ public class TracerLib implements LuaLib{
@Override @Override
public LuaTable get(Player player) { public LuaTable get(Player player) {
LuaTable rootTable = new LuaTable(); LuaTable rootTable = LuaValue.tableOf();
rootTable.set("getTraces", getter(() -> rootTable.set("getTraces", new ZeroArgFunction() {
LuaValue.listOf(TraceManager.instance.getAll() @Override
.stream() public LuaValue call() {
.map((trace) -> convertTrace(trace)) return LuaValue.listOf(TraceManager.instance.getAll()
.toArray(LuaValue[]::new)) .stream()
)); .map(TracerLib::convertTrace)
.toArray(LuaValue[]::new));
}
}
);
return rootTable; return rootTable;
} }
+19 -15
View File
@@ -200,21 +200,6 @@ function tnt.onlyTb() return nil end
---@return boolean ---@return boolean
function tnt.onlyBuild() return nil end function tnt.onlyBuild() return nil end
---@class trace
local trace = {}
---@return boolean
function trace.active() return nil end
---@return boolean
function trace.auto() return nil end
---@return string
function trace.status() return nil end
---@return number
function trace.time() return nil end
---@param name string ---@param name string
---@return iregion ---@return iregion
function region.get(name) return nil end function region.get(name) return nil end
@@ -222,6 +207,25 @@ function region.get(name) return nil end
---@return iregion[] ---@return iregion[]
function region.list() return nil end function region.list() return nil end
---@class tracerLib
tracer = {}
---@class TraceRecord
---@field pos Position
---@field vel Position
---@field ticksSinceStart number
---@field fuse number
---@field isExplosion boolean
---@field isInWater boolean
---@field hasDestroyedBuild boolean
---@field hasDestroyedTestblock boolean
---@class Tracer
---@field getId fun(): string
---@field getRecords fun(): {[number]: TraceRecord}
function tracer.getTraces() return nil end
---@class Position ---@class Position
---@field x number ---@field x number
---@field y number ---@field y number