From 34b3e74981fdf33d4acb96d1afe10219d89eccfd Mon Sep 17 00:00:00 2001 From: octacian Date: Tue, 4 Apr 2017 11:17:35 -0700 Subject: [PATCH] Add meta editor --- README.md | 4 +- config.txt | 2 + init.lua | 5 ++ meta_editor.lua | 121 ++++++++++++++++++++++++++++++ textures/debugger_meta_editor.png | Bin 0 -> 233 bytes 5 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 meta_editor.lua create mode 100644 textures/debugger_meta_editor.png diff --git a/README.md b/README.md index c132232..26da421 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ 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`. ### Planned Features -- [ ] Meta/Inv Editor +- [x] Meta/Inv Editor - [ ] Run Lua Code Under Environment (server won't crash) - [x] In-Game Formspec Editor (doc/form_editor.md) - [ ] In-Game HUD Editor (?) -
..and possibly more \ No newline at end of file +
..and possibly more diff --git a/config.txt b/config.txt index 336aa4b..d0ff8c8 100644 --- a/config.txt +++ b/config.txt @@ -1,2 +1,4 @@ not_in_creative = 1 formspec_editor = true +meta_editor = true +inv_editor = true diff --git a/init.lua b/init.lua index 7173a2c..b1c650c 100644 --- a/init.lua +++ b/init.lua @@ -25,3 +25,8 @@ end if Settings.formspec_editor ~= false then dofile(modpath.."/form_editor.lua") end + +-- Load meta/inv editor +if Settings.meta_editor ~= false then + dofile(modpath.."/meta_editor.lua") +end diff --git a/meta_editor.lua b/meta_editor.lua new file mode 100644 index 0000000..903026b --- /dev/null +++ b/meta_editor.lua @@ -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, +}) diff --git a/textures/debugger_meta_editor.png b/textures/debugger_meta_editor.png new file mode 100644 index 0000000000000000000000000000000000000000..b91937e93560474a21db3d14dc4b524d5a1662af GIT binary patch literal 233 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmPt3yTmd*GoOiP@qtmr;B5V#p&dP1p)?#BsOi@^xyO1 zj~^eC-PnLYY(|=?scDez|NkHnwUmjfTGf)%R=hd7l6kg`@UBHOtj|U8MlRYkUGZ6; zahrwk(#DRf3?<