diff options
author | Wuzzy <wuzzy2@mail.ru> | 2021-03-16 17:37:24 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-16 18:37:24 +0100 |
commit | 91135381421f646e0f6a8d9201b5cdc7e42605e1 (patch) | |
tree | c208de82e645da4564c533fad3ce46a5f0343acf /games/devtest/mods/testtools/init.lua | |
parent | 88f514ad7a849eb4a67b5ee2c41c93856cf3808f (diff) | |
download | minetest-91135381421f646e0f6a8d9201b5cdc7e42605e1.tar.gz minetest-91135381421f646e0f6a8d9201b5cdc7e42605e1.tar.bz2 minetest-91135381421f646e0f6a8d9201b5cdc7e42605e1.zip |
DevTest: Formspec tests, children getter, better lighttool (#10918)
Diffstat (limited to 'games/devtest/mods/testtools/init.lua')
-rw-r--r-- | games/devtest/mods/testtools/init.lua | 196 |
1 files changed, 175 insertions, 21 deletions
diff --git a/games/devtest/mods/testtools/init.lua b/games/devtest/mods/testtools/init.lua index d578b264a..1041acdeb 100644 --- a/games/devtest/mods/testtools/init.lua +++ b/games/devtest/mods/testtools/init.lua @@ -3,8 +3,6 @@ local F = minetest.formspec_escape dofile(minetest.get_modpath("testtools") .. "/light.lua") --- TODO: Add a Node Metadata tool - minetest.register_tool("testtools:param2tool", { description = S("Param2 Tool") .."\n".. S("Modify param2 value of nodes") .."\n".. @@ -111,25 +109,6 @@ minetest.register_tool("testtools:node_setter", { end, }) -minetest.register_on_player_receive_fields(function(player, formname, fields) - if formname == "testtools:node_setter" then - local playername = player:get_player_name() - local witem = player:get_wielded_item() - if witem:get_name() == "testtools:node_setter" then - if fields.nodename and fields.param2 then - local param2 = tonumber(fields.param2) - if not param2 then - return - end - local meta = witem:get_meta() - meta:set_string("node", fields.nodename) - meta:set_int("node_param2", param2) - player:set_wielded_item(witem) - end - end - end -end) - minetest.register_tool("testtools:remover", { description = S("Remover") .."\n".. S("Punch: Remove pointed node or object"), @@ -634,6 +613,54 @@ minetest.register_tool("testtools:object_attacher", { end, }) +local function print_object(obj) + if obj:is_player() then + return "player '"..obj:get_player_name().."'" + elseif obj:get_luaentity() then + return "LuaEntity '"..obj:get_luaentity().name.."'" + else + return "object" + end +end + +minetest.register_tool("testtools:children_getter", { + description = S("Children Getter") .."\n".. + S("Shows list of objects attached to object") .."\n".. + S("Punch object to show its 'children'") .."\n".. + S("Punch air to show your own 'children'"), + inventory_image = "testtools_children_getter.png", + groups = { testtool = 1, disable_repair = 1 }, + on_use = function(itemstack, user, pointed_thing) + if user and user:is_player() then + local name = user:get_player_name() + local selected_object + local self_name + if pointed_thing.type == "object" then + selected_object = pointed_thing.ref + elseif pointed_thing.type == "nothing" then + selected_object = user + else + return + end + self_name = print_object(selected_object) + local children = selected_object:get_children() + local ret = "" + for c=1, #children do + ret = ret .. "* " .. print_object(children[c]) + if c < #children then + ret = ret .. "\n" + end + end + if ret == "" then + ret = S("No children attached to @1.", self_name) + else + ret = S("Children of @1:", self_name) .. "\n" .. ret + end + minetest.chat_send_player(user:get_player_name(), ret) + end + end, +}) + -- Use loadstring to parse param as a Lua value local function use_loadstring(param, player) -- For security reasons, require 'server' priv, just in case @@ -666,6 +693,68 @@ local function use_loadstring(param, player) return true, errOrResult end +-- Node Meta Editor +local node_meta_posses = {} +local node_meta_latest_keylist = {} + +local function show_node_meta_formspec(user, pos, key, value, keylist) + local textlist + if keylist then + textlist = "textlist[0,0.5;2.5,6.5;keylist;"..keylist.."]" + else + textlist = "" + end + minetest.show_formspec(user:get_player_name(), + "testtools:node_meta_editor", + "size[15,9]".. + "label[0,0;"..F(S("Current keys:")).."]".. + textlist.. + "field[3,0.5;12,1;key;"..F(S("Key"))..";"..F(key).."]".. + "textarea[3,1.5;12,6;value;"..F(S("Value (use empty value to delete key)"))..";"..F(value).."]".. + "button[0,8;3,1;get;"..F(S("Get value")).."]".. + "button[4,8;3,1;set;"..F(S("Set value")).."]".. + "label[0,7.2;"..F(S("pos = @1", minetest.pos_to_string(pos))).."]") +end + +local function get_node_meta_keylist(meta, playername, escaped) + local keys = {} + local ekeys = {} + local mtable = meta:to_table() + for k,_ in pairs(mtable.fields) do + table.insert(keys, k) + if escaped then + table.insert(ekeys, F(k)) + else + table.insert(ekeys, k) + end + end + if playername then + node_meta_latest_keylist[playername] = keys + end + return table.concat(ekeys, ",") +end + +minetest.register_tool("testtools:node_meta_editor", { + description = S("Node Meta Editor") .. "\n" .. + S("Place: Edit node metadata"), + inventory_image = "testtools_node_meta_editor.png", + groups = { testtool = 1, disable_repair = 1 }, + on_place = function(itemstack, user, pointed_thing) + if pointed_thing.type ~= "node" then + return itemstack + end + if not user:is_player() then + return itemstack + end + local pos = pointed_thing.under + node_meta_posses[user:get_player_name()] = pos + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + show_node_meta_formspec(user, pos, "", "", get_node_meta_keylist(meta, user:get_player_name(), true)) + return itemstack + end, +}) + minetest.register_on_player_receive_fields(function(player, formname, fields) if not (player and player:is_player()) then return @@ -728,5 +817,70 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) editor_formspec(name, selected_objects[name], prop_to_string(props[key]), sel) return end + elseif formname == "testtools:node_setter" then + local playername = player:get_player_name() + local witem = player:get_wielded_item() + if witem:get_name() == "testtools:node_setter" then + if fields.nodename and fields.param2 then + local param2 = tonumber(fields.param2) + if not param2 then + return + end + local meta = witem:get_meta() + meta:set_string("node", fields.nodename) + meta:set_int("node_param2", param2) + player:set_wielded_item(witem) + end + end + elseif formname == "testtools:node_meta_editor" then + local name = player:get_player_name() + local pos = node_meta_posses[name] + if fields.keylist then + local evnt = minetest.explode_textlist_event(fields.keylist) + if evnt.type == "DCL" or evnt.type == "CHG" then + local keylist_table = node_meta_latest_keylist[name] + if not pos then + return + end + local meta = minetest.get_meta(pos) + if not keylist_table then + return + end + if #keylist_table == 0 then + return + end + local key = keylist_table[evnt.index] + local value = meta:get_string(key) + local keylist_escaped = {} + for k,v in pairs(keylist_table) do + keylist_escaped[k] = F(v) + end + local keylist = table.concat(keylist_escaped, ",") + show_node_meta_formspec(player, pos, key, value, keylist) + return + end + elseif fields.key and fields.key ~= "" and fields.value then + if not pos then + return + end + local meta = minetest.get_meta(pos) + if fields.get then + local value = meta:get_string(fields.key) + show_node_meta_formspec(player, pos, fields.key, value, + get_node_meta_keylist(meta, name, true)) + return + elseif fields.set then + meta:set_string(fields.key, fields.value) + show_node_meta_formspec(player, pos, fields.key, fields.value, + get_node_meta_keylist(meta, name, true)) + return + end + end end end) + +minetest.register_on_leaveplayer(function(player) + local name = player:get_player_name() + node_meta_latest_keylist[name] = nil + node_meta_posses[name] = nil +end) |