Change doors to default.can_interact_with_node()
authorThomas--S <Thomas--S@users.noreply.github.com>
Tue, 21 Feb 2017 18:43:34 +0000 (19:43 +0100)
committerAuke Kok <sofar+github@foo-projects.org>
Thu, 16 Mar 2017 16:11:20 +0000 (09:11 -0700)
mods/doors/init.lua

index 0ed98b3392596c4f4add5469d3b50106ff3f1db4..2b3d094aa723ba3dd21bafc8a436d990b95b0db5 100644 (file)
@@ -6,6 +6,15 @@ local _doors = {}
 _doors.registered_doors = {}
 _doors.registered_trapdoors = {}
 
+local function replace_old_owner_information(pos)
+       local meta = minetest.get_meta(pos)
+       local owner = meta:get_string("doors_owner")
+       if owner and owner ~= "" then
+               meta:set_string("owner", owner)
+               meta:set_string("doors_owner", "")
+       end
+end
+
 -- returns an object to a door object or nil
 function doors.get(pos)
        local node_name = minetest.get_node(pos).name
@@ -139,28 +148,10 @@ function _doors.door_toggle(pos, node, clicker)
                state = tonumber(state)
        end
 
-       if clicker and not minetest.check_player_privs(clicker, "protection_bypass") then
-               -- is player wielding the right key?
-               local item = clicker:get_wielded_item()
-               local owner = meta:get_string("doors_owner")
-               if item:get_name() == "default:key" then
-                       local key_meta = item:get_meta()
-                       local secret = meta:get_string("key_lock_secret")
-
-                       if key_meta:get_string("secret") == "" then
-                               key_meta:set_string("secret", minetest.parse_json(item:get_metadata()).secret)
-                               item:set_metadata("")
-                       end
+       replace_old_owner_information(pos)
 
-                       if secret ~= key_meta:get_string("secret") then
-                               return false
-                       end
-
-               elseif owner ~= "" then
-                       if clicker:get_player_name() ~= owner then
-                               return false
-                       end
-               end
+       if not default.can_interact_with_node(clicker, pos) then
+               return false
        end
 
        -- until Lua-5.2 we have no bitwise operators :(
@@ -211,11 +202,8 @@ local function on_place_node(place_to, newnode,
 end
 
 local function can_dig_door(pos, digger)
-       local digger_name = digger and digger:get_player_name()
-       if digger_name and minetest.get_player_privs(digger_name).protection_bypass then
-               return true
-       end
-       return minetest.get_meta(pos):get_string("doors_owner") == digger_name
+       replace_old_owner_information(pos)
+       return default.can_interact_with_node(digger, pos)
 end
 
 function doors.register(name, def)
@@ -330,7 +318,7 @@ function doors.register(name, def)
                        meta:set_int("state", state)
 
                        if def.protected then
-                               meta:set_string("doors_owner", pn)
+                               meta:set_string("owner", pn)
                                meta:set_string("infotext", "Owned by " .. pn)
                        end
 
@@ -394,8 +382,9 @@ function doors.register(name, def)
                        door:toggle(player)
                end
                def.on_skeleton_key_use = function(pos, player, newsecret)
+                       replace_old_owner_information(pos)
                        local meta = minetest.get_meta(pos)
-                       local owner = meta:get_string("doors_owner")
+                       local owner = meta:get_string("owner")
                        local pname = player:get_player_name()
 
                        -- verify placer is owner of lockable door
@@ -532,28 +521,11 @@ end
 
 function _doors.trapdoor_toggle(pos, node, clicker)
        node = node or minetest.get_node(pos)
-       if clicker and not minetest.check_player_privs(clicker, "protection_bypass") then
-               -- is player wielding the right key?
-               local item = clicker:get_wielded_item()
-               local meta = minetest.get_meta(pos)
-               local owner = meta:get_string("doors_owner")
-               if item:get_name() == "default:key" then
-                       local key_meta = item:get_meta()
-                       local secret = meta:get_string("key_lock_secret")
-
-                       if key_meta:get_string("secret") == "" then
-                               key_meta:set_string("secret", minetest.parse_json(item:get_metadata()).secret)
-                       end
 
-                       if secret ~= key_meta:get_string("secret") then
-                               return false
-                       end
+       replace_old_owner_information(pos)
 
-               elseif owner ~= "" then
-                       if clicker:get_player_name() ~= owner then
-                               return false
-                       end
-               end
+       if not default.can_interact_with_node(clicker, pos) then
+               return false
        end
 
        local def = minetest.registered_nodes[node.name]
@@ -595,7 +567,7 @@ function doors.register_trapdoor(name, def)
                def.after_place_node = function(pos, placer, itemstack, pointed_thing)
                        local pn = placer:get_player_name()
                        local meta = minetest.get_meta(pos)
-                       meta:set_string("doors_owner", pn)
+                       meta:set_string("owner", pn)
                        meta:set_string("infotext", "Owned by "..pn)
 
                        return minetest.setting_getbool("creative_mode")
@@ -607,8 +579,9 @@ function doors.register_trapdoor(name, def)
                        door:toggle(player)
                end
                def.on_skeleton_key_use = function(pos, player, newsecret)
+                       replace_old_owner_information(pos)
                        local meta = minetest.get_meta(pos)
-                       local owner = meta:get_string("doors_owner")
+                       local owner = meta:get_string("owner")
                        local pname = player:get_player_name()
 
                        -- verify placer is owner of lockable door