diff --git a/handlers/mesecons.lua b/handlers/mesecons.lua deleted file mode 100644 index 2003007..0000000 --- a/handlers/mesecons.lua +++ /dev/null @@ -1,142 +0,0 @@ -local allowed_nodes = { - ["mesecons_switch:mesecon_switch_off"] = true, - ["mesecons_switch:mesecon_switch_on"] = true -} - -local switch_off_def = assert(minetest.registered_nodes["mesecons_switch:mesecon_switch_off"]) -local switch_on_def = assert(minetest.registered_nodes["mesecons_switch:mesecon_switch_on"]) - --- ui -> game --- enable/disable switch -mtui.register_on_command("mesecons_set", function(data) - minetest.load_area(data.pos) - local node = minetest.get_node_or_nil(data.pos) - if node == nil then - -- not loaded - return { success = false } - end - - if data.state == "on" then - if node.name == "mesecons_switch:mesecon_switch_off" then - -- switch in off-state, turn on - switch_off_def.on_rightclick(data.pos, node) - return { success = true } - end - elseif data.state == "off" then - if node.name == "mesecons_switch:mesecon_switch_on" then - -- switch in on-state, turn off - switch_on_def.on_rightclick(data.pos, node) - return { success = true } - end - end - - return { success = false } -end) - --- game -> ui --- catch switch states - --- on -> off -local old_switch_on_rightclick_action = switch_on_def.on_rightclick -switch_on_def.on_rightclick = function(pos, node) - mtui.send_command({ - type = "mesecons_event", - data = { - pos = pos, - state = "off", - nodename = "mesecons_switch:mesecon_switch_off" - } - }) - return old_switch_on_rightclick_action(pos, node) -end - --- off -> on -local old_switch_off_rightclick_action = switch_off_def.on_rightclick -switch_off_def.on_rightclick = function(pos, node) - mtui.send_command({ - type = "mesecons_event", - data = { - pos = pos, - state = "on", - nodename = "mesecons_switch:mesecon_switch_on" - } - }) - return old_switch_off_rightclick_action(pos, node) -end - - --- catch on/off events from lightstones -local lightstone_colors = {"red", "green", "blue", "gray", "darkgray", - "yellow", "orange", "white", "pink", "magenta", "cyan", "violet"} -for _, color in ipairs(lightstone_colors) do - local on_nodename = "mesecons_lightstone:lightstone_" .. color .. "_on" - local off_nodename = "mesecons_lightstone:lightstone_" .. color .. "_off" - allowed_nodes[on_nodename] = true - allowed_nodes[off_nodename] = true - - local def_on = assert(minetest.registered_nodes[on_nodename]) - local old_action_off = assert(def_on.mesecons.effector.action_off) - def_on.mesecons.effector.action_off = function(pos, node) - mtui.send_command({ - type = "mesecons_event", - data = { - pos = pos, - state = "off", - nodename = off_nodename - } - }) - return old_action_off(pos, node) - end - - local def_off = assert(minetest.registered_nodes[off_nodename]) - local old_action_on = assert(def_off.mesecons.effector.action_on) - def_off.mesecons.effector.action_on = function(pos, node) - mtui.send_command({ - type = "mesecons_event", - data = { - pos = pos, - state = "on", - nodename = on_nodename - } - }) - return old_action_on(pos, node) - end -end - -minetest.register_tool("mtui:register_mesecons", { - description = "UI Register wand", - inventory_image = "mtui_register_mesecons.png", - stack_max = 1, - on_use = function(_, player, pointed_thing) - local pos = pointed_thing.under - if not pos then - return - end - local node = minetest.get_node_or_nil(pos) - local playername = player:get_player_name() - - if not allowed_nodes[node.name] then - minetest.chat_send_player(playername, "Node '" .. node.name .. - "' at position '" .. minetest.pos_to_string(pos) .. "' not supported!") - return - end - - if minetest.is_protected(pos, playername) then - minetest.chat_send_player(playername, "Node '" .. node.name .. - "' at position '" .. minetest.pos_to_string(pos) .. "' is protected!") - return - end - - mtui.send_command({ - type = "mesecons_register", - data = { - pos = pos, - playername = playername, - nodename = node.name - } - }) - - minetest.chat_send_player(playername, "Node '" .. node.name .. - "' at position '" .. minetest.pos_to_string(pos) .. "' has been registered") - end -}) \ No newline at end of file diff --git a/init.lua b/init.lua index 47be527..4d6a396 100644 --- a/init.lua +++ b/init.lua @@ -33,7 +33,10 @@ dofile(MP.."/handlers/lua.lua") dofile(MP.."/handlers/controls.lua") if minetest.get_modpath("mesecons_switch") and minetest.get_modpath("mesecons_lightstone") then - dofile(MP.."/handlers/mesecons.lua") + dofile(MP.."/mesecons/common.lua") + dofile(MP.."/mesecons/lightstones.lua") + dofile(MP.."/mesecons/switch.lua") + dofile(MP.."/mesecons/register_tool.lua") end if not minetest.get_modpath("xp_redo") then diff --git a/mesecons/common.lua b/mesecons/common.lua new file mode 100644 index 0000000..27183dd --- /dev/null +++ b/mesecons/common.lua @@ -0,0 +1,3 @@ +mtui.mesecons = { + allowed_nodes = {} +} diff --git a/mesecons/lightstones.lua b/mesecons/lightstones.lua new file mode 100644 index 0000000..6ac27da --- /dev/null +++ b/mesecons/lightstones.lua @@ -0,0 +1,52 @@ + +-- catch on/off events from lightstones +local lightstone_colors = { + "red", + "green", + "blue", + "gray", + "darkgray", + "yellow", + "orange", + "white", + "pink", + "magenta", + "cyan", + "violet" +} + +for _, color in ipairs(lightstone_colors) do + local on_nodename = "mesecons_lightstone:lightstone_" .. color .. "_on" + local off_nodename = "mesecons_lightstone:lightstone_" .. color .. "_off" + + mtui.mesecons.allowed_nodes[on_nodename] = true + mtui.mesecons.allowed_nodes[off_nodename] = true + + local def_on = assert(minetest.registered_nodes[on_nodename]) + local old_action_off = assert(def_on.mesecons.effector.action_off) + def_on.mesecons.effector.action_off = function(pos, node) + mtui.send_command({ + type = "mesecons_event", + data = { + pos = pos, + state = "off", + nodename = off_nodename + } + }) + return old_action_off(pos, node) + end + + local def_off = assert(minetest.registered_nodes[off_nodename]) + local old_action_on = assert(def_off.mesecons.effector.action_on) + def_off.mesecons.effector.action_on = function(pos, node) + mtui.send_command({ + type = "mesecons_event", + data = { + pos = pos, + state = "on", + nodename = on_nodename + } + }) + return old_action_on(pos, node) + end +end diff --git a/mesecons/register_tool.lua b/mesecons/register_tool.lua new file mode 100644 index 0000000..5cbc52f --- /dev/null +++ b/mesecons/register_tool.lua @@ -0,0 +1,38 @@ + +minetest.register_tool("mtui:register_mesecons", { + description = "UI Register wand", + inventory_image = "mtui_register_mesecons.png", + stack_max = 1, + on_use = function(_, player, pointed_thing) + local pos = pointed_thing.under + if not pos then + return + end + local node = minetest.get_node_or_nil(pos) + local playername = player:get_player_name() + + if not mtui.mesecons.allowed_nodes[node.name] then + minetest.chat_send_player(playername, "Node '" .. node.name .. + "' at position '" .. minetest.pos_to_string(pos) .. "' not supported!") + return + end + + if minetest.is_protected(pos, playername) then + minetest.chat_send_player(playername, "Node '" .. node.name .. + "' at position '" .. minetest.pos_to_string(pos) .. "' is protected!") + return + end + + mtui.send_command({ + type = "mesecons_register", + data = { + pos = pos, + playername = playername, + nodename = node.name + } + }) + + minetest.chat_send_player(playername, "Node '" .. node.name .. + "' at position '" .. minetest.pos_to_string(pos) .. "' has been registered") + end +}) \ No newline at end of file diff --git a/mesecons/switch.lua b/mesecons/switch.lua new file mode 100644 index 0000000..0d47698 --- /dev/null +++ b/mesecons/switch.lua @@ -0,0 +1,66 @@ +local switch_off_name = "mesecons_switch:mesecon_switch_off" +local switch_on_name = "mesecons_switch:mesecon_switch_on" + +local switch_off_def = assert(minetest.registered_nodes[switch_off_name]) +local switch_on_def = assert(minetest.registered_nodes[switch_on_name]) + +mtui.mesecons.allowed_nodes[switch_on_name] = true +mtui.mesecons.allowed_nodes[switch_off_name] = true + +-- ui -> game +-- enable/disable switch +mtui.register_on_command("mesecons_set", function(data) + minetest.load_area(data.pos) + local node = minetest.get_node_or_nil(data.pos) + if node == nil then + -- not loaded + return { success = false } + end + + if data.state == "on" then + if node.name == switch_off_name then + -- switch in off-state, turn on + switch_off_def.on_rightclick(data.pos, node) + return { success = true } + end + elseif data.state == "off" then + if node.name == switch_on_name then + -- switch in on-state, turn off + switch_on_def.on_rightclick(data.pos, node) + return { success = true } + end + end + + return { success = false } +end) + +-- game -> ui +-- catch switch states + +-- on -> off +local old_switch_on_rightclick_action = switch_on_def.on_rightclick +switch_on_def.on_rightclick = function(pos, node) + mtui.send_command({ + type = "mesecons_event", + data = { + pos = pos, + state = "off", + nodename = switch_off_name + } + }) + return old_switch_on_rightclick_action(pos, node) +end + +-- off -> on +local old_switch_off_rightclick_action = switch_off_def.on_rightclick +switch_off_def.on_rightclick = function(pos, node) + mtui.send_command({ + type = "mesecons_event", + data = { + pos = pos, + state = "on", + nodename = switch_on_name + } + }) + return old_switch_off_rightclick_action(pos, node) +end