aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfairiestoy <johannesvk@web.de>2013-09-11 16:59:48 +0200
committerKahrl <kahrl@gmx.net>2013-10-01 15:03:03 +0200
commit2170c9958a455c228459c795857ec1a890cd05c5 (patch)
tree83e79810d6481fa2f80839e9882a330f948e1f03
parent2134316c8083274fb1c7645cbb1ceab745073e84 (diff)
downloadminetest-2170c9958a455c228459c795857ec1a890cd05c5.tar.gz
minetest-2170c9958a455c228459c795857ec1a890cd05c5.tar.bz2
minetest-2170c9958a455c228459c795857ec1a890cd05c5.zip
Optimized minetest.get_connected_players()
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.
-rw-r--r--builtin/misc.lua21
1 files changed, 14 insertions, 7 deletions
diff --git a/builtin/misc.lua b/builtin/misc.lua
index 82a0ba298..3d2c496e3 100644
--- a/builtin/misc.lua
+++ b/builtin/misc.lua
@@ -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)