Skip to content

Commit 37dd910

Browse files
Thomas--Ssofar
authored andcommittedFeb 21, 2017
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
1 parent 89c4599 commit 37dd910

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed
 

Diff for: ‎mods/default/functions.lua

+36
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,39 @@ minetest.register_abm({
516516
minetest.set_node(pos, {name = "default:coral_skeleton"})
517517
end,
518518
})
519+
520+
521+
--
522+
-- NOTICE: This method is not an official part of the API yet!
523+
-- This method may change in future.
524+
--
525+
526+
function default.can_interact_with_node(player, pos)
527+
if player then
528+
if minetest.check_player_privs(player, "protection_bypass") then
529+
return true
530+
end
531+
else
532+
return false
533+
end
534+
535+
local meta = minetest.get_meta(pos)
536+
537+
-- is player wielding the right key?
538+
local item = player:get_wielded_item()
539+
if item:get_name() == "default:key" then
540+
local key_meta = minetest.parse_json(item:get_metadata())
541+
local secret = meta:get_string("key_lock_secret")
542+
if secret ~= key_meta.secret then
543+
return false
544+
end
545+
546+
return true
547+
end
548+
549+
if player:get_player_name() ~= meta:get_string("owner") then
550+
return false
551+
end
552+
553+
return true
554+
end

Diff for: ‎mods/default/nodes.lua

+5-37
Original file line numberDiff line numberDiff line change
@@ -1612,34 +1612,6 @@ local function get_locked_chest_formspec(pos)
16121612
return formspec
16131613
end
16141614

1615-
local function has_locked_chest_privilege(meta, player)
1616-
if player then
1617-
if minetest.check_player_privs(player, "protection_bypass") then
1618-
return true
1619-
end
1620-
else
1621-
return false
1622-
end
1623-
1624-
-- is player wielding the right key?
1625-
local item = player:get_wielded_item()
1626-
if item:get_name() == "default:key" then
1627-
local key_meta = minetest.parse_json(item:get_metadata())
1628-
local secret = meta:get_string("key_lock_secret")
1629-
if secret ~= key_meta.secret then
1630-
return false
1631-
end
1632-
1633-
return true
1634-
end
1635-
1636-
if player:get_player_name() ~= meta:get_string("owner") then
1637-
return false
1638-
end
1639-
1640-
return true
1641-
end
1642-
16431615
minetest.register_node("default:chest", {
16441616
description = "Chest",
16451617
tiles = {"default_chest_top.png", "default_chest_top.png", "default_chest_side.png",
@@ -1710,26 +1682,23 @@ minetest.register_node("default:chest_locked", {
17101682
can_dig = function(pos,player)
17111683
local meta = minetest.get_meta(pos);
17121684
local inv = meta:get_inventory()
1713-
return inv:is_empty("main") and has_locked_chest_privilege(meta, player)
1685+
return inv:is_empty("main") and default.can_interact_with_node(player, pos)
17141686
end,
17151687
allow_metadata_inventory_move = function(pos, from_list, from_index,
17161688
to_list, to_index, count, player)
1717-
local meta = minetest.get_meta(pos)
1718-
if not has_locked_chest_privilege(meta, player) then
1689+
if not default.can_interact_with_node(player, pos) then
17191690
return 0
17201691
end
17211692
return count
17221693
end,
17231694
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
1724-
local meta = minetest.get_meta(pos)
1725-
if not has_locked_chest_privilege(meta, player) then
1695+
if not default.can_interact_with_node(player, pos) then
17261696
return 0
17271697
end
17281698
return stack:get_count()
17291699
end,
17301700
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
1731-
local meta = minetest.get_meta(pos)
1732-
if not has_locked_chest_privilege(meta, player) then
1701+
if not default.can_interact_with_node(player, pos) then
17331702
return 0
17341703
end
17351704
return stack:get_count()
@@ -1745,8 +1714,7 @@ minetest.register_node("default:chest_locked", {
17451714
" from locked chest at " .. minetest.pos_to_string(pos))
17461715
end,
17471716
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
1748-
local meta = minetest.get_meta(pos)
1749-
if has_locked_chest_privilege(meta, clicker) then
1717+
if default.can_interact_with_node(clicker, pos) then
17501718
minetest.show_formspec(
17511719
clicker:get_player_name(),
17521720
"default:chest_locked",

0 commit comments

Comments
 (0)
Please sign in to comment.