diff --git a/Vortex.Adapter/Plugin.cs b/Vortex.Adapter/Plugin.cs index 91991d5..f56b7af 100644 --- a/Vortex.Adapter/Plugin.cs +++ b/Vortex.Adapter/Plugin.cs @@ -21,6 +21,8 @@ public class Plugin : TerrariaPlugin public override string Name => Assembly.GetExecutingAssembly().GetName().Name!; public override Version Version => new(1, 0, 0, 0); + private const string EmptyCommandHelpText = "[Vortex.Adapter] Empty placeholder command"; + internal static readonly List ServerPlayers = new(); internal static Channel Channeler = Channel.CreateBounded(1); internal static readonly Dictionary DamageBoss = []; @@ -64,7 +66,7 @@ public override void Initialize() ServerApi.Hooks.NpcKilled.Register(this, OnKillNpc); GetDataHandlers.KillMe.Register(this.OnKill); - Config.Instance.SocketConfig.EmptyCommand.ForEach(x => Commands.ChatCommands.Add(new("", (_) => { }, x))); + RegisterEmptyCommands(Config.Instance.SocketConfig.EmptyCommand); GeneralHooks.ReloadEvent += Config.Reload; Utils.HandleCommandLine(Environment.GetCommandLineArgs()); @@ -102,14 +104,56 @@ protected override void Dispose(bool disposing) _timer.Elapsed -= TimerUpdate; _timer.Stop(); - RemoveAssemblyCommands(Assembly.GetExecutingAssembly()); + RemoveAssemblyCommands(Assembly.GetExecutingAssembly(), Config.Instance.SocketConfig.EmptyCommand); } base.Dispose(disposing); } - public static void RemoveAssemblyCommands(Assembly assembly) + public static void RemoveAssemblyCommands(Assembly assembly, IEnumerable? emptyCommands = null) { Commands.ChatCommands.RemoveAll(cmd => cmd.GetType().Assembly == assembly); + RemoveRegisteredEmptyCommandsCore(NormalizeEmptyCommandNames(emptyCommands)); + } + + public static void RegisterEmptyCommands(IEnumerable? emptyCommands) + { + var normalizedNames = NormalizeEmptyCommandNames(emptyCommands); + RemoveRegisteredEmptyCommandsCore(normalizedNames); + + foreach (var commandName in normalizedNames) + { + Commands.ChatCommands.Add(new TShockAPI.Command("", _ => { }, commandName) + { + HelpText = EmptyCommandHelpText + }); + } + } + + private static void RemoveRegisteredEmptyCommandsCore(string[] normalizedNames) + { + if (normalizedNames.Length == 0) + { + return; + } + + var nameSet = new HashSet(normalizedNames, StringComparer.OrdinalIgnoreCase); + Commands.ChatCommands.RemoveAll(cmd => + cmd.HelpText == EmptyCommandHelpText && + nameSet.Contains(cmd.Name)); + } + + private static string[] NormalizeEmptyCommandNames(IEnumerable? emptyCommands) + { + if (emptyCommands == null) + { + return []; + } + + return emptyCommands + .Where(static x => !string.IsNullOrWhiteSpace(x)) + .Select(static x => x.Trim()) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToArray(); } private void TimerUpdate(object? sender, ElapsedEventArgs e) diff --git a/Vortex.Adapter/Setting/Config.cs b/Vortex.Adapter/Setting/Config.cs index b0755d0..37f947e 100644 --- a/Vortex.Adapter/Setting/Config.cs +++ b/Vortex.Adapter/Setting/Config.cs @@ -48,8 +48,8 @@ public static Config Read() public static void Reload(ReloadEventArgs e) { - Plugin.RemoveAssemblyCommands(Assembly.GetExecutingAssembly()); + Plugin.RemoveAssemblyCommands(Assembly.GetExecutingAssembly(), _instance?.SocketConfig.EmptyCommand); _instance = Read(); - Instance.SocketConfig.EmptyCommand.ForEach(x => Commands.ChatCommands.Add(new("", (_) => { }, x))); + Plugin.RegisterEmptyCommands(Instance.SocketConfig.EmptyCommand); } }