From f31ca3a42b30270db43073d5fcb3de5f23b39ad0 Mon Sep 17 00:00:00 2001 From: destoer Date: Wed, 28 Feb 2024 19:59:40 +0000 Subject: [PATCH] make entity.player not reinvent the wheel --- src/Debug.cs | 14 ++++++++++++++ src/Jail.cs | 5 +++-- src/Lib/Entity.cs | 38 +++++++++++++++++--------------------- src/Lib/Player.cs | 4 ++-- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/Debug.cs b/src/Debug.cs index 3a32f47..fd594ec 100644 --- a/src/Debug.cs +++ b/src/Debug.cs @@ -129,6 +129,20 @@ public static void TestLRInc(CCSPlayerController? invoke, CommandInfo command) { JailPlugin.WinLR(invoke, LastRequest.LRType.KNIFE); } + [RequiresPermissions("@jail/debug")] + public static void TestPlayer(CCSPlayerController? invoke, CommandInfo command) + { + if(invoke.IsLegal()) + { + var pawn = invoke.Pawn(); + + if(pawn != null) + { + invoke.PrintToChat($"name: {invoke.DesignerName} : {pawn.DesignerName}"); + } + } + } + // are these commands allowed or not? public static readonly bool enable = true; diff --git a/src/Jail.cs b/src/Jail.cs index 6c593f5..43a82eb 100644 --- a/src/Jail.cs +++ b/src/Jail.cs @@ -331,6 +331,7 @@ void RegisterCommands() AddCommand("lr_debug","debug : start an lr without restriction",lr.LRDebugCmd); AddCommand("is_blocked","debug : print block state",warden.block.IsBlocked); AddCommand("test_laser","test laser",Debug.TestLaser); + AddCommand("test_player","testt player",Debug.TestPlayer); AddCommand("test_strip","test weapon strip",Debug.TestStripCmd); AddCommand("join_ct_debug","debug : force join ct",Debug.JoinCtCmd); AddCommand("hide_weapon_debug","debug : hide player weapon on back",Debug.HideWeaponCmd); @@ -447,7 +448,7 @@ void OnClientVoice(int slot) // button log HookResult OnButtonPressed(CEntityIOOutput output, String name, CEntityInstance activator, CEntityInstance caller, CVariant value, float delay) { - CCSPlayerController? player = activator.Player(); + CCSPlayerController? player = new CBaseEntity(activator.Handle).Player(); // grab player controller from pawn CBaseEntity? ent = Utilities.GetEntityFromIndex((int)caller.Index); @@ -531,7 +532,7 @@ HookResult OnTakeDamage(DynamicHook handle) CHandle dealer = damage_info.Attacker; // get player and attacker - CCSPlayerController? player = victim.Player(); + CCSPlayerController? player = new CBaseEntity(victim.Handle).Player(); CCSPlayerController? attacker = dealer.Player(); if(player.IsLegal()) diff --git a/src/Lib/Entity.cs b/src/Lib/Entity.cs index a85fe3c..105f5e3 100644 --- a/src/Lib/Entity.cs +++ b/src/Lib/Entity.cs @@ -126,45 +126,41 @@ public static void ForceOpen() } - static public CCSPlayerController? Player(this CEntityInstance? instance) + static CCSPlayerController? PlayerFromPawn(CCSPlayerPawn? pawn) { - if(instance == null) - { - return null; - } - - // grab the pawn index - int player_index = (int)instance.Index; - - // grab player controller from pawn - CCSPlayerPawn? player_pawn = Utilities.GetEntityFromIndex(player_index); - // pawn valid - if(player_pawn == null || !player_pawn.IsValid) + if(pawn == null || !pawn.IsValid) { return null; } // controller valid - if(player_pawn.OriginalController == null || !player_pawn.OriginalController.IsValid) + if(pawn.OriginalController == null || !pawn.OriginalController.IsValid) { return null; } // any further validity is up to the caller - return player_pawn.OriginalController.Value; + return pawn.OriginalController.Value; + } + + static public CCSPlayerController? Player(this CBaseEntity? ent) + { + if(ent != null && ent.DesignerName == "player") + { + var pawn = new CCSPlayerPawn(ent.Handle); + + return PlayerFromPawn(pawn); + } + + return null; } static public CCSPlayerController? Player(this CHandle handle) { if(handle.IsValid) { - CBaseEntity? ent = handle.Value; - - if(ent != null) - { - return handle.Value.Player(); - } + return handle.Value.Player(); } return null; diff --git a/src/Lib/Player.cs b/src/Lib/Player.cs index 1edb6ab..2de0c45 100644 --- a/src/Lib/Player.cs +++ b/src/Lib/Player.cs @@ -178,7 +178,7 @@ static public void SetArmour(this CCSPlayerController? player, int hp) } } - static public void StripWeapons(this CCSPlayerController? player, bool remove_knife = false) + static public void StripWeapons(this CCSPlayerController? player, bool removeKnife = false) { // only care if player is valid if(!player.IsLegalAlive()) @@ -189,7 +189,7 @@ static public void StripWeapons(this CCSPlayerController? player, bool remove_kn player.RemoveWeapons(); // dont remove knife its buggy - if(!remove_knife) + if(!removeKnife) { player.GiveWeapon("knife"); }