-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbt.lua
270 lines (256 loc) · 8.66 KB
/
nbt.lua
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
ll.registerPlugin("PNE", "PHP-NBT-Editor", {0, 0, 2})
logger.setFile(os.date("logs/nbt/%m-%d.log"))
local help = [[
nbt help
nbt show @s "/"
nbt show @e "."
nbt set @e "xxx.xxx.xxx" "xxx" <- snbt
"xxx/xxx/xxx"
nbt show xx xx xx "!xxx/xxx/xxx" <- blockentity
nbt show "xxx/xxx/xxx" <- item
这东西很危险,用之前先备份!!!
]]
local backup = File("logs/nbt/backup.log", file.AppendMode)
-- #>
local NIL = {}
function dump(o) -- 这个是调试用的
local t = {}
local _t = {}
local _n = {}
local deepin = 4
local space, deep = ' ', 0
local function _ToString(o, _k)
if type(o) == ('number') then
table.insert(t, o)
elseif type(o) == ('string') then
table.insert(t, string.format('%q', o))
elseif type(o) == ('table') then
local mt = getmetatable(o)
if mt and mt.__tostring then
table.insert(t, tostring(o))
else
deep = deep + deepin
table.insert(t, '{')
for k, v in pairs(o) do
if v == _G then
table.insert(t, string.format('\n%s%s\t= %s;', string.rep(space, deep), k, "_G"))
elseif v ~= package.loaded then
if tonumber(k) then
k = string.format('[%s]', k)
else
k = string.format('[\"%s\"]', k)
end
table.insert(t, string.format('\n%s%s\t= ', string.rep(space, deep), k))
if v == NIL then
table.insert(t, string.format('%s;', "nil"))
elseif type(v) == ('table') then
if _t[tostring(v)] == nil then
_t[tostring(v)] = v
local _k = _k .. k
_t[tostring(v)] = _k
_ToString(v, _k)
else
table.insert(t, tostring(_t[tostring(v)]))
table.insert(t, ';')
end
else
_ToString(v, _k)
end
end
end
deep = deep - deepin
table.insert(t, string.format('\n%s}', string.rep(space, deep)))
end
else
table.insert(t, tostring(o))
end
table.insert(t, ";")
return t
end
t = _ToString(o, '')
return table.concat(t)
end
function string:split(delimiter) -- 拆分字符串
local input = tostring(self)
delimiter = tostring(delimiter)
if delimiter == "" or self == "" then
return {}
end
local pos, arr = 0, {}
for st, sp in function()
return string.find(input, delimiter, pos, true)
end do
table.insert(arr, string.sub(input, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(input, pos))
return arr
end
local function back2file(str, thing) -- 备份
backup:write(os.date("\n%x/%X BACKUP [" .. thing .. "] "), function()
backup:write(str, function()
backup:flush()
end)
end)
end
local function nbt2string(nbt) -- 标签转snbt,列表转json
-- [[
local t = nbt:getType()
if t == NBT.Compound then
return nbt:toJson(2)
elseif t == NBT.List then
return nbt:toString(2)
else
return tostring(nbt:get())
end
-- ]]
--[[
local err, bak = pcall(function()
return nbt:toSNBT(2)
end)
-- log(err,"",bak)
if err then
return bak
else
return nbt:toString(2)
end
--]]
end
local function setnbt(nbt, path, value) -- 设置nbt,获取路径
local nbt_list = {nbt} -- 每层的nbt
if path then
path = path:gsub("^!", ""):gsub("%.", "/"):gsub("^%/", ""):gsub("%/$", ""):split("/") -- 路径转列表
for d, k in ipairs(path) do -- 逐层提取nbt
if k:find("^!") then -- 数字索引模式
k = tonumber(k:sub(2, -1))
end
-- log(dump(nbt_list, nbt_list[#nbt_list]:getTag(k)))
table.insert(nbt_list, nbt_list[#nbt_list]:getTag(k))
end
end
if value then
if value == NIL then
nbt_list[#nbt_list - 1]:removeTag(path[#path])
for d = #nbt_list - 2, 1, -1 do -- 逐层塞回去
nbt_list[d] = nbt_list[d]:setTag(path[d], nbt_list[d + 1])
-- log(dump({d, k, nbt_list, path}))
end
else
-- log(dump({d, value, nbt_list, path}))
nbt_list[#nbt_list - 1]:setTag(path[#path],
NBT.parseSNBT(string.format("{\"value\":%s}", value)):getTag("value"))
for d = #nbt_list - 1, 1, -1 do -- 逐层塞回去
nbt_list[d] = nbt_list[d]:setTag(path[d], nbt_list[d + 1])
-- log(dump({d, k, nbt_list, path}))
end
end
return nbt_list[1]
else
return nbt_list[#nbt_list]
end
end
local function nbt2world(thing, nbt, res) -- 应用至世界
back2file(nbt:toSNBT(), "ToWorld")
return thing:setNbt(nbt)
--[[
if res.Block and (not res.Path:find("^!")) and false then
log(res.Block.x, " ", res.Block.y, " ", res.Block.z, " ", res.Block.dimid)
return mc.setBlock(res.Block, nbt)
else
return thing:setNbt(nbt)
end
--]]
end
local funs = { -- 枚举每个操作
show = function(thing, res)
if thing then
local snbt = nbt2string(setnbt(thing:getNbt(), res.Path))
back2file(snbt, "Show")
return snbt
else
return "NULL"
end
end,
list = function(thing, res)
if thing then
local nbt = setnbt(thing:getNbt(), res.Path)
if nbt:getType() == NBT.Compound then
return table.concat(nbt:getKeys(), " ")
elseif nbt:getType() == NBT.List() then
return nbt2string(nbt)
else
return "不是一个NBT标签或NBT列表"
end
else
return "NULL"
end
end,
set = function(thing, res)
if thing then
local nbt = thing:getNbt()
back2file(nbt2string(nbt), "Set")
nbt = setnbt(nbt, res.Path, res.Value)
return nbt2world(thing, nbt, res)
else
return "NULL"
end
end,
del = function(thing, res)
if thing then
local nbt = thing:getNbt()
back2file(nbt2string(nbt), "Del")
nbt = setnbt(nbt, res.Path, NIL)
return nbt2world(thing, nbt, res)
else
return "NULL"
end
end
}
local nbt_c = mc.newCommand("nbt", "NBT编辑器", PermType.GameMasters)
nbt_c:setEnum("ActionList", {"show", "list", "set", "del", "help"})
nbt_c:mandatory("Action", ParamType.Enum, "ActionList", 1)
-- item
nbt_c:mandatory("Block", ParamType.BlockPos)
nbt_c:mandatory("Entity", ParamType.Actor)
nbt_c:mandatory("Player", ParamType.Player)
nbt_c:mandatory("Path", ParamType.String)
nbt_c:optional("Value", ParamType.String)
-- help
nbt_c:overload({"Action"})
-- item
nbt_c:overload({"Action", "Path", "Value"})
-- block
nbt_c:overload({"Action", "Block", "Path", "Value"})
-- entity
nbt_c:overload({"Action", "Entity", "Path", "Value"})
-- player
nbt_c:overload({"Action", "Player", "Path", "Value"})
nbt_c:setCallback(function(cmd, origin, output, results)
if results.Action == "help" then
output:success(help)
elseif results.Block then -- Block
local block = mc.getBlock(results.Block)
if results.Path and results.Path:find("^!") then -- 方块实体模式
block = block:getBlockEntity()
end
output:success(tostring(funs[results.Action](block, results)))
elseif results.Entity and #results.Entity > 0 then -- Entity
if #results.Entity > 1 then
output:error("目标数量太多")
return
end
output:success(tostring(funs[results.Action](results.Entity[1], results)))
results.Entity[1]:refreshItems()
elseif results.Player and #results.Player > 0 then -- Player
if #results.Player > 1 then
output:error("目标数量太多")
return
end
output:success(tostring(funs[results.Action](results.Player[1], results)))
results.Player[1]:refreshItems()
else
output:success(tostring(funs[results.Action](origin.player:getHand(), results)))
origin.player:refreshItems()
end
end)
nbt_c:setup()