Skip to content

Commit 4292e3a

Browse files
committed
LuaTableBuilder#toNbtCompound
1 parent ccd0c3b commit 4292e3a

5 files changed

Lines changed: 58 additions & 11 deletions

File tree

autodocs/autodoc.lua

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

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

104104

105+
--- NBT Compound Tag
106+
---@class NbtTable
107+
local NbtTable = {}
108+
109+
105110
--- An entity.
106111
---@class Entity
107112
local Entity = {}
@@ -210,10 +215,16 @@ function ItemStack.get_item_type() end
210215
function ItemStack.get_item_id() end
211216

212217
--- Gets a component from this stack as NBT.
213-
---@param component_id string The id of the component to fetch.
218+
---@param component_id string The id of the component type to fetch.
214219
---@return table
215220
function ItemStack.get_component(component_id) end
216221

222+
--- Sets a component from this stack as NBT.
223+
---@param component_id string The id of the component type.
224+
---@param nbt table The data to write. Will be encoded into the item stack.
225+
---@return nil
226+
function ItemStack.set_component(component_id, nbt) end
227+
217228

218229
--#endregion
219230

@@ -311,6 +322,11 @@ function luafy.provide_hello_world() end
311322
---@return string
312323
function luafy.get_luaj_version() end
313324

325+
--- temp; convert table to nbt
326+
---@param table table table
327+
---@return NbtTable
328+
function luafy.nbt(table) end
329+
314330
math = {}
315331

316332
--- Creates a 3-component vector object.

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22

33
import dev.diamond.luafy.script.LuaScript;
44
import dev.diamond.luafy.script.object.AbstractScriptObject;
5-
import net.minecraft.nbt.IntTag;
5+
import net.minecraft.nbt.*;
66
import org.luaj.vm2.LuaString;
77
import org.luaj.vm2.LuaTable;
88
import org.luaj.vm2.LuaValue;
99
import org.luaj.vm2.Varargs;
1010
import org.luaj.vm2.lib.VarArgFunction;
1111

12+
import java.util.Arrays;
1213
import java.util.Collection;
1314
import java.util.List;
15+
import java.util.Optional;
1416
import java.util.function.Consumer;
1517
import java.util.function.Function;
16-
import net.minecraft.nbt.CompoundTag;
17-
import net.minecraft.nbt.ListTag;
18-
import net.minecraft.nbt.Tag;
1918

2019
public class LuaTableBuilder {
2120
private final LuaTable table;
@@ -144,12 +143,35 @@ private static LuaValue fromNbt(Tag element) {
144143
};
145144
}
146145

146+
public static CompoundTag toNbtCompound(LuaTable table) {
147+
CompoundTag nbt = new CompoundTag();
148+
for (LuaValue key : table.keys()) {
149+
toNbt(table.get(key)).ifPresent(value -> nbt.put(MetamethodImpl.tostring(key), value));
150+
}
151+
return nbt;
152+
}
147153

148-
private static Tag toNbt(LuaValue value) {
154+
private static Optional<Tag> toNbt(LuaValue value) {
149155
return switch (value.type()) {
150-
case LuaValue.TINT ->
156+
case LuaValue.TINT -> Optional.of(IntTag.valueOf(value.toint()));
157+
case LuaValue.TNUMBER -> Optional.of(FloatTag.valueOf(value.tofloat()));
158+
case LuaValue.TBOOLEAN -> Optional.of(ByteTag.valueOf(value.toboolean()));
159+
case LuaValue.TSTRING -> Optional.of(StringTag.valueOf(MetamethodImpl.tostring(value)));
160+
case LuaValue.TTABLE -> {
161+
LuaTable table = value.checktable();
162+
163+
if (Arrays.stream(table.keys()).allMatch(k -> k.type() == LuaValue.TINT)) {
164+
ListTag list = new ListTag();
165+
for (LuaValue key : table.keys()) {
166+
toNbt(table.get(key)).ifPresent(list::add);
167+
}
168+
yield Optional.of(list);
169+
} else {
170+
yield Optional.of(toNbtCompound(table));
171+
}
172+
}
151173

152-
default -> new CompoundTag();
174+
default -> Optional.empty();
153175
};
154176
}
155177
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public void addFunctions(ScriptApiBuilder apiBuilder) {
4646
builder.add("get_luaj_version", args -> {
4747
return LuaString.valueOf(Luafy.LUAJ_VER);
4848
}, "Returns the version of LuaJ used by the mod.", args -> {}, Argtypes.STRING);
49+
50+
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);
4957
});
5058

5159
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public void toTable(CompoundTag obj, LuaTableBuilder builder, LuaScript script)
3030

3131
@Override
3232
public CompoundTag toThing(LuaTable table, CommandSourceStack src, LuaScript script) {
33-
return null;
33+
LuaTable backing = table.get(PROP_TABLE).checktable();
34+
return LuaTableBuilder.toNbtCompound(backing);
3435
}
3536

3637
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void toTable(ItemStack obj, LuaTableBuilder builder, LuaScript script) {
8787
// get the component
8888
DataComponentType<Object> type = (DataComponentType<Object>) BuiltInRegistries.DATA_COMPONENT_TYPE.get(Identifier.parse(key)).orElseThrow().value();
8989

90-
CompoundTag nbt = LuaTableBuilder.toNbtCompound();
90+
CompoundTag nbt = LuaTableBuilder.toNbtCompound(data);
9191

9292
return LuaValue.NIL;
9393
});

0 commit comments

Comments
 (0)