Skip to content

Commit

Permalink
Add minetest.is_player (#7013)
Browse files Browse the repository at this point in the history
* Add minetest.is_player

* First use for is_player
  • Loading branch information
HybridDog authored and nerzhul committed Feb 5, 2018
1 parent b7ff40e commit 87e08b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
18 changes: 13 additions & 5 deletions builtin/game/misc.lua
Expand Up @@ -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 = {...}
Expand Down Expand Up @@ -85,6 +84,15 @@ function core.get_connected_players()
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
Expand Down
5 changes: 3 additions & 2 deletions doc/lua_api.txt
Expand Up @@ -2753,9 +2753,9 @@ and `minetest.auth_reload` call the authentication handler.
* Set node on all positions set in the first argument.
* e.g. `minetest.bulk_set_node({{x=0, y=1, z=1}, {x=1, y=2, z=2}}, {name="default:stone"})`
* For node specification or position syntax see `minetest.set_node` call
* Faster than set_node due to single call, but still considerably slower than
* Faster than set_node due to single call, but still considerably slower than
Voxel Manipulators (LVM) for large numbers of nodes.
Unlike LVMs, this will call node callbacks. It also allows setting nodes in spread out
Unlike LVMs, this will call node callbacks. It also allows setting nodes in spread out
positions which would cause LVMs to waste memory.
For setting a cube, this is 1.3x faster than set_node whereas LVM is 20x faster.
* `minetest.swap_node(pos, node)`
Expand Down Expand Up @@ -3329,6 +3329,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
Expand Down

0 comments on commit 87e08b1

Please sign in to comment.