Skip to content

Commit fb4c440

Browse files
sofarJeija
authored andcommittedFeb 14, 2016
Doors: Use new mesh door API if available
I've implemented a new door API in minetest_game that performs all the needed things to assure doors are properly openend and closed, without mods needing to know the inner details of what needs to be done. Mesecons can just fetch a reference to the door object and call the appropriate open or close method, which simplifies this code a lot. For compatibility, this code retains the old code path and tests whether the new API is available, so this code remains functional if the new API is not available. Since the metal trapdoor was only recently added, I added it to the new API codepath only, it's unlikely to be present on older versions of minetest_game anyway. As a benefit from the new door API, there is now absolutely no more accidental switching closed trapdoors to open on power off, which could happen with the old method - since that was just a simple toggle.
1 parent f56c4ce commit fb4c440

File tree

1 file changed

+78
-33
lines changed

1 file changed

+78
-33
lines changed
 

‎mesecons_doors/init.lua

+78-33
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,53 @@ local function on_rightclick(pos, dir, check_name, replace, replace_dir, params)
2020
end
2121

2222
local function meseconify_door(name)
23-
if not minetest.registered_items[name] then return end
23+
if minetest.registered_items[name .. "_b_1"] then
24+
-- old style double-node doors
25+
local function toggle_state1 (pos, node)
26+
on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
27+
end
2428

25-
local function toggle_state1 (pos, node)
26-
on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
27-
end
28-
29-
local function toggle_state2 (pos, node)
30-
on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
31-
end
29+
local function toggle_state2 (pos, node)
30+
on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
31+
end
3232

33-
minetest.override_item(name.."_b_1", {
34-
mesecons = {effector = {
35-
action_on = toggle_state1,
36-
action_off = toggle_state1,
37-
rules = mesecon.rules.pplate
38-
}},
39-
})
33+
minetest.override_item(name.."_b_1", {
34+
mesecons = {effector = {
35+
action_on = toggle_state1,
36+
action_off = toggle_state1,
37+
rules = mesecon.rules.pplate
38+
}}
39+
})
4040

41-
minetest.override_item(name.."_b_2", {
42-
mesecons = {effector = {
43-
action_on = toggle_state2,
44-
action_off = toggle_state2,
45-
rules = mesecon.rules.pplate
46-
}},
47-
})
41+
minetest.override_item(name.."_b_2", {
42+
mesecons = {effector = {
43+
action_on = toggle_state2,
44+
action_off = toggle_state2,
45+
rules = mesecon.rules.pplate
46+
}}
47+
})
48+
elseif minetest.registered_items[name .. "_a"] then
49+
-- new style mesh node based doors
50+
local override = {
51+
mesecons = {effector = {
52+
action_on = function(pos, node)
53+
local door = doors.get(pos)
54+
if door then
55+
door:open()
56+
end
57+
end,
58+
action_off = function(pos, node)
59+
local door = doors.get(pos)
60+
if door then
61+
door:close()
62+
end
63+
end,
64+
rules = mesecon.rules.pplate
65+
}}
66+
}
67+
minetest.override_item(name .. "_a", override)
68+
minetest.override_item(name .. "_b", override)
69+
end
4870
end
4971

5072
meseconify_door("doors:door_wood")
@@ -67,18 +89,41 @@ local function trapdoor_switch(pos, node)
6789
minetest.get_meta(pos):set_int("state", state == 1 and 0 or 1)
6890
end
6991

70-
if minetest.registered_nodes["doors:trapdoor"] then
71-
minetest.override_item("doors:trapdoor", {
92+
if doors and doors.get then
93+
local override = {
7294
mesecons = {effector = {
73-
action_on = trapdoor_switch,
74-
action_off = trapdoor_switch
95+
action_on = function(pos, node)
96+
local door = doors.get(pos)
97+
if door then
98+
door:open()
99+
end
100+
end,
101+
action_off = function(pos, node)
102+
local door = doors.get(pos)
103+
if door then
104+
door:close()
105+
end
106+
end,
75107
}},
76-
})
108+
}
109+
minetest.override_item("doors:trapdoor", override)
110+
minetest.override_item("doors:trapdoor_open", override)
111+
minetest.override_item("doors:trapdoor_steel", override)
112+
minetest.override_item("doors:trapdoor_steel_open", override)
113+
else
114+
if minetest.registered_nodes["doors:trapdoor"] then
115+
minetest.override_item("doors:trapdoor", {
116+
mesecons = {effector = {
117+
action_on = trapdoor_switch,
118+
action_off = trapdoor_switch
119+
}},
120+
})
77121

78-
minetest.override_item("doors:trapdoor_open", {
79-
mesecons = {effector = {
80-
action_on = trapdoor_switch,
81-
action_off = trapdoor_switch
82-
}},
83-
})
122+
minetest.override_item("doors:trapdoor_open", {
123+
mesecons = {effector = {
124+
action_on = trapdoor_switch,
125+
action_off = trapdoor_switch
126+
}},
127+
})
128+
end
84129
end

0 commit comments

Comments
 (0)
Please sign in to comment.