Feature: unload bukkit commands per config.yml
This commit is contained in:
parent
b260d9c341
commit
4a374c5f43
7 changed files with 104 additions and 1 deletions
|
|
@ -4,6 +4,8 @@ import co.aikar.commands.BukkitCommandCompletionContext;
|
|||
import co.aikar.commands.CommandCompletions;
|
||||
import co.aikar.commands.PaperCommandManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.commands.CommandPlugins;
|
||||
|
|
@ -17,11 +19,14 @@ import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
|||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
import net.frankheijden.serverutils.common.utils.StringUtils;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.command.defaults.PluginsCommand;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
||||
|
|
@ -115,6 +120,28 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
|||
this.commandPlugins = new CommandPlugins();
|
||||
commandManager.registerCommand(commandPlugins, true);
|
||||
}
|
||||
|
||||
getPlugin().getTaskManager().runTask(() -> BukkitPluginManager.unregisterCommands(getDisabledCommands()));
|
||||
}
|
||||
|
||||
private List<PluginCommand> getDisabledCommands() {
|
||||
List<PluginCommand> commands = new ArrayList<>();
|
||||
for (String cmd : Config.getInstance().getConfig().getStringList("disabled-commands")) {
|
||||
String[] split = cmd.split(":");
|
||||
|
||||
PluginCommand command;
|
||||
if (split.length > 1) {
|
||||
command = Bukkit.getPluginCommand(StringUtils.join(":", split, 1));
|
||||
|
||||
Plugin plugin = getPlugin().getPluginManager().getPlugin(split[0]);
|
||||
if (plugin == null || command == null) continue;
|
||||
if (!plugin.getName().equalsIgnoreCase(command.getPlugin().getName())) continue;
|
||||
} else {
|
||||
command = Bukkit.getPluginCommand(split[0]);
|
||||
}
|
||||
commands.add(command);
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
|
||||
public PaperCommandManager getCommandManager() {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import java.io.InputStream;
|
|||
import java.io.InputStreamReader;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||
|
|
@ -39,6 +40,11 @@ public class BukkitYamlConfig implements YamlConfig {
|
|||
return obj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getStringList(String path) {
|
||||
return config.getStringList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getMap(String path) {
|
||||
Object obj = config.get(path);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,9 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
|
@ -26,6 +28,7 @@ import net.frankheijden.serverutils.common.entities.Result;
|
|||
import net.frankheijden.serverutils.common.managers.AbstractPluginManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.command.PluginIdentifiableCommand;
|
||||
import org.bukkit.plugin.InvalidDescriptionException;
|
||||
import org.bukkit.plugin.InvalidPluginException;
|
||||
|
|
@ -275,6 +278,29 @@ public class BukkitPluginManager extends AbstractPluginManager<Plugin> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters all the specified PluginCommand's.
|
||||
* @param pluginCommands The commands to unregister.
|
||||
*/
|
||||
public static void unregisterCommands(Collection<? extends PluginCommand> pluginCommands) {
|
||||
Map<String, Command> knownCommands = getKnownCommands();
|
||||
if (knownCommands == null) return;
|
||||
|
||||
Set<String> commands = new HashSet<>();
|
||||
for (PluginCommand pc : pluginCommands) {
|
||||
commands.add(pc.getName().toLowerCase());
|
||||
pc.setExecutor(null);
|
||||
pc.setTabCompleter(null);
|
||||
|
||||
for (String alias : pc.getAliases()) {
|
||||
if (!pc.equals(Bukkit.getPluginCommand(alias))) continue;
|
||||
commands.add(alias);
|
||||
}
|
||||
}
|
||||
|
||||
knownCommands.values().removeIf(c -> commands.contains(c.getName().toLowerCase()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a command from the command map.
|
||||
* @param command The command string.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue