Skip to content

Commit f5d1d4c

Browse files
committed
Added unregistering command methods (Resolves #4)
1 parent 64fe852 commit f5d1d4c

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

src/main/java/me/despical/commandframework/CommandFramework.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void setAnyMatch(@NotNull Consumer<CommandArguments> anyMatchConsumer) {
125125
* @param instance object class
126126
*/
127127
public void registerCommands(@NotNull Object instance) {
128-
for (Method method : instance.getClass().getMethods()) {
128+
for (final Method method : instance.getClass().getMethods()) {
129129
final Command command = method.getAnnotation(Command.class);
130130

131131
if (command != null) {
@@ -185,6 +185,66 @@ private void registerCommand(Command command, Method method, Object instance) {
185185
}
186186
}
187187

188+
/**
189+
* Unregisters command and tab completer if there is with the given name.
190+
*
191+
* @param commandName name of the command that's going to be removed
192+
*/
193+
public void unregisterCommand(@NotNull String commandName) {
194+
if (commandName.contains(".")) commandName = commandName.split("\\.")[0];
195+
196+
final Map.Entry<Command, Map.Entry<Method, Object>> entry = this.getAssociatedCommand(commandName, new String[0]);
197+
198+
if (entry == null) {
199+
plugin.getLogger().log(Level.WARNING, "Command removal is failed because there is no command named ''{0}''!", commandName);
200+
return;
201+
}
202+
203+
final Command command = entry.getKey();
204+
final String name = command.name();
205+
final PluginCommand pluginCommand = plugin.getServer().getPluginCommand(name);
206+
207+
Optional.ofNullable(pluginCommand).ifPresent(cmd -> {
208+
try {
209+
cmd.unregister(commandMap);
210+
211+
this.commandMap.getKnownCommands().remove(name);
212+
} catch (Exception exception) {
213+
plugin.getLogger().log(Level.WARNING, "Something went wrong while trying to unregister command(name: {0}) from server!", name);
214+
}
215+
216+
this.commands.remove(command);
217+
new HashMap<>(this.subCommands).keySet().stream().filter(subCmd -> subCmd.name().startsWith(name)).forEach(this.subCommands::remove); // Copy elements to new map to avoid modification exception
218+
});
219+
}
220+
221+
/**
222+
* Unregisters commands and tab completers within the given instance class.
223+
*
224+
* @param instance the object using to get target class to unregister commands.
225+
*/
226+
public void unregisterCommands(@NotNull Object instance) {
227+
for (final Method method : instance.getClass().getMethods()) {
228+
final Command command = method.getAnnotation(Command.class);
229+
230+
if (command != null) {
231+
if (method.getParameterTypes().length > 0 && method.getParameterTypes()[0] != CommandArguments.class) {
232+
continue;
233+
}
234+
235+
this.unregisterCommand(command.name());
236+
}
237+
}
238+
}
239+
240+
/**
241+
* Unregisters all of registered commands and tab completers created using that instance.
242+
*/
243+
public void unregisterCommands() {
244+
// Copy elements to new map to avoid modification exception
245+
new HashMap<>(commands).keySet().stream().map(Command::name).forEach(this::unregisterCommand);
246+
}
247+
188248
@Nullable
189249
private Map.Entry<Command, Map.Entry<Method, Object>> getAssociatedCommand(@NotNull String commandName, @NotNull String[] possibleArgs) {
190250
Command command = null;

0 commit comments

Comments
 (0)