Globalize, rename and change the behaviour of has_locked_chest_privilege
authorThomas--S <Thomas--S@users.noreply.github.com>
Fri, 10 Feb 2017 19:35:26 +0000 (20:35 +0100)
committerAuke Kok <sofar+github@foo-projects.org>
Tue, 21 Feb 2017 18:17:12 +0000 (10:17 -0800)
* rename to default.can_interact_with_node()
* pass pos instead of meta
* change order of arguments

mods/default/functions.lua
mods/default/nodes.lua

index 957bce60a8dc5a8a8e035b3195e6a92c82f4836f..b9275209432cafc56712f85fce11dd7b25997d08 100644 (file)
@@ -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
\ No newline at end of file
index 76f7ed40c03b59be374602647649f87655d210a4..0ce62a49e5fcf0d04f043dab7e90bd91b8f5d0b3 100644 (file)
@@ -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",
@@ -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()
@@ -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",