diff --git a/mod/Jailbreak.Logs/Listeners/LogEntityListeners.cs b/mod/Jailbreak.Logs/Listeners/LogEntityListeners.cs index c2c7fe8a..0bfc2c70 100644 --- a/mod/Jailbreak.Logs/Listeners/LogEntityListeners.cs +++ b/mod/Jailbreak.Logs/Listeners/LogEntityListeners.cs @@ -4,6 +4,7 @@ using Jailbreak.Formatting.Views; using Jailbreak.Public.Behaviors; +using Jailbreak.Public.Extensions; namespace Jailbreak.Logs; @@ -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(index); - if (!pawn.IsValid) - return HookResult.Continue; - if (!pawn.OriginalController.IsValid) + if (!activator.TryGetController(out var player)) return HookResult.Continue; + CBaseEntity? ent = Utilities.GetEntityFromIndex((int)caller.Index); - if (!ent.IsValid) + + + _logs.Append( + $"{_logs.Player(player)} pressed a button: {ent.Entity?.Name ?? "Unlabeled"} -> {output?.Connections?.TargetDesc ?? "None"}"); + 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((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"}"); return HookResult.Continue; } } diff --git a/public/Jailbreak.Public/Extensions/EntityIOExtensions.cs b/public/Jailbreak.Public/Extensions/EntityIOExtensions.cs new file mode 100644 index 00000000..c4c5b6d7 --- /dev/null +++ b/public/Jailbreak.Public/Extensions/EntityIOExtensions.cs @@ -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(index); + + if (!playerPawn.IsValid) + return false; + + if (!playerPawn.OriginalController.IsValid) + return false; + + controller = playerPawn.OriginalController.Value; + + if (controller?.IsReal() != true) + return false; + + return true; + } +}