From 29cb1cc5dabb876fd401af652148f51ce9d9e885 Mon Sep 17 00:00:00 2001 From: D4rkr34lm Date: Sat, 8 Mar 2025 01:11:24 +0100 Subject: [PATCH 1/5] Implemented first tracer lib version --- .../features/script/lua/libs/TracerLib.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java new file mode 100644 index 00000000..1e6b005e --- /dev/null +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java @@ -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 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; + } +} From 3b41cc4ac5a85bed883998ee76cd2b0a11f3a7a4 Mon Sep 17 00:00:00 2001 From: D4rkr34lm Date: Mon, 10 Mar 2025 22:17:05 +0100 Subject: [PATCH 2/5] Improved tracer Lib --- .../features/script/lua/libs/TracerLib.java | 46 +++++++------------ 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java index 1e6b005e..e18cd84c 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java @@ -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 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; From 394591f3028340fee7b3c2164bd392c498df4e43 Mon Sep 17 00:00:00 2001 From: D4rkr34lm Date: Tue, 11 Mar 2025 22:58:03 +0100 Subject: [PATCH 3/5] First impl --- .../features/script/lua/libs/TracerLib.java | 72 ++++++++++++------- BauSystem/sw.def.lua | 34 +++++---- 2 files changed, 65 insertions(+), 41 deletions(-) diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java index e18cd84c..91b510e2 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java @@ -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; } diff --git a/BauSystem/sw.def.lua b/BauSystem/sw.def.lua index 613437a1..4503239c 100644 --- a/BauSystem/sw.def.lua +++ b/BauSystem/sw.def.lua @@ -200,21 +200,6 @@ function tnt.onlyTb() return nil end ---@return boolean 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 ---@return iregion function region.get(name) return nil end @@ -222,6 +207,25 @@ function region.get(name) return nil end ---@return iregion[] 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 ---@field x number ---@field y number From c94d67660afeee7ba2a420e757138e2ce031d986 Mon Sep 17 00:00:00 2001 From: D4rkr34lm Date: Wed, 12 Mar 2025 19:13:47 +0100 Subject: [PATCH 4/5] Fixed doc --- BauSystem/sw.def.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BauSystem/sw.def.lua b/BauSystem/sw.def.lua index 4503239c..50095a2d 100644 --- a/BauSystem/sw.def.lua +++ b/BauSystem/sw.def.lua @@ -222,7 +222,7 @@ tracer = {} ---@class Tracer ---@field getId fun(): string ----@field getRecords fun(): {[number]: TraceRecord} +---@field getRecords fun(): {[number]: {[number]: TraceRecord}} function tracer.getTraces() return nil end From 9467291020ecb84ab8a7e40ddbccf26988e5b9e1 Mon Sep 17 00:00:00 2001 From: D4rkr34lm Date: Tue, 18 Mar 2025 21:33:34 +0100 Subject: [PATCH 5/5] Add license header to tracer lib --- .../features/script/lua/libs/TracerLib.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java index 91b510e2..5178bdf9 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/lua/libs/TracerLib.java @@ -1,3 +1,22 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + package de.steamwar.bausystem.features.script.lua.libs; import de.steamwar.bausystem.features.tracer.TNTPoint;