@@ -21,6 +21,8 @@ public class Plugin : TerrariaPlugin
2121 public override string Name => Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ! ;
2222 public override Version Version => new ( 1 , 0 , 0 , 0 ) ;
2323
24+ private const string EmptyCommandHelpText = "[Vortex.Adapter] Empty placeholder command" ;
25+
2426 internal static readonly List < TSPlayer > ServerPlayers = new ( ) ;
2527 internal static Channel < int > Channeler = Channel . CreateBounded < int > ( 1 ) ;
2628 internal static readonly Dictionary < int , KillNpc > DamageBoss = [ ] ;
@@ -64,7 +66,7 @@ public override void Initialize()
6466 ServerApi . Hooks . NpcKilled . Register ( this , OnKillNpc ) ;
6567 GetDataHandlers . KillMe . Register ( this . OnKill ) ;
6668
67- Config . Instance . SocketConfig . EmptyCommand . ForEach ( x => Commands . ChatCommands . Add ( new ( "" , ( _ ) => { } , x ) ) ) ;
69+ RegisterEmptyCommands ( Config . Instance . SocketConfig . EmptyCommand ) ;
6870 GeneralHooks . ReloadEvent += Config . Reload ;
6971 Utils . HandleCommandLine ( Environment . GetCommandLineArgs ( ) ) ;
7072
@@ -102,14 +104,56 @@ protected override void Dispose(bool disposing)
102104 _timer . Elapsed -= TimerUpdate ;
103105 _timer . Stop ( ) ;
104106
105- RemoveAssemblyCommands ( Assembly . GetExecutingAssembly ( ) ) ;
107+ RemoveAssemblyCommands ( Assembly . GetExecutingAssembly ( ) , Config . Instance . SocketConfig . EmptyCommand ) ;
106108 }
107109 base . Dispose ( disposing ) ;
108110 }
109111
110- public static void RemoveAssemblyCommands ( Assembly assembly )
112+ public static void RemoveAssemblyCommands ( Assembly assembly , IEnumerable < string > ? emptyCommands = null )
111113 {
112114 Commands . ChatCommands . RemoveAll ( cmd => cmd . GetType ( ) . Assembly == assembly ) ;
115+ RemoveRegisteredEmptyCommandsCore ( NormalizeEmptyCommandNames ( emptyCommands ) ) ;
116+ }
117+
118+ public static void RegisterEmptyCommands ( IEnumerable < string > ? emptyCommands )
119+ {
120+ var normalizedNames = NormalizeEmptyCommandNames ( emptyCommands ) ;
121+ RemoveRegisteredEmptyCommandsCore ( normalizedNames ) ;
122+
123+ foreach ( var commandName in normalizedNames )
124+ {
125+ Commands . ChatCommands . Add ( new TShockAPI . Command ( "" , _ => { } , commandName )
126+ {
127+ HelpText = EmptyCommandHelpText
128+ } ) ;
129+ }
130+ }
131+
132+ private static void RemoveRegisteredEmptyCommandsCore ( string [ ] normalizedNames )
133+ {
134+ if ( normalizedNames . Length == 0 )
135+ {
136+ return ;
137+ }
138+
139+ var nameSet = new HashSet < string > ( normalizedNames , StringComparer . OrdinalIgnoreCase ) ;
140+ Commands . ChatCommands . RemoveAll ( cmd =>
141+ cmd . HelpText == EmptyCommandHelpText &&
142+ nameSet . Contains ( cmd . Name ) ) ;
143+ }
144+
145+ private static string [ ] NormalizeEmptyCommandNames ( IEnumerable < string > ? emptyCommands )
146+ {
147+ if ( emptyCommands == null )
148+ {
149+ return [ ] ;
150+ }
151+
152+ return emptyCommands
153+ . Where ( static x => ! string . IsNullOrWhiteSpace ( x ) )
154+ . Select ( static x => x . Trim ( ) )
155+ . Distinct ( StringComparer . OrdinalIgnoreCase )
156+ . ToArray ( ) ;
113157 }
114158
115159 private void TimerUpdate ( object ? sender , ElapsedEventArgs e )
0 commit comments