Add meta editor

This commit is contained in:
octacian 2017-04-04 11:17:35 -07:00
parent e8871b4f1a
commit 34b3e74981
5 changed files with 130 additions and 2 deletions

View File

@ -6,7 +6,7 @@ This is meant to be a simple tool for developers to use while attempting to debu
Future tools and/or buttons are hidden from the creative inventory by default. To allow access in creative rather than just via `/giveme`, set `not_in_creative` to `0` in the `config.txt`. Future tools and/or buttons are hidden from the creative inventory by default. To allow access in creative rather than just via `/giveme`, set `not_in_creative` to `0` in the `config.txt`.
### Planned Features ### Planned Features
- [ ] Meta/Inv Editor - [x] Meta/Inv Editor
- [ ] Run Lua Code Under Environment (server won't crash) - [ ] Run Lua Code Under Environment (server won't crash)
- [x] In-Game Formspec Editor (doc/form_editor.md) - [x] In-Game Formspec Editor (doc/form_editor.md)
- [ ] In-Game HUD Editor (?) - [ ] In-Game HUD Editor (?)

View File

@ -1,2 +1,4 @@
not_in_creative = 1 not_in_creative = 1
formspec_editor = true formspec_editor = true
meta_editor = true
inv_editor = true

View File

@ -25,3 +25,8 @@ end
if Settings.formspec_editor ~= false then if Settings.formspec_editor ~= false then
dofile(modpath.."/form_editor.lua") dofile(modpath.."/form_editor.lua")
end end
-- Load meta/inv editor
if Settings.meta_editor ~= false then
dofile(modpath.."/meta_editor.lua")
end

121
meta_editor.lua Normal file
View File

@ -0,0 +1,121 @@
-- debugger/meta_editor.lua
local meta_contexts = {}
-- [table] Formspecs
local forms = {
meta_main = {
get = function(name, pos)
local meta = minetest.get_meta(pos):to_table().fields
local keys = ""
local values = ""
for key, value in pairs(meta) do
keys = keys..key..","
values = values..value..","
end
-- Remove final ","s
keys = keys:sub(1, -2)
values = values:sub(1, -2)
meta_contexts[name] = {
pos = pos,
keys = keys,
values = values,
}
return [[
size[10,10]
tableoptions[highlight=#00000000]
table[-0.28,-0.29;4.7,10.7;keys;]]..keys..[[;1]
tableoptions[highlight=#467832]
table[4.4,-0.29;5.7,10.7;values;]]..values..[[;1]
]]
end,
handle = function(name, fields)
if fields.values then
local s = fields.values:split(":")
if s[1] == "DCL" and tonumber(s[3]) ~= 0 then
debugger.show_meta(name, "meta_change", true, tonumber(s[2]))
end
end
end,
},
meta_change = {
get = function(name, id)
local meta = meta_contexts[name]
if not meta then return end
local key = meta.keys:split(",")[id]
local value = meta.values:split(",")[id]
meta_contexts[name].key = key
return [[
size[10,5]
label[0,0;Editing: ]]..key..[[]
label[0,0.5;Old Value: ]]..value..[[]
field[0.3,1.3;9,1;value;;]]..value..[[]
button[0,2;2,1;back;< Back]
button[2,2;2,1;save;Save]
]]
end,
handle = function(name, fields)
local meta = meta_contexts[name]
if not meta then return end
local pos = meta.pos
if fields.back then
debugger.show_meta(name, "meta_main", true, pos)
end
if fields.save then
minetest.get_meta(pos):set_string(meta.key, fields.value)
debugger.show_meta(name, "meta_main", true, pos)
end
end,
},
}
-- [function] Show/Hide Formspecs
function debugger.show_meta(pname, fname, show, ...)
if forms[fname] then
if not minetest.get_player_by_name(pname) then
return
end
if show ~= false then
minetest.show_formspec(pname, "debugger:"..fname, forms[fname].get(pname, ...))
else
minetest.close_formspec(pname, "debugger:"..fname)
end
end
end
-- [event] on receive fields
minetest.register_on_player_receive_fields(function(player, formname, fields)
local formname = formname:split(":")
if formname[1] == "debugger" and forms[formname[2]] then
local handle = forms[formname[2]].handle
if handle then
handle(player:get_player_name(), fields)
end
end
end)
-- [register] Editor tool
minetest.register_craftitem("debugger:meta_editor", {
description = "[DEBUG] Node Meta Editor",
inventory_image = "debugger_meta_editor.png",
stack_max = 1,
groups = { not_in_creative_inventory = debugger.CREATIVE },
-- [on_use] Show editor
on_use = function(itemstack, player, pointed_thing)
local pos = pointed_thing.under
local name = player:get_player_name()
debugger.show_meta(name, "meta_main", true, pos)
end,
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 B