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.CommandCompletions;
|
||||||
import co.aikar.commands.PaperCommandManager;
|
import co.aikar.commands.PaperCommandManager;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import net.frankheijden.serverutils.bukkit.commands.CommandPlugins;
|
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.ServerUtilsApp;
|
||||||
import net.frankheijden.serverutils.common.config.Config;
|
import net.frankheijden.serverutils.common.config.Config;
|
||||||
import net.frankheijden.serverutils.common.config.Messenger;
|
import net.frankheijden.serverutils.common.config.Messenger;
|
||||||
|
import net.frankheijden.serverutils.common.utils.StringUtils;
|
||||||
import org.bstats.bukkit.Metrics;
|
import org.bstats.bukkit.Metrics;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
|
import org.bukkit.command.PluginCommand;
|
||||||
import org.bukkit.command.defaults.PluginsCommand;
|
import org.bukkit.command.defaults.PluginsCommand;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
||||||
|
|
@ -115,6 +120,28 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
||||||
this.commandPlugins = new CommandPlugins();
|
this.commandPlugins = new CommandPlugins();
|
||||||
commandManager.registerCommand(commandPlugins, true);
|
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() {
|
public PaperCommandManager getCommandManager() {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||||
|
|
@ -39,6 +40,11 @@ public class BukkitYamlConfig implements YamlConfig {
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getStringList(String path) {
|
||||||
|
return config.getStringList(path);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> getMap(String path) {
|
public Map<String, Object> getMap(String path) {
|
||||||
Object obj = config.get(path);
|
Object obj = config.get(path);
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,9 @@ import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
@ -26,6 +28,7 @@ import net.frankheijden.serverutils.common.entities.Result;
|
||||||
import net.frankheijden.serverutils.common.managers.AbstractPluginManager;
|
import net.frankheijden.serverutils.common.managers.AbstractPluginManager;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.PluginCommand;
|
||||||
import org.bukkit.command.PluginIdentifiableCommand;
|
import org.bukkit.command.PluginIdentifiableCommand;
|
||||||
import org.bukkit.plugin.InvalidDescriptionException;
|
import org.bukkit.plugin.InvalidDescriptionException;
|
||||||
import org.bukkit.plugin.InvalidPluginException;
|
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.
|
* Retrieves a command from the command map.
|
||||||
* @param command The command string.
|
* @param command The command string.
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,7 @@ settings:
|
||||||
check-updates: true
|
check-updates: true
|
||||||
check-updates-login: false
|
check-updates-login: false
|
||||||
download-updates: false
|
download-updates: false
|
||||||
download-at-startup-and-update: false
|
download-at-startup-and-update: false
|
||||||
|
|
||||||
|
disabled-commands:
|
||||||
|
- "plugin:command"
|
||||||
|
|
@ -5,6 +5,7 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||||
|
|
@ -40,6 +41,11 @@ public class BungeeYamlConfig implements YamlConfig {
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getStringList(String path) {
|
||||||
|
return config.getStringList(path);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> getMap(String path) {
|
public Map<String, Object> getMap(String path) {
|
||||||
Object obj = config.get(path);
|
Object obj = config.get(path);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package net.frankheijden.serverutils.common.config;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -16,6 +17,13 @@ public interface YamlConfig {
|
||||||
*/
|
*/
|
||||||
Object get(String path);
|
Object get(String path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of strings at a given path.
|
||||||
|
* @param path The path.
|
||||||
|
* @return The string list.
|
||||||
|
*/
|
||||||
|
List<String> getStringList(String path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves a map with key/values for the path specified.
|
* Retrieves a map with key/values for the path specified.
|
||||||
* @param path The path.
|
* @param path The path.
|
||||||
|
|
|
||||||
|
|
@ -17,4 +17,31 @@ public class StringUtils {
|
||||||
}
|
}
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins strings from an array starting from begin index (including).
|
||||||
|
* @param delimiter The delimiter to join the strings on.
|
||||||
|
* @param strings The string array.
|
||||||
|
* @param begin Begin index (including)
|
||||||
|
* @return The joined string.
|
||||||
|
*/
|
||||||
|
public static String join(String delimiter, String[] strings, int begin) {
|
||||||
|
return join(delimiter, strings, begin, strings.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins strings from an array from begin index (including) until end index (excluding).
|
||||||
|
* @param delimiter The delimiter to join the strings on.
|
||||||
|
* @param strings The string array.
|
||||||
|
* @param begin Begin index (including)
|
||||||
|
* @param end End index (excluding)
|
||||||
|
* @return The joined string.
|
||||||
|
*/
|
||||||
|
public static String join(String delimiter, String[] strings, int begin, int end) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (int i = begin; i < end; i++) {
|
||||||
|
sb.append(delimiter).append(strings[i]);
|
||||||
|
}
|
||||||
|
return sb.substring(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue