Skip to content

Instantly share code, notes, and snippets.

@Srlion
Created October 6, 2024 15:39
Show Gist options
  • Select an option

  • Save Srlion/ab4a0c6c41e0684c4bf6d2ae007c992f to your computer and use it in GitHub Desktop.

Select an option

Save Srlion/ab4a0c6c41e0684c4bf6d2ae007c992f to your computer and use it in GitHub Desktop.
Reinvent PlayerInitialSpawn
local goobie_utils = ...
local AddonName = goobie_utils.AddonName
local Util = goobie_utils.Util
local Log = goobie_utils.Log
local PlayerUtil = goobie_utils.PlayerUtil
local util = util
AddonName = Util.CapitalizeFirstLetter(AddonName)
local PLAYER_STEAMID_AUTHED = AddonName .. ".Player.SteamAuthed"
local PLAYER_AUTHING_HOOKS = AddonName .. ".Goobie.NetworkIDValidated.Player.SteamAuthed"
local authed_on_connect = {}
local called_authed = {}
-- thankfully, this hook is not called for singleplayer or LAN games, would have created problems when disconnecting/connecting resetting values
hook.Add("NetworkIDValidated", PLAYER_AUTHING_HOOKS, function(name, steamid)
local steamid64 = util.SteamIDTo64(steamid)
if called_authed[steamid64] then
return
end
local ply = player.GetBySteamID64(steamid64)
if not IsValid(ply) then
return
end
called_authed[steamid64] = true
hook.Call(PLAYER_STEAMID_AUTHED, nil, ply, steamid64)
end, PRE_HOOK)
hook.Add("OnEntityCreated", PLAYER_AUTHING_HOOKS, function(ply)
if not ply:IsValid() or not ply:IsPlayer() then return end
local userid = ply:UserID()
local steamid64 = ply:SteamID64()
if authed_on_connect[userid] then
called_authed[steamid64] = true
authed_on_connect[userid] = nil
hook.Call(PLAYER_STEAMID_AUTHED, nil, ply, steamid64)
return
end
if not called_authed[steamid64] and ply:IsFullyAuthenticated() then
called_authed[steamid64] = true
hook.Call(PLAYER_STEAMID_AUTHED, nil, ply, steamid64)
end
end, PRE_HOOK)
local is_single_player = game.SinglePlayer()
local SHOULDNT_HAPPEN_INFO = [[
This should not happen, steamid is STEAM_0:0:0, please report this ASAP!
Some info:
name: %s
userid: %s
index: %s
address: %s
is_single_player: %s
is_dedicated_server: %s]]
gameevent.Listen("player_connect")
hook.Add("player_connect", PLAYER_AUTHING_HOOKS, function(data)
local steamid = data.networkid
local userid = data.userid
if is_single_player or data.bot == 1 or steamid == "STEAM_ID_LAN" then
-- in single player, bots and LAN games, the steamid is not valid yet but they don't need to be authed
-- so we just wait till they spawn and call Connected then SteamAuthed hooks
authed_on_connect[userid] = true
elseif steamid == "STEAM_ID_PENDING" then
-- LET NetworkIDValidated hook handle this
elseif steamid == "STEAM_0:0:0" then
-- Some guy on wiki says that we could get this STEAM_0:0:0 in single player so I'm not sure if we will ever encounter this in a dedicated server
print("--------------------")
Log.Warn(SHOULDNT_HAPPEN_INFO:format(data.name, userid, data.index, data.address, is_single_player, game.IsDedicated()))
print("--------------------")
end
end)
gameevent.Listen("player_disconnect")
hook.Add("player_disconnect", PLAYER_AUTHING_HOOKS, function(data)
local steamid64 = util.SteamIDTo64(data.networkid)
local userid = data.userid
authed_on_connect[userid] = nil
called_authed[steamid64] = nil
end)
function PlayerUtil.IsSteamAuthed(ply)
return called_authed[ply:SteamID64()] or false
end
PlayerUtil.PLAYER_STEAMID_AUTHED_HOOK = PLAYER_STEAMID_AUTHED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment