Is it possible to unregister a command?


#1

Hello,
I am trying to implement plugin restarting to my plugin, but in the enable method last line is to register the plugin’s command.

So that leads to the exception

java.lang.IllegalArgumentException: A plugin may not register multiple commands for the same alias ('plan')!
   org.spongepowered.common.command.SpongeCommandManager.register(SpongeCommandManager.java:157)
   org.spongepowered.common.command.SpongeCommandManager.register(SpongeCommandManager.java:126)
   org.spongepowered.common.command.SpongeCommandManager.register(SpongeCommandManager.java:121)
   com.djrapitops.plugin.SpongePlugin.registerCommand(SpongePlugin.java:77)
   com.djrapitops.plan.PlanSponge.onEnable(PlanSponge.java:71)
   com.djrapitops.plugin.PluginCommon.reload(PluginCommon.java:19)
   com.djrapitops.plugin.SpongePlugin.reloadPlugin(SpongePlugin.java:40)
   com.djrapitops.plan.command.commands.ReloadCommand.onCommand(ReloadCommand.java:31)
   com.djrapitops.plugin.command.TreeCmdNode.onCommand(TreeCmdNode.java:111)
   com.djrapitops.plugin.command.sponge.SpongeCommand.process(SpongeCommand.java:38)
   org.spongepowered.api.command.dispatcher.SimpleDispatcher.process(SimpleDispatcher.java:341)
   org.spongepowered.common.command.SpongeCommandManager.process(SpongeCommandManager.java:328)
   net.minecraft.command.ServerCommandManager.func_71556_a(SourceFile:1083)
   net.minecraft.server.dedicated.DedicatedServer.func_71333_ah(SourceFile:382)
   net.minecraft.server.dedicated.DedicatedServer.func_71190_q(SourceFile:350)
   net.minecraft.server.MinecraftServer.func_71217_p(SourceFile:560)
   net.minecraft.server.MinecraftServer.run(SourceFile:464)
   java.lang.Thread.run(Unknown Source)
Error was logged: Apr 13, 08:52:15

On Bukkit and Bungee the registration works while reloading because behind the scenes the CommandExecutor is swapped to the new one.

So is there a way to unregister a command before re-registering it? (Not that it causes any issues since the old command instance still has the same plugin instance inside it.)

PS, code doing command registration (Uses my abstract CommandNode and a wrapper for it for Sponge)

@Override
public void registerCommand(String name, CommandNode command) {
    CommandManager commandManager = Sponge.getCommandManager();
    commandManager.register(this, new SpongeCommand(command), name);
    PluginCommon.saveCommandClassRelations(command, this.getClass());
}

I do see that there is CommandManager#removeMapping(CommandMapping) but I can’t figure out how to get CommandMapping object for the name


#2

Solution:

Store CommandMappings when registering command

Optional<CommandMapping> registered =
                         commandManager.register(this, new SpongeCommand(command), name);

#3

Sponge.getCommandManager().getOwnedBy(pluginInstance) to get all registered mappings.