From 75540b590cf8704da57e1b7607cbb34f17aeffae Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Thu, 14 Nov 2024 07:42:03 -0500 Subject: [PATCH] chore: Add support for YSF Server Functions --- src/Host/Extensions/HostEcsBuilderExtensions.cs | 12 +++++++++++- .../PlayerPauseStateChangeMiddleware.cs | 15 +++++++++++++++ src/Host/Startup.cs | 3 ++- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/Host/Middlewares/PlayerPauseStateChangeMiddleware.cs diff --git a/src/Host/Extensions/HostEcsBuilderExtensions.cs b/src/Host/Extensions/HostEcsBuilderExtensions.cs index b1deaf0a..2b768018 100644 --- a/src/Host/Extensions/HostEcsBuilderExtensions.cs +++ b/src/Host/Extensions/HostEcsBuilderExtensions.cs @@ -23,7 +23,8 @@ public static IEcsBuilder EnableExceptionHandler(this IEcsBuilder builder) "OnPlayerPickUpPickup", "OnPlayerSpawn", "OnPlayerRequestClass", - "OnPlayerRequestSpawn" + "OnPlayerRequestSpawn", + "OnPlayerPauseStateChange" ]; foreach (string @event in events) @@ -33,4 +34,13 @@ public static IEcsBuilder EnableExceptionHandler(this IEcsBuilder builder) return builder; } + + public static IEcsBuilder EnableYsfEvents(this IEcsBuilder builder) + { + builder + .EnableEvent("OnPlayerPauseStateChange"); + + builder.UseMiddleware("OnPlayerPauseStateChange"); + return builder; + } } diff --git a/src/Host/Middlewares/PlayerPauseStateChangeMiddleware.cs b/src/Host/Middlewares/PlayerPauseStateChangeMiddleware.cs new file mode 100644 index 00000000..cc9d04d4 --- /dev/null +++ b/src/Host/Middlewares/PlayerPauseStateChangeMiddleware.cs @@ -0,0 +1,15 @@ +namespace CTF.Host.Middlewares; + +public class PlayerPauseStateChangeMiddleware(EventDelegate next) +{ + public object Invoke(EventContext context, IEntityManager entityManager) + { + var playerEntity = SampEntities.GetPlayerId((int)context.Arguments[0]); + + if (!entityManager.Exists(playerEntity)) + return null; + + context.Arguments[0] = playerEntity; + return next(context); + } +} diff --git a/src/Host/Startup.cs b/src/Host/Startup.cs index 201c8dfb..981f014e 100644 --- a/src/Host/Startup.cs +++ b/src/Host/Startup.cs @@ -32,6 +32,7 @@ public void Configure(IEcsBuilder builder) .EnableSampEvents() .EnablePlayerCommands() .EnableRconCommands() - .EnableStreamerEvents(); + .EnableStreamerEvents() + .EnableYsfEvents(); } }