Skip to content

Commit

Permalink
Merge branch 'formspecModeChange' of https://github.com/SwissalpS/rep…
Browse files Browse the repository at this point in the history
  • Loading branch information
SwissalpS committed Jan 31, 2020
2 parents f88546b + 7a41f02 commit 445e188
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -49,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

Expand Down
86 changes: 83 additions & 3 deletions replacer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,52 @@ 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)
meta:set_string("color", mode_colours[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]"
.. "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
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
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
--[[
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
end

technic.register_power_tool("replacer:replacer", replacer.max_charge)

minetest.register_tool("replacer:replacer", {
Expand All @@ -66,12 +104,17 @@ 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
-- fetch current mode
local node, mode = get_data(itemstack)
-- increment and roll-over mode
mode = modes[modes[mode]%#modes+1]
-- update tool
set_data(itemstack, node, mode)
inform(name, "Mode changed to: "..mode..": "..mode_infos[mode])
-- spam chat
inform(name, "Mode changed to: " .. mode .. ": " .. mode_infos[mode])
-- return changed tool
return itemstack
end

Expand Down Expand Up @@ -166,6 +209,31 @@ 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)
--[[ 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)

local poshash = minetest.hash_node_position

-- cache results of minetest.get_node
Expand Down Expand Up @@ -449,7 +517,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
Expand Down

0 comments on commit 445e188

Please sign in to comment.