Skip to content

Commit

Permalink
Refactor entity I/O listener, add func_breakable IO listener
Browse files Browse the repository at this point in the history
* TODO: IO listener will only listen for when the breakable does something when it breaks... We need a better way to listen for eg. CTs pursuing vents.
  • Loading branch information
Mooshua committed Feb 6, 2024
1 parent 7318c55 commit 869a66a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
29 changes: 20 additions & 9 deletions mod/Jailbreak.Logs/Listeners/LogEntityListeners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Extensions;

namespace Jailbreak.Logs;

Expand All @@ -20,19 +21,29 @@ public LogEntityListeners(IRichLogService logs)
public HookResult OnButtonPressed(CEntityIOOutput output, string name, CEntityInstance activator,
CEntityInstance caller, CVariant value, float delay)
{
if (!activator.IsValid)
return HookResult.Continue;
int index = (int)activator.Index;
CCSPlayerPawn? pawn = Utilities.GetEntityFromIndex<CCSPlayerPawn>(index);
if (!pawn.IsValid)
return HookResult.Continue;
if (!pawn.OriginalController.IsValid)
if (!activator.TryGetController(out var player))
return HookResult.Continue;

CBaseEntity? ent = Utilities.GetEntityFromIndex<CBaseEntity>((int)caller.Index);
if (!ent.IsValid)


_logs.Append(
$"{_logs.Player(player)} pressed a button: {ent.Entity?.Name ?? "Unlabeled"} -> {output?.Connections?.TargetDesc ?? "None"}");

Check warning on line 31 in mod/Jailbreak.Logs/Listeners/LogEntityListeners.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'playerController' in 'FormatObject IRichLogService.Player(CCSPlayerController playerController)'.

Check warning on line 31 in mod/Jailbreak.Logs/Listeners/LogEntityListeners.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'playerController' in 'FormatObject IRichLogService.Player(CCSPlayerController playerController)'.
return HookResult.Continue;
}

[EntityOutputHook("func_breakable", "OnBreak")]
public HookResult OnBreakableBroken(CEntityIOOutput output, string name, CEntityInstance activator,
CEntityInstance caller, CVariant value, float delay)
{
if (!activator.TryGetController(out var player))
return HookResult.Continue;

CBaseEntity? ent = Utilities.GetEntityFromIndex<CBaseEntity>((int)caller.Index);


_logs.Append(
$"{_logs.Player(pawn.OriginalController.Value!)} pressed a button {ent.Entity?.Name ?? "Unlabeled"} -> {output?.Connections?.TargetDesc ?? "None"}");
$"{_logs.Player(player)} broke an entity: {ent.Entity?.Name ?? "Unlabeled"} -> {output?.Connections?.TargetDesc ?? "None"}");

Check warning on line 46 in mod/Jailbreak.Logs/Listeners/LogEntityListeners.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'playerController' in 'FormatObject IRichLogService.Player(CCSPlayerController playerController)'.

Check warning on line 46 in mod/Jailbreak.Logs/Listeners/LogEntityListeners.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'playerController' in 'FormatObject IRichLogService.Player(CCSPlayerController playerController)'.
return HookResult.Continue;
}
}
31 changes: 31 additions & 0 deletions public/Jailbreak.Public/Extensions/EntityIOExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;

namespace Jailbreak.Public.Extensions;

public static class EntityIOExtensions
{
public static bool TryGetController(this CEntityInstance pawn, out CCSPlayerController? controller)
{
controller = null;

if (!pawn.IsValid)
return false;

int index = (int)pawn.Index;
var playerPawn = Utilities.GetEntityFromIndex<CCSPlayerPawn>(index);

if (!playerPawn.IsValid)
return false;

if (!playerPawn.OriginalController.IsValid)
return false;

controller = playerPawn.OriginalController.Value;

if (controller?.IsReal() != true)
return false;

return true;
}
}

0 comments on commit 869a66a

Please sign in to comment.