Skip to content

Commit

Permalink
working mesecons switch in unloaded areas
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Oct 23, 2023
1 parent 1b30c8b commit 672c08d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
12 changes: 6 additions & 6 deletions mesecons/luacontroller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 5 additions & 12 deletions mesecons/switch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 672c08d

Please sign in to comment.