Skip to content

Commit

Permalink
Merge pull request #25 from destoer/dev
Browse files Browse the repository at this point in the history
make entity.player not reinvent the wheel
  • Loading branch information
destoer authored Feb 28, 2024
2 parents b3e0940 + f31ca3a commit 2bc1973
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
14 changes: 14 additions & 0 deletions src/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/Jail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<CBaseEntity>((int)caller.Index);
Expand Down Expand Up @@ -531,7 +532,7 @@ HookResult OnTakeDamage(DynamicHook handle)
CHandle<CBaseEntity> 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())
Expand Down
38 changes: 17 additions & 21 deletions src/Lib/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CCSPlayerPawn>(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<CBaseEntity> handle)
{
if(handle.IsValid)
{
CBaseEntity? ent = handle.Value;

if(ent != null)
{
return handle.Value.Player();
}
return handle.Value.Player();
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Lib/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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");
}
Expand Down

0 comments on commit 2bc1973

Please sign in to comment.