Skip to content

Commit 2b9f7c7

Browse files
committed
Command registration lifecycle fix
1 parent 9802d23 commit 2b9f7c7

2 files changed

Lines changed: 52 additions & 13 deletions

File tree

server/src/main/java/dev/plex/handlers/CommandHandler.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class CommandHandler
1616
private final List<PlexCommand> commands = new ArrayList<>();
1717
private boolean lifecycleRegistered;
1818
private boolean lifecycleReloadRequired;
19+
private boolean suppressLifecycleWarnings;
1920

2021
public CommandHandler(Plex plugin)
2122
{
@@ -31,7 +32,10 @@ public void registerCommand(PlexCommand command)
3132
if (lifecycleRegistered)
3233
{
3334
lifecycleReloadRequired = true;
34-
PlexLog.warn("Command {0} was registered after the Brigadier command lifecycle event; it will be included on the next command lifecycle rebuild.", command.getName());
35+
if (!suppressLifecycleWarnings)
36+
{
37+
PlexLog.warn("Command {0} was registered after the Brigadier command lifecycle event; it will be included on the next command lifecycle rebuild.", command.getName());
38+
}
3539
}
3640
}
3741

@@ -41,10 +45,20 @@ public void unregisterCommand(PlexCommand command)
4145
if (removed && lifecycleRegistered)
4246
{
4347
lifecycleReloadRequired = true;
44-
PlexLog.warn("Command {0} was unregistered after the Brigadier command lifecycle event; Paper may keep the active Brigadier node until the next command lifecycle rebuild.", command.getName());
48+
if (!suppressLifecycleWarnings)
49+
{
50+
PlexLog.warn("Command {0} was unregistered after the Brigadier command lifecycle event; Paper may keep the active Brigadier node until the next command lifecycle rebuild.", command.getName());
51+
}
4552
}
4653
}
4754

55+
public boolean setSuppressLifecycleWarnings(boolean suppress)
56+
{
57+
boolean previous = suppressLifecycleWarnings;
58+
suppressLifecycleWarnings = suppress;
59+
return previous;
60+
}
61+
4862
public boolean requiresLifecycleReload()
4963
{
5064
return lifecycleReloadRequired;

server/src/main/java/dev/plex/module/ModuleManager.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.collect.Lists;
44
import dev.plex.Plex;
5+
import dev.plex.handlers.CommandHandler;
56
import dev.plex.module.exception.ModuleLoadException;
67
import dev.plex.util.PlexLog;
78

@@ -152,25 +153,49 @@ public void loadModules()
152153

153154
public void enableModules()
154155
{
155-
this.modules.forEach(module ->
156+
CommandHandler handler = plugin.getCommandHandler();
157+
boolean previous = handler != null && handler.setSuppressLifecycleWarnings(true);
158+
try
156159
{
157-
PlexLog.log("Enabling module " + module.getPlexModuleFile().getName() + " with version " + module.getPlexModuleFile().getVersion());
158-
module.enable();
159-
});
160+
this.modules.forEach(module ->
161+
{
162+
PlexLog.log("Enabling module " + module.getPlexModuleFile().getName() + " with version " + module.getPlexModuleFile().getVersion());
163+
module.enable();
164+
});
165+
}
166+
finally
167+
{
168+
if (handler != null)
169+
{
170+
handler.setSuppressLifecycleWarnings(previous);
171+
}
172+
}
160173
}
161174

162175
public void disableModules()
163176
{
164-
this.modules.forEach(module ->
177+
CommandHandler handler = plugin.getCommandHandler();
178+
boolean previous = handler != null && handler.setSuppressLifecycleWarnings(true);
179+
try
165180
{
166-
PlexLog.log("Disabling module " + module.getPlexModuleFile().getName() + " with version " + module.getPlexModuleFile().getVersion());
167-
module.getCommands().stream().toList().forEach(plexCommand ->
181+
this.modules.forEach(module ->
168182
{
169-
module.unregisterCommand(plexCommand);
183+
PlexLog.log("Disabling module " + module.getPlexModuleFile().getName() + " with version " + module.getPlexModuleFile().getVersion());
184+
module.getCommands().stream().toList().forEach(plexCommand ->
185+
{
186+
module.unregisterCommand(plexCommand);
187+
});
188+
module.getListeners().stream().toList().forEach(module::unregisterListener);
189+
module.disable();
170190
});
171-
module.getListeners().stream().toList().forEach(module::unregisterListener);
172-
module.disable();
173-
});
191+
}
192+
finally
193+
{
194+
if (handler != null)
195+
{
196+
handler.setSuppressLifecycleWarnings(previous);
197+
}
198+
}
174199
}
175200

176201
public void unloadModules()

0 commit comments

Comments
 (0)