diff --git a/PugSharp/Player.cs b/PugSharp/Player.cs index b1f930fa..62e4f4c6 100644 --- a/PugSharp/Player.cs +++ b/PugSharp/Player.cs @@ -11,7 +11,8 @@ namespace PugSharp; public class Player : IPlayer { private static readonly ILogger _Logger = LogManager.CreateLogger(); - private readonly CCSPlayerController _PlayerController; + private uint _EntityIndex; + private CCSPlayerController _PlayerController; public Player(CCSPlayerController playerController) { @@ -20,6 +21,7 @@ public Player(CCSPlayerController playerController) _Logger.LogError("PlayerController is invalid!"); } + _EntityIndex = playerController.EntityIndex!.Value.Value; _PlayerController = playerController; if (_PlayerController.ActionTrackingServices != null) { @@ -29,17 +31,32 @@ public Player(CCSPlayerController playerController) private T DefaultIfInvalid(Func loadValue) where T : struct { + if (_PlayerController == null || !_PlayerController.IsValid) + { + _PlayerController = Utilities.GetPlayerFromIndex((int)_EntityIndex); + } + return _PlayerController != null && _PlayerController.IsValid ? loadValue() : default; } private T DefaultIfInvalid(Func loadValue, T defaultValue) { + if (_PlayerController == null || !_PlayerController.IsValid) + { + _PlayerController = Utilities.GetPlayerFromIndex((int)_EntityIndex); + } + return _PlayerController != null && _PlayerController.IsValid ? loadValue() : defaultValue; } private T? NullIfInvalid(Func loadValue) { + if (_PlayerController == null || !_PlayerController.IsValid) + { + _PlayerController = Utilities.GetPlayerFromIndex((int)_EntityIndex); + } + return _PlayerController != null && _PlayerController.IsValid ? loadValue() : default; }