]> git.lizzy.rs Git - minetest.git/commitdiff
Add minetest.is_player (#7013)
authoryou <ovvv@web.de>
Mon, 5 Feb 2018 14:17:10 +0000 (15:17 +0100)
committerSmallJoker <mk939@ymail.com>
Sun, 3 Jun 2018 15:32:00 +0000 (17:32 +0200)
* Add minetest.is_player

* First use for is_player

builtin/game/misc.lua
doc/lua_api.txt

index bfe407b9ded146537b3d38a0a1e77cd91eb98632..d8f7a638d535fb12f2c304b319302b7655f994d6 100644 (file)
@@ -5,12 +5,11 @@
 --
 
 function core.check_player_privs(name, ...)
-       local arg_type = type(name)
-       if (arg_type == "userdata" or arg_type == "table") and
-                       name.get_player_name then -- If it quacks like a Player...
+       if core.is_player(name) then
                name = name:get_player_name()
-       elseif arg_type ~= "string" then
-               error("Invalid core.check_player_privs argument type: " .. arg_type, 2)
+       elseif type(name) ~= "string" then
+               error("core.check_player_privs expects a player or playername as " ..
+                       "argument.", 2)
        end
 
        local requested_privs = {...}
@@ -70,6 +69,16 @@ function core.get_connected_players()
        return temp_table
 end
 
+
+function core.is_player(player)
+       -- a table being a player is also supported because it quacks sufficiently
+       -- like a player if it has the is_player function
+       local t = type(player)
+       return (t == "userdata" or t == "table") and
+               type(player.is_player) == "function" and player:is_player()
+end
+
+
 function minetest.player_exists(name)
        return minetest.get_auth_handler().get_auth(name) ~= nil
 end
index b35c816a15452e84f1a96b6a2c7c5ab5a07dfa6b..73572ac7836449d22517efb70e6f308886d5ee9e 100644 (file)
@@ -3013,6 +3013,7 @@ These functions return the leftover itemstack.
 
 ### Misc.
 * `minetest.get_connected_players()`: returns list of `ObjectRefs`
+* `minetest.is_player(o)`: boolean, whether `o` is a player
 * `minetest.player_exists(name)`: boolean, whether player exists (regardless of online status)
 * `minetest.hud_replace_builtin(name, hud_definition)`
     * Replaces definition of a builtin hud element