From 672c08d9fc972a8f161b434d842591d25a15778f Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Mon, 23 Oct 2023 18:10:32 +0200 Subject: [PATCH] working mesecons switch in unloaded areas --- mesecons/luacontroller.lua | 12 ++++++------ mesecons/switch.lua | 17 +++++------------ 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/mesecons/luacontroller.lua b/mesecons/luacontroller.lua index c498a48..c9a3534 100644 --- a/mesecons/luacontroller.lua +++ b/mesecons/luacontroller.lua @@ -27,18 +27,18 @@ mtui.register_on_command("luacontroller_set_program", function(data) local node = minetest.get_node_or_nil(data.pos) if node == nil then -- not loaded - return { success = false } + return { success = false, errmsg = "not loaded" } end if not mtui.mesecons.allowed_nodes[node.name] then -- unexpected node - return { success = false } + return { success = false, errmsg = "unexpected node: " .. node.name } end local def = minetest.registered_nodes[node.name] if not def or type(def.on_receive_fields) ~= "function" then -- node definition doesn't make sense - return { success = false } + return { success = false, errmsg = "nodedef error" } end -- shim player (the formspec callback only uses the `get_player_name` field) @@ -71,17 +71,17 @@ mtui.register_on_command("luacontroller_get_program", function(data) local node = minetest.get_node_or_nil(data.pos) if node == nil then -- not loaded - return { success = false } + return { success = false, errmsg = "not loaded" } end if not mtui.mesecons.allowed_nodes[node.name] then -- unexpected node - return { success = false } + return { success = false, errmsg = "unexpected node: " .. node.name } end if minetest.is_protected(data.pos, data.playername) then -- protected - return { success = false } + return { success = false, errmsg = "protected" } end local meta = minetest.get_meta(data.pos) diff --git a/mesecons/switch.lua b/mesecons/switch.lua index 0d47698..2277e00 100644 --- a/mesecons/switch.lua +++ b/mesecons/switch.lua @@ -10,28 +10,21 @@ 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 + local node = mesecon.get_node_force(data.pos) - if data.state == "on" then - if node.name == switch_off_name then + if node.name == switch_off_name or node.name == switch_on_name then + if data.state == "on" 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 + elseif data.state == "off" then -- switch in on-state, turn off switch_on_def.on_rightclick(data.pos, node) return { success = true } end end - return { success = false } + return { success = false, errmsg = "invalid node: " .. node.name .. " for state: " .. data.state } end) -- game -> ui