Move core.get_connected_players() implementation to C++
authorsfan5 <sfan5@live.de>
Wed, 12 Feb 2020 18:49:48 +0000 (19:49 +0100)
committersfan5 <sfan5@live.de>
Sun, 23 Feb 2020 21:24:12 +0000 (22:24 +0100)
Keeping the ObjectRefs around in a table isn't ideal and this allows
removing the somewhat nonsensical is_player_connected() added in 86ef7147.

builtin/game/misc.lua
src/script/common/c_internal.h
src/script/lua_api/l_env.cpp
src/script/lua_api/l_env.h
src/script/lua_api/l_object.cpp
src/serverenvironment.h

index 02c36ccb1f358cb69368b6430cb7bfd09a4dde21..0ed11ada024e41fb4ea940f8005d49ea2630f495 100644 (file)
@@ -40,9 +40,6 @@ function core.check_player_privs(name, ...)
 end
 
 
-local player_list = {}
-
-
 function core.send_join_message(player_name)
        if not core.is_singleplayer() then
                core.chat_send_all("*** " .. player_name .. " joined the game.")
@@ -61,7 +58,6 @@ end
 
 core.register_on_joinplayer(function(player)
        local player_name = player:get_player_name()
-       player_list[player_name] = player
        if not core.is_singleplayer() then
                local status = core.get_server_status(player_name, true)
                if status and status ~= "" then
@@ -74,22 +70,10 @@ end)
 
 core.register_on_leaveplayer(function(player, timed_out)
        local player_name = player:get_player_name()
-       player_list[player_name] = nil
        core.send_leave_message(player_name, timed_out)
 end)
 
 
-function core.get_connected_players()
-       local temp_table = {}
-       for index, value in pairs(player_list) do
-               if value:is_player_connected() then
-                       temp_table[#temp_table + 1] = value
-               end
-       end
-       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
index 69b8a7fdc2768d28d4f90da1d5660f70b79d3259..d8cf3fe767e33c0f5a8f9990d801c625031db35d 100644 (file)
@@ -103,5 +103,6 @@ int script_exception_wrapper(lua_State *L, lua_CFunction f);
 void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
 void script_run_callbacks_f(lua_State *L, int nargs,
        RunCallbacksMode mode, const char *fxn);
+
 void log_deprecated(lua_State *L, const std::string &message,
        int stack_depth=1);
index 762612af488cd1b524b96d88fd07863eb80d2658..7d5de896d750250561a4b80c5829bb77bb85bf9e 100644 (file)
@@ -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);
index 68a4eae5043052c77f59c76fee35ceb4afdea11d..ac2f8b5883b86a9c59329b5281d1e8574ba65500 100644 (file)
@@ -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);
 
index f23282a950bb665e3d175acd36af980e30c2652f..1ce37bb6b6c4efa76f4762da10cfe4990c0f641d 100644 (file)
@@ -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));
index cc4ecd797eae3f829d90f63dd12ffa3da15d41d8..3c7b7d059577490d6f1be64f2348754bf2b899c5 100644 (file)
@@ -358,6 +358,7 @@ public:
 
        RemotePlayer *getPlayer(const session_t peer_id);
        RemotePlayer *getPlayer(const char* name);
+       const std::vector<RemotePlayer *> getPlayers() const { return m_players; }
        u32 getPlayerCount() const { return m_players.size(); }
 
        static bool migratePlayersDatabase(const GameParams &game_params,