Skip to content

Commit

Permalink
tidy mesecons code
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Oct 13, 2023
1 parent 199d536 commit 19064fd
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 143 deletions.
142 changes: 0 additions & 142 deletions handlers/mesecons.lua

This file was deleted.

5 changes: 4 additions & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions mesecons/common.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mtui.mesecons = {
allowed_nodes = {}
}
52 changes: 52 additions & 0 deletions mesecons/lightstones.lua
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions mesecons/register_tool.lua
Original file line number Diff line number Diff line change
@@ -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
})
66 changes: 66 additions & 0 deletions mesecons/switch.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 19064fd

Please sign in to comment.