Merge pull request #3 from FrankHeijden/feature/bungee
BungeeCord edition
This commit is contained in:
commit
5db7fd2ba3
84 changed files with 2499 additions and 510 deletions
|
|
@ -3,58 +3,58 @@ package net.frankheijden.serverutils.bukkit;
|
||||||
import co.aikar.commands.BukkitCommandCompletionContext;
|
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.io.File;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import net.frankheijden.serverutils.bukkit.commands.CommandPlugins;
|
import net.frankheijden.serverutils.bukkit.commands.CommandPlugins;
|
||||||
import net.frankheijden.serverutils.bukkit.commands.CommandServerUtils;
|
import net.frankheijden.serverutils.bukkit.commands.CommandServerUtils;
|
||||||
import net.frankheijden.serverutils.bukkit.config.Config;
|
import net.frankheijden.serverutils.bukkit.entities.BukkitPlugin;
|
||||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
import net.frankheijden.serverutils.bukkit.listeners.MainListener;
|
import net.frankheijden.serverutils.bukkit.listeners.BukkitListener;
|
||||||
import net.frankheijden.serverutils.bukkit.managers.VersionManager;
|
import net.frankheijden.serverutils.bukkit.managers.BukkitPluginManager;
|
||||||
import net.frankheijden.serverutils.bukkit.reflection.BukkitReflection;
|
|
||||||
import net.frankheijden.serverutils.bukkit.reflection.RCommandMap;
|
import net.frankheijden.serverutils.bukkit.reflection.RCommandMap;
|
||||||
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
||||||
import net.frankheijden.serverutils.bukkit.tasks.UpdateCheckerTask;
|
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||||
|
import net.frankheijden.serverutils.common.config.Config;
|
||||||
|
import net.frankheijden.serverutils.common.config.Messenger;
|
||||||
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.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 {
|
||||||
|
|
||||||
private static final int BSTATS_METRICS_ID = 7790;
|
|
||||||
|
|
||||||
private static ServerUtils instance;
|
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 PaperCommandManager commandManager;
|
||||||
private CommandPlugins commandPlugins;
|
private CommandPlugins commandPlugins;
|
||||||
|
|
||||||
public static ServerUtils getInstance() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
super.onEnable();
|
super.onEnable();
|
||||||
instance = this;
|
instance = this;
|
||||||
|
|
||||||
new Metrics(this, BSTATS_METRICS_ID);
|
this.plugin = new BukkitPlugin(this);
|
||||||
|
ServerUtilsApp.init(this, plugin);
|
||||||
|
|
||||||
|
new Metrics(this, ServerUtilsApp.BSTATS_METRICS_ID);
|
||||||
new BukkitReflection();
|
new BukkitReflection();
|
||||||
|
|
||||||
this.commandManager = new PaperCommandManager(this);
|
this.commandManager = new PaperCommandManager(this);
|
||||||
commandManager.registerCommand(new CommandServerUtils());
|
commandManager.registerCommand(new CommandServerUtils());
|
||||||
this.commandPlugins = null;
|
this.commandPlugins = null;
|
||||||
|
|
||||||
|
BukkitPluginManager manager = plugin.getPluginManager();
|
||||||
CommandCompletions<BukkitCommandCompletionContext> completions = commandManager.getCommandCompletions();
|
CommandCompletions<BukkitCommandCompletionContext> completions = commandManager.getCommandCompletions();
|
||||||
completions.registerAsyncCompletion("plugins", context -> getPluginNames());
|
completions.registerAsyncCompletion("plugins", context -> manager.getPluginNames());
|
||||||
completions.registerAsyncCompletion("pluginJars", context -> getPluginFileNames());
|
completions.registerAsyncCompletion("pluginJars", context -> manager.getPluginFileNames());
|
||||||
completions.registerAsyncCompletion("supportedConfigs ", context -> CommandServerUtils.getSupportedConfigs());
|
completions.registerAsyncCompletion("supportedConfigs ", context -> CommandServerUtils.getSupportedConfigs());
|
||||||
completions.registerAsyncCompletion("commands", context -> {
|
completions.registerAsyncCompletion("commands", context -> {
|
||||||
try {
|
try {
|
||||||
|
|
@ -66,10 +66,17 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
||||||
});
|
});
|
||||||
reload();
|
reload();
|
||||||
|
|
||||||
Bukkit.getPluginManager().registerEvents(new MainListener(), this);
|
Bukkit.getPluginManager().registerEvents(new BukkitListener(), this);
|
||||||
|
|
||||||
new VersionManager();
|
ServerUtilsApp.tryCheckForUpdates();
|
||||||
checkForUpdates();
|
}
|
||||||
|
|
||||||
|
public static ServerUtils getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BukkitPlugin getPlugin() {
|
||||||
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -78,18 +85,6 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
||||||
restoreBukkitPluginCommand();
|
restoreBukkitPluginCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getPluginNames() {
|
|
||||||
return Arrays.stream(Bukkit.getPluginManager().getPlugins())
|
|
||||||
.map(Plugin::getName)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<String> getPluginFileNames() {
|
|
||||||
return Arrays.stream(getJars())
|
|
||||||
.map(File::getName)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeCommands(String... commands) {
|
private void removeCommands(String... commands) {
|
||||||
Map<String, Command> map;
|
Map<String, Command> map;
|
||||||
try {
|
try {
|
||||||
|
|
@ -118,10 +113,10 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
||||||
restoreBukkitPluginCommand();
|
restoreBukkitPluginCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
new Config(copyResourceIfNotExists("config.yml", "bukkit-config.yml"));
|
new Config("config.yml", CONFIG_RESOURCE);
|
||||||
new Messenger(copyResourceIfNotExists("messages.yml", "bukkit-messages.yml"));
|
new Messenger("messages.yml", MESSAGES_RESOURCE);
|
||||||
|
|
||||||
if (!Config.getInstance().getBoolean("settings.disable-plugins-command")) {
|
if (!Config.getInstance().getConfig().getBoolean("settings.disable-plugins-command")) {
|
||||||
this.removeCommands("pl", "plugins");
|
this.removeCommands("pl", "plugins");
|
||||||
this.commandPlugins = new CommandPlugins();
|
this.commandPlugins = new CommandPlugins();
|
||||||
commandManager.registerCommand(commandPlugins);
|
commandManager.registerCommand(commandPlugins);
|
||||||
|
|
@ -131,39 +126,4 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
||||||
public PaperCommandManager getCommandManager() {
|
public PaperCommandManager getCommandManager() {
|
||||||
return commandManager;
|
return commandManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves all files with a jar extension in the plugins/ folder.
|
|
||||||
* @return An array of jar files.
|
|
||||||
*/
|
|
||||||
public File[] getJars() {
|
|
||||||
File parent = getDataFolder().getParentFile();
|
|
||||||
if (parent == null) return new File[0];
|
|
||||||
return parent.listFiles(f -> f.getName().endsWith(".jar"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createDataFolderIfNotExists() {
|
|
||||||
if (!getDataFolder().exists()) {
|
|
||||||
getDataFolder().mkdirs();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private File copyResourceIfNotExists(String targetName, String resource) {
|
|
||||||
createDataFolderIfNotExists();
|
|
||||||
|
|
||||||
File file = new File(getDataFolder(), targetName);
|
|
||||||
if (!file.exists()) {
|
|
||||||
getLogger().info(String.format("'%s' not found, creating!", targetName));
|
|
||||||
saveResource(resource, false);
|
|
||||||
File copiedFile = new File(getDataFolder(), resource);
|
|
||||||
copiedFile.renameTo(file);
|
|
||||||
}
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkForUpdates() {
|
|
||||||
if (Config.getInstance().getBoolean("settings.check-updates")) {
|
|
||||||
UpdateCheckerTask.start(Bukkit.getConsoleSender(), true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,19 +6,18 @@ import co.aikar.commands.annotation.CommandPermission;
|
||||||
import co.aikar.commands.annotation.Default;
|
import co.aikar.commands.annotation.Default;
|
||||||
import co.aikar.commands.annotation.Description;
|
import co.aikar.commands.annotation.Description;
|
||||||
import co.aikar.commands.annotation.Subcommand;
|
import co.aikar.commands.annotation.Subcommand;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Comparator;
|
import net.frankheijden.serverutils.bukkit.managers.BukkitPluginManager;
|
||||||
import java.util.List;
|
import net.frankheijden.serverutils.bukkit.utils.BukkitUtils;
|
||||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
import net.frankheijden.serverutils.common.commands.Plugins;
|
||||||
import net.frankheijden.serverutils.common.utils.ListBuilder;
|
import net.frankheijden.serverutils.common.config.Messenger;
|
||||||
import net.frankheijden.serverutils.common.utils.ListFormat;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.plugin.Plugin;
|
|
||||||
|
|
||||||
@CommandAlias("plugins|pl")
|
@CommandAlias("plugins|pl")
|
||||||
public class CommandPlugins extends BaseCommand {
|
public class CommandPlugins extends BaseCommand {
|
||||||
|
|
||||||
|
private static final BukkitPluginManager manager = BukkitPluginManager.get();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends the plugin list to the sender, without plugin version.
|
* Sends the plugin list to the sender, without plugin version.
|
||||||
* @param sender The sender of the command.
|
* @param sender The sender of the command.
|
||||||
|
|
@ -27,7 +26,7 @@ public class CommandPlugins extends BaseCommand {
|
||||||
@CommandPermission("serverutils.plugins")
|
@CommandPermission("serverutils.plugins")
|
||||||
@Description("Shows the plugins of this server.")
|
@Description("Shows the plugins of this server.")
|
||||||
public void onPlugins(CommandSender sender) {
|
public void onPlugins(CommandSender sender) {
|
||||||
sendPlugins(sender, pl -> {
|
Plugins.sendPlugins(BukkitUtils.wrap(sender), manager.getPluginsSorted(), pl -> {
|
||||||
String format = "serverutils.plugins.format" + (pl.isEnabled() ? "" : "_disabled");
|
String format = "serverutils.plugins.format" + (pl.isEnabled() ? "" : "_disabled");
|
||||||
return Messenger.getMessage(format, "%plugin%", pl.getName());
|
return Messenger.getMessage(format, "%plugin%", pl.getName());
|
||||||
});
|
});
|
||||||
|
|
@ -41,30 +40,11 @@ public class CommandPlugins extends BaseCommand {
|
||||||
@CommandPermission("serverutils.plugins.version")
|
@CommandPermission("serverutils.plugins.version")
|
||||||
@Description("Shows the plugins of this server with version.")
|
@Description("Shows the plugins of this server with version.")
|
||||||
public void onPluginsWithVersion(CommandSender sender) {
|
public void onPluginsWithVersion(CommandSender sender) {
|
||||||
sendPlugins(sender, pl -> {
|
Plugins.sendPlugins(BukkitUtils.wrap(sender), manager.getPluginsSorted(), pl -> {
|
||||||
String format = "serverutils.plugins.format" + (pl.isEnabled() ? "" : "_disabled");
|
String format = "serverutils.plugins.format" + (pl.isEnabled() ? "" : "_disabled");
|
||||||
String version = Messenger.getMessage("serverutils.plugins.version",
|
String version = Messenger.getMessage("serverutils.plugins.version",
|
||||||
"%version%", pl.getDescription().getVersion());
|
"%version%", pl.getDescription().getVersion());
|
||||||
return Messenger.getMessage(format, "%plugin%", pl.getName()) + version;
|
return Messenger.getMessage(format, "%plugin%", pl.getName()) + version;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void sendPlugins(CommandSender sender, ListFormat<Plugin> pluginFormat) {
|
|
||||||
Messenger.sendMessage(sender, "serverutils.plugins.header");
|
|
||||||
List<Plugin> plugins = getPluginsSorted();
|
|
||||||
String prefix = Messenger.getMessage("serverutils.plugins.prefix",
|
|
||||||
"%count%", String.valueOf(plugins.size()));
|
|
||||||
sender.sendMessage(Messenger.color(prefix + ListBuilder.create(plugins)
|
|
||||||
.seperator(Messenger.getMessage("serverutils.plugins.seperator"))
|
|
||||||
.lastSeperator(Messenger.getMessage("serverutils.plugins.last_seperator"))
|
|
||||||
.format(pluginFormat)
|
|
||||||
.toString()));
|
|
||||||
Messenger.sendMessage(sender, "serverutils.plugins.footer");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<Plugin> getPluginsSorted() {
|
|
||||||
List<Plugin> plugins = Arrays.asList(Bukkit.getPluginManager().getPlugins());
|
|
||||||
plugins.sort(Comparator.comparing(Plugin::getName));
|
|
||||||
return plugins;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package net.frankheijden.serverutils.bukkit.commands;
|
package net.frankheijden.serverutils.bukkit.commands;
|
||||||
|
|
||||||
import static net.frankheijden.serverutils.bukkit.config.Messenger.sendMessage;
|
import static net.frankheijden.serverutils.bukkit.entities.BukkitReflection.MINOR;
|
||||||
import static net.frankheijden.serverutils.bukkit.reflection.BukkitReflection.MINOR;
|
import static net.frankheijden.serverutils.common.config.Messenger.sendMessage;
|
||||||
|
|
||||||
import co.aikar.commands.BaseCommand;
|
import co.aikar.commands.BaseCommand;
|
||||||
import co.aikar.commands.annotation.CommandAlias;
|
import co.aikar.commands.annotation.CommandAlias;
|
||||||
|
|
@ -18,15 +18,17 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
import net.frankheijden.serverutils.bukkit.entities.BukkitLoadResult;
|
||||||
import net.frankheijden.serverutils.bukkit.managers.CloseableResult;
|
import net.frankheijden.serverutils.bukkit.managers.BukkitPluginManager;
|
||||||
import net.frankheijden.serverutils.bukkit.managers.LoadResult;
|
|
||||||
import net.frankheijden.serverutils.bukkit.managers.PluginManager;
|
|
||||||
import net.frankheijden.serverutils.bukkit.managers.Result;
|
|
||||||
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
||||||
import net.frankheijden.serverutils.bukkit.utils.FormatBuilder;
|
import net.frankheijden.serverutils.bukkit.utils.BukkitUtils;
|
||||||
import net.frankheijden.serverutils.bukkit.utils.ForwardFilter;
|
|
||||||
import net.frankheijden.serverutils.bukkit.utils.ReloadHandler;
|
import net.frankheijden.serverutils.bukkit.utils.ReloadHandler;
|
||||||
|
import net.frankheijden.serverutils.common.config.Messenger;
|
||||||
|
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||||
|
import net.frankheijden.serverutils.common.entities.Result;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import net.frankheijden.serverutils.common.utils.FormatBuilder;
|
||||||
|
import net.frankheijden.serverutils.common.utils.ForwardFilter;
|
||||||
import net.frankheijden.serverutils.common.utils.ListBuilder;
|
import net.frankheijden.serverutils.common.utils.ListBuilder;
|
||||||
import net.frankheijden.serverutils.common.utils.ListFormat;
|
import net.frankheijden.serverutils.common.utils.ListFormat;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
|
@ -64,13 +66,14 @@ public class CommandServerUtils extends BaseCommand {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows the help page to the sender.
|
* Shows the help page to the sender.
|
||||||
* @param sender The sender of the command.
|
* @param commandSender The sender of the command.
|
||||||
*/
|
*/
|
||||||
@Default
|
@Default
|
||||||
@Subcommand("help")
|
@Subcommand("help")
|
||||||
@CommandPermission("serverutils.help")
|
@CommandPermission("serverutils.help")
|
||||||
@Description("Shows a help page with a few commands.")
|
@Description("Shows a help page with a few commands.")
|
||||||
public void onHelp(CommandSender sender) {
|
public void onHelp(CommandSender commandSender) {
|
||||||
|
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||||
Messenger.sendMessage(sender, "serverutils.help.header");
|
Messenger.sendMessage(sender, "serverutils.help.header");
|
||||||
|
|
||||||
FormatBuilder builder = FormatBuilder.create(Messenger.getMessage("serverutils.help.format"))
|
FormatBuilder builder = FormatBuilder.create(Messenger.getMessage("serverutils.help.format"))
|
||||||
|
|
@ -97,26 +100,27 @@ public class CommandServerUtils extends BaseCommand {
|
||||||
@Description("Reloads the ServerUtils plugin.")
|
@Description("Reloads the ServerUtils plugin.")
|
||||||
public void onReload(CommandSender sender) {
|
public void onReload(CommandSender sender) {
|
||||||
plugin.reload();
|
plugin.reload();
|
||||||
sendMessage(sender, "serverutils.success",
|
sendMessage(BukkitUtils.wrap(sender), "serverutils.success",
|
||||||
"%action%", "reload",
|
"%action%", "reload",
|
||||||
"%what%", "ServerUtils configurations");
|
"%what%", "ServerUtils configurations");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reloads a config from a set of configurations of the server.
|
* Reloads a config from a set of configurations of the server.
|
||||||
* @param sender The sender of the command.
|
* @param commandSender The sender of the command.
|
||||||
* @param config The configuration to reload.
|
* @param config The configuration to reload.
|
||||||
*/
|
*/
|
||||||
@Subcommand("reloadconfig")
|
@Subcommand("reloadconfig")
|
||||||
@CommandCompletion("@supportedConfigs")
|
@CommandCompletion("@supportedConfigs")
|
||||||
@CommandPermission("serverutils.reloadconfig")
|
@CommandPermission("serverutils.reloadconfig")
|
||||||
@Description("Reloads individual Server configurations.")
|
@Description("Reloads individual Server configurations.")
|
||||||
public void onReloadCommands(CommandSender sender, String config) {
|
public void onReloadCommands(CommandSender commandSender, String config) {
|
||||||
ReloadHandler handler = supportedConfigs.get(config);
|
ReloadHandler handler = supportedConfigs.get(config);
|
||||||
if (handler == null) {
|
if (handler == null) {
|
||||||
this.doHelp(sender);
|
this.doHelp(commandSender);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||||
|
|
||||||
String[] replacements = new String[]{ "%action%", "reload", "%what%", config };
|
String[] replacements = new String[]{ "%action%", "reload", "%what%", config };
|
||||||
|
|
||||||
|
|
@ -138,41 +142,45 @@ public class CommandServerUtils extends BaseCommand {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the specified plugin on the server.
|
* Loads the specified plugin on the server.
|
||||||
* @param sender The sender of the command.
|
* @param commandSender The sender of the command.
|
||||||
* @param jarFile The filename of the plugin in the plugins/ directory.
|
* @param jarFile The filename of the plugin in the plugins/ directory.
|
||||||
*/
|
*/
|
||||||
@Subcommand("loadplugin")
|
@Subcommand("loadplugin")
|
||||||
@CommandCompletion("@pluginJars")
|
@CommandCompletion("@pluginJars")
|
||||||
@CommandPermission("serverutils.loadplugin")
|
@CommandPermission("serverutils.loadplugin")
|
||||||
@Description("Loads the specified jar file as a plugin.")
|
@Description("Loads the specified jar file as a plugin.")
|
||||||
public void onLoadPlugin(CommandSender sender, String jarFile) {
|
public void onLoadPlugin(CommandSender commandSender, String jarFile) {
|
||||||
LoadResult loadResult = PluginManager.loadPlugin(jarFile);
|
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||||
|
|
||||||
|
BukkitLoadResult loadResult = BukkitPluginManager.get().loadPlugin(jarFile);
|
||||||
if (!loadResult.isSuccess()) {
|
if (!loadResult.isSuccess()) {
|
||||||
loadResult.getResult().sendTo(sender, "load", jarFile);
|
loadResult.getResult().sendTo(sender, "load", jarFile);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result result = PluginManager.enablePlugin(loadResult.getPlugin());
|
Result result = BukkitPluginManager.get().enablePlugin(loadResult.get());
|
||||||
result.sendTo(sender, "load", jarFile);
|
result.sendTo(sender, "load", jarFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unloads the specified plugin from the server.
|
* Unloads the specified plugin from the server.
|
||||||
* @param sender The sender of the command.
|
* @param commandSender The sender of the command.
|
||||||
* @param pluginName The plugin name.
|
* @param pluginName The plugin name.
|
||||||
*/
|
*/
|
||||||
@Subcommand("unloadplugin")
|
@Subcommand("unloadplugin")
|
||||||
@CommandCompletion("@plugins")
|
@CommandCompletion("@plugins")
|
||||||
@CommandPermission("serverutils.unloadplugin")
|
@CommandPermission("serverutils.unloadplugin")
|
||||||
@Description("Disables and unloads the specified plugin.")
|
@Description("Disables and unloads the specified plugin.")
|
||||||
public void onUnloadPlugin(CommandSender sender, String pluginName) {
|
public void onUnloadPlugin(CommandSender commandSender, String pluginName) {
|
||||||
Result disableResult = PluginManager.disablePlugin(pluginName);
|
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||||
|
|
||||||
|
Result disableResult = BukkitPluginManager.disablePlugin(pluginName);
|
||||||
if (disableResult != Result.SUCCESS && disableResult != Result.ALREADY_DISABLED) {
|
if (disableResult != Result.SUCCESS && disableResult != Result.ALREADY_DISABLED) {
|
||||||
disableResult.sendTo(sender, "disabl", pluginName);
|
disableResult.sendTo(sender, "disabl", pluginName);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CloseableResult result = PluginManager.unloadPlugin(pluginName);
|
CloseableResult result = BukkitPluginManager.get().unloadPlugin(pluginName);
|
||||||
result.getResult().sendTo(sender, "unload", pluginName);
|
result.getResult().sendTo(sender, "unload", pluginName);
|
||||||
result.tryClose();
|
result.tryClose();
|
||||||
}
|
}
|
||||||
|
|
@ -187,8 +195,8 @@ public class CommandServerUtils extends BaseCommand {
|
||||||
@CommandPermission("serverutils.reloadplugin")
|
@CommandPermission("serverutils.reloadplugin")
|
||||||
@Description("Reloads a specified plugin.")
|
@Description("Reloads a specified plugin.")
|
||||||
public void onReloadPlugin(CommandSender sender, String pluginName) {
|
public void onReloadPlugin(CommandSender sender, String pluginName) {
|
||||||
CloseableResult result = PluginManager.reloadPlugin(pluginName);
|
CloseableResult result = BukkitPluginManager.get().reloadPlugin(pluginName);
|
||||||
result.getResult().sendTo(sender, "reload", pluginName);
|
result.getResult().sendTo(BukkitUtils.wrap(sender), "reload", pluginName);
|
||||||
result.tryClose();
|
result.tryClose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -202,8 +210,8 @@ public class CommandServerUtils extends BaseCommand {
|
||||||
@CommandPermission("serverutils.enableplugin")
|
@CommandPermission("serverutils.enableplugin")
|
||||||
@Description("Enables the loaded plugin.")
|
@Description("Enables the loaded plugin.")
|
||||||
public void onEnablePlugin(CommandSender sender, String pluginName) {
|
public void onEnablePlugin(CommandSender sender, String pluginName) {
|
||||||
Result result = PluginManager.enablePlugin(pluginName);
|
Result result = BukkitPluginManager.get().enablePlugin(pluginName);
|
||||||
result.sendTo(sender, "enabl", pluginName);
|
result.sendTo(BukkitUtils.wrap(sender), "enabl", pluginName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -216,20 +224,22 @@ public class CommandServerUtils extends BaseCommand {
|
||||||
@CommandPermission("serverutils.disableplugin")
|
@CommandPermission("serverutils.disableplugin")
|
||||||
@Description("Disables the specified plugin.")
|
@Description("Disables the specified plugin.")
|
||||||
public void onDisablePlugin(CommandSender sender, String pluginName) {
|
public void onDisablePlugin(CommandSender sender, String pluginName) {
|
||||||
Result result = PluginManager.disablePlugin(pluginName);
|
Result result = BukkitPluginManager.disablePlugin(pluginName);
|
||||||
result.sendTo(sender, "disabl", pluginName);
|
result.sendTo(BukkitUtils.wrap(sender), "disabl", pluginName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows information about the specified plugin.
|
* Shows information about the specified plugin.
|
||||||
* @param sender The sender of the command.
|
* @param commandSender The sender of the command.
|
||||||
* @param pluginName The plugin name.
|
* @param pluginName The plugin name.
|
||||||
*/
|
*/
|
||||||
@Subcommand("plugininfo")
|
@Subcommand("plugininfo")
|
||||||
@CommandCompletion("@plugins")
|
@CommandCompletion("@plugins")
|
||||||
@CommandPermission("serverutils.plugininfo")
|
@CommandPermission("serverutils.plugininfo")
|
||||||
@Description("Shows information about the specified plugin.")
|
@Description("Shows information about the specified plugin.")
|
||||||
public void onPluginInfo(CommandSender sender, String pluginName) {
|
public void onPluginInfo(CommandSender commandSender, String pluginName) {
|
||||||
|
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||||
|
|
||||||
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
|
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
|
||||||
if (plugin == null) {
|
if (plugin == null) {
|
||||||
Result.NOT_EXISTS.sendTo(sender, "fetch", pluginName);
|
Result.NOT_EXISTS.sendTo(sender, "fetch", pluginName);
|
||||||
|
|
@ -289,15 +299,17 @@ public class CommandServerUtils extends BaseCommand {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows information about a provided command.
|
* Shows information about a provided command.
|
||||||
* @param sender The sender of the command.
|
* @param commandSender The sender of the command.
|
||||||
* @param command The command to lookup.
|
* @param command The command to lookup.
|
||||||
*/
|
*/
|
||||||
@Subcommand("commandinfo")
|
@Subcommand("commandinfo")
|
||||||
@CommandCompletion("@commands")
|
@CommandCompletion("@commands")
|
||||||
@CommandPermission("serverutils.commandinfo")
|
@CommandPermission("serverutils.commandinfo")
|
||||||
@Description("Shows information about the specified command.")
|
@Description("Shows information about the specified command.")
|
||||||
public void onCommandInfo(CommandSender sender, String command) {
|
public void onCommandInfo(CommandSender commandSender, String command) {
|
||||||
Command cmd = PluginManager.getCommand(command);
|
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||||
|
|
||||||
|
Command cmd = BukkitPluginManager.getCommand(command);
|
||||||
if (cmd == null) {
|
if (cmd == null) {
|
||||||
Messenger.sendMessage(sender, "serverutils.commandinfo.not_exists");
|
Messenger.sendMessage(sender, "serverutils.commandinfo.not_exists");
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
package net.frankheijden.serverutils.bukkit.config;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
public class Config extends YamlResource {
|
|
||||||
|
|
||||||
private static Config instance;
|
|
||||||
|
|
||||||
public Config(File file) {
|
|
||||||
super(file, "bukkit-config.yml");
|
|
||||||
instance = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Config getInstance() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean getBoolean(String path) {
|
|
||||||
return getConfiguration().getBoolean(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,80 +0,0 @@
|
||||||
package net.frankheijden.serverutils.bukkit.config;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
public class Messenger extends YamlResource {
|
|
||||||
|
|
||||||
private static final ServerUtils plugin = ServerUtils.getInstance();
|
|
||||||
private static Messenger instance;
|
|
||||||
|
|
||||||
public Messenger(File file) {
|
|
||||||
super(file, "bukkit-messages.yml");
|
|
||||||
instance = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves a message from the config.
|
|
||||||
* @param path The yml path to the message.
|
|
||||||
* @param replacements The replacements to be taken into account.
|
|
||||||
* @return The config message with translated placeholders.
|
|
||||||
*/
|
|
||||||
public static String getMessage(String path, String... replacements) {
|
|
||||||
String message = instance.getConfiguration().getString(path);
|
|
||||||
if (message != null) {
|
|
||||||
return apply(message, replacements);
|
|
||||||
} else {
|
|
||||||
plugin.getLogger().severe("Missing locale in bukkit-messages.yml at path '" + path + "'!");
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Applies placeholders to a message.
|
|
||||||
* @param message The message.
|
|
||||||
* @param replacements The replacements of the message. Expects input to be even and in a key-value like format.
|
|
||||||
* Example: ["%player%", "Player"]
|
|
||||||
* @return The message with translated placeholders.
|
|
||||||
*/
|
|
||||||
public static String apply(String message, String... replacements) {
|
|
||||||
if (message == null || message.isEmpty()) return null;
|
|
||||||
message = message.replace("\\n", "\n");
|
|
||||||
for (int i = 0; i < replacements.length; i++, i++) {
|
|
||||||
message = message.replace(replacements[i], replacements[i + 1]);
|
|
||||||
}
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a message to a player with translated placeholders.
|
|
||||||
* @param sender The receiver.
|
|
||||||
* @param msg The message to be sent.
|
|
||||||
* @param replacements The replacements to be taken into account.
|
|
||||||
*/
|
|
||||||
public static void sendRawMessage(CommandSender sender, String msg, String... replacements) {
|
|
||||||
String message = apply(msg, replacements);
|
|
||||||
if (message != null) {
|
|
||||||
sender.sendMessage(color(message));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a message from the specified config path to a player with translated placeholders.
|
|
||||||
* @param sender The receiver.
|
|
||||||
* @param path The yml path to the message.
|
|
||||||
* @param replacements The replacements to be taken into account.
|
|
||||||
*/
|
|
||||||
public static void sendMessage(CommandSender sender, String path, String... replacements) {
|
|
||||||
String message = getMessage(path, replacements);
|
|
||||||
if (message != null) {
|
|
||||||
sender.sendMessage(color(message));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String color(String str) {
|
|
||||||
return ChatColor.translateAlternateColorCodes('&', str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
package net.frankheijden.serverutils.bukkit.config;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
|
|
||||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
|
||||||
import net.frankheijden.serverutils.bukkit.utils.YamlUtils;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
|
|
||||||
public abstract class YamlResource {
|
|
||||||
|
|
||||||
private static final ServerUtils plugin = ServerUtils.getInstance();
|
|
||||||
|
|
||||||
private final YamlConfiguration configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new YamlResource instance.
|
|
||||||
* Loads the resource from the jar file.
|
|
||||||
* @param file The destination file.
|
|
||||||
* @param resource The resource from the jar file.
|
|
||||||
*/
|
|
||||||
public YamlResource(File file, String resource) {
|
|
||||||
InputStream is = plugin.getResource(resource);
|
|
||||||
YamlConfiguration def = YamlConfiguration.loadConfiguration(new InputStreamReader(is));
|
|
||||||
configuration = YamlUtils.init(file, def);
|
|
||||||
}
|
|
||||||
|
|
||||||
public YamlConfiguration getConfiguration() {
|
|
||||||
return configuration;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
package net.frankheijden.serverutils.bukkit.entities;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.utils.BukkitUtils;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import net.frankheijden.serverutils.common.providers.ChatProvider;
|
||||||
|
import net.md_5.bungee.api.ChatColor;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides basic chat functionality for Bukkit servers.
|
||||||
|
*/
|
||||||
|
public class BukkitChatProvider extends ChatProvider {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the console sender of a Bukkit instance.
|
||||||
|
* @return The console sender.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ServerCommandSender getConsoleSender() {
|
||||||
|
return BukkitUtils.wrap(Bukkit.getConsoleSender());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Colorizes the given string.
|
||||||
|
* @param str The string to color.
|
||||||
|
* @return The colored string.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String color(String str) {
|
||||||
|
return ChatColor.translateAlternateColorCodes('&', str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Broadcasts a message over a Bukkit instance.
|
||||||
|
* @param permission The permission the receivers need to have.
|
||||||
|
* @param message The message to broadcast.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void broadcast(String permission, String message) {
|
||||||
|
Bukkit.broadcast(message, permission);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
package net.frankheijden.serverutils.bukkit.entities;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A wrap for a Bukkit CommandSender.
|
||||||
|
*/
|
||||||
|
public class BukkitCommandSender implements ServerCommandSender {
|
||||||
|
|
||||||
|
private final CommandSender sender;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new CommandSender instance.
|
||||||
|
* @param sender The sender to wrap.
|
||||||
|
*/
|
||||||
|
public BukkitCommandSender(CommandSender sender) {
|
||||||
|
this.sender = sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a message to a CommandSender.
|
||||||
|
* @param message The message to send.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void sendMessage(String message) {
|
||||||
|
sender.sendMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the CommandSender has a permission.
|
||||||
|
* @param permission The permission to check.
|
||||||
|
* @return Whether or not they have the permission.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean hasPermission(String permission) {
|
||||||
|
return sender.hasPermission(permission);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
package net.frankheijden.serverutils.bukkit.entities;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.entities.LoadResult;
|
||||||
|
import net.frankheijden.serverutils.common.entities.Result;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
public class BukkitLoadResult extends LoadResult<Plugin> {
|
||||||
|
|
||||||
|
private BukkitLoadResult(Plugin obj, Result result) {
|
||||||
|
super(obj, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BukkitLoadResult(Plugin obj) {
|
||||||
|
this(obj, Result.SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BukkitLoadResult(Result result) {
|
||||||
|
this(null, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
package net.frankheijden.serverutils.bukkit.entities;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||||
|
import net.frankheijden.serverutils.bukkit.managers.BukkitPluginManager;
|
||||||
|
import net.frankheijden.serverutils.bukkit.managers.BukkitTaskManager;
|
||||||
|
import net.frankheijden.serverutils.bukkit.managers.BukkitVersionManager;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||||
|
|
||||||
|
public class BukkitPlugin extends ServerUtilsPlugin {
|
||||||
|
|
||||||
|
private final ServerUtils plugin;
|
||||||
|
private final BukkitPluginManager pluginManager;
|
||||||
|
private final BukkitTaskManager taskManager;
|
||||||
|
private final BukkitResourceProvider resourceProvider;
|
||||||
|
private final BukkitChatProvider chatProvider;
|
||||||
|
private final BukkitVersionManager versionManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new BukkitPlugin instance of ServerUtils.
|
||||||
|
* @param plugin The ServerUtils plugin.
|
||||||
|
*/
|
||||||
|
public BukkitPlugin(ServerUtils plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.pluginManager = new BukkitPluginManager(plugin);
|
||||||
|
this.taskManager = new BukkitTaskManager();
|
||||||
|
this.resourceProvider = new BukkitResourceProvider(plugin);
|
||||||
|
this.chatProvider = new BukkitChatProvider();
|
||||||
|
this.versionManager = new BukkitVersionManager(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public BukkitPluginManager getPluginManager() {
|
||||||
|
return pluginManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BukkitTaskManager getTaskManager() {
|
||||||
|
return taskManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BukkitResourceProvider getResourceProvider() {
|
||||||
|
return resourceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BukkitChatProvider getChatProvider() {
|
||||||
|
return chatProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BukkitVersionManager getVersionManager() {
|
||||||
|
return versionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Logger getLogger() {
|
||||||
|
return plugin.getLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getDataFolder() {
|
||||||
|
return plugin.getDataFolder();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package net.frankheijden.serverutils.bukkit.reflection;
|
package net.frankheijden.serverutils.bukkit.entities;
|
||||||
|
|
||||||
import net.frankheijden.serverutils.common.reflection.ReflectionUtils;
|
import net.frankheijden.serverutils.common.reflection.ReflectionUtils;
|
||||||
import net.frankheijden.serverutils.common.reflection.VersionParam;
|
import net.frankheijden.serverutils.common.reflection.VersionParam;
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
package net.frankheijden.serverutils.bukkit.entities;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||||
|
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||||
|
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||||
|
|
||||||
|
public class BukkitResourceProvider implements ResourceProvider {
|
||||||
|
|
||||||
|
private final ServerUtils plugin;
|
||||||
|
|
||||||
|
public BukkitResourceProvider(ServerUtils plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getResource(String resource) {
|
||||||
|
return plugin.getResource(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public YamlConfig load(InputStream is) {
|
||||||
|
return new BukkitYamlConfig(is);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public YamlConfig load(File file) {
|
||||||
|
return new BukkitYamlConfig(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
package net.frankheijden.serverutils.bukkit.entities;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||||
|
import org.bukkit.configuration.MemorySection;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
|
public class BukkitYamlConfig implements YamlConfig {
|
||||||
|
|
||||||
|
private final MemorySection config;
|
||||||
|
private File file = null;
|
||||||
|
|
||||||
|
public BukkitYamlConfig(File file) {
|
||||||
|
this.config = YamlConfiguration.loadConfiguration(file);
|
||||||
|
this.file = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BukkitYamlConfig(InputStream in) {
|
||||||
|
this.config = YamlConfiguration.loadConfiguration(new InputStreamReader(in));
|
||||||
|
}
|
||||||
|
|
||||||
|
public BukkitYamlConfig(MemorySection section) {
|
||||||
|
this.config = section;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object get(String path) {
|
||||||
|
Object obj = config.get(path);
|
||||||
|
if (obj instanceof MemorySection) {
|
||||||
|
return new BukkitYamlConfig((MemorySection) obj);
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(String path, Object value) {
|
||||||
|
config.set(path, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getString(String path) {
|
||||||
|
return config.getString(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(String path) {
|
||||||
|
return config.getBoolean(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<? extends String> getKeys() {
|
||||||
|
return config.getKeys(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save() throws IOException {
|
||||||
|
if (!(config instanceof YamlConfiguration)) throw new IllegalArgumentException("Not a YamlConfiguration!");
|
||||||
|
YamlConfiguration yml = (YamlConfiguration) config;
|
||||||
|
yml.save(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,16 +1,13 @@
|
||||||
package net.frankheijden.serverutils.bukkit.listeners;
|
package net.frankheijden.serverutils.bukkit.listeners;
|
||||||
|
|
||||||
import net.frankheijden.serverutils.bukkit.config.Config;
|
import net.frankheijden.serverutils.bukkit.utils.BukkitUtils;
|
||||||
import net.frankheijden.serverutils.bukkit.tasks.UpdateCheckerTask;
|
import net.frankheijden.serverutils.common.listeners.ServerListener;
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
|
||||||
public class MainListener implements Listener {
|
public class BukkitListener implements Listener {
|
||||||
|
|
||||||
private static final Config config = Config.getInstance();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when a player joins the server.
|
* Called when a player joins the server.
|
||||||
|
|
@ -19,11 +16,6 @@ public class MainListener implements Listener {
|
||||||
*/
|
*/
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||||
if (!config.getBoolean("settings.check-updates-login")) return;
|
ServerListener.handleUpdate(BukkitUtils.wrap(event.getPlayer()));
|
||||||
|
|
||||||
Player player = event.getPlayer();
|
|
||||||
if (player.hasPermission("serverutils.notification.update")) {
|
|
||||||
UpdateCheckerTask.start(player);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,17 +2,23 @@ package net.frankheijden.serverutils.bukkit.managers;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitLoadResult;
|
||||||
import net.frankheijden.serverutils.bukkit.reflection.RCommandMap;
|
import net.frankheijden.serverutils.bukkit.reflection.RCommandMap;
|
||||||
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
import net.frankheijden.serverutils.bukkit.reflection.RCraftServer;
|
||||||
import net.frankheijden.serverutils.bukkit.reflection.RCraftingManager;
|
import net.frankheijden.serverutils.bukkit.reflection.RCraftingManager;
|
||||||
import net.frankheijden.serverutils.bukkit.reflection.RJavaPlugin;
|
import net.frankheijden.serverutils.bukkit.reflection.RJavaPlugin;
|
||||||
import net.frankheijden.serverutils.bukkit.reflection.RPluginClassLoader;
|
import net.frankheijden.serverutils.bukkit.reflection.RPluginClassLoader;
|
||||||
import net.frankheijden.serverutils.bukkit.reflection.RSimplePluginManager;
|
import net.frankheijden.serverutils.bukkit.reflection.RSimplePluginManager;
|
||||||
|
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||||
|
import net.frankheijden.serverutils.common.entities.Result;
|
||||||
|
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.PluginCommand;
|
||||||
|
|
@ -23,14 +29,27 @@ import org.bukkit.plugin.PluginDescriptionFile;
|
||||||
import org.bukkit.plugin.PluginLoader;
|
import org.bukkit.plugin.PluginLoader;
|
||||||
import org.bukkit.plugin.UnknownDependencyException;
|
import org.bukkit.plugin.UnknownDependencyException;
|
||||||
|
|
||||||
public class PluginManager {
|
public class BukkitPluginManager extends AbstractPluginManager<Plugin> {
|
||||||
|
|
||||||
|
private final ServerUtils plugin;
|
||||||
|
private static BukkitPluginManager instance;
|
||||||
|
|
||||||
|
public BukkitPluginManager(ServerUtils plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
instance = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BukkitPluginManager get() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the specified file as a plugin.
|
* Loads the specified file as a plugin.
|
||||||
* @param jarFile The name of the file in the plugins/ folder.
|
* @param jarFile The name of the file in the plugins/ folder.
|
||||||
* @return The result of the loading procedure.
|
* @return The result of the loading procedure.
|
||||||
*/
|
*/
|
||||||
public static LoadResult loadPlugin(String jarFile) {
|
@Override
|
||||||
|
public BukkitLoadResult loadPlugin(String jarFile) {
|
||||||
return loadPlugin(new File(ServerUtils.getInstance().getDataFolder().getParent(), jarFile));
|
return loadPlugin(new File(ServerUtils.getInstance().getDataFolder().getParent(), jarFile));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,33 +58,34 @@ public class PluginManager {
|
||||||
* @param file The file to be loaded.
|
* @param file The file to be loaded.
|
||||||
* @return The result of the loading procedure.
|
* @return The result of the loading procedure.
|
||||||
*/
|
*/
|
||||||
public static LoadResult loadPlugin(File file) {
|
@Override
|
||||||
if (!file.exists()) return new LoadResult(Result.NOT_EXISTS);
|
public BukkitLoadResult loadPlugin(File file) {
|
||||||
|
if (!file.exists()) return new BukkitLoadResult(Result.NOT_EXISTS);
|
||||||
|
|
||||||
Plugin loadedPlugin = getLoadedPlugin(file);
|
Plugin loadedPlugin = getLoadedPlugin(file);
|
||||||
if (loadedPlugin != null) return new LoadResult(Result.ALREADY_LOADED);
|
if (loadedPlugin != null) return new BukkitLoadResult(Result.ALREADY_LOADED);
|
||||||
|
|
||||||
Plugin plugin;
|
Plugin plugin;
|
||||||
try {
|
try {
|
||||||
plugin = Bukkit.getPluginManager().loadPlugin(file);
|
plugin = Bukkit.getPluginManager().loadPlugin(file);
|
||||||
} catch (InvalidDescriptionException ex) {
|
} catch (InvalidDescriptionException ex) {
|
||||||
return new LoadResult(Result.INVALID_DESCRIPTION);
|
return new BukkitLoadResult(Result.INVALID_DESCRIPTION);
|
||||||
} catch (UnknownDependencyException ex) {
|
} catch (UnknownDependencyException ex) {
|
||||||
return new LoadResult(Result.UNKNOWN_DEPENDENCY.arg(ex.getMessage()));
|
return new BukkitLoadResult(Result.UNKNOWN_DEPENDENCY.arg(ex.getMessage()));
|
||||||
} catch (InvalidPluginException ex) {
|
} catch (InvalidPluginException ex) {
|
||||||
if (ex.getCause() instanceof IllegalArgumentException) {
|
if (ex.getCause() instanceof IllegalArgumentException) {
|
||||||
IllegalArgumentException e = (IllegalArgumentException) ex.getCause();
|
IllegalArgumentException e = (IllegalArgumentException) ex.getCause();
|
||||||
if (e.getMessage().equalsIgnoreCase("Plugin already initialized!")) {
|
if (e.getMessage().equalsIgnoreCase("Plugin already initialized!")) {
|
||||||
return new LoadResult(Result.ALREADY_ENABLED);
|
return new BukkitLoadResult(Result.ALREADY_ENABLED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
return new LoadResult(Result.ERROR);
|
return new BukkitLoadResult(Result.ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (plugin == null) return new LoadResult(Result.INVALID_PLUGIN);
|
if (plugin == null) return new BukkitLoadResult(Result.INVALID_PLUGIN);
|
||||||
plugin.onLoad();
|
plugin.onLoad();
|
||||||
return new LoadResult(plugin);
|
return new BukkitLoadResult(plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -101,7 +121,8 @@ public class PluginManager {
|
||||||
* @param pluginName The plugin to unload.
|
* @param pluginName The plugin to unload.
|
||||||
* @return The result of the unload.
|
* @return The result of the unload.
|
||||||
*/
|
*/
|
||||||
public static CloseableResult unloadPlugin(String pluginName) {
|
@Override
|
||||||
|
public CloseableResult unloadPlugin(String pluginName) {
|
||||||
return unloadPlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
return unloadPlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -110,7 +131,8 @@ public class PluginManager {
|
||||||
* @param plugin The plugin to unload.
|
* @param plugin The plugin to unload.
|
||||||
* @return The result of the unload.
|
* @return The result of the unload.
|
||||||
*/
|
*/
|
||||||
public static CloseableResult unloadPlugin(Plugin plugin) {
|
@Override
|
||||||
|
public CloseableResult unloadPlugin(Plugin plugin) {
|
||||||
if (plugin == null) return new CloseableResult(Result.NOT_EXISTS);
|
if (plugin == null) return new CloseableResult(Result.NOT_EXISTS);
|
||||||
Closeable closeable;
|
Closeable closeable;
|
||||||
try {
|
try {
|
||||||
|
|
@ -129,7 +151,7 @@ public class PluginManager {
|
||||||
* @param pluginName The plugin to enable.
|
* @param pluginName The plugin to enable.
|
||||||
* @return The result of the enabling.
|
* @return The result of the enabling.
|
||||||
*/
|
*/
|
||||||
public static Result enablePlugin(String pluginName) {
|
public Result enablePlugin(String pluginName) {
|
||||||
return enablePlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
return enablePlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,7 +160,8 @@ public class PluginManager {
|
||||||
* @param plugin The plugin to enable.
|
* @param plugin The plugin to enable.
|
||||||
* @return The result of the enabling.
|
* @return The result of the enabling.
|
||||||
*/
|
*/
|
||||||
public static Result enablePlugin(Plugin plugin) {
|
@Override
|
||||||
|
public Result enablePlugin(Plugin plugin) {
|
||||||
if (plugin == null) return Result.NOT_EXISTS;
|
if (plugin == null) return Result.NOT_EXISTS;
|
||||||
if (Bukkit.getPluginManager().isPluginEnabled(plugin.getName())) return Result.ALREADY_ENABLED;
|
if (Bukkit.getPluginManager().isPluginEnabled(plugin.getName())) return Result.ALREADY_ENABLED;
|
||||||
Bukkit.getPluginManager().enablePlugin(plugin);
|
Bukkit.getPluginManager().enablePlugin(plugin);
|
||||||
|
|
@ -153,7 +176,8 @@ public class PluginManager {
|
||||||
* @param pluginName The plugin to reload.
|
* @param pluginName The plugin to reload.
|
||||||
* @return The result of the reload.
|
* @return The result of the reload.
|
||||||
*/
|
*/
|
||||||
public static CloseableResult reloadPlugin(String pluginName) {
|
@Override
|
||||||
|
public CloseableResult reloadPlugin(String pluginName) {
|
||||||
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
|
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
|
||||||
if (plugin == null) return new CloseableResult(Result.NOT_EXISTS);
|
if (plugin == null) return new CloseableResult(Result.NOT_EXISTS);
|
||||||
return reloadPlugin(plugin);
|
return reloadPlugin(plugin);
|
||||||
|
|
@ -164,7 +188,8 @@ public class PluginManager {
|
||||||
* @param plugin The plugin to reload.
|
* @param plugin The plugin to reload.
|
||||||
* @return The result of the reload.
|
* @return The result of the reload.
|
||||||
*/
|
*/
|
||||||
public static CloseableResult reloadPlugin(Plugin plugin) {
|
@Override
|
||||||
|
public CloseableResult reloadPlugin(Plugin plugin) {
|
||||||
Result disableResult = disablePlugin(plugin);
|
Result disableResult = disablePlugin(plugin);
|
||||||
if (disableResult != Result.SUCCESS && disableResult != Result.ALREADY_DISABLED) {
|
if (disableResult != Result.SUCCESS && disableResult != Result.ALREADY_DISABLED) {
|
||||||
return new CloseableResult(disableResult);
|
return new CloseableResult(disableResult);
|
||||||
|
|
@ -176,9 +201,9 @@ public class PluginManager {
|
||||||
File pluginFile = getPluginFile(plugin.getName());
|
File pluginFile = getPluginFile(plugin.getName());
|
||||||
if (pluginFile == null) return result.set(Result.FILE_DELETED);
|
if (pluginFile == null) return result.set(Result.FILE_DELETED);
|
||||||
|
|
||||||
LoadResult loadResult = loadPlugin(pluginFile);
|
BukkitLoadResult loadResult = loadPlugin(pluginFile);
|
||||||
if (!loadResult.isSuccess()) return result.set(loadResult.getResult());
|
if (!loadResult.isSuccess()) return result.set(loadResult.getResult());
|
||||||
return result.set(enablePlugin(loadResult.getPlugin()));
|
return result.set(enablePlugin(loadResult.get()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -290,8 +315,8 @@ public class PluginManager {
|
||||||
* @param pluginName The plugin name.
|
* @param pluginName The plugin name.
|
||||||
* @return The file, or null if invalid or not found.
|
* @return The file, or null if invalid or not found.
|
||||||
*/
|
*/
|
||||||
public static File getPluginFile(String pluginName) {
|
public File getPluginFile(String pluginName) {
|
||||||
for (File file : ServerUtils.getInstance().getJars()) {
|
for (File file : getPluginJars()) {
|
||||||
PluginDescriptionFile descriptionFile;
|
PluginDescriptionFile descriptionFile;
|
||||||
try {
|
try {
|
||||||
descriptionFile = getPluginDescription(file);
|
descriptionFile = getPluginDescription(file);
|
||||||
|
|
@ -303,4 +328,28 @@ public class PluginManager {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getPluginFile(Plugin plugin) {
|
||||||
|
try {
|
||||||
|
return RJavaPlugin.getFile(plugin);
|
||||||
|
} catch (ReflectiveOperationException ex) {
|
||||||
|
throw new RuntimeException("Error retrieving current plugin file", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getPluginsFolder() {
|
||||||
|
return plugin.getDataFolder().getParentFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Plugin> getPlugins() {
|
||||||
|
return Arrays.asList(Bukkit.getPluginManager().getPlugins());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPluginName(Plugin plugin) {
|
||||||
|
return plugin.getName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
package net.frankheijden.serverutils.bukkit.managers;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||||
|
import net.frankheijden.serverutils.common.managers.AbstractTaskManager;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
|
public class BukkitTaskManager extends AbstractTaskManager {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void runTaskAsynchronously(Runnable runnable) {
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(ServerUtils.getInstance(), runnable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package net.frankheijden.serverutils.bukkit.managers;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||||
|
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
||||||
|
|
||||||
|
public class BukkitVersionManager extends AbstractVersionManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new VersionManager instance.
|
||||||
|
* Used for automatic updating.
|
||||||
|
*/
|
||||||
|
public BukkitVersionManager(ServerUtils plugin) {
|
||||||
|
super(plugin.getDescription().getVersion());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
package net.frankheijden.serverutils.bukkit.managers;
|
|
||||||
|
|
||||||
import org.bukkit.plugin.Plugin;
|
|
||||||
|
|
||||||
public class LoadResult {
|
|
||||||
private final Plugin plugin;
|
|
||||||
private final Result result;
|
|
||||||
|
|
||||||
private LoadResult(Plugin plugin, Result result) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public LoadResult(Plugin plugin) {
|
|
||||||
this(plugin, Result.SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
public LoadResult(Result result) {
|
|
||||||
this(null, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Result getResult() {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Plugin getPlugin() {
|
|
||||||
return plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isSuccess() {
|
|
||||||
return plugin != null && result == Result.SUCCESS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,45 +0,0 @@
|
||||||
package net.frankheijden.serverutils.bukkit.managers;
|
|
||||||
|
|
||||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
|
||||||
|
|
||||||
public class VersionManager {
|
|
||||||
|
|
||||||
private static VersionManager instance;
|
|
||||||
private final ServerUtils plugin = ServerUtils.getInstance();
|
|
||||||
private final String currentVersion;
|
|
||||||
private String downloadedVersion;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new VersionManager instance.
|
|
||||||
* Used for automatic updating.
|
|
||||||
*/
|
|
||||||
public VersionManager() {
|
|
||||||
instance = this;
|
|
||||||
this.currentVersion = plugin.getDescription().getVersion();
|
|
||||||
this.downloadedVersion = currentVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static VersionManager getInstance() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCurrentVersion() {
|
|
||||||
return currentVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDownloadedVersion() {
|
|
||||||
return downloadedVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasDownloaded() {
|
|
||||||
return !downloadedVersion.equals(currentVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDownloadedVersion(String version) {
|
|
||||||
return downloadedVersion.equals(version);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDownloadedVersion(String downloadedVersion) {
|
|
||||||
this.downloadedVersion = downloadedVersion;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Warning;
|
import org.bukkit.Warning;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package net.frankheijden.serverutils.bukkit.reflection;
|
package net.frankheijden.serverutils.bukkit.reflection;
|
||||||
|
|
||||||
import static net.frankheijden.serverutils.bukkit.reflection.BukkitReflection.MINOR;
|
import static net.frankheijden.serverutils.bukkit.entities.BukkitReflection.MINOR;
|
||||||
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
|
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
|
||||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
||||||
|
|
@ -14,6 +14,7 @@ import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
import net.frankheijden.serverutils.common.utils.MapUtils;
|
import net.frankheijden.serverutils.common.utils.MapUtils;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package net.frankheijden.serverutils.bukkit.reflection;
|
package net.frankheijden.serverutils.bukkit.reflection;
|
||||||
|
|
||||||
import static net.frankheijden.serverutils.bukkit.reflection.BukkitReflection.MINOR;
|
import static net.frankheijden.serverutils.bukkit.entities.BukkitReflection.MINOR;
|
||||||
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||||
import static net.frankheijden.serverutils.common.reflection.MethodParam.methodOf;
|
import static net.frankheijden.serverutils.common.reflection.MethodParam.methodOf;
|
||||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
|
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
|
||||||
|
|
@ -16,6 +16,8 @@ import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
|
|
||||||
public class RDedicatedServer {
|
public class RDedicatedServer {
|
||||||
|
|
||||||
private static Class<?> dedicatedServerClass;
|
private static Class<?> dedicatedServerClass;
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VE
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
|
|
||||||
public class RDedicatedServerProperties {
|
public class RDedicatedServerProperties {
|
||||||
|
|
||||||
private static Class<?> serverPropertiesClass;
|
private static Class<?> serverPropertiesClass;
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VE
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
|
|
||||||
public class RDedicatedServerSettings {
|
public class RDedicatedServerSettings {
|
||||||
|
|
||||||
private static Class<?> serverSettingsClass;
|
private static Class<?> serverSettingsClass;
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get
|
||||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.invoke;
|
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.invoke;
|
||||||
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -20,7 +21,8 @@ public class RJavaPlugin {
|
||||||
try {
|
try {
|
||||||
javaPluginClass = JavaPlugin.class;
|
javaPluginClass = JavaPlugin.class;
|
||||||
methods = getAllMethods(javaPluginClass,
|
methods = getAllMethods(javaPluginClass,
|
||||||
methodOf("getClassLoader", ALL_VERSIONS));
|
methodOf("getClassLoader", ALL_VERSIONS),
|
||||||
|
methodOf("getFile", ALL_VERSIONS));
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
@ -29,4 +31,8 @@ public class RJavaPlugin {
|
||||||
public static ClassLoader getClassLoader(Object instance) throws InvocationTargetException, IllegalAccessException {
|
public static ClassLoader getClassLoader(Object instance) throws InvocationTargetException, IllegalAccessException {
|
||||||
return (ClassLoader) invoke(methods, instance, "getClassLoader");
|
return (ClassLoader) invoke(methods, instance, "getClassLoader");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static File getFile(Object instance) throws ReflectiveOperationException {
|
||||||
|
return (File) invoke(methods, instance, "getFile");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
|
|
||||||
public class RJsonList {
|
public class RJsonList {
|
||||||
|
|
||||||
private static Class<?> jsonListClass;
|
private static Class<?> jsonListClass;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package net.frankheijden.serverutils.bukkit.reflection;
|
package net.frankheijden.serverutils.bukkit.reflection;
|
||||||
|
|
||||||
import static net.frankheijden.serverutils.bukkit.reflection.BukkitReflection.MINOR;
|
import static net.frankheijden.serverutils.bukkit.entities.BukkitReflection.MINOR;
|
||||||
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
|
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
|
||||||
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
||||||
|
|
@ -13,6 +13,7 @@ import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
public class RMinecraftKey {
|
public class RMinecraftKey {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VE
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
|
|
||||||
public class RMinecraftServer {
|
public class RMinecraftServer {
|
||||||
|
|
||||||
private static Class<?> minecraftServerClass;
|
private static Class<?> minecraftServerClass;
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VE
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
|
|
||||||
public class RPlayerList {
|
public class RPlayerList {
|
||||||
|
|
||||||
private static Class<?> playerListClass;
|
private static Class<?> playerListClass;
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VE
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
|
|
||||||
public class RPropertyManager {
|
public class RPropertyManager {
|
||||||
|
|
||||||
private static Class<?> propertyManagerClass;
|
private static Class<?> propertyManagerClass;
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import java.lang.reflect.Field;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
import net.frankheijden.serverutils.common.utils.MapUtils;
|
import net.frankheijden.serverutils.common.utils.MapUtils;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import java.lang.reflect.Field;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitReflection;
|
||||||
import net.frankheijden.serverutils.common.utils.MapUtils;
|
import net.frankheijden.serverutils.common.utils.MapUtils;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package net.frankheijden.serverutils.bukkit.utils;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bukkit.entities.BukkitCommandSender;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
public class BukkitUtils {
|
||||||
|
|
||||||
|
public static ServerCommandSender wrap(CommandSender sender) {
|
||||||
|
return new BukkitCommandSender(sender);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,50 +0,0 @@
|
||||||
package net.frankheijden.serverutils.bukkit.utils;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.bukkit.configuration.MemorySection;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
|
|
||||||
public class YamlUtils {
|
|
||||||
|
|
||||||
public static void addDefaults(MemorySection defaults, YamlConfiguration yml) {
|
|
||||||
addDefaults(defaults, yml, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void addDefaults(MemorySection defaults, YamlConfiguration yml, String root) {
|
|
||||||
if (defaults == null) return;
|
|
||||||
for (String key : defaults.getKeys(false)) {
|
|
||||||
String newKey = (root.isEmpty() ? "" : root + ".") + key;
|
|
||||||
Object value = defaults.get(key);
|
|
||||||
if (value instanceof MemorySection) {
|
|
||||||
addDefaults((MemorySection) value, yml, newKey);
|
|
||||||
} else if (yml.get(newKey) == null) {
|
|
||||||
yml.set(newKey, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initiates a YamlConfiguration from a file with associated defaults.
|
|
||||||
* @param file The yml file.
|
|
||||||
* @param def The default YamlConfiguration to be applied.
|
|
||||||
* @return The loaded YamlConfiguration of the file with defaults.
|
|
||||||
*/
|
|
||||||
public static YamlConfiguration init(File file, YamlConfiguration def) {
|
|
||||||
YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
|
|
||||||
YamlUtils.addDefaults(def, yml);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Idk somehow the order messes up
|
|
||||||
// of the messages if we don't do this
|
|
||||||
file.delete();
|
|
||||||
file.createNewFile();
|
|
||||||
|
|
||||||
yml.save(file);
|
|
||||||
} catch (IOException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
return yml;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
28
Bungee/build.gradle
Normal file
28
Bungee/build.gradle
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
group = rootProject.group + '.bungee'
|
||||||
|
String dependencyDir = group + '.dependencies'
|
||||||
|
version = rootProject.version
|
||||||
|
archivesBaseName = rootProject.name + '-Bungee'
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation 'co.aikar:acf-bungee:0.5.0-SNAPSHOT'
|
||||||
|
implementation 'org.bstats:bstats-bungeecord:1.7'
|
||||||
|
implementation project(":Common")
|
||||||
|
compileOnly 'net.md-5:bungeecord-api:1.16-R0.2-SNAPSHOT'
|
||||||
|
}
|
||||||
|
|
||||||
|
processResources {
|
||||||
|
from('src/main/resources') {
|
||||||
|
include 'bungee.yml'
|
||||||
|
expand(version: project.version)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
shadowJar {
|
||||||
|
relocate 'org.bstats.bungeecord', dependencyDir + '.bstats'
|
||||||
|
relocate 'co.aikar.commands', dependencyDir + '.acf'
|
||||||
|
relocate 'co.aikar.locales', dependencyDir + '.locales'
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
package net.frankheijden.serverutils.bungee;
|
||||||
|
|
||||||
|
import co.aikar.commands.BungeeCommandCompletionContext;
|
||||||
|
import co.aikar.commands.BungeeCommandManager;
|
||||||
|
import co.aikar.commands.CommandCompletions;
|
||||||
|
import net.frankheijden.serverutils.bungee.commands.CommandPlugins;
|
||||||
|
import net.frankheijden.serverutils.bungee.commands.CommandServerUtils;
|
||||||
|
import net.frankheijden.serverutils.bungee.entities.BungeePlugin;
|
||||||
|
import net.frankheijden.serverutils.bungee.entities.BungeeReflection;
|
||||||
|
import net.frankheijden.serverutils.bungee.listeners.BungeeListener;
|
||||||
|
import net.frankheijden.serverutils.bungee.managers.BungeePluginManager;
|
||||||
|
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||||
|
import net.frankheijden.serverutils.common.config.Config;
|
||||||
|
import net.frankheijden.serverutils.common.config.Messenger;
|
||||||
|
import net.md_5.bungee.api.plugin.Plugin;
|
||||||
|
import org.bstats.bungeecord.Metrics;
|
||||||
|
|
||||||
|
public class ServerUtils extends Plugin {
|
||||||
|
|
||||||
|
private static ServerUtils instance;
|
||||||
|
private static final String CONFIG_RESOURCE = "bungee-config.yml";
|
||||||
|
private static final String MESSAGES_RESOURCE = "bungee-messages.yml";
|
||||||
|
|
||||||
|
private BungeePlugin plugin;
|
||||||
|
private BungeeCommandManager commandManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEnable() {
|
||||||
|
super.onEnable();
|
||||||
|
instance = this;
|
||||||
|
|
||||||
|
this.plugin = new BungeePlugin(this);
|
||||||
|
ServerUtilsApp.init(this, plugin);
|
||||||
|
|
||||||
|
new Metrics(this, ServerUtilsApp.BSTATS_METRICS_ID);
|
||||||
|
new BungeeReflection();
|
||||||
|
|
||||||
|
this.commandManager = new BungeeCommandManager(this);
|
||||||
|
commandManager.registerCommand(new CommandPlugins());
|
||||||
|
commandManager.registerCommand(new CommandServerUtils());
|
||||||
|
|
||||||
|
BungeePluginManager manager = plugin.getPluginManager();
|
||||||
|
CommandCompletions<BungeeCommandCompletionContext> commandCompletions = commandManager.getCommandCompletions();
|
||||||
|
commandCompletions.registerAsyncCompletion("plugins", context -> manager.getPluginNames());
|
||||||
|
commandCompletions.registerAsyncCompletion("pluginJars", context -> manager.getPluginFileNames());
|
||||||
|
|
||||||
|
reload();
|
||||||
|
getProxy().getPluginManager().registerListener(this, new BungeeListener());
|
||||||
|
|
||||||
|
ServerUtilsApp.tryCheckForUpdates();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ServerUtils getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BungeePlugin getPlugin() {
|
||||||
|
return plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BungeeCommandManager getCommandManager() {
|
||||||
|
return commandManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reload() {
|
||||||
|
new Config("config.yml", CONFIG_RESOURCE);
|
||||||
|
new Messenger("messages.yml", MESSAGES_RESOURCE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.commands;
|
||||||
|
|
||||||
|
import co.aikar.commands.BaseCommand;
|
||||||
|
import co.aikar.commands.annotation.CommandAlias;
|
||||||
|
import co.aikar.commands.annotation.CommandCompletion;
|
||||||
|
import co.aikar.commands.annotation.CommandPermission;
|
||||||
|
import co.aikar.commands.annotation.Default;
|
||||||
|
import co.aikar.commands.annotation.Description;
|
||||||
|
import net.frankheijden.serverutils.bungee.managers.BungeePluginManager;
|
||||||
|
import net.frankheijden.serverutils.bungee.utils.BungeeUtils;
|
||||||
|
import net.frankheijden.serverutils.common.commands.Plugins;
|
||||||
|
import net.frankheijden.serverutils.common.config.Messenger;
|
||||||
|
import net.md_5.bungee.api.CommandSender;
|
||||||
|
|
||||||
|
@CommandAlias("bpl|bplugins|bungeepl")
|
||||||
|
public class CommandPlugins extends BaseCommand {
|
||||||
|
|
||||||
|
private static final BungeePluginManager manager = BungeePluginManager.get();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends the plugin list to the sender.
|
||||||
|
* The `-v` flag will output the plugins with version.
|
||||||
|
* The `-m` flag will also output modules in the plugin list.
|
||||||
|
* @param sender The sender of the command.
|
||||||
|
*/
|
||||||
|
@Default
|
||||||
|
@CommandCompletion("-v|-m -v|-m")
|
||||||
|
@CommandPermission("serverutils.plugins")
|
||||||
|
@Description("Shows the plugins of this proxy.")
|
||||||
|
public void onPlugins(CommandSender sender, String... args) {
|
||||||
|
boolean version = contains(args, "-v");
|
||||||
|
boolean modules = contains(args, "-m");
|
||||||
|
Plugins.sendPlugins(BungeeUtils.wrap(sender), manager.getPluginsSorted(modules), pl -> {
|
||||||
|
String ver = version ? Messenger.getMessage("serverutils.plugins.version",
|
||||||
|
"%version%", pl.getDescription().getVersion()) : "";
|
||||||
|
return Messenger.getMessage("serverutils.plugins.format",
|
||||||
|
"%plugin%", pl.getDescription().getName()) + ver;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean contains(String[] arr, String val) {
|
||||||
|
for (String s : arr) {
|
||||||
|
if (s.equalsIgnoreCase(val)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,254 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.commands;
|
||||||
|
|
||||||
|
import static net.frankheijden.serverutils.common.config.Messenger.sendMessage;
|
||||||
|
|
||||||
|
import co.aikar.commands.BaseCommand;
|
||||||
|
import co.aikar.commands.annotation.CommandAlias;
|
||||||
|
import co.aikar.commands.annotation.CommandCompletion;
|
||||||
|
import co.aikar.commands.annotation.CommandPermission;
|
||||||
|
import co.aikar.commands.annotation.Default;
|
||||||
|
import co.aikar.commands.annotation.Description;
|
||||||
|
import co.aikar.commands.annotation.Subcommand;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||||
|
import net.frankheijden.serverutils.bungee.entities.BungeeLoadResult;
|
||||||
|
import net.frankheijden.serverutils.bungee.managers.BungeePluginManager;
|
||||||
|
import net.frankheijden.serverutils.bungee.reflection.RPluginManager;
|
||||||
|
import net.frankheijden.serverutils.bungee.utils.BungeeUtils;
|
||||||
|
import net.frankheijden.serverutils.common.config.Messenger;
|
||||||
|
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||||
|
import net.frankheijden.serverutils.common.entities.Result;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import net.frankheijden.serverutils.common.utils.FormatBuilder;
|
||||||
|
import net.frankheijden.serverutils.common.utils.ListBuilder;
|
||||||
|
import net.frankheijden.serverutils.common.utils.ListFormat;
|
||||||
|
import net.md_5.bungee.api.CommandSender;
|
||||||
|
import net.md_5.bungee.api.ProxyServer;
|
||||||
|
import net.md_5.bungee.api.plugin.Command;
|
||||||
|
import net.md_5.bungee.api.plugin.Plugin;
|
||||||
|
import net.md_5.bungee.api.plugin.PluginDescription;
|
||||||
|
|
||||||
|
@CommandAlias("bsu|bserverutils")
|
||||||
|
public class CommandServerUtils extends BaseCommand {
|
||||||
|
|
||||||
|
private static final ProxyServer proxy = ProxyServer.getInstance();
|
||||||
|
private static final ServerUtils plugin = ServerUtils.getInstance();
|
||||||
|
private static final Set<String> ALIASES;
|
||||||
|
|
||||||
|
static {
|
||||||
|
ALIASES = new HashSet<>();
|
||||||
|
ALIASES.add("bserverutils");
|
||||||
|
ALIASES.add("bplugins");
|
||||||
|
ALIASES.add("bungeepl");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shows the help page to the sender.
|
||||||
|
* @param commandSender The sender of the command.
|
||||||
|
*/
|
||||||
|
@Default
|
||||||
|
@Subcommand("help")
|
||||||
|
@CommandPermission("serverutils.help")
|
||||||
|
@Description("Shows a help page with a few commands.")
|
||||||
|
public void onHelp(CommandSender commandSender) {
|
||||||
|
ServerCommandSender sender = BungeeUtils.wrap(commandSender);
|
||||||
|
Messenger.sendMessage(sender, "serverutils.help.header");
|
||||||
|
|
||||||
|
FormatBuilder builder = FormatBuilder.create(Messenger.getMessage("serverutils.help.format"))
|
||||||
|
.orderedKeys("%command%", "%subcommand%", "%help%");
|
||||||
|
plugin.getCommandManager().getRegisteredRootCommands().stream()
|
||||||
|
.filter(c -> !ALIASES.contains(c.getCommandName().toLowerCase()))
|
||||||
|
.forEach(rootCommand -> {
|
||||||
|
builder.add(rootCommand.getCommandName(), "", rootCommand.getDescription());
|
||||||
|
|
||||||
|
rootCommand.getSubCommands().forEach((str, cmd) -> {
|
||||||
|
if (cmd.getPrefSubCommand().isEmpty()) return;
|
||||||
|
builder.add(rootCommand.getCommandName(), " " + cmd.getPrefSubCommand(), cmd.getHelpText());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
builder.sendTo(sender);
|
||||||
|
Messenger.sendMessage(sender, "serverutils.help.footer");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reloads the configurations of ServerUtils.
|
||||||
|
* @param sender The sender of the command.
|
||||||
|
*/
|
||||||
|
@Subcommand("reload")
|
||||||
|
@CommandPermission("serverutils.reload")
|
||||||
|
@Description("Reloads the ServerUtils plugin.")
|
||||||
|
public void onReload(CommandSender sender) {
|
||||||
|
plugin.reload();
|
||||||
|
sendMessage(BungeeUtils.wrap(sender), "serverutils.success",
|
||||||
|
"%action%", "reload",
|
||||||
|
"%what%", "ServerUtils Bungee configurations");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the specified plugin on the proxy.
|
||||||
|
* @param commandSender The sender of the command.
|
||||||
|
* @param jarFile The filename of the plugin in the plugins/ directory.
|
||||||
|
*/
|
||||||
|
@Subcommand("loadplugin")
|
||||||
|
@CommandCompletion("@pluginJars")
|
||||||
|
@CommandPermission("serverutils.loadplugin")
|
||||||
|
@Description("Loads the specified jar file as a plugin.")
|
||||||
|
public void onLoadPlugin(CommandSender commandSender, String jarFile) {
|
||||||
|
ServerCommandSender sender = BungeeUtils.wrap(commandSender);
|
||||||
|
|
||||||
|
BungeeLoadResult loadResult = BungeePluginManager.get().loadPlugin(jarFile);
|
||||||
|
if (!loadResult.isSuccess()) {
|
||||||
|
loadResult.getResult().sendTo(sender, "load", jarFile);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Plugin plugin = loadResult.get();
|
||||||
|
Result result = BungeePluginManager.get().enablePlugin(plugin);
|
||||||
|
result.sendTo(sender, "load", plugin.getDescription().getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unloads the specified plugin from the proxy.
|
||||||
|
* @param commandSender The sender of the command.
|
||||||
|
* @param pluginName The plugin name.
|
||||||
|
*/
|
||||||
|
@Subcommand("unloadplugin")
|
||||||
|
@CommandCompletion("@plugins")
|
||||||
|
@CommandPermission("serverutils.unloadplugin")
|
||||||
|
@Description("Disables and unloads the specified plugin.")
|
||||||
|
public void onUnloadPlugin(CommandSender commandSender, String pluginName) {
|
||||||
|
CloseableResult result = BungeePluginManager.get().unloadPlugin(pluginName);
|
||||||
|
result.getResult().sendTo(BungeeUtils.wrap(commandSender), "unload", pluginName);
|
||||||
|
result.tryClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reloads the specified plugin on the proxy.
|
||||||
|
* @param sender The sender of the command.
|
||||||
|
* @param pluginName The plugin name.
|
||||||
|
*/
|
||||||
|
@Subcommand("reloadplugin")
|
||||||
|
@CommandCompletion("@plugins")
|
||||||
|
@CommandPermission("serverutils.reloadplugin")
|
||||||
|
@Description("Reloads a specified plugin.")
|
||||||
|
public void onReloadPlugin(CommandSender sender, String pluginName) {
|
||||||
|
CloseableResult result = BungeePluginManager.get().reloadPlugin(pluginName);
|
||||||
|
result.getResult().sendTo(BungeeUtils.wrap(sender), "reload", pluginName);
|
||||||
|
result.tryClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shows information about the specified plugin.
|
||||||
|
* @param commandSender The sender of the command.
|
||||||
|
* @param pluginName The plugin name.
|
||||||
|
*/
|
||||||
|
@Subcommand("plugininfo")
|
||||||
|
@CommandCompletion("@plugins")
|
||||||
|
@CommandPermission("serverutils.plugininfo")
|
||||||
|
@Description("Shows information about the specified plugin.")
|
||||||
|
public void onPluginInfo(CommandSender commandSender, String pluginName) {
|
||||||
|
ServerCommandSender sender = BungeeUtils.wrap(commandSender);
|
||||||
|
|
||||||
|
Plugin plugin = ProxyServer.getInstance().getPluginManager().getPlugin(pluginName);
|
||||||
|
if (plugin == null) {
|
||||||
|
Result.NOT_EXISTS.sendTo(sender, "fetch", pluginName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PluginDescription desc = plugin.getDescription();
|
||||||
|
String format = Messenger.getMessage("serverutils.plugininfo.format");
|
||||||
|
String listFormatString = Messenger.getMessage("serverutils.plugininfo.list_format");
|
||||||
|
String seperator = Messenger.getMessage("serverutils.plugininfo.seperator");
|
||||||
|
String lastSeperator = Messenger.getMessage("serverutils.plugininfo.last_seperator");
|
||||||
|
|
||||||
|
ListFormat<String> listFormat = str -> listFormatString.replace("%value%", str);
|
||||||
|
|
||||||
|
Messenger.sendMessage(sender, "serverutils.plugininfo.header");
|
||||||
|
|
||||||
|
FormatBuilder builder = FormatBuilder.create(format)
|
||||||
|
.orderedKeys("%key%", "%value%")
|
||||||
|
.add("Name", desc.getName())
|
||||||
|
.add("Version", desc.getVersion())
|
||||||
|
.add("Author", desc.getAuthor())
|
||||||
|
.add("Description", desc.getDescription())
|
||||||
|
.add("Main", desc.getMain())
|
||||||
|
.add("File", desc.getFile().getName())
|
||||||
|
.add("Depend", ListBuilder.create(desc.getDepends())
|
||||||
|
.format(listFormat)
|
||||||
|
.seperator(seperator)
|
||||||
|
.lastSeperator(lastSeperator)
|
||||||
|
.toString())
|
||||||
|
.add("Soft Depend", ListBuilder.create(desc.getSoftDepends())
|
||||||
|
.format(listFormat)
|
||||||
|
.seperator(seperator)
|
||||||
|
.lastSeperator(lastSeperator)
|
||||||
|
.toString());
|
||||||
|
|
||||||
|
builder.sendTo(sender);
|
||||||
|
Messenger.sendMessage(sender, "serverutils.plugininfo.footer");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shows information about a provided command.
|
||||||
|
* @param commandSender The sender of the command.
|
||||||
|
* @param command The command to lookup.
|
||||||
|
*/
|
||||||
|
@Subcommand("commandinfo")
|
||||||
|
@CommandCompletion("@commands")
|
||||||
|
@CommandPermission("serverutils.commandinfo")
|
||||||
|
@Description("Shows information about the specified command.")
|
||||||
|
public void onCommandInfo(CommandSender commandSender, String command) {
|
||||||
|
ServerCommandSender sender = BungeeUtils.wrap(commandSender);
|
||||||
|
|
||||||
|
Map<String, Command> commands;
|
||||||
|
try {
|
||||||
|
commands = RPluginManager.getCommands(proxy.getPluginManager());
|
||||||
|
} catch (IllegalAccessException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Command cmd = commands.get(command);
|
||||||
|
if (cmd == null) {
|
||||||
|
Messenger.sendMessage(sender, "serverutils.commandinfo.not_exists");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Plugin plugin;
|
||||||
|
try {
|
||||||
|
plugin = RPluginManager.getPlugin(proxy.getPluginManager(), cmd);
|
||||||
|
} catch (IllegalAccessException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (plugin == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String format = Messenger.getMessage("serverutils.commandinfo.format");
|
||||||
|
String listFormatString = Messenger.getMessage("serverutils.commandinfo.list_format");
|
||||||
|
String seperator = Messenger.getMessage("serverutils.commandinfo.seperator");
|
||||||
|
String lastSeperator = Messenger.getMessage("serverutils.commandinfo.last_seperator");
|
||||||
|
|
||||||
|
ListFormat<String> listFormat = str -> listFormatString.replace("%value%", str);
|
||||||
|
|
||||||
|
Messenger.sendMessage(sender, "serverutils.commandinfo.header");
|
||||||
|
FormatBuilder builder = FormatBuilder.create(format)
|
||||||
|
.orderedKeys("%key%", "%value%")
|
||||||
|
.add("Name", cmd.getName())
|
||||||
|
.add("Plugin", plugin.getDescription().getName())
|
||||||
|
.add("Aliases", ListBuilder.create(cmd.getAliases())
|
||||||
|
.format(listFormat)
|
||||||
|
.seperator(seperator)
|
||||||
|
.lastSeperator(lastSeperator)
|
||||||
|
.toString())
|
||||||
|
.add("Permission", cmd.getPermission());
|
||||||
|
|
||||||
|
builder.sendTo(sender);
|
||||||
|
Messenger.sendMessage(sender, "serverutils.commandinfo.footer");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.entities;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bungee.utils.BungeeUtils;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import net.frankheijden.serverutils.common.providers.ChatProvider;
|
||||||
|
import net.md_5.bungee.api.ChatColor;
|
||||||
|
import net.md_5.bungee.api.ProxyServer;
|
||||||
|
|
||||||
|
public class BungeeChatProvider extends ChatProvider {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServerCommandSender getConsoleSender() {
|
||||||
|
return BungeeUtils.wrap(ProxyServer.getInstance().getConsole());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String color(String str) {
|
||||||
|
return ChatColor.translateAlternateColorCodes('&', str);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void broadcast(String permission, String message) {
|
||||||
|
ProxyServer.getInstance().getPlayers().stream()
|
||||||
|
.filter(p -> p.hasPermission(permission))
|
||||||
|
.forEach(p -> p.sendMessage(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.entities;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import net.md_5.bungee.api.CommandSender;
|
||||||
|
|
||||||
|
public class BungeeCommandSender implements ServerCommandSender {
|
||||||
|
|
||||||
|
private final CommandSender sender;
|
||||||
|
|
||||||
|
public BungeeCommandSender(CommandSender sender) {
|
||||||
|
this.sender = sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendMessage(String message) {
|
||||||
|
sender.sendMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasPermission(String permission) {
|
||||||
|
return sender.hasPermission(permission);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.entities;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.entities.LoadResult;
|
||||||
|
import net.frankheijden.serverutils.common.entities.Result;
|
||||||
|
import net.md_5.bungee.api.plugin.Plugin;
|
||||||
|
|
||||||
|
public class BungeeLoadResult extends LoadResult<Plugin> {
|
||||||
|
|
||||||
|
public BungeeLoadResult(Plugin obj, Result result) {
|
||||||
|
super(obj, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BungeeLoadResult(Plugin obj) {
|
||||||
|
super(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BungeeLoadResult(Result result) {
|
||||||
|
super(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.entities;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||||
|
import net.frankheijden.serverutils.bungee.managers.BungeePluginManager;
|
||||||
|
import net.frankheijden.serverutils.bungee.managers.BungeeTaskManager;
|
||||||
|
import net.frankheijden.serverutils.bungee.managers.BungeeVersionManager;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||||
|
|
||||||
|
public class BungeePlugin extends ServerUtilsPlugin {
|
||||||
|
|
||||||
|
private final ServerUtils plugin;
|
||||||
|
private final BungeePluginManager pluginManager;
|
||||||
|
private final BungeeTaskManager taskManager;
|
||||||
|
private final BungeeResourceProvider resourceProvider;
|
||||||
|
private final BungeeChatProvider chatProvider;
|
||||||
|
private final BungeeVersionManager versionManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new BungeePlugin instance of ServerUtils.
|
||||||
|
* @param plugin The ServerUtils plugin.
|
||||||
|
*/
|
||||||
|
public BungeePlugin(ServerUtils plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.pluginManager = new BungeePluginManager();
|
||||||
|
this.taskManager = new BungeeTaskManager();
|
||||||
|
this.resourceProvider = new BungeeResourceProvider(plugin);
|
||||||
|
this.chatProvider = new BungeeChatProvider();
|
||||||
|
this.versionManager = new BungeeVersionManager(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public BungeePluginManager getPluginManager() {
|
||||||
|
return pluginManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BungeeTaskManager getTaskManager() {
|
||||||
|
return taskManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BungeeResourceProvider getResourceProvider() {
|
||||||
|
return resourceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BungeeChatProvider getChatProvider() {
|
||||||
|
return chatProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BungeeVersionManager getVersionManager() {
|
||||||
|
return versionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Logger getLogger() {
|
||||||
|
return plugin.getLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getDataFolder() {
|
||||||
|
return plugin.getDataFolder();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.entities;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.reflection.ReflectionUtils;
|
||||||
|
import net.frankheijden.serverutils.common.reflection.VersionParam;
|
||||||
|
|
||||||
|
public class BungeeReflection extends ReflectionUtils {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCompatible(VersionParam param) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.entities;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||||
|
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||||
|
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||||
|
|
||||||
|
public class BungeeResourceProvider implements ResourceProvider {
|
||||||
|
|
||||||
|
private final ServerUtils plugin;
|
||||||
|
|
||||||
|
public BungeeResourceProvider(ServerUtils plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getResource(String resource) {
|
||||||
|
return plugin.getResourceAsStream(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public YamlConfig load(InputStream is) {
|
||||||
|
return new BungeeYamlConfig(is);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public YamlConfig load(File file) {
|
||||||
|
try {
|
||||||
|
return new BungeeYamlConfig(file);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.entities;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||||
|
import net.md_5.bungee.config.Configuration;
|
||||||
|
import net.md_5.bungee.config.ConfigurationProvider;
|
||||||
|
import net.md_5.bungee.config.YamlConfiguration;
|
||||||
|
|
||||||
|
public class BungeeYamlConfig implements YamlConfig {
|
||||||
|
|
||||||
|
private static final ConfigurationProvider provider = ConfigurationProvider.getProvider(YamlConfiguration.class);
|
||||||
|
private final Configuration config;
|
||||||
|
private File file = null;
|
||||||
|
|
||||||
|
public BungeeYamlConfig(File file) throws IOException {
|
||||||
|
this.config = provider.load(file);
|
||||||
|
this.file = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BungeeYamlConfig(InputStream in) {
|
||||||
|
this.config = provider.load(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BungeeYamlConfig(Configuration config) {
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object get(String path) {
|
||||||
|
Object obj = config.get(path);
|
||||||
|
if (obj instanceof Configuration) {
|
||||||
|
return new BungeeYamlConfig((Configuration) obj);
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(String path, Object value) {
|
||||||
|
config.set(path, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getString(String path) {
|
||||||
|
return config.getString(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(String path) {
|
||||||
|
return config.getBoolean(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<? extends String> getKeys() {
|
||||||
|
return config.getKeys();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save() throws IOException {
|
||||||
|
provider.save(config, file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.listeners;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bungee.utils.BungeeUtils;
|
||||||
|
import net.frankheijden.serverutils.common.listeners.ServerListener;
|
||||||
|
import net.md_5.bungee.api.event.ServerConnectEvent;
|
||||||
|
import net.md_5.bungee.api.plugin.Listener;
|
||||||
|
import net.md_5.bungee.event.EventHandler;
|
||||||
|
|
||||||
|
public class BungeeListener implements Listener {
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onServerConnect(ServerConnectEvent event) {
|
||||||
|
if (event.getReason() != ServerConnectEvent.Reason.JOIN_PROXY) return;
|
||||||
|
ServerListener.handleUpdate(BungeeUtils.wrap(event.getPlayer()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,268 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.managers;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||||
|
import net.frankheijden.serverutils.bungee.entities.BungeeLoadResult;
|
||||||
|
import net.frankheijden.serverutils.bungee.reflection.RPluginClassLoader;
|
||||||
|
import net.frankheijden.serverutils.bungee.reflection.RPluginManager;
|
||||||
|
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||||
|
import net.frankheijden.serverutils.common.entities.Result;
|
||||||
|
import net.frankheijden.serverutils.common.managers.AbstractPluginManager;
|
||||||
|
import net.md_5.bungee.api.ProxyServer;
|
||||||
|
import net.md_5.bungee.api.plugin.Plugin;
|
||||||
|
import net.md_5.bungee.api.plugin.PluginDescription;
|
||||||
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
|
||||||
|
public class BungeePluginManager extends AbstractPluginManager<Plugin> {
|
||||||
|
|
||||||
|
private static final ProxyServer proxy = ProxyServer.getInstance();
|
||||||
|
private static final ServerUtils plugin = ServerUtils.getInstance();
|
||||||
|
|
||||||
|
private static BungeePluginManager instance;
|
||||||
|
|
||||||
|
public BungeePluginManager() {
|
||||||
|
instance = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BungeePluginManager get() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether a loaded plugin is a module.
|
||||||
|
* @param plugin The plugin to check.
|
||||||
|
* @return Whether or not it's a module.
|
||||||
|
*/
|
||||||
|
public static boolean isModule(Plugin plugin) {
|
||||||
|
return plugin.getFile().getParent().equalsIgnoreCase("modules");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether a loaded plugin is an actual plugin and not a module.
|
||||||
|
* @param plugin The plugin to check.
|
||||||
|
* @return Whether or not it's a plugin.
|
||||||
|
*/
|
||||||
|
public static boolean isPlugin(Plugin plugin) {
|
||||||
|
return !isModule(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BungeeLoadResult loadPlugin(String pluginFile) {
|
||||||
|
File file = getPluginFileExact(pluginFile);
|
||||||
|
if (!file.exists()) return new BungeeLoadResult(Result.NOT_EXISTS);
|
||||||
|
return loadPlugin(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BungeeLoadResult loadPlugin(File file) {
|
||||||
|
PluginDescription desc;
|
||||||
|
try {
|
||||||
|
desc = getPluginDescription(file);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
proxy.getLogger().log(Level.WARNING, "Could not load plugin from file " + file, ex);
|
||||||
|
return new BungeeLoadResult(Result.INVALID_DESCRIPTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
URL url = desc.getFile().toURI().toURL();
|
||||||
|
URLClassLoader loader = (URLClassLoader) RPluginClassLoader.newInstance(proxy, desc, url);
|
||||||
|
|
||||||
|
Class<?> main = loader.loadClass(desc.getMain());
|
||||||
|
Plugin plugin = (Plugin) main.getDeclaredConstructor().newInstance();
|
||||||
|
|
||||||
|
RPluginManager.getPlugins(proxy.getPluginManager()).put(desc.getName(), plugin);
|
||||||
|
plugin.onLoad();
|
||||||
|
proxy.getLogger().log(Level.INFO, "Loaded plugin {0} version {1} by {2}", new Object[] {
|
||||||
|
desc.getName(), desc.getVersion(), desc.getAuthor()
|
||||||
|
});
|
||||||
|
return new BungeeLoadResult(plugin);
|
||||||
|
} catch (Throwable th) {
|
||||||
|
proxy.getLogger().log(Level.WARNING, "Error loading plugin " + desc.getName(), th);
|
||||||
|
return new BungeeLoadResult(Result.ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result enablePlugin(Plugin plugin) {
|
||||||
|
PluginDescription desc = plugin.getDescription();
|
||||||
|
String name = desc.getName();
|
||||||
|
try {
|
||||||
|
plugin.onEnable();
|
||||||
|
Object[] args = new Object[] { name, desc.getVersion(), desc.getAuthor() };
|
||||||
|
proxy.getLogger().log(Level.INFO, "Enabled plugin {0} version {1} by {2}", args);
|
||||||
|
return Result.SUCCESS;
|
||||||
|
} catch (Throwable th) {
|
||||||
|
proxy.getLogger().log(Level.WARNING, "Exception encountered when loading plugin: " + name, th);
|
||||||
|
return Result.ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CloseableResult reloadPlugin(String pluginName) {
|
||||||
|
Plugin plugin = proxy.getPluginManager().getPlugin(pluginName);
|
||||||
|
if (plugin == null) return new CloseableResult(Result.NOT_ENABLED);
|
||||||
|
return reloadPlugin(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CloseableResult reloadPlugin(Plugin plugin) {
|
||||||
|
CloseableResult result = unloadPlugin(plugin);
|
||||||
|
if (result.getResult() != Result.SUCCESS) return result;
|
||||||
|
|
||||||
|
File file = getPluginFile(plugin.getDescription().getName());
|
||||||
|
if (file == null) return result.set(Result.FILE_DELETED);
|
||||||
|
|
||||||
|
BungeeLoadResult loadResult = loadPlugin(file);
|
||||||
|
if (!loadResult.isSuccess()) return result.set(loadResult.getResult());
|
||||||
|
|
||||||
|
return result.set(enablePlugin(loadResult.get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CloseableResult unloadPlugin(String pluginName) {
|
||||||
|
Plugin plugin = proxy.getPluginManager().getPlugin(pluginName);
|
||||||
|
if (plugin == null) return new CloseableResult(Result.NOT_ENABLED);
|
||||||
|
return unloadPlugin(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CloseableResult unloadPlugin(Plugin plugin) {
|
||||||
|
plugin.onDisable();
|
||||||
|
proxy.getPluginManager().unregisterCommands(plugin);
|
||||||
|
proxy.getPluginManager().unregisterListeners(plugin);
|
||||||
|
try {
|
||||||
|
RPluginManager.clearPlugin(proxy.getPluginManager(), plugin);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
return new CloseableResult(Result.ERROR);
|
||||||
|
}
|
||||||
|
return new CloseableResult(getCloseable(plugin));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static File getPluginFileExact(String fileName) {
|
||||||
|
return new File(proxy.getPluginsFolder(), fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the File of a plugin associated with a name.
|
||||||
|
* @param pluginName The plugin name to search for.
|
||||||
|
* @return The File if the plugin exists with that name.
|
||||||
|
*/
|
||||||
|
public File getPluginFile(String pluginName) {
|
||||||
|
for (File file : getPluginJars()) {
|
||||||
|
PluginDescription desc;
|
||||||
|
try {
|
||||||
|
desc = getPluginDescription(file);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (desc.getName().equals(pluginName)) return file;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getPluginFile(Plugin plugin) {
|
||||||
|
return plugin.getFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the PluginDescription of a (plugin's) File.
|
||||||
|
* @param file The file.
|
||||||
|
* @return The PluginDescription.
|
||||||
|
* @throws Exception Iff and I/O exception occurred, or notNullChecks failed.
|
||||||
|
*/
|
||||||
|
public static PluginDescription getPluginDescription(File file) throws Exception {
|
||||||
|
try (JarFile jar = new JarFile(file)) {
|
||||||
|
JarEntry entry = getPluginDescriptionEntry(jar);
|
||||||
|
Preconditions.checkNotNull(entry, "Plugin must have a plugin.yml or bungee.yml");
|
||||||
|
|
||||||
|
try (InputStream in = jar.getInputStream(entry)) {
|
||||||
|
Yaml yaml = RPluginManager.getYaml(proxy.getPluginManager());
|
||||||
|
PluginDescription desc = yaml.loadAs(in, PluginDescription.class);
|
||||||
|
Preconditions.checkNotNull(desc.getName(), "Plugin from %s has no name", file);
|
||||||
|
Preconditions.checkNotNull(desc.getMain(), "Plugin from %s has no main", file);
|
||||||
|
|
||||||
|
desc.setFile(file);
|
||||||
|
return desc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the JarEntry which contains the Description file of the JarFile.
|
||||||
|
* @param jar The JarFile.
|
||||||
|
* @return The description JarEntry.
|
||||||
|
*/
|
||||||
|
public static JarEntry getPluginDescriptionEntry(JarFile jar) {
|
||||||
|
JarEntry entry = jar.getJarEntry("bungee.yml");
|
||||||
|
if (entry == null) return jar.getJarEntry("plugin.yml");
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the closable classloader of the plugin, if possible.
|
||||||
|
* @param plugin The plugin.
|
||||||
|
* @return The closable instance.
|
||||||
|
*/
|
||||||
|
public static Closeable getCloseable(Plugin plugin) {
|
||||||
|
ClassLoader loader = plugin.getClass().getClassLoader();
|
||||||
|
if (loader instanceof Closeable) return (Closeable) loader;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getPluginsFolder() {
|
||||||
|
return plugin.getProxy().getPluginsFolder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Plugin> getPlugins() {
|
||||||
|
return getPlugins(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of plugins.
|
||||||
|
* @param modules Whether or not to include `module` plugins.
|
||||||
|
* @return The list of plugins.
|
||||||
|
*/
|
||||||
|
public List<Plugin> getPlugins(boolean modules) {
|
||||||
|
Collection<Plugin> plugins = plugin.getProxy().getPluginManager().getPlugins();
|
||||||
|
if (modules) return new ArrayList<>(plugins);
|
||||||
|
return plugins.stream()
|
||||||
|
.filter(BungeePluginManager::isPlugin)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPluginName(Plugin plugin) {
|
||||||
|
return plugin.getDataFolder().getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the plugins sorted by their names.
|
||||||
|
* @param modules Whether or not to include `module` plugins
|
||||||
|
* @return The sorted plugins.
|
||||||
|
*/
|
||||||
|
public List<Plugin> getPluginsSorted(boolean modules) {
|
||||||
|
List<Plugin> plugins = getPlugins(modules);
|
||||||
|
plugins.sort(Comparator.comparing(this::getPluginName));
|
||||||
|
return plugins;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.managers;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||||
|
import net.frankheijden.serverutils.common.managers.AbstractTaskManager;
|
||||||
|
import net.md_5.bungee.api.ProxyServer;
|
||||||
|
|
||||||
|
public class BungeeTaskManager extends AbstractTaskManager {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void runTaskAsynchronously(Runnable runnable) {
|
||||||
|
ProxyServer.getInstance().getScheduler().runAsync(ServerUtils.getInstance(), runnable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.managers;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||||
|
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
||||||
|
|
||||||
|
public class BungeeVersionManager extends AbstractVersionManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new VersionManager instance.
|
||||||
|
* Used for automatic updating.
|
||||||
|
*/
|
||||||
|
public BungeeVersionManager(ServerUtils plugin) {
|
||||||
|
super(plugin.getDescription().getVersion());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.reflection;
|
||||||
|
|
||||||
|
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||||
|
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
||||||
|
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class RPlugin {
|
||||||
|
|
||||||
|
private static Class<?> pluginClass;
|
||||||
|
private static Map<String, Field> fields;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
pluginClass = Class.forName("net.md_5.bungee.api.plugin.Plugin");
|
||||||
|
fields = getAllFields(pluginClass,
|
||||||
|
fieldOf("plugins", ALL_VERSIONS),
|
||||||
|
fieldOf("toLoad", ALL_VERSIONS),
|
||||||
|
fieldOf("commandsByPlugin", ALL_VERSIONS),
|
||||||
|
fieldOf("listenersByPlugin", ALL_VERSIONS));
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.reflection;
|
||||||
|
|
||||||
|
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.md_5.bungee.api.ProxyServer;
|
||||||
|
import net.md_5.bungee.api.plugin.PluginDescription;
|
||||||
|
|
||||||
|
public class RPluginClassLoader {
|
||||||
|
|
||||||
|
private static Class<?> loaderClass;
|
||||||
|
private static Map<String, Field> fields;
|
||||||
|
private static Constructor<?> constructor;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
loaderClass = Class.forName("net.md_5.bungee.api.plugin.PluginClassloader");
|
||||||
|
constructor = loaderClass.getDeclaredConstructor(ProxyServer.class, PluginDescription.class, URL[].class);
|
||||||
|
constructor.setAccessible(true);
|
||||||
|
fields = getAllFields(loaderClass);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Object newInstance(ProxyServer proxy, PluginDescription desc, URL... urls) throws Exception {
|
||||||
|
return constructor.newInstance(proxy, desc, urls);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,82 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.reflection;
|
||||||
|
|
||||||
|
import static net.frankheijden.serverutils.common.reflection.FieldParam.fieldOf;
|
||||||
|
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.get;
|
||||||
|
import static net.frankheijden.serverutils.common.reflection.ReflectionUtils.getAllFields;
|
||||||
|
import static net.frankheijden.serverutils.common.reflection.VersionParam.ALL_VERSIONS;
|
||||||
|
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.utils.MapUtils;
|
||||||
|
import net.md_5.bungee.api.plugin.Command;
|
||||||
|
import net.md_5.bungee.api.plugin.Plugin;
|
||||||
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
|
||||||
|
public class RPluginManager {
|
||||||
|
|
||||||
|
private static Class<?> pluginManagerClass;
|
||||||
|
private static Map<String, Field> fields;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
pluginManagerClass = Class.forName("net.md_5.bungee.api.plugin.PluginManager");
|
||||||
|
fields = getAllFields(pluginManagerClass,
|
||||||
|
fieldOf("yaml", ALL_VERSIONS),
|
||||||
|
fieldOf("plugins", ALL_VERSIONS),
|
||||||
|
fieldOf("commandMap", ALL_VERSIONS),
|
||||||
|
fieldOf("toLoad", ALL_VERSIONS),
|
||||||
|
fieldOf("commandsByPlugin", ALL_VERSIONS));
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the plugin from the PluginManager.
|
||||||
|
* @param instance The instance of the PluginManager.
|
||||||
|
* @param plugin The plugin to clear.
|
||||||
|
* @throws ReflectiveOperationException Iff a reflection error happened.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
public static void clearPlugin(Object instance, Plugin plugin) throws ReflectiveOperationException {
|
||||||
|
String pluginName = plugin.getDescription().getName();
|
||||||
|
MapUtils.remove((Map) get(fields, instance, "plugins"), pluginName);
|
||||||
|
MapUtils.remove((Map) get(fields, instance, "toLoad"), pluginName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static Map<String, Plugin> getPlugins(Object instance) throws ReflectiveOperationException {
|
||||||
|
return (Map<String, Plugin>) get(fields, instance, "plugins");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Yaml getYaml(Object instance) throws IllegalAccessException {
|
||||||
|
return (Yaml) get(fields, instance, "yaml");
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static Map<String, Command> getCommands(Object instance) throws IllegalAccessException {
|
||||||
|
return (Map<String, Command>) get(fields, instance, "commandMap");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the registered plugin of the command.
|
||||||
|
* @param instance The PluginManager instance.
|
||||||
|
* @param cmd The command to check the plugin of.
|
||||||
|
* @return The plugin of the command
|
||||||
|
* @throws IllegalAccessException Iff some reflection error occurred.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static Plugin getPlugin(Object instance, Command cmd) throws IllegalAccessException {
|
||||||
|
Object obj = get(fields, instance, "commandsByPlugin");
|
||||||
|
Multimap<Plugin, Command> plugins = (Multimap<Plugin, Command>) obj;
|
||||||
|
if (plugins == null) return null;
|
||||||
|
|
||||||
|
for (Map.Entry<Plugin, Command> entry : plugins.entries()) {
|
||||||
|
if (entry.getValue().equals(cmd)) return entry.getKey();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package net.frankheijden.serverutils.bungee.utils;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.bungee.entities.BungeeCommandSender;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import net.md_5.bungee.api.CommandSender;
|
||||||
|
|
||||||
|
public class BungeeUtils {
|
||||||
|
|
||||||
|
public static ServerCommandSender wrap(CommandSender sender) {
|
||||||
|
return new BungeeCommandSender(sender);
|
||||||
|
}
|
||||||
|
}
|
||||||
5
Bungee/src/main/resources/bungee-config.yml
Normal file
5
Bungee/src/main/resources/bungee-config.yml
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
settings:
|
||||||
|
check-updates: true
|
||||||
|
check-updates-login: false
|
||||||
|
download-updates: false
|
||||||
|
download-at-startup-and-update: false
|
||||||
56
Bungee/src/main/resources/bungee-messages.yml
Normal file
56
Bungee/src/main/resources/bungee-messages.yml
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
serverutils:
|
||||||
|
success: "&3Successfully %action%ed &b%what%&3!"
|
||||||
|
warning: "&3Successfully %action%ed &b%what%&3, but with warnings."
|
||||||
|
error: "&cAn error occurred while %action%ing &4%what%&c, please check the console!"
|
||||||
|
not_exists: "&cAn error occurred while %action%ing &4%what%&c, plugin does not exist!"
|
||||||
|
not_enabled: "&cAn error occurred while %action%ing &4%what%&c, plugin is not enabled!"
|
||||||
|
already_loaded: "&cAn error occurred while %action%ing &4%what%&c, plugin is already loaded!"
|
||||||
|
already_enabled: "&cAn error occurred while %action%ing &4%what%&c, plugin is already enabled!"
|
||||||
|
already_disabled: "&cAn error occurred while %action%ing &4%what%&c, plugin is already disabled!"
|
||||||
|
file_deleted: "&cAccessing the jar file while %action%ing &4%what%&c went wrong, plugin has been deleted!"
|
||||||
|
invalid_description: "&cAn error occurred while %action%ing &4%what%&c, plugin doesn't have a valid description, please check the console!"
|
||||||
|
invalid_plugin: "&cAn error occurred while %action%ing &4%what%&c, plugin is invalid!"
|
||||||
|
unknown_dependency: "&cAn error occurred while %action%ing &4%what%&c, plugin has a dependeny which is not loaded: &4%arg%"
|
||||||
|
update:
|
||||||
|
available: |-
|
||||||
|
&8&m---------=&r&8[ &b&lServerUtils Bungee Update&r &8]&m=----------
|
||||||
|
&3Current version: &b%old%
|
||||||
|
&3New version: &b%new%
|
||||||
|
&3Release info: &b%info%
|
||||||
|
&8&m-------------------------------------------------
|
||||||
|
downloading: |-
|
||||||
|
&8&m---------=&r&8[ &b&lServerUtils Bungee Update&r &8]&m=----------
|
||||||
|
&3A new version of ServerUtils will be downloaded and installed after a restart!
|
||||||
|
&3Current version: &b%old%
|
||||||
|
&3New version: &b%new%
|
||||||
|
&3Release info: &b%info%
|
||||||
|
&8&m-------------------------------------------------
|
||||||
|
download_failed: "&cFailed to download version %new% of ServerUtils. Please update manually."
|
||||||
|
download_success: "&3ServerUtils has been downloaded and will be installed on the next restart."
|
||||||
|
help:
|
||||||
|
header: "&8&m----------=&r&8[ &b&lServerUtils Bungee Help&r &8]&m=----------"
|
||||||
|
format: "&8/&3%command%&b%subcommand% &f(&7%help%&f)"
|
||||||
|
footer: "&8&m-------------------------------------------------"
|
||||||
|
plugins:
|
||||||
|
header: "&8&m---------=&r&8[ &b&lServerUtils Bungee Plugins&r &8]&m=--------"
|
||||||
|
prefix: " &3Plugins &8(&a%count%&8)&b: "
|
||||||
|
format: "&3%plugin%"
|
||||||
|
seperator: "&b, "
|
||||||
|
last_seperator: " &band "
|
||||||
|
version: " &8(&a%version%&8)"
|
||||||
|
footer: "&8&m-------------------------------------------------"
|
||||||
|
plugininfo:
|
||||||
|
header: "&8&m-------=&r&8[ &b&lServerUtils Bungee PluginInfo&r &8]&m=-------"
|
||||||
|
format: " &3%key%&8: &b%value%"
|
||||||
|
list_format: "&b%value%"
|
||||||
|
seperator: "&8, "
|
||||||
|
last_seperator: " &8and "
|
||||||
|
footer: "&8&m-------------------------------------------------"
|
||||||
|
commandinfo:
|
||||||
|
header: "&8&m------=&r&8[ &b&lServerUtils Bungee CommandInfo&r &8]&m=-------"
|
||||||
|
format: " &3%key%&8: &b%value%"
|
||||||
|
list_format: "&b%value%"
|
||||||
|
seperator: "&8, "
|
||||||
|
last_seperator: " &8and "
|
||||||
|
footer: "&8&m-------------------------------------------------"
|
||||||
|
not_exists: "&cThat command is not a valid registered command."
|
||||||
4
Bungee/src/main/resources/bungee.yml
Normal file
4
Bungee/src/main/resources/bungee.yml
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
name: ServerUtils
|
||||||
|
main: net.frankheijden.serverutils.bungee.ServerUtils
|
||||||
|
version: ${version}
|
||||||
|
author: FrankHeijden
|
||||||
|
|
@ -7,5 +7,5 @@ repositories {
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly 'com.google.code.gson:gson:2.8.6'
|
compileOnly 'com.google.code.gson:gson:2.8.0'
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
package net.frankheijden.serverutils.common;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.config.Config;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||||
|
import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask;
|
||||||
|
|
||||||
|
public class ServerUtilsApp<T> {
|
||||||
|
|
||||||
|
public static final int BSTATS_METRICS_ID = 7790;
|
||||||
|
|
||||||
|
private final T platformPlugin;
|
||||||
|
private final ServerUtilsPlugin plugin;
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
private static ServerUtilsApp instance;
|
||||||
|
|
||||||
|
private ServerUtilsApp(T platformPlugin, ServerUtilsPlugin plugin) {
|
||||||
|
this.platformPlugin = platformPlugin;
|
||||||
|
this.plugin = plugin;
|
||||||
|
instance = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> void init(T obj, ServerUtilsPlugin plugin) {
|
||||||
|
new ServerUtilsApp<>(obj, plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tries checking for updates if enabled by the config.
|
||||||
|
*/
|
||||||
|
public static void tryCheckForUpdates() {
|
||||||
|
if (Config.getInstance().getConfig().getBoolean("settings.check-updates")) {
|
||||||
|
UpdateCheckerTask.start(getPlugin().getChatProvider().getConsoleSender(), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ServerUtilsPlugin getPlugin() {
|
||||||
|
return instance.plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> T getPlatformPlugin() {
|
||||||
|
return (T) instance.platformPlugin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
package net.frankheijden.serverutils.common.commands;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.config.Messenger;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import net.frankheijden.serverutils.common.utils.ListBuilder;
|
||||||
|
import net.frankheijden.serverutils.common.utils.ListFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides some common utility methods for the Plugins command.
|
||||||
|
*/
|
||||||
|
public class Plugins {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a plugin list to the receiver.
|
||||||
|
* @param sender The receiver of the plugin list.
|
||||||
|
* @param plugins The plugins to be sent.
|
||||||
|
* @param pluginFormat The format of the plugins to be sent.
|
||||||
|
* @param <T> The plugin type.
|
||||||
|
*/
|
||||||
|
public static <T> void sendPlugins(ServerCommandSender sender, List<T> plugins, ListFormat<T> pluginFormat) {
|
||||||
|
Messenger.sendMessage(sender, "serverutils.plugins.header");
|
||||||
|
String prefix = Messenger.getMessage("serverutils.plugins.prefix",
|
||||||
|
"%count%", String.valueOf(plugins.size()));
|
||||||
|
sender.sendMessage(Messenger.color(prefix + ListBuilder.create(plugins)
|
||||||
|
.seperator(Messenger.getMessage("serverutils.plugins.seperator"))
|
||||||
|
.lastSeperator(Messenger.getMessage("serverutils.plugins.last_seperator"))
|
||||||
|
.format(pluginFormat)
|
||||||
|
.toString()));
|
||||||
|
Messenger.sendMessage(sender, "serverutils.plugins.footer");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package net.frankheijden.serverutils.common.config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The general common config class.
|
||||||
|
*/
|
||||||
|
public class Config extends YamlResource {
|
||||||
|
|
||||||
|
private static Config instance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new Config with the config file name and the resource name from the jar.
|
||||||
|
* @param fileName The file name in the data folder.
|
||||||
|
* @param resource The resource name in the jar file.
|
||||||
|
*/
|
||||||
|
public Config(String fileName, String resource) {
|
||||||
|
super(fileName, resource);
|
||||||
|
instance = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the current instance of the Config.
|
||||||
|
* @return The current instance.
|
||||||
|
*/
|
||||||
|
public static Config getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,84 @@
|
||||||
|
package net.frankheijden.serverutils.common.config;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||||
|
import net.frankheijden.serverutils.common.utils.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The general common messenger class.
|
||||||
|
*/
|
||||||
|
public class Messenger extends YamlResource {
|
||||||
|
|
||||||
|
private static Messenger instance;
|
||||||
|
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new Messenger with the messages file name and the resource name from the jar.
|
||||||
|
* @param fileName The file name in the data folder.
|
||||||
|
* @param resource The resource name in the jar file.
|
||||||
|
*/
|
||||||
|
public Messenger(String fileName, String resource) {
|
||||||
|
super(fileName, resource);
|
||||||
|
instance = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the current instance of the Messenger.
|
||||||
|
* @return The current instance.
|
||||||
|
*/
|
||||||
|
public static Messenger getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a message from the config.
|
||||||
|
* @param path The yml path to the message.
|
||||||
|
* @param replacements The replacements to be taken into account.
|
||||||
|
* @return The config message with translated placeholders.
|
||||||
|
*/
|
||||||
|
public static String getMessage(String path, String... replacements) {
|
||||||
|
String message = instance.getConfig().getString(path);
|
||||||
|
if (message != null) {
|
||||||
|
return StringUtils.apply(message, replacements);
|
||||||
|
} else {
|
||||||
|
Messenger.plugin.getLogger().severe("Missing locale in messages.yml at path '" + path + "'!");
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a message to a player with translated placeholders.
|
||||||
|
* @param sender The receiver.
|
||||||
|
* @param msg The message to be sent.
|
||||||
|
* @param replacements The replacements to be taken into account.
|
||||||
|
*/
|
||||||
|
public static void sendRawMessage(ServerCommandSender sender, String msg, String... replacements) {
|
||||||
|
String message = StringUtils.apply(msg, replacements);
|
||||||
|
if (message != null) {
|
||||||
|
sender.sendMessage(Messenger.plugin.getChatProvider().color(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a message from the specified config path to a player with translated placeholders.
|
||||||
|
* @param sender The receiver.
|
||||||
|
* @param path The yml path to the message.
|
||||||
|
* @param replacements The replacements to be taken into account.
|
||||||
|
*/
|
||||||
|
public static void sendMessage(ServerCommandSender sender, String path, String... replacements) {
|
||||||
|
String message = getMessage(path, replacements);
|
||||||
|
if (message != null) {
|
||||||
|
sender.sendMessage(Messenger.plugin.getChatProvider().color(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Colorizes the given string.
|
||||||
|
* @param str The string to color.
|
||||||
|
* @return The colored string.
|
||||||
|
*/
|
||||||
|
public static String color(String str) {
|
||||||
|
return Messenger.plugin.getChatProvider().color(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,95 @@
|
||||||
|
package net.frankheijden.serverutils.common.config;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A wrap for a Yaml Configuration file.
|
||||||
|
*/
|
||||||
|
public interface YamlConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the value at a given path.
|
||||||
|
* @param path The path.
|
||||||
|
* @return The object.
|
||||||
|
*/
|
||||||
|
Object get(String path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value to a path.
|
||||||
|
* @param path The path.
|
||||||
|
* @param value The object to set the path's value to.
|
||||||
|
*/
|
||||||
|
void set(String path, Object value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a string from a path.
|
||||||
|
* @param path The path.
|
||||||
|
* @return The string at given path.
|
||||||
|
*/
|
||||||
|
String getString(String path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a boolean from a path.
|
||||||
|
* @param path The path.
|
||||||
|
* @return The boolean at given path.
|
||||||
|
*/
|
||||||
|
boolean getBoolean(String path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the key nodes at the current level.
|
||||||
|
* @return The keys.
|
||||||
|
*/
|
||||||
|
Collection<? extends String> getKeys();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the YamlConfig to disk.
|
||||||
|
* @throws IOException Iff an I/O error occurred.
|
||||||
|
*/
|
||||||
|
void save() throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds defaults if keys don't exist to the configuration specified.
|
||||||
|
* @param def The defaults to copy values over from.
|
||||||
|
* @param conf The configuration to copy the defaults to.
|
||||||
|
*/
|
||||||
|
static void addDefaults(YamlConfig def, YamlConfig conf) {
|
||||||
|
addDefaults(def, conf, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds defaults if keys don't exist to the configuration specified.
|
||||||
|
* @param def The defaults to copy values over from.
|
||||||
|
* @param conf The configuration to copy the defaults to.
|
||||||
|
* @param root The current root path of the iteration.
|
||||||
|
*/
|
||||||
|
static void addDefaults(YamlConfig def, YamlConfig conf, String root) {
|
||||||
|
if (def == null) return;
|
||||||
|
for (String key : def.getKeys()) {
|
||||||
|
String newKey = (root.isEmpty() ? "" : root + ".") + key;
|
||||||
|
Object value = def.get(key);
|
||||||
|
if (value instanceof YamlConfig) {
|
||||||
|
addDefaults((YamlConfig) value, conf, newKey);
|
||||||
|
} else if (conf.get(newKey) == null) {
|
||||||
|
conf.set(newKey, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initiates a Configuration from a file with associated defaults.
|
||||||
|
* @param def The default Configuration to be applied.
|
||||||
|
* @param conf The Configuration where the defaults will be applied to.
|
||||||
|
* @return The loaded Configuration of the file with defaults.
|
||||||
|
*/
|
||||||
|
static YamlConfig init(YamlConfig def, YamlConfig conf) {
|
||||||
|
YamlConfig.addDefaults(def, conf);
|
||||||
|
|
||||||
|
try {
|
||||||
|
conf.save();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
return conf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
package net.frankheijden.serverutils.common.config;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||||
|
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class which provides functionality for loading and setting defaults of Yaml Configurations.
|
||||||
|
*/
|
||||||
|
public class YamlResource {
|
||||||
|
|
||||||
|
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();
|
||||||
|
|
||||||
|
private final YamlConfig config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new YamlResource instance.
|
||||||
|
* Loads the resource from the jar file.
|
||||||
|
* @param fileName The destination file.
|
||||||
|
* @param resource The resource from the jar file.
|
||||||
|
*/
|
||||||
|
public YamlResource(String fileName, String resource) {
|
||||||
|
ResourceProvider provider = plugin.getResourceProvider();
|
||||||
|
InputStream is = provider.getResource(resource);
|
||||||
|
File file = plugin.copyResourceIfNotExists(fileName, resource);
|
||||||
|
config = YamlConfig.init(provider.load(is), provider.load(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the YamlConfig of this resource.
|
||||||
|
* @return The YamlConfig.
|
||||||
|
*/
|
||||||
|
public YamlConfig getConfig() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
package net.frankheijden.serverutils.bukkit.managers;
|
package net.frankheijden.serverutils.common.entities;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A result which should be closed when done.
|
||||||
|
*/
|
||||||
public class CloseableResult implements Closeable {
|
public class CloseableResult implements Closeable {
|
||||||
|
|
||||||
private Result result;
|
private Result result;
|
||||||
|
|
@ -20,18 +23,35 @@ public class CloseableResult implements Closeable {
|
||||||
this.closeable = closeable;
|
this.closeable = closeable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new closable result with no closable instance.
|
||||||
|
* @param result The result of the procedure
|
||||||
|
*/
|
||||||
public CloseableResult(Result result) {
|
public CloseableResult(Result result) {
|
||||||
this(result, null);
|
this(result, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new closable result with a closable instance and success result.
|
||||||
|
* @param closeable The closable of the procedure.
|
||||||
|
*/
|
||||||
public CloseableResult(Closeable closeable) {
|
public CloseableResult(Closeable closeable) {
|
||||||
this(Result.SUCCESS, closeable);
|
this(Result.SUCCESS, closeable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the result.
|
||||||
|
* @return The result.
|
||||||
|
*/
|
||||||
public Result getResult() {
|
public Result getResult() {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the result of this instance.
|
||||||
|
* @param result The result to set.
|
||||||
|
* @return The current instance.
|
||||||
|
*/
|
||||||
public CloseableResult set(Result result) {
|
public CloseableResult set(Result result) {
|
||||||
this.result = result;
|
this.result = result;
|
||||||
return this;
|
return this;
|
||||||
|
|
@ -49,6 +69,10 @@ public class CloseableResult implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the closable.
|
||||||
|
* @throws IOException Iff an I/O error occurred.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
closeable.close();
|
closeable.close();
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
package net.frankheijden.serverutils.common.entities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A result which contains a loaded object from a load operation.
|
||||||
|
* @param <T> The loaded object type
|
||||||
|
*/
|
||||||
|
public class LoadResult<T> {
|
||||||
|
|
||||||
|
private final T obj;
|
||||||
|
private final Result result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new LoadResult with an object and a result.
|
||||||
|
* @param obj The object of the load operation.
|
||||||
|
* @param result The result of the load operation.
|
||||||
|
*/
|
||||||
|
public LoadResult(T obj, Result result) {
|
||||||
|
this.obj = obj;
|
||||||
|
this.result = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new LoadResult with an object and a success result.
|
||||||
|
* @param obj The object of the load operation.
|
||||||
|
*/
|
||||||
|
public LoadResult(T obj) {
|
||||||
|
this(obj, Result.SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new LoadResult without a loaded object, just a result.
|
||||||
|
* @param result The result of the load operation.
|
||||||
|
*/
|
||||||
|
public LoadResult(Result result) {
|
||||||
|
this(null, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the loaded object.
|
||||||
|
* @return The loaded object.
|
||||||
|
*/
|
||||||
|
public T get() {
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The result of the LoadResult.
|
||||||
|
* @return The result.
|
||||||
|
*/
|
||||||
|
public Result getResult() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the result is a success.
|
||||||
|
* @return Whether there is success or not.
|
||||||
|
*/
|
||||||
|
public boolean isSuccess() {
|
||||||
|
return obj != null && result == Result.SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
package net.frankheijden.serverutils.bukkit.managers;
|
package net.frankheijden.serverutils.common.entities;
|
||||||
|
|
||||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
import net.frankheijden.serverutils.common.config.Messenger;
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An enum containing possible results.
|
||||||
|
*/
|
||||||
public enum Result {
|
public enum Result {
|
||||||
NOT_EXISTS,
|
NOT_EXISTS,
|
||||||
NOT_ENABLED,
|
NOT_ENABLED,
|
||||||
|
|
@ -18,10 +20,18 @@ public enum Result {
|
||||||
|
|
||||||
private String arg;
|
private String arg;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* private constructor which initializes a result with an empty argument.
|
||||||
|
*/
|
||||||
Result() {
|
Result() {
|
||||||
this.arg = "";
|
this.arg = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the argument of the result's message.
|
||||||
|
* @param arg The argument
|
||||||
|
* @return The current instance.
|
||||||
|
*/
|
||||||
public Result arg(String arg) {
|
public Result arg(String arg) {
|
||||||
this.arg = arg;
|
this.arg = arg;
|
||||||
return this;
|
return this;
|
||||||
|
|
@ -34,7 +44,7 @@ public enum Result {
|
||||||
* @param action The action which let to the result.
|
* @param action The action which let to the result.
|
||||||
* @param what An associated variable.
|
* @param what An associated variable.
|
||||||
*/
|
*/
|
||||||
public void sendTo(CommandSender sender, String action, String what) {
|
public void sendTo(ServerCommandSender sender, String action, String what) {
|
||||||
Messenger.sendMessage(sender, "serverutils." + this.name().toLowerCase(),
|
Messenger.sendMessage(sender, "serverutils." + this.name().toLowerCase(),
|
||||||
"%action%", action,
|
"%action%", action,
|
||||||
"%what%", what,
|
"%what%", what,
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
package net.frankheijden.serverutils.common.entities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic wrapper for a CommandSender.
|
||||||
|
*/
|
||||||
|
public interface ServerCommandSender {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a message to a CommandSender.
|
||||||
|
* @param message The message to send.
|
||||||
|
*/
|
||||||
|
void sendMessage(String message);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the CommandSender has a permission.
|
||||||
|
* @param permission The permission to check.
|
||||||
|
* @return Whether or not they have the permission.
|
||||||
|
*/
|
||||||
|
boolean hasPermission(String permission);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
package net.frankheijden.serverutils.common.entities;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.managers.AbstractPluginManager;
|
||||||
|
import net.frankheijden.serverutils.common.managers.AbstractTaskManager;
|
||||||
|
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
||||||
|
import net.frankheijden.serverutils.common.providers.ChatProvider;
|
||||||
|
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||||
|
import net.frankheijden.serverutils.common.utils.FileUtils;
|
||||||
|
|
||||||
|
public abstract class ServerUtilsPlugin {
|
||||||
|
|
||||||
|
public abstract <T> AbstractPluginManager<T> getPluginManager();
|
||||||
|
|
||||||
|
public abstract AbstractTaskManager getTaskManager();
|
||||||
|
|
||||||
|
public abstract ResourceProvider getResourceProvider();
|
||||||
|
|
||||||
|
public abstract ChatProvider getChatProvider();
|
||||||
|
|
||||||
|
public abstract AbstractVersionManager getVersionManager();
|
||||||
|
|
||||||
|
public abstract Logger getLogger();
|
||||||
|
|
||||||
|
public abstract File getDataFolder();
|
||||||
|
|
||||||
|
public void createDataFolderIfNotExists() {
|
||||||
|
if (getDataFolder().exists()) return;
|
||||||
|
getDataFolder().mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies a resource from the jar to the specified target file name under the datafolder.
|
||||||
|
* @param targetName The target file under the datafolder.
|
||||||
|
* @param resource The resource from the jar file to copy.
|
||||||
|
* @return The target file.
|
||||||
|
*/
|
||||||
|
public File copyResourceIfNotExists(String targetName, String resource) {
|
||||||
|
createDataFolderIfNotExists();
|
||||||
|
|
||||||
|
File file = new File(getDataFolder(), targetName);
|
||||||
|
if (!file.exists()) {
|
||||||
|
getLogger().info(String.format("'%s' not found, creating!", targetName));
|
||||||
|
try {
|
||||||
|
FileUtils.saveResource(getResourceProvider().getResource(resource), file);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package net.frankheijden.serverutils.common.listeners;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.config.Config;
|
||||||
|
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask;
|
||||||
|
|
||||||
|
public class ServerListener {
|
||||||
|
|
||||||
|
private static final YamlConfig config = Config.getInstance().getConfig();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the update check on the given ServerCommandSender.
|
||||||
|
* @param sender The sender which triggered the update.
|
||||||
|
*/
|
||||||
|
public static void handleUpdate(ServerCommandSender sender) {
|
||||||
|
if (!config.getBoolean("settings.check-updates-login")) return;
|
||||||
|
|
||||||
|
if (sender.hasPermission("serverutils.notification.update")) {
|
||||||
|
UpdateCheckerTask.start(sender);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
package net.frankheijden.serverutils.common.managers;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||||
|
import net.frankheijden.serverutils.common.entities.LoadResult;
|
||||||
|
import net.frankheijden.serverutils.common.entities.Result;
|
||||||
|
import net.frankheijden.serverutils.common.providers.PluginProvider;
|
||||||
|
|
||||||
|
public abstract class AbstractPluginManager<T> extends PluginProvider<T> {
|
||||||
|
|
||||||
|
public abstract LoadResult<T> loadPlugin(String pluginFile);
|
||||||
|
|
||||||
|
public abstract LoadResult<T> loadPlugin(File file);
|
||||||
|
|
||||||
|
public abstract Result enablePlugin(T plugin);
|
||||||
|
|
||||||
|
public abstract CloseableResult reloadPlugin(String pluginName);
|
||||||
|
|
||||||
|
public abstract CloseableResult reloadPlugin(T plugin);
|
||||||
|
|
||||||
|
public abstract CloseableResult unloadPlugin(String pluginName);
|
||||||
|
|
||||||
|
public abstract CloseableResult unloadPlugin(T plugin);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
package net.frankheijden.serverutils.common.managers;
|
||||||
|
|
||||||
|
public abstract class AbstractTaskManager {
|
||||||
|
|
||||||
|
public abstract void runTaskAsynchronously(Runnable runnable);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
package net.frankheijden.serverutils.common.managers;
|
||||||
|
|
||||||
|
public abstract class AbstractVersionManager {
|
||||||
|
|
||||||
|
private final String currentVersion;
|
||||||
|
private String downloadedVersion;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new VersionManager instance.
|
||||||
|
* Used for automatic updating.
|
||||||
|
* @param currentVersion The current version of the plugin.
|
||||||
|
*/
|
||||||
|
public AbstractVersionManager(String currentVersion) {
|
||||||
|
this.currentVersion = currentVersion;
|
||||||
|
this.downloadedVersion = currentVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCurrentVersion() {
|
||||||
|
return currentVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDownloadedVersion() {
|
||||||
|
return downloadedVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasDownloaded() {
|
||||||
|
return !downloadedVersion.equals(currentVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDownloaded(String version) {
|
||||||
|
return downloadedVersion.equals(version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDownloaded(String version) {
|
||||||
|
this.downloadedVersion = version;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
package net.frankheijden.serverutils.common.providers;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A basic chat provider class.
|
||||||
|
*/
|
||||||
|
public abstract class ChatProvider {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the console sender of a server instance.
|
||||||
|
* @return The console sender.
|
||||||
|
*/
|
||||||
|
public abstract ServerCommandSender getConsoleSender();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Colorizes the given string.
|
||||||
|
* @param str The string to color.
|
||||||
|
* @return The colored string.
|
||||||
|
*/
|
||||||
|
public abstract String color(String str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Broadcasts a message over a server instance.
|
||||||
|
* @param permission The permission the receivers need to have.
|
||||||
|
* @param message The message to broadcast.
|
||||||
|
*/
|
||||||
|
public abstract void broadcast(String permission, String message);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
package net.frankheijden.serverutils.common.providers;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public abstract class PluginProvider<T> {
|
||||||
|
|
||||||
|
public abstract File getPluginsFolder();
|
||||||
|
|
||||||
|
public abstract List<T> getPlugins();
|
||||||
|
|
||||||
|
public abstract String getPluginName(T plugin);
|
||||||
|
|
||||||
|
public abstract File getPluginFile(T plugin);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of plugins, sorted by name.
|
||||||
|
* @return The list of plugins.
|
||||||
|
*/
|
||||||
|
public List<T> getPluginsSorted() {
|
||||||
|
List<T> plugins = getPlugins();
|
||||||
|
plugins.sort(Comparator.comparing(this::getPluginName));
|
||||||
|
return plugins;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of plugin names.
|
||||||
|
* @return The plugin names.
|
||||||
|
*/
|
||||||
|
public List<String> getPluginNames() {
|
||||||
|
return getPlugins().stream()
|
||||||
|
.map(this::getPluginName)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves all files with a jar extension in the plugins/ folder and returns solely their name.
|
||||||
|
* @return An list of jar file names.
|
||||||
|
*/
|
||||||
|
public List<String> getPluginFileNames() {
|
||||||
|
return Arrays.stream(getPluginJars())
|
||||||
|
.map(File::getName)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves all files with a jar extension in the plugins/ folder.
|
||||||
|
* @return An array of jar files.
|
||||||
|
*/
|
||||||
|
public File[] getPluginJars() {
|
||||||
|
File parent = getPluginsFolder();
|
||||||
|
if (parent == null || !parent.exists()) return new File[0];
|
||||||
|
return parent.listFiles(f -> f.getName().endsWith(".jar"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package net.frankheijden.serverutils.common.providers;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||||
|
|
||||||
|
public interface ResourceProvider {
|
||||||
|
|
||||||
|
InputStream getResource(String resource);
|
||||||
|
|
||||||
|
YamlConfig load(InputStream is);
|
||||||
|
|
||||||
|
YamlConfig load(File file);
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package net.frankheijden.serverutils.bukkit.tasks;
|
package net.frankheijden.serverutils.common.tasks;
|
||||||
|
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
|
|
@ -7,30 +7,29 @@ import com.google.gson.JsonObject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||||
import net.frankheijden.serverutils.bukkit.config.Config;
|
import net.frankheijden.serverutils.common.config.Config;
|
||||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
import net.frankheijden.serverutils.common.config.Messenger;
|
||||||
import net.frankheijden.serverutils.bukkit.managers.CloseableResult;
|
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||||
import net.frankheijden.serverutils.bukkit.managers.PluginManager;
|
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||||
import net.frankheijden.serverutils.bukkit.managers.VersionManager;
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||||
|
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
||||||
import net.frankheijden.serverutils.common.utils.FileUtils;
|
import net.frankheijden.serverutils.common.utils.FileUtils;
|
||||||
import net.frankheijden.serverutils.common.utils.VersionUtils;
|
import net.frankheijden.serverutils.common.utils.VersionUtils;
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
|
|
||||||
public class UpdateCheckerTask implements Runnable {
|
public class UpdateCheckerTask implements Runnable {
|
||||||
|
|
||||||
private static final ServerUtils plugin = ServerUtils.getInstance();
|
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();
|
||||||
private static final VersionManager versionManager = VersionManager.getInstance();
|
private static final YamlConfig config = Config.getInstance().getConfig();
|
||||||
private final CommandSender sender;
|
|
||||||
private final String currentVersion;
|
private final AbstractVersionManager versionManager;
|
||||||
|
private final ServerCommandSender sender;
|
||||||
private final boolean startup;
|
private final boolean startup;
|
||||||
|
|
||||||
private static final String GITHUB_LINK = "https://api.github.com/repos/FrankHeijden/ServerUtils/releases/latest";
|
private static final String GITHUB_LINK = "https://api.github.com/repos/FrankHeijden/ServerUtils/releases/latest";
|
||||||
|
|
@ -47,19 +46,19 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
private static final String DOWNLOADED_RESTART = "Downloaded ServerUtils version v%s. Restarting plugin now...";
|
private static final String DOWNLOADED_RESTART = "Downloaded ServerUtils version v%s. Restarting plugin now...";
|
||||||
private static final String UP_TO_DATE = "We are up-to-date!";
|
private static final String UP_TO_DATE = "We are up-to-date!";
|
||||||
|
|
||||||
private UpdateCheckerTask(CommandSender sender, boolean startup) {
|
private UpdateCheckerTask(ServerCommandSender sender, boolean startup) {
|
||||||
|
this.versionManager = plugin.getVersionManager();
|
||||||
this.sender = sender;
|
this.sender = sender;
|
||||||
this.currentVersion = plugin.getDescription().getVersion();
|
|
||||||
this.startup = startup;
|
this.startup = startup;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void start(CommandSender sender) {
|
public static void start(ServerCommandSender sender) {
|
||||||
start(sender, false);
|
start(sender, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void start(CommandSender sender, boolean startup) {
|
public static void start(ServerCommandSender sender, boolean startup) {
|
||||||
UpdateCheckerTask task = new UpdateCheckerTask(sender, startup);
|
UpdateCheckerTask task = new UpdateCheckerTask(sender, startup);
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, task);
|
plugin.getTaskManager().runTaskAsynchronously(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isStartupCheck() {
|
public boolean isStartupCheck() {
|
||||||
|
|
@ -100,7 +99,9 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
String body = jsonObject.getAsJsonPrimitive("body").getAsString();
|
String body = jsonObject.getAsJsonPrimitive("body").getAsString();
|
||||||
String downloadUrl = getDownloadUrl(jsonObject);
|
String downloadUrl = getDownloadUrl(jsonObject);
|
||||||
|
|
||||||
if (VersionUtils.isNewVersion(currentVersion, githubVersion)) {
|
String downloaded = versionManager.getDownloadedVersion();
|
||||||
|
String current = versionManager.getCurrentVersion();
|
||||||
|
if (VersionUtils.isNewVersion(downloaded, githubVersion)) {
|
||||||
if (isStartupCheck()) {
|
if (isStartupCheck()) {
|
||||||
plugin.getLogger().info(String.format(UPDATE_AVAILABLE, githubVersion));
|
plugin.getLogger().info(String.format(UPDATE_AVAILABLE, githubVersion));
|
||||||
plugin.getLogger().info("Release info: " + body);
|
plugin.getLogger().info("Release info: " + body);
|
||||||
|
|
@ -110,7 +111,7 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
plugin.getLogger().info(String.format(DOWNLOAD_START, downloadUrl));
|
plugin.getLogger().info(String.format(DOWNLOAD_START, downloadUrl));
|
||||||
} else {
|
} else {
|
||||||
Messenger.sendMessage(sender, "serverutils.update.downloading",
|
Messenger.sendMessage(sender, "serverutils.update.downloading",
|
||||||
"%old%", currentVersion,
|
"%old%", current,
|
||||||
"%new%", githubVersion,
|
"%new%", githubVersion,
|
||||||
"%info%", body);
|
"%info%", body);
|
||||||
}
|
}
|
||||||
|
|
@ -118,7 +119,7 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
tryReloadPlugin();
|
tryReloadPlugin();
|
||||||
} else if (!isStartupCheck()) {
|
} else if (!isStartupCheck()) {
|
||||||
Messenger.sendMessage(sender, "serverutils.update.available",
|
Messenger.sendMessage(sender, "serverutils.update.available",
|
||||||
"%old%", currentVersion,
|
"%old%", current,
|
||||||
"%new%", githubVersion,
|
"%new%", githubVersion,
|
||||||
"%info%", body);
|
"%info%", body);
|
||||||
}
|
}
|
||||||
|
|
@ -143,12 +144,12 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean canDownloadPlugin() {
|
private boolean canDownloadPlugin() {
|
||||||
if (isStartupCheck()) return Config.getInstance().getBoolean("settings.download-at-startup-and-update");
|
if (isStartupCheck()) return config.getBoolean("settings.download-at-startup-and-update");
|
||||||
return Config.getInstance().getBoolean("settings.download-updates");
|
return config.getBoolean("settings.download-updates");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void downloadPlugin(String githubVersion, String downloadLink) {
|
private void downloadPlugin(String githubVersion, String downloadLink) {
|
||||||
if (versionManager.isDownloadedVersion(githubVersion)) {
|
if (versionManager.isDownloaded(githubVersion)) {
|
||||||
broadcastDownloadStatus(githubVersion, false);
|
broadcastDownloadStatus(githubVersion, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -158,14 +159,15 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File pluginFile = plugin.getPluginManager().getPluginFile(ServerUtilsApp.getPlatformPlugin());
|
||||||
try {
|
try {
|
||||||
FileUtils.download(downloadLink, getPluginFile());
|
FileUtils.download(downloadLink, pluginFile);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
broadcastDownloadStatus(githubVersion, true);
|
broadcastDownloadStatus(githubVersion, true);
|
||||||
throw new RuntimeException(DOWNLOAD_ERROR, ex);
|
throw new RuntimeException(DOWNLOAD_ERROR, ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
versionManager.setDownloadedVersion(githubVersion);
|
versionManager.setDownloaded(githubVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryReloadPlugin() {
|
private void tryReloadPlugin() {
|
||||||
|
|
@ -173,7 +175,7 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
|
|
||||||
if (isStartupCheck()) {
|
if (isStartupCheck()) {
|
||||||
plugin.getLogger().info(String.format(DOWNLOADED_RESTART, downloadedVersion));
|
plugin.getLogger().info(String.format(DOWNLOADED_RESTART, downloadedVersion));
|
||||||
CloseableResult result = PluginManager.reloadPlugin(plugin);
|
CloseableResult result = plugin.getPluginManager().reloadPlugin(ServerUtilsApp.getPlatformPlugin());
|
||||||
plugin.getLogger().info(String.format(UPGRADE_SUCCESS, downloadedVersion));
|
plugin.getLogger().info(String.format(UPGRADE_SUCCESS, downloadedVersion));
|
||||||
result.tryClose();
|
result.tryClose();
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -183,20 +185,7 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
|
|
||||||
private void broadcastDownloadStatus(String githubVersion, boolean isError) {
|
private void broadcastDownloadStatus(String githubVersion, boolean isError) {
|
||||||
final String path = "serverutils.update." + (isError ? "failed" : "success");
|
final String path = "serverutils.update." + (isError ? "failed" : "success");
|
||||||
Bukkit.getOnlinePlayers().forEach((p) -> {
|
String message = Messenger.getMessage(path,"%new%", githubVersion);
|
||||||
if (p.hasPermission("serverutils.notification.update")) {
|
plugin.getChatProvider().broadcast("serverutils.notification.update", message);
|
||||||
Messenger.sendMessage(sender, path, "%new%", githubVersion);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private File getPluginFile() {
|
|
||||||
try {
|
|
||||||
Method method = JavaPlugin.class.getDeclaredMethod("getFile");
|
|
||||||
method.setAccessible(true);
|
|
||||||
return (File) method.invoke(plugin);
|
|
||||||
} catch (ReflectiveOperationException ex) {
|
|
||||||
throw new RuntimeException("Error retrieving current plugin file", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -14,6 +14,7 @@ import java.net.URL;
|
||||||
import java.nio.channels.Channels;
|
import java.nio.channels.Channels;
|
||||||
import java.nio.channels.ReadableByteChannel;
|
import java.nio.channels.ReadableByteChannel;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
|
||||||
public class FileUtils {
|
public class FileUtils {
|
||||||
|
|
||||||
|
|
@ -80,4 +81,17 @@ public class FileUtils {
|
||||||
return new JsonParser().parse(jsonText);
|
return new JsonParser().parse(jsonText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves an InputStream to a file.
|
||||||
|
* @param in The InputStream.
|
||||||
|
* @param target The target file.
|
||||||
|
* @return Whether or not file was created / already exists.
|
||||||
|
* @throws IOException If an I/O exception occurs.
|
||||||
|
*/
|
||||||
|
public static boolean saveResource(InputStream in, File target) throws IOException {
|
||||||
|
if (target.exists()) return false;
|
||||||
|
Files.copy(in, target.toPath());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
package net.frankheijden.serverutils.bukkit.utils;
|
package net.frankheijden.serverutils.common.utils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
import net.frankheijden.serverutils.common.config.Messenger;
|
||||||
import org.bukkit.command.CommandSender;
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
|
|
||||||
public class FormatBuilder {
|
public class FormatBuilder {
|
||||||
|
|
||||||
|
|
@ -43,13 +44,13 @@ public class FormatBuilder {
|
||||||
* Builds the format and sends it to the CommandSender.
|
* Builds the format and sends it to the CommandSender.
|
||||||
* @param sender The receiver of the list.
|
* @param sender The receiver of the list.
|
||||||
*/
|
*/
|
||||||
public void sendTo(CommandSender sender) {
|
public void sendTo(ServerCommandSender sender) {
|
||||||
valueList.forEach(values -> {
|
valueList.forEach(values -> {
|
||||||
int length = Math.min(values.length, orderedKeys.length);
|
int length = Math.min(values.length, orderedKeys.length);
|
||||||
String message = format;
|
String message = format;
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
String value = values[i];
|
String value = values[i];
|
||||||
if ((value == null || value.isEmpty()) && !alwaysSend) return;
|
if (value == null && !alwaysSend) return;
|
||||||
message = message.replace(orderedKeys[i], String.valueOf(value));
|
message = message.replace(orderedKeys[i], String.valueOf(value));
|
||||||
}
|
}
|
||||||
Messenger.sendRawMessage(sender, message);
|
Messenger.sendRawMessage(sender, message);
|
||||||
|
|
@ -1,27 +1,29 @@
|
||||||
package net.frankheijden.serverutils.bukkit.utils;
|
package net.frankheijden.serverutils.common.utils;
|
||||||
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.LogRecord;
|
import java.util.logging.LogRecord;
|
||||||
|
|
||||||
import net.frankheijden.serverutils.common.utils.PredicateFilter;
|
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
public class ForwardFilter extends PredicateFilter {
|
public class ForwardFilter extends PredicateFilter {
|
||||||
|
|
||||||
|
private static final char INFO_COLOR = 'a';
|
||||||
|
private static final char WARNING_COLOR = '6';
|
||||||
|
private static final char SEVERE_COLOR = 'c';
|
||||||
|
|
||||||
private boolean warnings;
|
private boolean warnings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a filter which forwards all output to the sender.
|
* Creates a filter which forwards all output to the sender.
|
||||||
* @param sender The sender to forward logs to.
|
* @param sender The sender to forward logs to.
|
||||||
*/
|
*/
|
||||||
public ForwardFilter(CommandSender sender) {
|
public ForwardFilter(ServerCommandSender sender) {
|
||||||
this.warnings = false;
|
this.warnings = false;
|
||||||
|
|
||||||
setPredicate(rec -> {
|
setPredicate(rec -> {
|
||||||
ChatColor color = getColor(rec.getLevel());
|
char color = getColor(rec.getLevel());
|
||||||
if (color != ChatColor.GREEN) warnings = true;
|
if (color != INFO_COLOR) warnings = true;
|
||||||
sender.sendMessage(color + format(rec));
|
sender.sendMessage('&' + color + format(rec));
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -30,13 +32,13 @@ public class ForwardFilter extends PredicateFilter {
|
||||||
return warnings;
|
return warnings;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ChatColor getColor(Level level) {
|
private static char getColor(Level level) {
|
||||||
if (Level.SEVERE.equals(level)) {
|
if (Level.SEVERE.equals(level)) {
|
||||||
return ChatColor.RED;
|
return SEVERE_COLOR;
|
||||||
} else if (Level.WARNING.equals(level)) {
|
} else if (Level.WARNING.equals(level)) {
|
||||||
return ChatColor.GOLD;
|
return WARNING_COLOR;
|
||||||
}
|
}
|
||||||
return ChatColor.GREEN;
|
return INFO_COLOR;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String format(LogRecord record) {
|
private static String format(LogRecord record) {
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
package net.frankheijden.serverutils.common.utils;
|
package net.frankheijden.serverutils.common.utils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ListBuilder<T> {
|
public class ListBuilder<T> {
|
||||||
|
|
@ -17,6 +20,15 @@ public class ListBuilder<T> {
|
||||||
return new ListBuilder<>(list);
|
return new ListBuilder<>(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> ListBuilder<T> create(Collection<T> list) {
|
||||||
|
return new ListBuilder<>(new ArrayList<>(list));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public static <T> ListBuilder<T> create(T... elements) {
|
||||||
|
return new ListBuilder<>(Arrays.asList(elements));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a pre-defined ListBuilder with type String.
|
* Creates a pre-defined ListBuilder with type String.
|
||||||
* @param list The collection to be used.
|
* @param list The collection to be used.
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ public class MapUtils {
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
public static void removeKeys(Map map, Predicate<Object> predicate) {
|
public static void removeKeys(Map map, Predicate<Object> predicate) {
|
||||||
|
if (map == null) return;
|
||||||
Set<Object> keysToRemove = new HashSet<>();
|
Set<Object> keysToRemove = new HashSet<>();
|
||||||
map.forEach((k, v) -> {
|
map.forEach((k, v) -> {
|
||||||
if (predicate.test(k)) {
|
if (predicate.test(k)) {
|
||||||
|
|
@ -30,12 +31,24 @@ public class MapUtils {
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
public static void removeValues(Map map, Predicate<Object> predicate) {
|
public static void removeValues(Map map, Predicate<Object> predicate) {
|
||||||
Set<Object> keysToRemove = new HashSet<>();
|
if (map == null) return;
|
||||||
|
Set<Object> valuesToRemove = new HashSet<>();
|
||||||
map.forEach((k, v) -> {
|
map.forEach((k, v) -> {
|
||||||
if (predicate.test(v)) {
|
if (predicate.test(v)) {
|
||||||
keysToRemove.add(k);
|
valuesToRemove.add(k);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
keysToRemove.forEach(map::remove);
|
valuesToRemove.forEach(map::remove);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a key from a map.
|
||||||
|
* @param map The map instance.
|
||||||
|
* @param obj The object to remove.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
public static void remove(Map map, Object obj) {
|
||||||
|
if (map == null) return;
|
||||||
|
map.remove(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
package net.frankheijden.serverutils.common.utils;
|
||||||
|
|
||||||
|
public class StringUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies placeholders to a message.
|
||||||
|
* @param message The message.
|
||||||
|
* @param replacements The replacements of the message. Expects input to be even and in a key-value like format.
|
||||||
|
* Example: ["%player%", "Player"]
|
||||||
|
* @return The message with translated placeholders.
|
||||||
|
*/
|
||||||
|
public static String apply(String message, String... replacements) {
|
||||||
|
if (message == null || message.isEmpty()) return null;
|
||||||
|
message = message.replace("\\n", "\n");
|
||||||
|
for (int i = 0; i < replacements.length; i++, i++) {
|
||||||
|
message = message.replace(replacements[i], replacements[i + 1]);
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
12
build.gradle
12
build.gradle
|
|
@ -4,7 +4,8 @@ plugins {
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'net.frankheijden.serverutils'
|
group = 'net.frankheijden.serverutils'
|
||||||
version = '1.5.3'
|
String dependencyDir = group + '.dependencies'
|
||||||
|
version = '2.0.0'
|
||||||
|
|
||||||
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
|
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
|
||||||
|
|
||||||
|
|
@ -50,9 +51,14 @@ subprojects {
|
||||||
shadowJar.dependsOn checkstyleMain, checkstyleTest, test
|
shadowJar.dependsOn checkstyleMain, checkstyleTest, test
|
||||||
}
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile project(path: ':Common', configuration: 'shadow')
|
implementation project(path: ':Common', configuration: 'shadow')
|
||||||
compile project(path: ':Bukkit', configuration: 'shadow')
|
implementation project(path: ':Bukkit', configuration: 'shadow')
|
||||||
|
implementation project(path: ':Bungee', configuration: 'shadow')
|
||||||
}
|
}
|
||||||
|
|
||||||
build.dependsOn shadowJar
|
build.dependsOn shadowJar
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,2 @@
|
||||||
rootProject.name = 'ServerUtils'
|
rootProject.name = 'ServerUtils'
|
||||||
include 'Common', 'Bukkit'
|
include 'Common', 'Bukkit', 'Bungee'
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue