From f2e97f9b406465104f792087bcefa01cf54932f6 Mon Sep 17 00:00:00 2001 From: SwissalpS <Luke@SwissalpS.ws> Date: Fri, 31 Jan 2020 03:30:39 +0100 Subject: [PATCH 1/8] whitespace --- replacer.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/replacer.lua b/replacer.lua index 5151469..4c5b7ee 100644 --- a/replacer.lua +++ b/replacer.lua @@ -35,7 +35,7 @@ local function set_data(stack, node, mode) mode = mode or modes[1] local metadata = (node.name or "default:dirt") .. " " .. (node.param1 or 0) .. " " - .. (node.param2 or 0) .." " + .. (node.param2 or 0) .. " " .. mode local meta = stack:get_meta() meta:set_string("replacer", metadata) From 07e286c379377fffc74c0f043ad5dd32ccfffdd2 Mon Sep 17 00:00:00 2001 From: SwissalpS <Luke@SwissalpS.ws> Date: Fri, 31 Jan 2020 04:12:12 +0100 Subject: [PATCH 2/8] change mode via formspec --- replacer.lua | 59 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/replacer.lua b/replacer.lua index 4c5b7ee..c43dbe1 100644 --- a/replacer.lua +++ b/replacer.lua @@ -43,6 +43,32 @@ local function set_data(stack, node, mode) return metadata end +local replacer_form_name_modes = "replacer_replacer_mode_change" +local function get_form_modes(current_mode) + -- TODO: possibly add the info here instead of as + -- a chat message + -- TODO: add close button for mobile users who possibly can't esc + -- need feedback from mobile user to know if this is required + local formspec = "size[3.9,2]" + .. "label[0,0;Choose mode]" + if current_mode ~= modes[1] then + formspec = formspec .. "button_exit[0.0,0.6;2,0.5;mode;" .. modes[1] .. "]" + end + if current_mode ~= modes[2] then + formspec = formspec .. "button_exit[1.9,0.6;2,0.5;mode;" .. modes[2] .. "]" + end + if current_mode ~= modes[3] then + formspec = formspec .. "button_exit[0.0,1.4;2,0.5;mode;" .. modes[3] .. "]" + end + -- TODO: enable mode when it is available + --[[ + if current_mode ~= modes[4] then + formspec = formspec .. "button_exit[1.9,1.4;2,0.5;mode;" .. modes[] .. "]" + end + --]] + return formspec +end + technic.register_power_tool("replacer:replacer", replacer.max_charge) minetest.register_tool("replacer:replacer", { @@ -66,12 +92,13 @@ minetest.register_tool("replacer:replacer", { local creative_enabled = creative.is_enabled_for(name) local has_give = minetest.check_player_privs(name, "give") + -- is special-key held? (aka fast-key) if keys.aux1 then - -- Change Mode when holding the fast key - local node, mode = get_data(itemstack) - mode = modes[modes[mode]%#modes+1] - set_data(itemstack, node, mode) - inform(name, "Mode changed to: "..mode..": "..mode_infos[mode]) + -- fetch current mode + local _, mode = get_data(itemstack) + -- Show formspec to choose mode + minetest.show_formspec(name, replacer_form_name_modes, get_form_modes(mode)) + -- return unchanged tool return itemstack end @@ -166,6 +193,28 @@ minetest.register_tool("replacer:replacer", { end, }) +local function replacer_register_on_player_receive_fields(player, form_name, fields) + -- no need to process if it's not expected formspec that triggered call + if form_name ~= replacer_form_name_modes then return end + -- no need to process if user closed formspec without changing mode + if nil == fields.mode then return end + + -- collect some information + local itemstack = player:get_wielded_item() + local node, _ = get_data(itemstack) + local mode = fields.mode + local name = player:get_player_name() + + -- set metadata and itemstring + set_data(itemstack, node, mode) + -- update wielded item + player:set_wielded_item(itemstack) + -- spam players chat with information + inform(name, "Mode changed to: " .. mode .. ": " .. mode_infos[mode]) +end +-- listen to submitted fields +minetest.register_on_player_receive_fields(replacer_register_on_player_receive_fields) + local poshash = minetest.hash_node_position -- cache results of minetest.get_node From b89a261fae0b0ab39556c7a48cc5298f2b1599ba Mon Sep 17 00:00:00 2001 From: SwissalpS <Luke@SwissalpS.ws> Date: Fri, 31 Jan 2020 04:33:22 +0100 Subject: [PATCH 3/8] allow changing mode with special+left-click To help avoid confusion with drills which set mode with left-click For backward compatibility we keep right-click working too Nice side-effect: can now change mode without actually pointing at a node. --- replacer.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/replacer.lua b/replacer.lua index c43dbe1..391d64c 100644 --- a/replacer.lua +++ b/replacer.lua @@ -498,7 +498,19 @@ function replacer.replace(itemstack, user, pt, right_clicked) return end + local keys = user:get_player_control() local name = user:get_player_name() + + -- is special-key held? (aka fast-key) + if keys.aux1 then + -- fetch current mode + local _, mode = get_data(itemstack) + -- Show formspec to choose mode + minetest.show_formspec(name, replacer_form_name_modes, get_form_modes(mode)) + -- return unchanged tool + return itemstack + end + local creative_enabled = creative.is_enabled_for(name) if pt.type ~= "node" then From e2d47238f673c8626a9d50bac75303f27cc4f6ea Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS <Luke@SwissalpS.ws> Date: Fri, 31 Jan 2020 05:04:44 +0100 Subject: [PATCH 4/8] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e65ccf8..3a3e1d4 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ will be taken from your inventory. # Modes -Special-right-click to cycle through the modes. Single-mode does not need any charge. The other modes do. +Special-right-click on a node or special-left-click anywhere to change the mode. +Single-mode does not need any charge. The other modes do. The second tool included in this mod is the inspector. From de4cf4f2ef6892e1329ac04f8ac2d5fd31ab6595 Mon Sep 17 00:00:00 2001 From: SwissalpS <Luke@SwissalpS.ws> Date: Fri, 31 Jan 2020 16:01:04 +0100 Subject: [PATCH 5/8] restore old behaviour for less user-friction --- replacer.lua | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/replacer.lua b/replacer.lua index 391d64c..6be58bf 100644 --- a/replacer.lua +++ b/replacer.lua @@ -95,10 +95,14 @@ minetest.register_tool("replacer:replacer", { -- is special-key held? (aka fast-key) if keys.aux1 then -- fetch current mode - local _, mode = get_data(itemstack) - -- Show formspec to choose mode - minetest.show_formspec(name, replacer_form_name_modes, get_form_modes(mode)) - -- return unchanged tool + local node, mode = get_data(itemstack) + -- increment and roll-over mode + mode = modes[modes[mode]%#modes+1] + -- update tool + set_data(itemstack, node, mode) + -- spam chat + inform(name, "Mode changed to: " .. mode .. ": " .. mode_infos[mode]) + -- return changed tool return itemstack end From 93de871b3a2fb4c0179cbca249970bfefb8d59ab Mon Sep 17 00:00:00 2001 From: SwissalpS <Luke@SwissalpS.ws> Date: Fri, 31 Jan 2020 16:50:09 +0100 Subject: [PATCH 6/8] nicer formspec also worked using button instead of exit-buttons for current setting. This way is a little more elegant imo. --- replacer.lua | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/replacer.lua b/replacer.lua index 6be58bf..bf31c06 100644 --- a/replacer.lua +++ b/replacer.lua @@ -51,19 +51,31 @@ local function get_form_modes(current_mode) -- need feedback from mobile user to know if this is required local formspec = "size[3.9,2]" .. "label[0,0;Choose mode]" - if current_mode ~= modes[1] then - formspec = formspec .. "button_exit[0.0,0.6;2,0.5;mode;" .. modes[1] .. "]" + .. "button_exit[0.0,0.6;2,0.5;" + if current_mode == modes[1] then + formspec = formspec .. "_;< " .. modes[1] .. " >]" + else + formspec = formspec .. "mode;" .. modes[1] .. "]" end - if current_mode ~= modes[2] then - formspec = formspec .. "button_exit[1.9,0.6;2,0.5;mode;" .. modes[2] .. "]" + formspec = formspec .. "button_exit[1.9,0.6;2,0.5;" + if current_mode == modes[2] then + formspec = formspec .. "_;< " .. modes[2] .. " >]" + else + formspec = formspec .. "mode;" .. modes[2] .. "]" end - if current_mode ~= modes[3] then - formspec = formspec .. "button_exit[0.0,1.4;2,0.5;mode;" .. modes[3] .. "]" + formspec = formspec .. "button_exit[0.0,1.4;2,0.5;" + if current_mode == modes[3] then + formspec = formspec .. "_;< " .. modes[3] .. " >]" + else + formspec = formspec .. "mode;" .. modes[3] .. "]" end -- TODO: enable mode when it is available --[[ - if current_mode ~= modes[4] then - formspec = formspec .. "button_exit[1.9,1.4;2,0.5;mode;" .. modes[] .. "]" + formspec = formspec .. "button_exit[1.9,1.4;2,0.5;" + if current_mode == modes[4] then + formspec = formspec .. "_;< " .. modes[4] .. " >]" + else + formspec = formspec .. "mode;" .. modes[4] .. "]" end --]] return formspec From a243a0248b8829d0fbac85d57e30b89bae27d898 Mon Sep 17 00:00:00 2001 From: SwissalpS <Luke@SwissalpS.ws> Date: Fri, 31 Jan 2020 17:00:27 +0100 Subject: [PATCH 7/8] no chat spam when using formspec --- replacer.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/replacer.lua b/replacer.lua index bf31c06..b0fac24 100644 --- a/replacer.lua +++ b/replacer.lua @@ -225,8 +225,11 @@ local function replacer_register_on_player_receive_fields(player, form_name, fie set_data(itemstack, node, mode) -- update wielded item player:set_wielded_item(itemstack) + --[[ NOTE: for now I leave this code here in case we later make this a setting in + some way that does not mute all messages of tool -- spam players chat with information inform(name, "Mode changed to: " .. mode .. ": " .. mode_infos[mode]) + --]] end -- listen to submitted fields minetest.register_on_player_receive_fields(replacer_register_on_player_receive_fields) From 7a41f0278a8f35ae65d5fcd8fa538072afe27aaa Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS <Luke@SwissalpS.ws> Date: Fri, 31 Jan 2020 17:08:10 +0100 Subject: [PATCH 8/8] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3a3e1d4..d30cbdf 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ Just wield it and click on any node or entity you want to know more about. A lim * SwissalpS * OgelGames * BuckarooBanzay +* S-S-X # License