Merge pull request #13 from FrankHeijden/fix/command-unregistering

Allow non-PluginCommand's to be unloaded
This commit is contained in:
Frank van der Heijden 2021-03-25 16:53:22 +01:00 committed by GitHub
commit 5b79a121ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 8 deletions

View file

@ -117,23 +117,37 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
commandManager.registerCommand(commandPlugins, true); commandManager.registerCommand(commandPlugins, true);
} }
getPlugin().getTaskManager().runTask(() -> BukkitPluginManager.unregisterCommands(getDisabledCommands())); getPlugin().getTaskManager().runTask(() -> BukkitPluginManager.unregisterExactCommands(getDisabledCommands()));
} }
private List<PluginCommand> getDisabledCommands() { private List<Command> getDisabledCommands() {
List<PluginCommand> commands = new ArrayList<>(); List<Command> commands = new ArrayList<>();
for (String cmd : Config.getInstance().getConfig().getStringList("disabled-commands")) { for (String cmd : Config.getInstance().getConfig().getStringList("disabled-commands")) {
String[] split = cmd.split(":"); String[] split = cmd.split(":");
PluginCommand command; Command command;
if (split.length > 1) { if (split.length > 1) {
command = Bukkit.getPluginCommand(StringUtils.join(":", split, 1)); String commandString = StringUtils.join(":", split, 1);
PluginCommand pluginCommand = Bukkit.getPluginCommand(commandString);
Plugin plugin = getPlugin().getPluginManager().getPlugin(split[0]); Plugin plugin = getPlugin().getPluginManager().getPlugin(split[0]);
if (plugin == null || command == null) continue; if (plugin == null) {
if (!plugin.getName().equalsIgnoreCase(command.getPlugin().getName())) continue; getLogger().warning("Unknown plugin '" + split[0] + "' in disabled-commands!");
continue;
} else if (pluginCommand == null) {
getLogger().warning("Unknown command '" + commandString + "' in disabled-commands!");
continue;
} else if (!plugin.getName().equalsIgnoreCase(pluginCommand.getPlugin().getName())) {
// No output here, plugin didn't match!
continue;
}
command = pluginCommand;
} else { } else {
command = Bukkit.getPluginCommand(split[0]); command = BukkitPluginManager.getCommand(split[0]);
if (command == null) {
getLogger().warning("Unknown command '" + split[0] + "' in disabled-commands!");
continue;
}
} }
commands.add(command); commands.add(command);
} }

View file

@ -306,6 +306,16 @@ public class BukkitPluginManager extends AbstractPluginManager<Plugin> {
RCraftServer.syncCommands(); RCraftServer.syncCommands();
} }
/**
* Unregisters all specified commands exactly.
* @param commands The commands to unregister.
*/
public static void unregisterExactCommands(Collection<? extends Command> commands) {
Map<String, Command> knownCommands = getKnownCommands();
if (knownCommands == null) return;
knownCommands.values().removeAll(commands);
}
/** /**
* Retrieves a command from the command map. * Retrieves a command from the command map.
* @param command The command string. * @param command The command string.