diff --git a/garrysmod/lua/includes/extensions/player.lua b/garrysmod/lua/includes/extensions/player.lua index cd046db3fa..9cf7a90a2c 100644 --- a/garrysmod/lua/includes/extensions/player.lua +++ b/garrysmod/lua/includes/extensions/player.lua @@ -261,53 +261,9 @@ function meta:HasGodMode() end --- These are totally in the wrong place. -function player.GetByAccountID( ID ) - local players = player.GetAll() - for i = 1, #players do - if ( players[i]:AccountID() == ID ) then - return players[i] - end - end - - return false -end - -function player.GetByUniqueID( ID ) - local players = player.GetAll() - for i = 1, #players do - if ( players[i]:UniqueID() == ID ) then - return players[i] - end - end - - return false -end - -function player.GetBySteamID( ID ) - ID = string.upper( ID ) - local players = player.GetAll() - for i = 1, #players do - if ( players[i]:SteamID() == ID ) then - return players[i] - end - end - - return false -end - -function player.GetBySteamID64( ID ) - ID = tostring( ID ) - local players = player.GetAll() - for i = 1, #players do - if ( players[i]:SteamID64() == ID ) then - return players[i] - end - end - - return false -end - +--[[--------------------------------------------------------- + Iterator for players +-----------------------------------------------------------]] local inext = ipairs( {} ) local PlayerCache = nil diff --git a/garrysmod/lua/includes/extensions/player_finders.lua b/garrysmod/lua/includes/extensions/player_finders.lua new file mode 100644 index 0000000000..ed40845a98 --- /dev/null +++ b/garrysmod/lua/includes/extensions/player_finders.lua @@ -0,0 +1,143 @@ + +--[[--------------------------------------------------------- + Name: GetByAccountID( ID ) + Desc: Attempts to get the player by the AccountID +-----------------------------------------------------------]] +local AccountIDMap = {} + +function player.GetByAccountID( ID ) + + ID = tostring( ID ) + local ply = AccountIDMap[ID] + + if ( ply ) then + return ply + end + + return false + +end + +--[[--------------------------------------------------------- + Name: GetByUniqueID( ID ) + Desc: Attempts to get the player by the UniqueID +-----------------------------------------------------------]] +local UniqueIDMap = {} + +function player.GetByUniqueID( ID ) + + ID = tostring( ID ) + local ply = UniqueIDMap[ID] + + if ( ply ) then + return ply + end + + return false + +end + +--[[--------------------------------------------------------- + Name: GetBySteamID( ID ) + Desc: Attempts to get the player by the SteamID +-----------------------------------------------------------]] +local SteamIDMap = {} + +function player.GetBySteamID( ID ) + + ID = string.upper( tostring( ID ) ) + local ply = SteamIDMap[ID] + + if ( ply ) then + return ply + end + + return false + +end + +--[[--------------------------------------------------------- + Name: GetBySteamID64( ID ) + Desc: Attempts to get the player by the SteamID64 +-----------------------------------------------------------]] +local SteamID64Map = {} + +function player.GetBySteamID64( ID ) + + ID = tostring( ID ) + local ply = SteamID64Map[ID] + + if ( ply ) then + return ply + end + + return false + +end + +--[[--------------------------------------------------------- + Manage the maps +-----------------------------------------------------------]] +hook.Add( "OnEntityCreated", "PlayerFinders", function( ent ) + + if ( not ent:IsPlayer() ) then + return + end + + local ply = ent + + -- + -- Store in the maps + -- + local AID = tostring( ply:AccountID() ) + local UID = tostring( ply:UniqueID() ) + local SID = ply:SteamID() + local S64 = ply:SteamID64() + + AccountIDMap[AID] = ply + AccountIDMap[ply] = AID + + UniqueIDMap[UID] = ply + UniqueIDMap[ply] = UID + + SteamIDMap[SID] = ply + SteamIDMap[ply] = SID + + SteamID64Map[S64] = ply + SteamID64Map[ply] = S64 + +end ) + +hook.Add( "EntityRemoved", "PlayerFinders", function( ent, fullUpdate ) + + if ( fullUpdate ) then + return + end + + if ( not ent:IsPlayer() ) then + return + end + + local ply = ent + + -- + -- Clear from the maps + -- + local AID = AccountIDMap[ply] + local UID = UniqueIDMap[ply] + local SID = SteamIDMap[ply] + local S64 = SteamID64Map[ply] + + AccountIDMap[AID] = nil + AccountIDMap[ply] = nil + + UniqueIDMap[UID] = nil + UniqueIDMap[ply] = nil + + SteamIDMap[SID] = nil + SteamIDMap[ply] = nil + + SteamID64Map[S64] = nil + SteamID64Map[ply] = nil + +end ) diff --git a/garrysmod/lua/includes/init.lua b/garrysmod/lua/includes/init.lua index eaf46fb3ac..b235253e28 100644 --- a/garrysmod/lua/includes/init.lua +++ b/garrysmod/lua/includes/init.lua @@ -101,6 +101,7 @@ include ( "extensions/entity.lua" ) include ( "extensions/ents.lua" ) include ( "extensions/math.lua" ) include ( "extensions/player.lua" ) +include ( "extensions/player_finders.lua" ) include ( "extensions/player_auth.lua" ) include ( "extensions/string.lua" ) include ( "extensions/table.lua" ) diff --git a/garrysmod/lua/send.txt b/garrysmod/lua/send.txt index 5f873b0215..cac34b741f 100644 --- a/garrysmod/lua/send.txt +++ b/garrysmod/lua/send.txt @@ -57,6 +57,7 @@ lua\includes\extensions\net.lua lua\includes\extensions\math.lua lua\includes\extensions\math\ease.lua lua\includes\extensions\player.lua +lua\includes\extensions\player_finders.lua lua\includes\extensions\player_auth.lua lua\includes\extensions\string.lua lua\includes\extensions\table.lua