Skip to content

Commit b329f49

Browse files
Merge pull request #7 from AX-17/master
修复一个空指令bug
2 parents 810c4ba + 391823d commit b329f49

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

Vortex.Adapter/Plugin.cs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

Vortex.Adapter/Setting/Config.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public static Config Read()
4848

4949
public static void Reload(ReloadEventArgs e)
5050
{
51-
Plugin.RemoveAssemblyCommands(Assembly.GetExecutingAssembly());
51+
Plugin.RemoveAssemblyCommands(Assembly.GetExecutingAssembly(), _instance?.SocketConfig.EmptyCommand);
5252
_instance = Read();
53-
Instance.SocketConfig.EmptyCommand.ForEach(x => Commands.ChatCommands.Add(new("", (_) => { }, x)));
53+
Plugin.RegisterEmptyCommands(Instance.SocketConfig.EmptyCommand);
5454
}
5555
}

0 commit comments

Comments
 (0)