From f57a16f5cc7f6310095b72c2510805b23862fe15 Mon Sep 17 00:00:00 2001 From: anth64 Date: Sun, 28 Jun 2026 15:33:58 +0200 Subject: [PATCH] refactor(api)make Register/Unregister static, hide Instance --- CommandHook/CommandHookModSystem.cs | 19 ++++++++++++++----- CommandHook/modinfo.json | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CommandHook/CommandHookModSystem.cs b/CommandHook/CommandHookModSystem.cs index 849e2a3..ddc08c9 100644 --- a/CommandHook/CommandHookModSystem.cs +++ b/CommandHook/CommandHookModSystem.cs @@ -23,7 +23,7 @@ namespace CommandHook; /// public class CommandHookModSystem : ModSystem { - public static CommandHookModSystem? Instance; + internal static CommandHookModSystem? Instance; private readonly List listeners = new(); @@ -54,6 +54,7 @@ public class CommandHookModSystem : ModSystem /// /// Registers a listener for the commands it returns from . + /// No-ops silently if CommandHook hasn't finished server-side startup yet. /// /// /// Calling this again for a that is already @@ -63,8 +64,11 @@ public class CommandHookModSystem : ModSystem /// list, this just calls instead. /// /// The listener to register or update. - public void Register(ICommandHookListener listener) + public static void Register(ICommandHookListener listener) { + if (Instance == null) + return; + var commands = listener.Commands; if (commands == null || commands.Count == 0) @@ -73,6 +77,7 @@ public class CommandHookModSystem : ModSystem return; } + var listeners = Instance.listeners; int index = listeners.FindIndex(l => l.ModId == listener.ModId); bool changed = false; @@ -88,7 +93,7 @@ public class CommandHookModSystem : ModSystem } if (changed) - Rebuild(); + Instance.Rebuild(); } /// @@ -96,15 +101,19 @@ public class CommandHookModSystem : ModSystem /// that was never registered, this is a no-op in that case. /// /// The listener to remove, matched by . - public void Unregister(ICommandHookListener listener) + public static void Unregister(ICommandHookListener listener) { + if (Instance == null) + return; + + var listeners = Instance.listeners; int index = listeners.FindIndex(l => l.ModId == listener.ModId); if (index < 0) return; listeners.RemoveAt(index); - Rebuild(); + Instance.Rebuild(); } // Rebuilds the dispatch table from listeners. Runs unconditionally and diff --git a/CommandHook/modinfo.json b/CommandHook/modinfo.json index fae9270..322736d 100644 --- a/CommandHook/modinfo.json +++ b/CommandHook/modinfo.json @@ -6,7 +6,7 @@ "authors": [ "anth64" ], - "version": "2.0.0", + "version": "2.1.0", "side": "Server", "dependencies": { "game": "1.22.3"