140 lines
5 KiB
Java
140 lines
5 KiB
Java
package net.frankheijden.serverutils.bukkit;
|
|
|
|
import co.aikar.commands.BukkitCommandCompletionContext;
|
|
import co.aikar.commands.CommandCompletions;
|
|
import co.aikar.commands.PaperCommandManager;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.util.Collections;
|
|
import java.util.Map;
|
|
|
|
import net.frankheijden.serverutils.bukkit.commands.CommandPlugins;
|
|
import net.frankheijden.serverutils.bukkit.commands.CommandServerUtils;
|
|
import net.frankheijden.serverutils.bukkit.entities.BukkitPlugin;
|
|
import net.frankheijden.serverutils.bukkit.listeners.MainListener;
|
|
import net.frankheijden.serverutils.bukkit.managers.VersionManager;
|
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
|
import net.frankheijden.serverutils.bukkit.reflection.RCommandMap;
|
|
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
|
import net.frankheijden.serverutils.bukkit.tasks.UpdateCheckerTask;
|
|
import net.frankheijden.serverutils.bukkit.utils.BukkitUtils;
|
|
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.providers.PluginProvider;
|
|
import org.bstats.bukkit.Metrics;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandExecutor;
|
|
import org.bukkit.command.defaults.PluginsCommand;
|
|
import org.bukkit.plugin.Plugin;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
|
|
|
private static ServerUtils instance;
|
|
private static final String CONFIG_RESOURCE = "bukkit-config.yml";
|
|
private static final String MESSAGES_RESOURCE = "bukkit-messages.yml";
|
|
|
|
private BukkitPlugin plugin;
|
|
private PaperCommandManager commandManager;
|
|
private CommandPlugins commandPlugins;
|
|
|
|
@Override
|
|
public void onEnable() {
|
|
super.onEnable();
|
|
instance = this;
|
|
|
|
this.plugin = new BukkitPlugin(this);
|
|
ServerUtilsApp.init(plugin);
|
|
|
|
new Metrics(this, ServerUtilsApp.BSTATS_METRICS_ID);
|
|
new BukkitReflection();
|
|
|
|
this.commandManager = new PaperCommandManager(this);
|
|
commandManager.registerCommand(new CommandServerUtils());
|
|
this.commandPlugins = null;
|
|
|
|
PluginProvider<Plugin> provider = plugin.getPluginProvider();
|
|
CommandCompletions<BukkitCommandCompletionContext> completions = commandManager.getCommandCompletions();
|
|
completions.registerAsyncCompletion("plugins", context -> provider.getPluginNames());
|
|
completions.registerAsyncCompletion("pluginJars", context -> provider.getPluginFileNames());
|
|
completions.registerAsyncCompletion("supportedConfigs ", context -> CommandServerUtils.getSupportedConfigs());
|
|
completions.registerAsyncCompletion("commands", context -> {
|
|
try {
|
|
return RCommandMap.getKnownCommands(RCraftServer.getCommandMap()).keySet();
|
|
} catch (IllegalAccessException | InvocationTargetException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
return Collections.emptyList();
|
|
});
|
|
reload();
|
|
|
|
Bukkit.getPluginManager().registerEvents(new MainListener(), this);
|
|
|
|
new VersionManager();
|
|
checkForUpdates();
|
|
}
|
|
|
|
public static ServerUtils getInstance() {
|
|
return instance;
|
|
}
|
|
|
|
public BukkitPlugin getPlugin() {
|
|
return plugin;
|
|
}
|
|
|
|
@Override
|
|
public void onDisable() {
|
|
super.onDisable();
|
|
restoreBukkitPluginCommand();
|
|
}
|
|
|
|
private void removeCommands(String... commands) {
|
|
Map<String, Command> map;
|
|
try {
|
|
map = RCommandMap.getKnownCommands(RCraftServer.getCommandMap());
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
return;
|
|
}
|
|
|
|
for (String command : commands) {
|
|
map.remove(command);
|
|
}
|
|
}
|
|
|
|
public void restoreBukkitPluginCommand() {
|
|
RCraftServer.getCommandMap().register("bukkit", new PluginsCommand("plugins"));
|
|
}
|
|
|
|
/**
|
|
* Reloads the configurations of the plugin.
|
|
* Also makes sure the bukkit /pl command gets restored.
|
|
*/
|
|
public void reload() {
|
|
if (commandPlugins != null) {
|
|
commandManager.unregisterCommand(commandPlugins);
|
|
restoreBukkitPluginCommand();
|
|
}
|
|
|
|
new Config("config.yml", CONFIG_RESOURCE);
|
|
new Messenger("messages.yml", MESSAGES_RESOURCE);
|
|
|
|
if (!Config.getInstance().getConfig().getBoolean("settings.disable-plugins-command")) {
|
|
this.removeCommands("pl", "plugins");
|
|
this.commandPlugins = new CommandPlugins();
|
|
commandManager.registerCommand(commandPlugins);
|
|
}
|
|
}
|
|
|
|
public PaperCommandManager getCommandManager() {
|
|
return commandManager;
|
|
}
|
|
|
|
private void checkForUpdates() {
|
|
if (Config.getInstance().getConfig().getBoolean("settings.check-updates")) {
|
|
UpdateCheckerTask.start(BukkitUtils.wrap(Bukkit.getConsoleSender()), true);
|
|
}
|
|
}
|
|
}
|