forked from SteamWar/SteamWar
Improved tracer Lib
This commit is contained in:
+17
-29
@@ -6,15 +6,8 @@ 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
|
||||
@@ -23,12 +16,19 @@ public class TracerLib implements LuaLib{
|
||||
}
|
||||
|
||||
private LuaTable convertTrace(Trace trace) {
|
||||
return LuaValue.listOf(
|
||||
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)
|
||||
);
|
||||
.stream()
|
||||
.map((history) -> LuaValue.listOf(history.stream().map(this::convertTntPoint).toArray(LuaValue[]::new)))
|
||||
.toArray(LuaValue[]::new)
|
||||
)));
|
||||
|
||||
luaTrace.set("getId", getter(() -> TraceManager.instance.getId(trace)));
|
||||
|
||||
|
||||
return luaTrace;
|
||||
}
|
||||
|
||||
private LuaTable convertTntPoint(TNTPoint tntPoint) {
|
||||
@@ -62,23 +62,11 @@ public class TracerLib implements LuaLib{
|
||||
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))
|
||||
rootTable.set("getTraces", getter(() ->
|
||||
LuaValue.listOf(TraceManager.instance.getAll()
|
||||
.stream()
|
||||
.map((trace) -> convertTrace(trace))
|
||||
.toArray(LuaValue[]::new))
|
||||
));
|
||||
|
||||
return rootTable;
|
||||
|
||||
Reference in New Issue
Block a user