Allow non-PluginCommand's to be unloaded

This commit is contained in:
Frank van der Heijden 2021-03-25 16:29:38 +01:00
parent b51ff445dd
commit b83da96cac
No known key found for this signature in database
GPG key ID: 26DA56488D314D11
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);
}
getPlugin().getTaskManager().runTask(() -> BukkitPluginManager.unregisterCommands(getDisabledCommands()));
getPlugin().getTaskManager().runTask(() -> BukkitPluginManager.unregisterExactCommands(getDisabledCommands()));
}
private List<PluginCommand> getDisabledCommands() {
List<PluginCommand> commands = new ArrayList<>();
private List<Command> getDisabledCommands() {
List<Command> commands = new ArrayList<>();
for (String cmd : Config.getInstance().getConfig().getStringList("disabled-commands")) {
String[] split = cmd.split(":");
PluginCommand command;
Command command;
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]);
if (plugin == null || command == null) continue;
if (!plugin.getName().equalsIgnoreCase(command.getPlugin().getName())) continue;
if (plugin == null) {
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 {
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);
}

View file

@ -306,6 +306,16 @@ public class BukkitPluginManager extends AbstractPluginManager<Plugin> {
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.
* @param command The command string.