Skip to content

Commit

Permalink
Globalize, rename and change the behaviour of has_locked_chest_privilege
Browse files Browse the repository at this point in the history
* rename to default.can_interact_with_node()
* pass pos instead of meta
* change order of arguments
  • Loading branch information
Thomas--S authored and sofar committed Feb 21, 2017
1 parent 89c4599 commit 37dd910
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
36 changes: 36 additions & 0 deletions mods/default/functions.lua
Expand Up @@ -516,3 +516,39 @@ minetest.register_abm({
minetest.set_node(pos, {name = "default:coral_skeleton"})
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
42 changes: 5 additions & 37 deletions mods/default/nodes.lua
Expand Up @@ -1612,34 +1612,6 @@ local function get_locked_chest_formspec(pos)
return formspec
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", {
description = "Chest",
tiles = {"default_chest_top.png", "default_chest_top.png", "default_chest_side.png",
Expand Down Expand Up @@ -1710,26 +1682,23 @@ minetest.register_node("default:chest_locked", {
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
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,
allow_metadata_inventory_move = function(pos, from_list, from_index,
to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
if not has_locked_chest_privilege(meta, player) then
if not default.can_interact_with_node(player, pos) then
return 0
end
return count
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
if not has_locked_chest_privilege(meta, player) then
if not default.can_interact_with_node(player, pos) then
return 0
end
return stack:get_count()
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
if not has_locked_chest_privilege(meta, player) then
if not default.can_interact_with_node(player, pos) then
return 0
end
return stack:get_count()
Expand All @@ -1745,8 +1714,7 @@ minetest.register_node("default:chest_locked", {
" from locked chest at " .. minetest.pos_to_string(pos))
end,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local meta = minetest.get_meta(pos)
if has_locked_chest_privilege(meta, clicker) then
if default.can_interact_with_node(clicker, pos) then
minetest.show_formspec(
clicker:get_player_name(),
"default:chest_locked",
Expand Down

0 comments on commit 37dd910

Please sign in to comment.