Skip to content

Commit 882be23

Browse files
committed
remove nbttablescriptobject and just use tables bro
1 parent 4292e3a commit 882be23

7 files changed

Lines changed: 39 additions & 62 deletions

File tree

autodocs/autodoc.lua

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
--#region Information
44
-- GENERATED AUTODOC
5-
-- Generated: 2026-01-20T18:25:29.060359500
5+
-- Generated: 2026-01-21T10:19:56.649403100
66
-- Luafy Version: 2.0.0
77
-- Format: Lua LS library file
88
--#endregion
@@ -102,11 +102,6 @@ function ScriptResult.await_error() end
102102
function ScriptResult.release() end
103103

104104

105-
--- NBT Compound Tag
106-
---@class NbtTable
107-
local NbtTable = {}
108-
109-
110105
--- An entity.
111106
---@class Entity
112107
local Entity = {}
@@ -322,10 +317,10 @@ function luafy.provide_hello_world() end
322317
---@return string
323318
function luafy.get_luaj_version() end
324319

325-
--- temp; convert table to nbt
326-
---@param table table table
327-
---@return NbtTable
328-
function luafy.nbt(table) end
320+
--- Dump a table to a string.
321+
---@param table table Table to dump.
322+
---@return string
323+
function luafy.dump(table) end
329324

330325
math = {}
331326

src/main/java/dev/diamond/luafy/lua/LuaTableBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public static CompoundTag toNbtCompound(LuaTable table) {
152152
}
153153

154154
private static Optional<Tag> toNbt(LuaValue value) {
155+
155156
return switch (value.type()) {
156157
case LuaValue.TINT -> Optional.of(IntTag.valueOf(value.toint()));
157158
case LuaValue.TNUMBER -> Optional.of(FloatTag.valueOf(value.tofloat()));
@@ -160,7 +161,7 @@ private static Optional<Tag> toNbt(LuaValue value) {
160161
case LuaValue.TTABLE -> {
161162
LuaTable table = value.checktable();
162163

163-
if (Arrays.stream(table.keys()).allMatch(k -> k.type() == LuaValue.TINT)) {
164+
if (Arrays.stream(table.keys()).allMatch(LuaValue::isint)) {
164165
ListTag list = new ListTag();
165166
for (LuaValue key : table.keys()) {
166167
toNbt(table.get(key)).ifPresent(list::add);

src/main/java/dev/diamond/luafy/registry/ScriptObjects.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class ScriptObjects {
1616
public static Vec3dScriptObject VEC3D = new Vec3dScriptObject();
1717
public static ModScriptObject MOD = new ModScriptObject();
1818
public static ScriptResultScriptObject SCRIPT_RESULT = new ScriptResultScriptObject();
19-
public static NbtTableScriptObject NBT_TABLE = new NbtTableScriptObject();
2019

2120
// entities
2221
public static EntityScriptObject ENTITY = new EntityScriptObject();
@@ -32,7 +31,6 @@ public static void registerAll() {
3231
Registry.register(LuafyRegistries.SCRIPT_OBJECTS, Luafy.id("vec3d"), VEC3D);
3332
Registry.register(LuafyRegistries.SCRIPT_OBJECTS, Luafy.id("mod"), MOD);
3433
Registry.register(LuafyRegistries.SCRIPT_OBJECTS, Luafy.id("script_result"), SCRIPT_RESULT);
35-
Registry.register(LuafyRegistries.SCRIPT_OBJECTS, Luafy.id("nbt_table"), NBT_TABLE);
3634

3735
Registry.register(LuafyRegistries.SCRIPT_OBJECTS, Luafy.id("entity"), ENTITY);
3836
Registry.register(LuafyRegistries.SCRIPT_OBJECTS, Luafy.id("living_entity"), LIVING_ENTITY);

src/main/java/dev/diamond/luafy/script/api/LuafyApi.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.luaj.vm2.LuaTable;
1414
import org.luaj.vm2.LuaValue;
1515

16+
import java.util.function.Function;
17+
1618
public class LuafyApi extends AbstractScriptApi {
1719
public LuafyApi(LuaScript script) {
1820
super("luafy", script);
@@ -47,14 +49,35 @@ public void addFunctions(ScriptApiBuilder apiBuilder) {
4749
return LuaString.valueOf(Luafy.LUAJ_VER);
4850
}, "Returns the version of LuaJ used by the mod.", args -> {}, Argtypes.STRING);
4951

52+
builder.add("dump", args -> {
53+
LuaTable table = args.arg1().checktable();
5054

51-
52-
builder.add("nbt", args -> {
53-
return LuaTableBuilder.provide(ScriptObjects.NBT_TABLE, LuaTableBuilder.toNbtCompound(args.arg1().checktable()), script);
54-
}, "temp; convert table to nbt", args -> {
55-
args.add("table", Argtypes.TABLE, "table");
56-
}, ScriptObjects.NBT_TABLE);
55+
return LuaValue.valueOf(dumpTable(table));
56+
}, "Dump a table to a string.", args -> {
57+
args.add("table", Argtypes.TABLE, "Table to dump.");
58+
}, Argtypes.STRING);
5759
});
5860

5961
}
62+
63+
64+
private static String dumpTable(LuaTable t) {
65+
StringBuilder b = new StringBuilder();
66+
67+
b.append("{");
68+
for (LuaValue key : t.keys()) {
69+
LuaValue v = t.get(key);
70+
b.append(MetamethodImpl.tostring(key));
71+
b.append(":");
72+
if (v.istable()) {
73+
b.append(dumpTable(v.checktable()));
74+
} else {
75+
b.append(MetamethodImpl.tostring(v));
76+
}
77+
b.append(",");
78+
}
79+
b.append("}");
80+
81+
return b.toString();
82+
}
6083
}

src/main/java/dev/diamond/luafy/script/api/MinecraftApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import net.minecraft.world.phys.Vec3;
3232
import org.luaj.vm2.LuaBoolean;
3333
import org.luaj.vm2.LuaString;
34+
import org.luaj.vm2.LuaTable;
3435
import org.luaj.vm2.LuaValue;
3536

3637
import java.util.List;
@@ -204,7 +205,6 @@ public void addFunctions(ScriptApiBuilder apiBuilder) {
204205
args.add("count", Argtypes.INTEGER, "Count.");
205206
}, ScriptObjects.ITEM_STACK);
206207

207-
208208
});
209209
}
210210

src/main/java/dev/diamond/luafy/script/object/NbtTableScriptObject.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/main/java/dev/diamond/luafy/script/object/game/ItemStackScriptObject.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ public void toTable(ItemStack obj, LuaTableBuilder builder, LuaScript script) {
8686

8787
// get the component
8888
DataComponentType<Object> type = (DataComponentType<Object>) BuiltInRegistries.DATA_COMPONENT_TYPE.get(Identifier.parse(key)).orElseThrow().value();
89-
9089
CompoundTag nbt = LuaTableBuilder.toNbtCompound(data);
90+
Object component = type.codec().decode(NbtOps.INSTANCE, nbt).getOrThrow().getFirst();
91+
obj.set(type, component);
9192

9293
return LuaValue.NIL;
9394
});

0 commit comments

Comments
 (0)