Skip to content

Commit

Permalink
Added SkyUtils#unregisterCommand(String)
Browse files Browse the repository at this point in the history
Commands can now be unregistered on all platforms
  • Loading branch information
xDec0de committed Dec 19, 2024
1 parent be16178 commit 48593a0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,25 +351,35 @@ public void registerCommands(@NotNull GlobalCommand<P>... commands) {
}

/**
* Unregisters a command by {@code name}. In order to unregister
* a command, the command must have been registered by this
* plugin, as {@link JavaPlugin#getCommand(String)} is used in order to
* get the command to unregister.
* <p>
* <b>Note</b>: This method uses reflection and may be considered
* unsafe by some developers as it breaks the encapsulation principle
* by accessing the internal {@link SimpleCommandMap}
* Unregisters a command by {@code name}.
*
* @param name the name of the command to unregister, using
* aliases won't work.
* @param name the name of the command to unregister.
*
* @return {@code true} if the command exists, was registered and
* has been unregistered successfully, {@code false} otherwise.
*
* @since SkyUtils 1.0.0
*/
@Override
public boolean unregisterCommand(@NotNull String name) {
final PluginCommand plCommand = getPlugin().getCommand(name);
final SimpleCommandMap commandMap = getCommandMap();
return plCommand != null && commandMap != null && plCommand.unregister(commandMap);
final SimpleCommandMap map = getCommandMap();
if (map == null)
return false;
final Command cmd = map.getCommand(name);
return cmd != null && cmd.unregister(map);
}

/**
* Unregisters the provided {@code command}.
*
* @param command The {@link Command} to unregister.
*
* @return {@code true} if the command was previously registered
* and has been
*/
public boolean unregisterCommand(@NotNull Command command) {
final SimpleCommandMap map = getCommandMap();
return map != null && command.unregister(map);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,13 @@ public void registerCommands(CustomVelocityCommand<P, ? extends VelocityCommandS
manager.register(meta, command);
}
}

@Override
public boolean unregisterCommand(@NotNull String name) {
final CommandManager manager = getProxy().getCommandManager();
if (!manager.hasCommand(name))
return false;
manager.unregister(name);
return true;
}
}
24 changes: 24 additions & 0 deletions shared/src/main/java/net/codersky/skyutils/SkyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,32 @@ public final String getSkyUtilsVersion() {
- Commands
*/

/**
* Registers any amount of {@code commands}. Keep in mind that SkyUtils
* will attempt to register commands no matter the circumstances, meaning
* that, on platforms like Spigot, commands will be registered even if they
* aren't present on your {@code plugin.yml}. SkyUtils will "understand"
* how your command is meant to be registered and do it that way. You can
* of course check platform specific methods to know the details.
*
* @param commands The {@link GlobalCommand commands} to register.
*
* @since SkyUtils 1.0.0
*/
public abstract void registerCommands(GlobalCommand<P>... commands);

/**
* Unregisters a command by {@code name}.
*
* @param name the name of the command to unregister.
*
* @return {@code true} if the command exists, was registered and
* has been unregistered successfully, {@code false} otherwise.
*
* @since SkyUtils 1.0.0
*/
public abstract boolean unregisterCommand(@NotNull String name);

/*
- Reloadables
*/
Expand Down

0 comments on commit 48593a0

Please sign in to comment.