From ccaf44d50b040556c2413f4f03002ec756971347 Mon Sep 17 00:00:00 2001 From: code002lover Date: Mon, 14 Jul 2025 08:11:10 +0200 Subject: [PATCH] refactor config handling --- CustomClasses/CustomClasses.cs | 78 ++++++++++------------------ CustomClasses/ExtendedClass.cs | 8 --- CustomClasses/NegromancerHandler.cs | 4 +- CustomClasses/PlayerExtensions.cs | 11 ++++ CustomClasses/SerpentsHandManager.cs | 6 +-- CustomClasses/SetCClassCommand.cs | 37 ++++++------- 6 files changed, 56 insertions(+), 88 deletions(-) delete mode 100644 CustomClasses/ExtendedClass.cs create mode 100644 CustomClasses/PlayerExtensions.cs diff --git a/CustomClasses/CustomClasses.cs b/CustomClasses/CustomClasses.cs index 26b71f5..64ef050 100644 --- a/CustomClasses/CustomClasses.cs +++ b/CustomClasses/CustomClasses.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using System.Runtime.Remoting.Messaging; using CustomPlayerEffects; using HintServiceMeow.Core.Models.Hints; using Interactables.Interobjects.DoorUtils; @@ -53,45 +54,6 @@ public sealed class CustomClasses : Plugin public const ushort BroadcastDuration = 10; - /// - /// Configuration for the Janitor class. - /// - public JanitorConfig JanitorConfig { get; private set; } = new(); - - /// - /// Configuration for the Research Subject class. - /// - public ResearchSubjectConfig ResearchSubjectConfig { get; private set; } = new(); - - /// - /// Configuration for the Head Guard class. - /// - public HeadGuardConfig HeadGuardConfig { get; private set; } = new(); - - /// - /// Configuration for the Medic class. - /// - public MedicConfig MedicConfig { get; private set; } = new(); - - /// - /// Configuration for the Gambler class. - /// - public GamblerConfig GamblerConfig { get; private set; } = new(); - - /// - /// Configuration for the ShadowStepper class. - /// - public ShadowStepperConfig ShadowStepperConfig { get; private set; } = new(); - - public MtfDemolitionistConfig MtfDemolitionistConfig { get; private set; } = new(); - public ScoutConfig ScoutConfig { get; private set; } = new(); - public ExplosiveMasterConfig ExplosiveMasterConfig { get; private set; } = new(); - public FlashMasterConfig FlashMasterConfig { get; private set; } = new(); - public SerpentsHandConfig SerpentsHandConfig { get; private set; } = new(); - public NegromancerConfig NegromancerConfig { get; private set; } = new(); - public NegromancerShadowConfig NegromancerShadowConfig { get; private set; } = new(); - public BloodFueledConfig BloodFueledConfig { get; private set; } = new(); - internal readonly Dictionary Hints = new(); public static CustomClasses Instance { get; private set; } @@ -223,9 +185,9 @@ public sealed class CustomClasses : Plugin { SerpentsHandManager.SpawnSerpentWave(); - ClassManager.TryHandleSpawn(spectator, ScoutConfig, typeof(ScoutConfig), () => + ClassManager.TryHandleSpawn(spectator, ClassManager.GetConfig(), typeof(ScoutConfig), () => { - if (!ClassManager.ForceSpawn(spectator, SerpentsHandConfig, typeof(SerpentsHandConfig), PreSpawn)) + if (!ClassManager.ForceSpawn(spectator, ClassManager.GetConfig(), typeof(SerpentsHandConfig), PreSpawn)) Logger.Error("Serpents Hand didn't spawn"); return; @@ -239,7 +201,7 @@ public sealed class CustomClasses : Plugin } } - if (ClassManager.TryHandleSpawn(spectator, ScoutConfig, typeof(ScoutConfig), () => + if (ClassManager.TryHandleSpawn(spectator, ClassManager.GetConfig(), typeof(ScoutConfig), () => { spectator.SetRole(ev.Wave.Faction == Faction.FoundationStaff ? RoleTypeId.NtfPrivate : RoleTypeId.ChaosConscript, RoleChangeReason.Respawn, RoleSpawnFlags.UseSpawnpoint); })) return; @@ -259,7 +221,7 @@ public sealed class CustomClasses : Plugin ev.IsAllowed = false; foreach (var evSpawningPlayer in ev.SpawningPlayers) { - if (!ClassManager.ForceSpawn(evSpawningPlayer, SerpentsHandConfig, typeof(SerpentsHandConfig), PreSpawn)) + if (!ClassManager.ForceSpawn(evSpawningPlayer, ClassManager.GetConfig(), typeof(SerpentsHandConfig), PreSpawn)) Logger.Error("Serpents Hand didn't spawn"); continue; @@ -280,16 +242,10 @@ public sealed class CustomClasses : Plugin { ev.Player.CustomInfo = ""; - if (ClassManager.TryHandleSpawn(ev.Player, JanitorConfig, typeof(JanitorConfig), null)) return; - if (ClassManager.TryHandleSpawn(ev.Player, ResearchSubjectConfig, typeof(ResearchSubjectConfig), null)) return; - if (ClassManager.TryHandleSpawn(ev.Player, HeadGuardConfig, typeof(HeadGuardConfig), null)) return; - if (ClassManager.TryHandleSpawn(ev.Player, MedicConfig, typeof(MedicConfig), null)) return; - if (ClassManager.TryHandleSpawn(ev.Player, GamblerConfig, typeof(GamblerConfig), null)) return; - if (ClassManager.TryHandleSpawn(ev.Player, ShadowStepperConfig, typeof(ShadowStepperConfig), null)) return; - if (ClassManager.TryHandleSpawn(ev.Player, MtfDemolitionistConfig, typeof(MtfDemolitionistConfig), null)) + if (ClassManager.Configs.Any(classManagerConfig => ClassManager.TryHandleSpawn(ev.Player, classManagerConfig.Value, classManagerConfig.Key, null))) + { return; - if (ClassManager.TryHandleSpawn(ev.Player, ExplosiveMasterConfig, typeof(ExplosiveMasterConfig), null)) return; - if (ClassManager.TryHandleSpawn(ev.Player, BloodFueledConfig, typeof(BloodFueledConfig), null)) return; + } } private static void OnScp914ProcessingPickup(Scp914ProcessingPickupEventArgs ev) @@ -330,6 +286,7 @@ public class CustomClassManager private readonly object _lock = new(); private readonly Random _random = new(); private readonly Dictionary _spawnStates = new(); + public Dictionary Configs { get; } = new(); /// /// Initializes a new instance of the class and registers all handlers. @@ -378,13 +335,30 @@ public class CustomClassManager /// Optional custom spawn state private void RegisterHandler(ICustomClassHandler handler, [CanBeNull] SpawnState spawnState = null) where T : CustomClassConfig { + T config; + try + { + config = Activator.CreateInstance(); + } + catch (Exception ex) + { + Logger.Error($"Failed to create config instance for {typeof(T).Name}: {ex.Message}"); + return; + } + lock (_lock) { _spawnStates[typeof(T)] = spawnState ?? new SpawnState(); _handlers[typeof(T)] = handler; + Configs[typeof(T)] = config; } } + public T GetConfig() where T : CustomClassConfig + { + return (T)Configs[typeof(T)]; + } + /// /// Resets all spawn states for a new round. /// diff --git a/CustomClasses/ExtendedClass.cs b/CustomClasses/ExtendedClass.cs deleted file mode 100644 index e2970d0..0000000 --- a/CustomClasses/ExtendedClass.cs +++ /dev/null @@ -1,8 +0,0 @@ -using PlayerRoles; - -namespace CustomClasses; - -public abstract class ExtendedClass: PlayerRoleBase -{ - -} \ No newline at end of file diff --git a/CustomClasses/NegromancerHandler.cs b/CustomClasses/NegromancerHandler.cs index f10d23c..33f77d0 100644 --- a/CustomClasses/NegromancerHandler.cs +++ b/CustomClasses/NegromancerHandler.cs @@ -1,4 +1,3 @@ -using System.Net; using CustomPlayerEffects; using LabApi.Events.Arguments.Scp049Events; using LabApi.Events.Handlers; @@ -153,12 +152,11 @@ public class NegromancerManager private void OnScp049ResurrectedBody(Scp049ResurrectedBodyEventArgs ev) { var classManager = _plugin.ClassManager; - // Check if the reviver is a Negromancer if (classManager == null || !IsNegromancer(ev.Player)) return; ev.Target.SetRole(RoleTypeId.Scp106, RoleChangeReason.Respawn, RoleSpawnFlags.None); - classManager.ForceSpawn(ev.Target, _plugin.NegromancerShadowConfig, typeof(NegromancerShadowConfig), null); + classManager.ForceSpawn(ev.Target, classManager.GetConfig(), typeof(NegromancerShadowConfig), null); } } \ No newline at end of file diff --git a/CustomClasses/PlayerExtensions.cs b/CustomClasses/PlayerExtensions.cs new file mode 100644 index 0000000..21cf8ee --- /dev/null +++ b/CustomClasses/PlayerExtensions.cs @@ -0,0 +1,11 @@ +using LabApi.Features.Wrappers; + +namespace CustomClasses; + +public static class PlayerExtensions +{ + public static string GetExtendedClass(this Player player) + { + return ""; + } +} \ No newline at end of file diff --git a/CustomClasses/SerpentsHandManager.cs b/CustomClasses/SerpentsHandManager.cs index 76a98fd..c383929 100644 --- a/CustomClasses/SerpentsHandManager.cs +++ b/CustomClasses/SerpentsHandManager.cs @@ -151,11 +151,11 @@ public class SerpentsHandManager // ReSharper disable once IteratorNeverReturns } - public void SpawnSerpentWave() + public static void SpawnSerpentWave() { var state = (SerpentsHandState)CustomClasses.Instance.ClassManager.GetSpawnState(typeof(SerpentsHandConfig)); - var serpentsHandConfig = _customClasses.SerpentsHandConfig; + var serpentsHandConfig = CustomClasses.Instance.ClassManager.GetConfig(); state.SetSpawned(); state.SetWillSpawn(); @@ -322,7 +322,7 @@ public class SpawnSerpentsCommand : ICommand return false; } - CustomClasses.Instance.SerpentsHandManager.SpawnSerpentWave(); + SerpentsHandManager.SpawnSerpentWave(); response = "success"; return true; diff --git a/CustomClasses/SetCClassCommand.cs b/CustomClasses/SetCClassCommand.cs index b55fbc6..c7f2b77 100644 --- a/CustomClasses/SetCClassCommand.cs +++ b/CustomClasses/SetCClassCommand.cs @@ -33,10 +33,7 @@ public class SetCClassCommand : ICommand return false; } - // Find the last argument as the class name var className = args[arguments.Offset + arguments.Count - 1].ToLower(); - - // Join all arguments except the last one to get the full player name var playerName = string.Join(" ", args.Skip(arguments.Offset).Take(arguments.Count - 1)); var player = Player.ReadyList.FirstOrDefault(x => x.Nickname == playerName || x.UserId == playerName); @@ -46,28 +43,24 @@ public class SetCClassCommand : ICommand return false; } - var customClasses = CustomClasses.Instance; - var manager = new CustomClassManager(); + var manager = CustomClasses.Instance.ClassManager; var success = className switch { - "janitor" => manager.ForceSpawn(player, customClasses.JanitorConfig, typeof(JanitorConfig), null), - "subject" or "researchsubject" => manager.ForceSpawn(player, customClasses.ResearchSubjectConfig, typeof(ResearchSubjectConfig), null), - "headguard" => manager.ForceSpawn(player, customClasses.HeadGuardConfig, typeof(HeadGuardConfig), null), - "medic" => manager.ForceSpawn(player, customClasses.MedicConfig, typeof(MedicConfig), null), - "gambler" => manager.ForceSpawn(player, customClasses.GamblerConfig, typeof(GamblerConfig), null), - "shadowstepper" => manager.ForceSpawn(player, customClasses.ShadowStepperConfig, typeof(ShadowStepperConfig), null), - "demolitionist" => manager.ForceSpawn(player, customClasses.MtfDemolitionistConfig, typeof(MtfDemolitionistConfig), null), - "scout" => manager.ForceSpawn(player, customClasses.ScoutConfig, typeof(ScoutConfig), null), - "explosivemaster" => manager.ForceSpawn(player, customClasses.ExplosiveMasterConfig, typeof(ExplosiveMasterConfig), null), - "flashmaster" => manager.ForceSpawn(player, customClasses.FlashMasterConfig, typeof(FlashMasterConfig), null), - "serpentshand" => manager.ForceSpawn(player, customClasses.SerpentsHandConfig, typeof(SerpentsHandConfig), - () => - { - SerpentsHandManager.PreSpawn(player); - }), - "negromancer" => manager.ForceSpawn(player, customClasses.NegromancerConfig, typeof(NegromancerConfig), null), - "bloodfueled" => manager.ForceSpawn(player, customClasses.BloodFueledConfig, typeof(BloodFueledConfig), null), + "janitor" => manager.ForceSpawn(player, manager.GetConfig(), typeof(JanitorConfig), null), + "subject" or "researchsubject" => manager.ForceSpawn(player, manager.GetConfig(), typeof(ResearchSubjectConfig), null), + "headguard" => manager.ForceSpawn(player, manager.GetConfig(), typeof(HeadGuardConfig), null), + "medic" => manager.ForceSpawn(player, manager.GetConfig(), typeof(MedicConfig), null), + "gambler" => manager.ForceSpawn(player, manager.GetConfig(), typeof(GamblerConfig), null), + "shadowstepper" => manager.ForceSpawn(player, manager.GetConfig(), typeof(ShadowStepperConfig), null), + "demolitionist" => manager.ForceSpawn(player, manager.GetConfig(), typeof(MtfDemolitionistConfig), null), + "scout" => manager.ForceSpawn(player, manager.GetConfig(), typeof(ScoutConfig), null), + "explosivemaster" => manager.ForceSpawn(player, manager.GetConfig(), typeof(ExplosiveMasterConfig), null), + "flashmaster" => manager.ForceSpawn(player, manager.GetConfig(), typeof(FlashMasterConfig), null), + "serpentshand" => manager.ForceSpawn(player, manager.GetConfig(), typeof(SerpentsHandConfig), + () => SerpentsHandManager.PreSpawn(player)), + "negromancer" => manager.ForceSpawn(player, manager.GetConfig(), typeof(NegromancerConfig), null), + "bloodfueled" => manager.ForceSpawn(player, manager.GetConfig(), typeof(BloodFueledConfig), null), _ => false };