Fix metadata loss after using a locked door

This commit is contained in:
Sys Quatre 2020-08-04 20:15:39 +02:00
parent bd6909326c
commit 4d3a7e51df

View File

@ -6,216 +6,209 @@
local door_bottom = {-0.5, -0.5, -0.5, 0.5, 0.5, -0.4} local door_bottom = {-0.5, -0.5, -0.5, 0.5, 0.5, -0.4}
local door_top = { local door_top = {
{-0.5, -0.5, -0.5, -0.3, 0.5, -0.4}, {-0.5, -0.5, -0.5, -0.3, 0.5, -0.4},
{0.3, -0.5, -0.5, 0.5, 0.5, -0.4}, {0.3, -0.5, -0.5, 0.5, 0.5, -0.4},
{-0.3, 0.3, -0.5, 0.3, 0.5, -0.4}, {-0.3, 0.3, -0.5, 0.3, 0.5, -0.4},
{-0.3, -0.5, -0.5, 0.3, -0.4, -0.4}, {-0.3, -0.5, -0.5, 0.3, -0.4, -0.4},
{-0.05, -0.4, -0.5, 0.05, 0.3, -0.4}, {-0.05, -0.4, -0.5, 0.05, 0.3, -0.4},
{-0.3, -0.1, -0.5, -0.05, 0, -0.4}, {-0.3, -0.1, -0.5, -0.05, 0, -0.4},
{0.05, -0.1, -0.5, 0.3, 0, -0.4} {0.05, -0.1, -0.5, 0.3, 0, -0.4}
} }
local is_top = function(name) local is_top = function(name)
return name:sub(12, 12) == "t" return name:sub(12, 12) == "t"
end end
local xdoors2_transform = function(pos, node, puncher) local xdoors2_transform = function(pos, node, puncher)
if( not( locks:lock_allow_use( pos, puncher ))) then if is_top(node.name) then
minetest.chat_send_player( puncher:get_player_name(), "This door is locked. It can only be opened by its owner or people with a key that fits."); pos = {x = pos.x, y = pos.y - 1, z = pos.z}
return; end
end
if is_top(node.name) then if( not( locks:lock_allow_use( pos, puncher ))) then
pos = {x = pos.x, y = pos.y - 1, z = pos.z} minetest.chat_send_player( puncher:get_player_name(), "This door is locked. It can only be opened by its owner or people with a key that fits.")
end return
local t = 3 - node.name:sub(-1) end
local p2 = 0
if t == 2 then local t = 3 - node.name:sub(-1)
p2 = (node.param2 + 1) % 4 local p2 = 0
minetest.sound_play("doors_door_open", { pos = pos, gain = 0.3, max_hear_distance = 10}) if t == 2 then
else p2 = (node.param2 + 1) % 4
p2 = (node.param2 + 3) % 4 minetest.sound_play("doors_door_open", { pos = pos, gain = 0.3, max_hear_distance = 10})
minetest.sound_play("doors_door_close", { pos = pos, gain = 0.3, max_hear_distance = 10}) else
end p2 = (node.param2 + 3) % 4
minetest.sound_play("doors_door_close", { pos = pos, gain = 0.3, max_hear_distance = 10})
end
local olddata = locks:get_lockdata( pos ); -- No need to remember who owns the door, what passwords are set etc.
minetest.add_node(pos, {name = "locks:door_bottom_"..t, param2 = p2}) -- because minetest.swap_node() keeps metadata at pos
minetest.add_node({x = pos.x, y = pos.y + 1, z = pos.z}, {name = "locks:door_top_"..t, param2 = p2}) minetest.swap_node(pos, {name = "locks:door_bottom_"..t, param2 = p2})
minetest.swap_node({x = pos.x, y = pos.y + 1, z = pos.z}, {name = "locks:door_top_"..t, param2 = p2})
-- remember who owns the door, what passwords are set etc.
locks:set_lockdata( pos, olddata );
locks:set_lockdata( {x = pos.x, y = pos.y + 1, z = pos.z}, olddata );
end end
local xdoors2_destruct = function(pos, oldnode) local xdoors2_destruct = function(pos, oldnode)
if is_top(oldnode.name) then if is_top(oldnode.name) then
pos = {x = pos.x, y = pos.y - 1, z = pos.z} pos = {x = pos.x, y = pos.y - 1, z = pos.z}
end end
minetest.remove_node(pos) minetest.remove_node(pos)
minetest.remove_node({x = pos.x, y = pos.y + 1, z = pos.z}) minetest.remove_node({x = pos.x, y = pos.y + 1, z = pos.z})
end end
for i = 1, 2 do for i = 1, 2 do
minetest.register_node("locks:door_top_"..i, { minetest.register_node("locks:door_top_"..i, {
tiles = {"xdoors2_side.png", "xdoors2_side.png", "xdoors2_top.png", "xdoors2_bottom.png", "xdoors2_top_"..(3 - i)..".png", "xdoors2_top_"..i..".png"}, tiles = {"xdoors2_side.png", "xdoors2_side.png", "xdoors2_top.png", "xdoors2_bottom.png", "xdoors2_top_"..(3 - i)..".png", "xdoors2_top_"..i..".png"},
paramtype = "light", paramtype = "light",
paramtype2 = "facedir", paramtype2 = "facedir",
drawtype = "nodebox", drawtype = "nodebox",
drop = "locks:door", drop = "locks:door",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = door_top fixed = door_top
}, },
selection_box = { selection_box = {
type = "fixed", type = "fixed",
fixed = door_bottom fixed = door_bottom
}, },
on_punch = xdoors2_transform, on_punch = xdoors2_transform,
after_dig_node = xdoors2_destruct, after_dig_node = xdoors2_destruct,
on_construct = function(pos) on_construct = function(pos)
locks:lock_init( pos, locks:lock_init( pos,
"size[8,2]".. "size[8,2]"..locks.uniform_background..
locks.uniform_background.. "button_exit[6.3,1.2;1.7,1;locks_sent_input;Proceed]" )
"button_exit[6.3,1.2;1.7,1;locks_sent_input;Proceed]" ); end,
end,
on_receive_fields = function(pos, formname, fields, sender) on_receive_fields = function(pos, formname, fields, sender)
locks:lock_handle_input( pos, formname, fields, sender ); locks:lock_handle_input( {x=pos.x, y=pos.y-1, z=pos.z}, formname, fields, sender )
end, end,
can_dig = function(pos,player) can_dig = function(pos,player)
return locks:lock_allow_dig( pos, player ); return locks:lock_allow_dig( {x=pos.x, y=pos.y-1, z=pos.z}, player )
end end
}) })
minetest.register_node("locks:door_bottom_"..i, { minetest.register_node("locks:door_bottom_"..i, {
tiles = {"xdoors2_side.png", "xdoors2_side.png", "xdoors2_top.png", "xdoors2_bottom.png", "locks_xdoors2_bottom_"..(3 - i)..".png", "locks_xdoors2_bottom_"..i..".png"}, tiles = {"xdoors2_side.png", "xdoors2_side.png", "xdoors2_top.png", "xdoors2_bottom.png", "locks_xdoors2_bottom_"..(3 - i)..".png", "locks_xdoors2_bottom_"..i..".png"},
paramtype = "light", paramtype = "light",
paramtype2 = "facedir", paramtype2 = "facedir",
drawtype = "nodebox", drawtype = "nodebox",
drop = "locks:door", drop = "locks:door",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = door_bottom fixed = door_bottom
}, },
selection_box = { selection_box = {
type = "fixed", type = "fixed",
fixed = door_bottom fixed = door_bottom
}, },
on_punch = xdoors2_transform, on_punch = xdoors2_transform,
after_dig_node = xdoors2_destruct, after_dig_node = xdoors2_destruct,
on_construct = function(pos) on_construct = function(pos)
locks:lock_init( pos, locks:lock_init( pos,
"size[8,2]".. "size[8,2]"..locks.uniform_background..
locks.uniform_background.. "button_exit[6.3,1.2;1.7,1;locks_sent_input;Proceed]" )
"button_exit[6.3,1.2;1.7,1;locks_sent_input;Proceed]" ); end,
end,
on_receive_fields = function(pos, formname, fields, sender) on_receive_fields = function(pos, formname, fields, sender)
locks:lock_handle_input( pos, formname, fields, sender ); locks:lock_handle_input( pos, formname, fields, sender )
end, end,
can_dig = function(pos,player) can_dig = function(pos,player)
return locks:lock_allow_dig( pos, player ); return locks:lock_allow_dig( pos, player )
end end
}) })
end end
local delta = { local delta = {
{x = -1, z = 0}, {x = -1, z = 0},
{x = 0, z = 1}, {x = 0, z = 1},
{x = 1, z = 0}, {x = 1, z = 0},
{x = 0, z = -1} {x = 0, z = -1}
} }
minetest.register_node("locks:door", { minetest.register_node("locks:door", {
description = "Shared locked Wooden Door", description = "Shared locked Wooden Door",
node_placement_prediction = "", node_placement_prediction = "",
inventory_image = 'locks_xdoors2_door.png', inventory_image = 'locks_xdoors2_door.png',
wield_image = 'locks_xdoors2_door.png', wield_image = 'locks_xdoors2_door.png',
stack_max = 1, stack_max = 1,
sunlight_propogates = true, sunlight_propogates = true,
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
local above = pointed_thing.above local pname = placer:get_player_name()
local above1 = {x = above.x, y = above.y + 1, z = above.z}; local above = pointed_thing.above
if (minetest.is_protected(above, placer:get_player_name()) local above1 = {x = above.x, y = above.y + 1, z = above.z}
or minetest.is_protected(above1, placer:get_player_name())) then if (minetest.is_protected(above, pname)
minetest.chat_send_player(placer:get_player_name(), "This area is protected!") or minetest.is_protected(above1, pname)) then
return itemstack minetest.chat_send_player(pname, "This area is protected!")
else return itemstack
-- there should be 2 empty nodes else
if minetest.get_node(above1).name ~= "air" then -- there should be 2 empty nodes
return itemstack if minetest.get_node(above1).name ~= "air" then
end return itemstack
end
local fdir = 0
local placer_pos = placer:get_pos() local fdir = 0
if placer_pos then local placer_pos = placer:get_pos()
fdir = minetest.dir_to_facedir({ if placer_pos then
x = above.x - placer_pos.x, fdir = minetest.dir_to_facedir({
y = above.y - placer_pos.y, x = above.x - placer_pos.x,
z = above.z - placer_pos.z y = above.y - placer_pos.y,
}) z = above.z - placer_pos.z
end })
end
local t = 1 local t = 1
local another_door = minetest.get_node({x = above.x + delta[fdir + 1].x, y = above.y, z = above.z + delta[fdir + 1].z}) local another_door = minetest.get_node({x = above.x + delta[fdir + 1].x, y = above.y, z = above.z + delta[fdir + 1].z})
if (another_door.name:sub(-1) == "1" and another_door.param2 == fdir) if (another_door.name:sub(-1) == "1" and another_door.param2 == fdir)
or (another_door.name:sub(-1) == "2" and another_door.param2 == (fdir + 1) % 4) then or (another_door.name:sub(-1) == "2" and another_door.param2 == (fdir + 1) % 4) then
t = 2 t = 2
end end
minetest.add_node(above, {name = "locks:door_bottom_"..t, param2 = fdir}) minetest.add_node(above, {name = "locks:door_bottom_"..t, param2 = fdir})
minetest.add_node({x = above.x, y = above.y + 1, z = above.z}, {name = "locks:door_top_"..t, param2 = fdir}) minetest.add_node(above1, {name = "locks:door_top_"..t, param2 = fdir})
-- store who owns the door; the other data can be default for now -- store who owns the door; the other data can be default for now
locks:lock_set_owner( above, placer:get_player_name() or "", "Shared locked door"); locks:lock_set_owner( above, pname or "", "Shared locked door")
locks:lock_set_owner( {x = above.x, y = above.y + 1, z = above.z}, placer:get_player_name() or "", "Shared locked door"); locks:lock_set_owner( above1, pname or "", "Shared locked door")
return ItemStack("") return ItemStack("")
end
end end
end
}) })
-- if xdoors2 is installed -- if xdoors2 is installed
if( minetest.get_modpath("xdoors2") ~= nil ) then if( minetest.get_modpath("xdoors2") ~= nil ) then
minetest.register_craft({ minetest.register_craft({
output = 'locks:door', output = 'locks:door',
recipe = { recipe = {
{ 'xdoors2:door', 'locks:lock', '' }, { 'xdoors2:door', 'locks:lock', '' },
}, },
});
-- if the normal doors are installed
else if( minetest.get_modpath("doors") ~= nil ) then
minetest.register_craft({
output = 'locks:door',
recipe = {
{ 'doors:door_wood', 'locks:lock', '' },
},
}) })
-- fallback if no doors can be found -- if the normal doors are installed
elseif( minetest.get_modpath("doors") ~= nil ) then
minetest.register_craft({
output = 'locks:door',
recipe = {
{ 'doors:door_wood', 'locks:lock', '' },
},
})
-- fallback if no doors can be found
else else
minetest.register_craft({ minetest.register_craft({
output = 'locks:door', output = 'locks:door',
recipe = { recipe = {
{ 'default:wood', 'default:wood', '' }, { 'default:wood', 'default:wood', '' },
{ 'default:wood', 'default:wood', 'locks:lock' }, { 'default:wood', 'default:wood', 'locks:lock' },
{ 'default:wood', 'default:wood', '' }, { 'default:wood', 'default:wood', '' },
}, },
}); })
end end
end -- of previous else
print( "[Mod] locks: loading locks:door")
print( "[Mod] locks: loading locks:door");