diff options
author | sfan5 <sfan5@live.de> | 2020-02-12 19:49:48 +0100 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2020-02-23 22:24:12 +0100 |
commit | 0b8d3f99a5424113178329e56c2ebe4b38fd2b46 (patch) | |
tree | 0533bd86aef3bbf1e871ef1558ff9de0109e7f08 /src/script/lua_api | |
parent | c657fb343f120b7462f7ca580852636ad98b8ae0 (diff) | |
download | minetest-0b8d3f99a5424113178329e56c2ebe4b38fd2b46.tar.gz minetest-0b8d3f99a5424113178329e56c2ebe4b38fd2b46.tar.bz2 minetest-0b8d3f99a5424113178329e56c2ebe4b38fd2b46.zip |
Move core.get_connected_players() implementation to C++
Keeping the ObjectRefs around in a table isn't ideal and this allows
removing the somewhat nonsensical is_player_connected() added in 86ef7147.
Diffstat (limited to 'src/script/lua_api')
-rw-r--r-- | src/script/lua_api/l_env.cpp | 20 | ||||
-rw-r--r-- | src/script/lua_api/l_env.h | 3 | ||||
-rw-r--r-- | src/script/lua_api/l_object.cpp | 3 |
3 files changed, 25 insertions, 1 deletions
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 762612af4..7d5de896d 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -640,6 +640,23 @@ int ModApiEnvMod::l_add_item(lua_State *L) return 1; } +// get_connected_players() +int ModApiEnvMod::l_get_connected_players(lua_State *L) +{ + GET_ENV_PTR; + + lua_createtable(L, env->getPlayerCount(), 0); + u32 i = 0; + for (RemotePlayer *player : env->getPlayers()) { + PlayerSAO *sao = player->getPlayerSAO(); + if (sao) { + getScriptApiBase(L)->objectrefGetOrCreate(L, sao); + lua_rawseti(L, -2, ++i); + } + } + return 1; +} + // get_player_by_name(name) int ModApiEnvMod::l_get_player_by_name(lua_State *L) { @@ -647,7 +664,7 @@ int ModApiEnvMod::l_get_player_by_name(lua_State *L) // Do it const char *name = luaL_checkstring(L, 1); - RemotePlayer *player = dynamic_cast<RemotePlayer *>(env->getPlayer(name)); + RemotePlayer *player = env->getPlayer(name); if (player == NULL){ lua_pushnil(L); return 1; @@ -1319,6 +1336,7 @@ void ModApiEnvMod::Initialize(lua_State *L, int top) API_FCT(find_nodes_with_meta); API_FCT(get_meta); API_FCT(get_node_timer); + API_FCT(get_connected_players); API_FCT(get_player_by_name); API_FCT(get_objects_inside_radius); API_FCT(set_timeofday); diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index 68a4eae50..ac2f8b588 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -101,6 +101,9 @@ private: // pos = {x=num, y=num, z=num} static int l_add_item(lua_State *L); + // get_connected_players() + static int l_get_connected_players(lua_State *L); + // get_player_by_name(name) static int l_get_player_by_name(lua_State *L); diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index f23282a95..1ce37bb6b 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1063,6 +1063,9 @@ int ObjectRef::l_get_luaentity(lua_State *L) int ObjectRef::l_is_player_connected(lua_State *L) { NO_MAP_LOCK_REQUIRED; + // This method was once added for a bugfix, but never documented + log_deprecated(L, "is_player_connected is undocumented and " + "will be removed in a future release"); ObjectRef *ref = checkobject(L, 1); RemotePlayer *player = getplayer(ref); lua_pushboolean(L, (player != NULL && player->getPeerId() != PEER_ID_INEXISTENT)); |