Globalize, rename and change the behaviour of has_locked_chest_privilege
* rename to default.can_interact_with_node() * pass pos instead of meta * change order of arguments
This commit is contained in:
parent
89c45993ac
commit
37dd910747
@ -516,3 +516,39 @@ minetest.register_abm({
|
|||||||
minetest.set_node(pos, {name = "default:coral_skeleton"})
|
minetest.set_node(pos, {name = "default:coral_skeleton"})
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
--
|
||||||
|
-- NOTICE: This method is not an official part of the API yet!
|
||||||
|
-- This method may change in future.
|
||||||
|
--
|
||||||
|
|
||||||
|
function default.can_interact_with_node(player, pos)
|
||||||
|
if player then
|
||||||
|
if minetest.check_player_privs(player, "protection_bypass") then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local meta = minetest.get_meta(pos)
|
||||||
|
|
||||||
|
-- is player wielding the right key?
|
||||||
|
local item = player:get_wielded_item()
|
||||||
|
if item:get_name() == "default:key" then
|
||||||
|
local key_meta = minetest.parse_json(item:get_metadata())
|
||||||
|
local secret = meta:get_string("key_lock_secret")
|
||||||
|
if secret ~= key_meta.secret then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
if player:get_player_name() ~= meta:get_string("owner") then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
@ -1612,34 +1612,6 @@ local function get_locked_chest_formspec(pos)
|
|||||||
return formspec
|
return formspec
|
||||||
end
|
end
|
||||||
|
|
||||||
local function has_locked_chest_privilege(meta, player)
|
|
||||||
if player then
|
|
||||||
if minetest.check_player_privs(player, "protection_bypass") then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
-- is player wielding the right key?
|
|
||||||
local item = player:get_wielded_item()
|
|
||||||
if item:get_name() == "default:key" then
|
|
||||||
local key_meta = minetest.parse_json(item:get_metadata())
|
|
||||||
local secret = meta:get_string("key_lock_secret")
|
|
||||||
if secret ~= key_meta.secret then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
if player:get_player_name() ~= meta:get_string("owner") then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
minetest.register_node("default:chest", {
|
minetest.register_node("default:chest", {
|
||||||
description = "Chest",
|
description = "Chest",
|
||||||
tiles = {"default_chest_top.png", "default_chest_top.png", "default_chest_side.png",
|
tiles = {"default_chest_top.png", "default_chest_top.png", "default_chest_side.png",
|
||||||
@ -1710,26 +1682,23 @@ minetest.register_node("default:chest_locked", {
|
|||||||
can_dig = function(pos,player)
|
can_dig = function(pos,player)
|
||||||
local meta = minetest.get_meta(pos);
|
local meta = minetest.get_meta(pos);
|
||||||
local inv = meta:get_inventory()
|
local inv = meta:get_inventory()
|
||||||
return inv:is_empty("main") and has_locked_chest_privilege(meta, player)
|
return inv:is_empty("main") and default.can_interact_with_node(player, pos)
|
||||||
end,
|
end,
|
||||||
allow_metadata_inventory_move = function(pos, from_list, from_index,
|
allow_metadata_inventory_move = function(pos, from_list, from_index,
|
||||||
to_list, to_index, count, player)
|
to_list, to_index, count, player)
|
||||||
local meta = minetest.get_meta(pos)
|
if not default.can_interact_with_node(player, pos) then
|
||||||
if not has_locked_chest_privilege(meta, player) then
|
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
return count
|
return count
|
||||||
end,
|
end,
|
||||||
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
|
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
|
||||||
local meta = minetest.get_meta(pos)
|
if not default.can_interact_with_node(player, pos) then
|
||||||
if not has_locked_chest_privilege(meta, player) then
|
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
return stack:get_count()
|
return stack:get_count()
|
||||||
end,
|
end,
|
||||||
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
|
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
|
||||||
local meta = minetest.get_meta(pos)
|
if not default.can_interact_with_node(player, pos) then
|
||||||
if not has_locked_chest_privilege(meta, player) then
|
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
return stack:get_count()
|
return stack:get_count()
|
||||||
@ -1745,8 +1714,7 @@ minetest.register_node("default:chest_locked", {
|
|||||||
" from locked chest at " .. minetest.pos_to_string(pos))
|
" from locked chest at " .. minetest.pos_to_string(pos))
|
||||||
end,
|
end,
|
||||||
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||||
local meta = minetest.get_meta(pos)
|
if default.can_interact_with_node(clicker, pos) then
|
||||||
if has_locked_chest_privilege(meta, clicker) then
|
|
||||||
minetest.show_formspec(
|
minetest.show_formspec(
|
||||||
clicker:get_player_name(),
|
clicker:get_player_name(),
|
||||||
"default:chest_locked",
|
"default:chest_locked",
|
||||||
|
Loading…
Reference in New Issue
Block a user