Repository navigation
Expand file tree
/
Copy pathregistry.lua
More file actions
128 lines (86 loc) · 3.23 KB
/
Copy pathregistry.lua
File metadata and controls
128 lines (86 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
local packet = require("packet")
local types = require("types")
local nbt = require("nbt")
local datapack = require("datapack")
local registry = {}
local TagClass = nbt.TagClass
-- Convert JSON-decoded entry data into NBT tags.
local function tableToNBT(value, name)
local t = type(value)
if t == "table" then
-- dkjson tags decoded arrays/objects with __jsontype so empty
-- [] and {} can be told apart.
local mt = getmetatable(value)
if mt and mt.__jsontype == "array" then
local elements = {}
for i, v in ipairs(value) do
elements[i] = tableToNBT(v)
end
-- NBT lists are homogeneous; normalize mixed int/double lists
-- to all doubles (NbtOps is lenient about numeric types).
local allNumeric = #elements > 0
local allInts = true
for _, el in ipairs(elements) do
local id = el:getTypeID()
if id ~= nbt.TAG_INT and id ~= nbt.TAG_DOUBLE then
allNumeric = false
break
end
if id ~= nbt.TAG_INT then
allInts = false
end
end
if allNumeric and not allInts then
for i, el in ipairs(elements) do
if el:getTypeID() == nbt.TAG_INT then
elements[i] = nbt.newDouble(el:getValue())
end
end
end
return TagClass.new(nbt.TAG_LIST, elements, name)
else
local compound = {}
for k, v in pairs(value) do
compound[#compound + 1] = tableToNBT(v, k)
end
return TagClass.new(nbt.TAG_COMPOUND, compound, name)
end
elseif t == "string" then
return TagClass.new(nbt.TAG_STRING, value, name)
elseif t == "boolean" then
return TagClass.new(nbt.TAG_BYTE, value and 1 or 0, name)
elseif t == "number" then
if math.floor(value) == value then
return TagClass.new(nbt.TAG_INT, value, name)
else
return TagClass.new(nbt.TAG_DOUBLE, value, name)
end
else
error("Unsupported NBT type: " .. t)
end
end
function registry.send(client)
for _, reg in ipairs(datapack.registries()) do
local payload = ""
-- Registry identifier
payload = payload .. types.writeString(reg.id)
-- Entry count
payload = payload .. types.writeVarInt(#reg.entries)
for _, entry in ipairs(reg.entries) do
-- Entry identifier
payload = payload .. types.writeString(entry.name)
-- Optional NBT
if next(entry.data) then
payload = payload .. types.writeBoolean(true)
local tag = tableToNBT(entry.data)
-- Network NBT: type byte + value, no root name (NbtIo.writeAnyTag, 26.x)
payload = payload .. string.char(tag:getTypeID()) .. tag:encode(true)
else
payload = payload .. types.writeBoolean(false)
end
end
-- One registry per packet
client:send(packet.encode(7, payload))
end
end
return registry