Implemented first tracer lib version

This commit is contained in:
D4rkr34lm
2025-03-08 01:11:24 +01:00
parent 9bc1f8b328
commit 29cb1cc5da
@@ -0,0 +1,86 @@
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 org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaInteger;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class TracerLib implements LuaLib{
@Override
public String name() {
return "tracer";
}
private LuaTable convertTrace(Trace trace) {
return LuaValue.listOf(
trace.getHistories()
.stream()
.map((history) -> LuaValue.listOf(history.stream().map(this::convertTntPoint).toArray(LuaValue[]::new)))
.toArray(LuaValue[]::new)
);
}
private 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()),
});
Vector pointVel = tntPoint.getVelocity();
LuaTable luaVel = LuaValue.listOf(new LuaValue[] {
LuaValue.valueOf(pointVel.getX()),
LuaValue.valueOf(pointVel.getY()),
LuaValue.valueOf(pointVel.getZ()),
});
return LuaValue.tableOf(new LuaValue[] {
LuaValue.valueOf("pos"), luaPos,
LuaValue.valueOf("vel"), luaVel,
LuaValue.valueOf("ticksSinceStart"), LuaValue.valueOf(tntPoint.getTicksSinceStart()),
LuaValue.valueOf("fuse"), LuaValue.valueOf(tntPoint.getFuse()),
LuaValue.valueOf("isExplosion"), LuaValue.valueOf(tntPoint.isExplosion()),
LuaValue.valueOf("isInWater"), LuaValue.valueOf(tntPoint.isInWater()),
LuaValue.valueOf("hasDestroyedBuild"), LuaValue.valueOf(tntPoint.isDestroyedBuildArea()),
LuaValue.valueOf("hasDestroyedTestblock"), LuaValue.valueOf(tntPoint.isDestroyedTestBlock())
});
}
@Override
public LuaTable get(Player player) {
LuaTable rootTable = new LuaTable();
rootTable.set("getTrace", new OneArgFunction() {
@Override
public LuaValue call(LuaValue arg) {
int id = arg.checkint();
Optional<Trace> traceResult = TraceManager.instance.get(id);
if(traceResult.isEmpty()) {
throw new LuaError("No trace found with id " + id);
} else {
return convertTrace(traceResult.get());
}
}
});
rootTable.set("getAllTraceIds", getter(() -> LuaValue.listOf(TraceManager.instance.getAllIds()
.stream()
.map((id) -> LuaValue.valueOf(id)).toArray(LuaValue[]::new))
));
return rootTable;
}
}