Optimized minetest.get_connected_players()
authorfairiestoy <johannesvk@web.de>
Wed, 11 Sep 2013 14:59:48 +0000 (16:59 +0200)
committerKahrl <kahrl@gmx.net>
Tue, 1 Oct 2013 13:03:03 +0000 (15:03 +0200)
Instead of collecting all objects within a huge radius (which could be
a big value), just register each player that connects and give back the
current hold list.

builtin/misc.lua

index 82a0ba298bfbba5afc0349002517cdf8be0e3052..3d2c496e33b549ae006e19f5e10395b93be625f8 100644 (file)
@@ -40,15 +40,22 @@ function minetest.check_player_privs(name, privs)
        return true, ""
 end
 
+local player_list = {}
+
+minetest.register_on_joinplayer(function(player)
+       player_list[player:get_player_name()] = player
+end)
+
+minetest.register_on_leaveplayer(function(player)
+       player_list[player:get_player_name()] = nil
+end)
+
 function minetest.get_connected_players()
-       -- This could be optimized a bit, but leave that for later
-       local list = {}
-       for _, obj in pairs(minetest.get_objects_inside_radius({x=0,y=0,z=0}, 1000000)) do
-               if obj:is_player() then
-                       table.insert(list, obj)
-               end
+       local temp_table = {}
+       for index, value in pairs(player_list) do
+               table.insert(temp_table, value)
        end
-       return list
+       return temp_table
 end
 
 function minetest.hash_node_position(pos)