diff --git a/build.gradle b/build.gradle index 3fe80e8..c65ab46 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ plugins { id 'java' + id 'checkstyle' id 'com.github.johnrengelman.shadow' version '5.2.0' } @@ -39,4 +40,13 @@ shadowJar { relocate 'org.bstats.bukkit', packagePath + '.dependencies.bstats' } +checkstyle { + toolVersion "8.25" + configFile = rootProject.file('config/checkstyle/checkstyle.xml') + + ignoreFailures = false + maxErrors = 0 + maxWarnings = 0 +} + build.dependsOn shadowJar \ No newline at end of file diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 0000000..0395a5d --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,268 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/net/frankheijden/serverutils/ServerUtils.java b/src/main/java/net/frankheijden/serverutils/ServerUtils.java index c8bbb25..9397661 100644 --- a/src/main/java/net/frankheijden/serverutils/ServerUtils.java +++ b/src/main/java/net/frankheijden/serverutils/ServerUtils.java @@ -1,26 +1,32 @@ package net.frankheijden.serverutils; +import co.aikar.commands.BukkitCommandCompletionContext; +import co.aikar.commands.CommandCompletions; import co.aikar.commands.PaperCommandManager; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; import net.frankheijden.serverutils.commands.CommandPlugins; import net.frankheijden.serverutils.commands.CommandServerUtils; import net.frankheijden.serverutils.config.Config; import net.frankheijden.serverutils.config.Messenger; import net.frankheijden.serverutils.listeners.MainListener; import net.frankheijden.serverutils.managers.VersionManager; -import net.frankheijden.serverutils.reflection.*; +import net.frankheijden.serverutils.reflection.RCommandMap; +import net.frankheijden.serverutils.reflection.RCraftServer; import net.frankheijden.serverutils.tasks.UpdateCheckerTask; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; -import org.bukkit.command.*; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; import org.bukkit.command.defaults.PluginsCommand; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; -import java.io.File; -import java.lang.reflect.InvocationTargetException; -import java.util.*; -import java.util.stream.Collectors; - public class ServerUtils extends JavaPlugin implements CommandExecutor { private static final int BSTATS_METRICS_ID = 7790; @@ -44,14 +50,11 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor { commandManager.registerCommand(new CommandServerUtils()); this.commandPlugins = null; - commandManager.getCommandCompletions().registerAsyncCompletion("plugins", context -> Arrays.stream(Bukkit.getPluginManager().getPlugins()) - .map(Plugin::getName) - .collect(Collectors.toList())); - commandManager.getCommandCompletions().registerAsyncCompletion("pluginJars", context -> Arrays.stream(getJars()) - .map(File::getName) - .collect(Collectors.toList())); - commandManager.getCommandCompletions().registerAsyncCompletion("supportedConfigs", context -> CommandServerUtils.getSupportedConfigs()); - commandManager.getCommandCompletions().registerAsyncCompletion("commands", context -> { + CommandCompletions completions = commandManager.getCommandCompletions(); + completions.registerAsyncCompletion("plugins", context -> getPluginNames()); + completions.registerAsyncCompletion("pluginJars", context -> getPluginFileNames()); + completions.registerAsyncCompletion("supportedConfigs", context -> CommandServerUtils.getSupportedConfigs()); + completions.registerAsyncCompletion("commands", context -> { try { return RCommandMap.getKnownCommands(RCraftServer.getCommandMap()).keySet(); } catch (IllegalAccessException | InvocationTargetException ex) { @@ -73,6 +76,18 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor { restoreBukkitPluginCommand(); } + private List getPluginNames() { + return Arrays.stream(Bukkit.getPluginManager().getPlugins()) + .map(Plugin::getName) + .collect(Collectors.toList()); + } + + private List getPluginFileNames() { + return Arrays.stream(getJars()) + .map(File::getName) + .collect(Collectors.toList()); + } + private void removeCommands(String... commands) { Map map; try { @@ -91,6 +106,10 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor { RCraftServer.getCommandMap().register("bukkit", new PluginsCommand("plugins")); } + /** + * Reloads the configurations of the plugin. + * Also makes sure the bukkit /pl command gets restored. + */ public void reload() { if (commandPlugins != null) { commandManager.unregisterCommand(commandPlugins); diff --git a/src/main/java/net/frankheijden/serverutils/commands/CommandPlugins.java b/src/main/java/net/frankheijden/serverutils/commands/CommandPlugins.java index b5c9a6c..02e4f16 100644 --- a/src/main/java/net/frankheijden/serverutils/commands/CommandPlugins.java +++ b/src/main/java/net/frankheijden/serverutils/commands/CommandPlugins.java @@ -1,7 +1,14 @@ package net.frankheijden.serverutils.commands; import co.aikar.commands.BaseCommand; -import co.aikar.commands.annotation.*; +import co.aikar.commands.annotation.CommandAlias; +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.Arrays; +import java.util.Comparator; +import java.util.List; import net.frankheijden.serverutils.config.Messenger; import net.frankheijden.serverutils.utils.ListBuilder; import net.frankheijden.serverutils.utils.ListFormat; @@ -9,11 +16,13 @@ import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; -import java.util.*; - @CommandAlias("plugins|pl") public class CommandPlugins extends BaseCommand { + /** + * Sends the plugin list to the sender, without plugin version. + * @param sender The sender of the command. + */ @Default @CommandPermission("serverutils.plugins") @Description("Shows the plugins of this server.") @@ -24,13 +33,18 @@ public class CommandPlugins extends BaseCommand { }); } + /** + * Sends the plugin list to the sender, with plugin version. + * @param sender The sender of the command. + */ @Subcommand("-v") @CommandPermission("serverutils.plugins.version") @Description("Shows the plugins of this server with version.") public void onPluginsWithVersion(CommandSender sender) { sendPlugins(sender, pl -> { String format = "serverutils.plugins.format" + (pl.isEnabled() ? "" : "_disabled"); - String version = Messenger.getMessage("serverutils.plugins.version", "%version%", pl.getDescription().getVersion()); + String version = Messenger.getMessage("serverutils.plugins.version", + "%version%", pl.getDescription().getVersion()); return Messenger.getMessage(format, "%plugin%", pl.getName()) + version; }); } diff --git a/src/main/java/net/frankheijden/serverutils/commands/CommandServerUtils.java b/src/main/java/net/frankheijden/serverutils/commands/CommandServerUtils.java index d6daff7..c460475 100644 --- a/src/main/java/net/frankheijden/serverutils/commands/CommandServerUtils.java +++ b/src/main/java/net/frankheijden/serverutils/commands/CommandServerUtils.java @@ -1,27 +1,46 @@ package net.frankheijden.serverutils.commands; -import co.aikar.commands.BaseCommand; -import co.aikar.commands.annotation.*; -import net.frankheijden.serverutils.ServerUtils; -import net.frankheijden.serverutils.config.Messenger; -import net.frankheijden.serverutils.managers.*; -import net.frankheijden.serverutils.reflection.*; -import net.frankheijden.serverutils.utils.*; -import org.bukkit.Bukkit; -import org.bukkit.command.*; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.PluginDescriptionFile; - -import java.util.*; - import static net.frankheijden.serverutils.config.Messenger.sendMessage; import static net.frankheijden.serverutils.reflection.ReflectionUtils.MINOR; +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.Dependency; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Subcommand; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import net.frankheijden.serverutils.ServerUtils; +import net.frankheijden.serverutils.config.Messenger; +import net.frankheijden.serverutils.managers.LoadResult; +import net.frankheijden.serverutils.managers.PluginManager; +import net.frankheijden.serverutils.managers.Result; +import net.frankheijden.serverutils.reflection.RCraftServer; +import net.frankheijden.serverutils.utils.FormatBuilder; +import net.frankheijden.serverutils.utils.ForwardFilter; +import net.frankheijden.serverutils.utils.ListBuilder; +import net.frankheijden.serverutils.utils.ListFormat; +import net.frankheijden.serverutils.utils.ReloadHandler; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginIdentifiableCommand; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginDescriptionFile; + @CommandAlias("serverutils|su") public class CommandServerUtils extends BaseCommand { private static final Set ALIASES; private static final Map supportedConfigs; + static { ALIASES = new HashSet<>(); ALIASES.add("serverutils"); @@ -31,7 +50,7 @@ public class CommandServerUtils extends BaseCommand { supportedConfigs.put("bukkit", RCraftServer::reloadBukkitConfiguration); supportedConfigs.put("commands.yml", RCraftServer::reloadCommandsConfiguration); supportedConfigs.put("server-icon.png", RCraftServer::loadIcon); - supportedConfigs.put("banned-ips.json", RCraftServer::reloadIPBans); + supportedConfigs.put("banned-ips.json", RCraftServer::reloadIpBans); supportedConfigs.put("banned-players.json", RCraftServer::reloadProfileBans); } @@ -42,6 +61,10 @@ public class CommandServerUtils extends BaseCommand { return supportedConfigs.keySet(); } + /** + * Shows the help page to the sender. + * @param sender The sender of the command. + */ @Default @Subcommand("help") @CommandPermission("serverutils.help") @@ -59,11 +82,15 @@ public class CommandServerUtils extends BaseCommand { 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.") @@ -74,6 +101,11 @@ public class CommandServerUtils extends BaseCommand { "%what%", "ServerUtils configurations"); } + /** + * Reloads a config from a set of configurations of the server. + * @param sender The sender of the command. + * @param config The configuration to reload. + */ @Subcommand("reloadconfig") @CommandCompletion("@supportedConfigs") @CommandPermission("serverutils.reloadconfig") @@ -103,6 +135,11 @@ public class CommandServerUtils extends BaseCommand { } } + /** + * Loads the specified plugin on the server. + * @param sender The sender of the command. + * @param jarFile The filename of the plugin in the plugins/ directory. + */ @Subcommand("loadplugin") @CommandCompletion("@pluginJars") @CommandPermission("serverutils.loadplugin") @@ -118,6 +155,11 @@ public class CommandServerUtils extends BaseCommand { result.sendTo(sender, "load", jarFile); } + /** + * Unloads the specified plugin from the server. + * @param sender The sender of the command. + * @param pluginName The plugin name. + */ @Subcommand("unloadplugin") @CommandCompletion("@plugins") @CommandPermission("serverutils.unloadplugin") @@ -133,6 +175,11 @@ public class CommandServerUtils extends BaseCommand { unloadResult.sendTo(sender, "unload", pluginName); } + /** + * Reloads the specified plugin on the server. + * @param sender The sender of the command. + * @param pluginName The plugin name. + */ @Subcommand("reloadplugin") @CommandCompletion("@plugins") @CommandPermission("serverutils.reloadplugin") @@ -142,6 +189,11 @@ public class CommandServerUtils extends BaseCommand { result.sendTo(sender, "reload", pluginName); } + /** + * Enables the specified plugin on the server. + * @param sender The sender of the command. + * @param pluginName The plugin name. + */ @Subcommand("enableplugin") @CommandCompletion("@plugins") @CommandPermission("serverutils.enableplugin") @@ -151,6 +203,11 @@ public class CommandServerUtils extends BaseCommand { result.sendTo(sender, "enabl", pluginName); } + /** + * Disables the specified plugin on the server. + * @param sender The sender of the command. + * @param pluginName The plugin name. + */ @Subcommand("disableplugin") @CommandCompletion("@plugins") @CommandPermission("serverutils.disableplugin") @@ -160,6 +217,11 @@ public class CommandServerUtils extends BaseCommand { result.sendTo(sender, "disabl", pluginName); } + /** + * Shows information about the specified plugin. + * @param sender The sender of the command. + * @param pluginName The plugin name. + */ @Subcommand("plugininfo") @CommandCompletion("@plugins") @CommandPermission("serverutils.plugininfo") @@ -186,7 +248,7 @@ public class CommandServerUtils extends BaseCommand { .add("Name", plugin.getName()) .add("Full Name", description.getFullName()) .add("Version", description.getVersion()); - if (MINOR >= 13) builder.add( "API Version", description.getAPIVersion()); + if (MINOR >= 13) builder.add("API Version", description.getAPIVersion()); builder.add("Website", description.getWebsite()) .add("Authors", ListBuilder.create(description.getAuthors()) .format(listFormat) @@ -222,6 +284,11 @@ public class CommandServerUtils extends BaseCommand { Messenger.sendMessage(sender, "serverutils.plugininfo.footer"); } + /** + * Shows information about a provided command. + * @param sender The sender of the command. + * @param command The command to lookup. + */ @Subcommand("commandinfo") @CommandCompletion("@commands") @CommandPermission("serverutils.commandinfo") diff --git a/src/main/java/net/frankheijden/serverutils/config/Config.java b/src/main/java/net/frankheijden/serverutils/config/Config.java index b4adf84..7af4a81 100644 --- a/src/main/java/net/frankheijden/serverutils/config/Config.java +++ b/src/main/java/net/frankheijden/serverutils/config/Config.java @@ -1,39 +1,21 @@ package net.frankheijden.serverutils.config; -import net.frankheijden.serverutils.ServerUtils; -import org.bukkit.configuration.file.YamlConfiguration; - import java.io.File; -public class Config { +public class Config extends YamlResource { - private static Defaults DEFAULT_CONFIG = Defaults.of( - "settings", Defaults.of( - "disable-plugins-command", false, - "check-updates", true, - "download-updates", false, - "download-at-startup-and-update", false - ) - ); - - private static final ServerUtils plugin = ServerUtils.getInstance(); private static Config instance; - private final YamlConfiguration config; public Config(File file) { + super(file, "config.yml"); instance = this; - config = Defaults.init(file, DEFAULT_CONFIG); } public static Config getInstance() { return instance; } - public YamlConfiguration getConfig() { - return config; - } - public boolean getBoolean(String path) { - return config.getBoolean(path, (boolean) DEFAULT_CONFIG.get(path)); + return getConfiguration().getBoolean(path); } } diff --git a/src/main/java/net/frankheijden/serverutils/config/Defaults.java b/src/main/java/net/frankheijden/serverutils/config/Defaults.java deleted file mode 100644 index b640d67..0000000 --- a/src/main/java/net/frankheijden/serverutils/config/Defaults.java +++ /dev/null @@ -1,69 +0,0 @@ -package net.frankheijden.serverutils.config; - -import org.bukkit.configuration.file.YamlConfiguration; - -import java.io.File; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; - -public class Defaults { - - private final Map rootMap; - - private Defaults(Object... objects) { - this.rootMap = new LinkedHashMap<>(); - for (int i = 0; i < objects.length; i += 2) { - this.rootMap.put(String.valueOf(objects[i]), objects[i + 1]); - } - } - - public static Defaults of(Object... objects) { - return new Defaults(objects); - } - - public static void addDefaults(Defaults defaults, YamlConfiguration yml) { - addDefaults(defaults, yml, ""); - } - - private static void addDefaults(Defaults defaults, YamlConfiguration yml, String root) { - for (Map.Entry entry : defaults.rootMap.entrySet()) { - String key = (root.isEmpty() ? "" : root + ".") + entry.getKey(); - Object value = entry.getValue(); - if (value instanceof Defaults) { - addDefaults((Defaults) value, yml, key); - } else if (yml.get(key) == null) { - yml.set(key, value); - } - } - } - - public Object get(String path) { - return get(this, path); - } - - private Object get(Defaults defaults, String path) { - String[] split = path.split("\\."); - if (split.length > 1) { - return get((Defaults) defaults.rootMap.get(split[0]), path.substring(split[0].length() + 1)); - } - return defaults.rootMap.get(split[0]); - } - - public static YamlConfiguration init(File file, Defaults defaults) { - YamlConfiguration yml = YamlConfiguration.loadConfiguration(file); - Defaults.addDefaults(defaults, 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; - } -} diff --git a/src/main/java/net/frankheijden/serverutils/config/Messenger.java b/src/main/java/net/frankheijden/serverutils/config/Messenger.java index 57fe9a6..ddce16d 100644 --- a/src/main/java/net/frankheijden/serverutils/config/Messenger.java +++ b/src/main/java/net/frankheijden/serverutils/config/Messenger.java @@ -1,88 +1,29 @@ package net.frankheijden.serverutils.config; +import java.io.File; + import net.frankheijden.serverutils.ServerUtils; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; -import org.bukkit.configuration.file.YamlConfiguration; -import java.io.File; - -public class Messenger { - - private static final Defaults DEFAULT_MESSAGES = Defaults.of( - "serverutils", Defaults.of( - "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_changed", "&cAccessing the jar file while %action%ing &4%what%&c went wrong, please load the plugin manually!", - "invalid_description", "&cAn error occurred while %action%ing &4%what%&c, plugin doesn't have a valid description!", - "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", Defaults.of( - "available", "&8&m------------=&r&8[ &b&lServerUtils Update&r &8]&m=--------------\n" - + " &3Current version: &b%old%\n" - + " &3New version: &b%new%\n" - + " &3Release info: &b%info%\n" - + "&8&m-------------------------------------------------", - "downloading", "&8&m------------=&r&8[ &b&lServerUtils Update&r &8]&m=--------------\n" - + " &3A new version of ServerUtils will be downloaded and installed after a restart!\n" - + " &3Current version: &b%old%\n" - + " &3New version: &b%new%\n" - + " &3Release info: &b%info%\n" - + "&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", Defaults.of( - "header", "&8&m-------------=&r&8[ &b&lServerUtils Help&r &8]&m=---------------", - "format", "&8/&3%command%&b%subcommand% &f(&7%help%&f)", - "footer", "&8&m-------------------------------------------------" - ), - "plugins", Defaults.of( - "header", "&8&m------------=&r&8[ &b&lServerUtils Plugins&r &8]&m=-------------", - "format", "&3%plugin%", - "format_disabled", "&c%plugin%", - "seperator", "&b, ", - "last_seperator", " &band ", - "version", " &8(&a%version%&8)", - "footer", "&8&m-------------------------------------------------" - ), - "plugininfo", Defaults.of( - "header", "&8&m-----------=&r&8[ &b&lServerUtils PluginInfo&r &8]&m=-----------", - "format", " &3%key%&8: &b%value%", - "list_format", "&b%value%", - "seperator", "&8, ", - "last_seperator", " &8and ", - "footer", "&8&m-------------------------------------------------" - ), - "commandinfo", Defaults.of( - "header", "&8&m-----------=&r&8[ &b&lServerUtils 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." - ) - ) - ); +public class Messenger extends YamlResource { private static final ServerUtils plugin = ServerUtils.getInstance(); private static Messenger instance; - private final YamlConfiguration messages; public Messenger(File file) { + super(file, "messages.yml"); instance = this; - messages = Defaults.init(file, DEFAULT_MESSAGES); } + /** + * 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.messages.getString(path); + String message = instance.getConfiguration().getString(path); if (message != null) { return apply(message, replacements); } else { @@ -91,6 +32,13 @@ public class Messenger { 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; for (int i = 0; i < replacements.length; i++, i++) { @@ -99,6 +47,12 @@ public class Messenger { 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) { @@ -106,6 +60,12 @@ public class Messenger { } } + /** + * 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) { diff --git a/src/main/java/net/frankheijden/serverutils/config/YamlResource.java b/src/main/java/net/frankheijden/serverutils/config/YamlResource.java new file mode 100644 index 0000000..0716f24 --- /dev/null +++ b/src/main/java/net/frankheijden/serverutils/config/YamlResource.java @@ -0,0 +1,32 @@ +package net.frankheijden.serverutils.config; + +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; + +import net.frankheijden.serverutils.ServerUtils; +import net.frankheijden.serverutils.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; + } +} diff --git a/src/main/java/net/frankheijden/serverutils/listeners/MainListener.java b/src/main/java/net/frankheijden/serverutils/listeners/MainListener.java index 3c4788b..73db05c 100644 --- a/src/main/java/net/frankheijden/serverutils/listeners/MainListener.java +++ b/src/main/java/net/frankheijden/serverutils/listeners/MainListener.java @@ -3,13 +3,20 @@ package net.frankheijden.serverutils.listeners; import net.frankheijden.serverutils.config.Config; import net.frankheijden.serverutils.tasks.UpdateCheckerTask; import org.bukkit.entity.Player; -import org.bukkit.event.*; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; public class MainListener implements Listener { private static final Config config = Config.getInstance(); + /** + * Called when a player joins the server. + * Used for sending an update message to the player, if enabled and has permission. + * @param event The PlayerJoinEvent. + */ @EventHandler(priority = EventPriority.LOWEST) public void onPlayerJoin(PlayerJoinEvent event) { if (!config.getBoolean("settings.check-updates")) return; diff --git a/src/main/java/net/frankheijden/serverutils/managers/PluginManager.java b/src/main/java/net/frankheijden/serverutils/managers/PluginManager.java index 08d6507..c30f46b 100644 --- a/src/main/java/net/frankheijden/serverutils/managers/PluginManager.java +++ b/src/main/java/net/frankheijden/serverutils/managers/PluginManager.java @@ -1,23 +1,45 @@ package net.frankheijden.serverutils.managers; -import net.frankheijden.serverutils.ServerUtils; -import net.frankheijden.serverutils.reflection.*; -import org.bukkit.Bukkit; -import org.bukkit.command.*; -import org.bukkit.plugin.*; - import java.io.File; import java.lang.reflect.InvocationTargetException; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import net.frankheijden.serverutils.ServerUtils; +import net.frankheijden.serverutils.reflection.RCommandMap; +import net.frankheijden.serverutils.reflection.RCraftServer; +import net.frankheijden.serverutils.reflection.RCraftingManager; +import net.frankheijden.serverutils.reflection.RJavaPlugin; +import net.frankheijden.serverutils.reflection.RPlugin; +import net.frankheijden.serverutils.reflection.RPluginClassLoader; +import net.frankheijden.serverutils.reflection.RSimplePluginManager; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.PluginCommand; +import org.bukkit.plugin.InvalidDescriptionException; +import org.bukkit.plugin.InvalidPluginException; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.PluginLoader; +import org.bukkit.plugin.UnknownDependencyException; + public class PluginManager { + /** + * Loads the specified file as a plugin. + * @param jarFile The name of the file in the plugins/ folder. + * @return The result of the loading procedure. + */ public static LoadResult loadPlugin(String jarFile) { return loadPlugin(new File(ServerUtils.getInstance().getDataFolder().getParent(), jarFile)); } + /** + * Loads the specified file as a plugin. + * @param file The file to be loaded. + * @return The result of the loading procedure. + */ public static LoadResult loadPlugin(File file) { if (!file.exists()) return new LoadResult(Result.NOT_EXISTS); @@ -47,10 +69,20 @@ public class PluginManager { return new LoadResult(plugin); } + /** + * Disables the specified plugin by name and cleans all commands and recipes of the plugin. + * @param pluginName The plugin to disable. + * @return The result of the disable call. + */ public static Result disablePlugin(String pluginName) { return disablePlugin(Bukkit.getPluginManager().getPlugin(pluginName)); } + /** + * Disables the specified plugin and cleans all commands and recipes of the plugin. + * @param plugin The plugin to disable. + * @return The result of the disable call. + */ public static Result disablePlugin(Plugin plugin) { if (plugin == null) return Result.NOT_ENABLED; if (!plugin.isEnabled()) return Result.ALREADY_DISABLED; @@ -65,10 +97,20 @@ public class PluginManager { return Result.SUCCESS; } + /** + * Unloads the specified plugin by name and cleans all traces within bukkit. + * @param pluginName The plugin to unload. + * @return The result of the unload. + */ public static Result unloadPlugin(String pluginName) { return unloadPlugin(Bukkit.getPluginManager().getPlugin(pluginName)); } + /** + * Unloads the specified plugin and cleans all traces within bukkit. + * @param plugin The plugin to unload. + * @return The result of the unload. + */ public static Result unloadPlugin(Plugin plugin) { if (plugin == null) return Result.NOT_EXISTS; try { @@ -82,10 +124,20 @@ public class PluginManager { return Result.SUCCESS; } + /** + * Enables the specified plugin by name. + * @param pluginName The plugin to enable. + * @return The result of the enabling. + */ public static Result enablePlugin(String pluginName) { return enablePlugin(Bukkit.getPluginManager().getPlugin(pluginName)); } + /** + * Enables the specified plugin. + * @param plugin The plugin to enable. + * @return The result of the enabling. + */ public static Result enablePlugin(Plugin plugin) { if (plugin == null) return Result.NOT_EXISTS; if (Bukkit.getPluginManager().isPluginEnabled(plugin.getName())) return Result.ALREADY_ENABLED; @@ -96,12 +148,22 @@ public class PluginManager { return Result.ERROR; } + /** + * Reloads the specified plugin by name. + * @param pluginName The plugin to reload. + * @return The result of the reload. + */ public static Result reloadPlugin(String pluginName) { Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName); if (plugin == null) return Result.NOT_EXISTS; return reloadPlugin(plugin); } + /** + * Reloads the specified plugin. + * @param plugin The plugin to reload. + * @return The result of the reload. + */ public static Result reloadPlugin(Plugin plugin) { Result disableResult = disablePlugin(plugin); if (disableResult != Result.SUCCESS && disableResult != Result.ALREADY_DISABLED) return disableResult; @@ -125,6 +187,10 @@ public class PluginManager { return enablePlugin(loadResult.getPlugin()); } + /** + * Retrieves all known commands registered to bukkit. + * @return A map with all known commands. + */ public static Map getKnownCommands() { try { return RCommandMap.getKnownCommands(RCraftServer.getCommandMap()); @@ -134,6 +200,10 @@ public class PluginManager { } } + /** + * Unregisters all commands from the specified plugin. + * @param plugin The plugin. + */ public static void unregisterCommands(Plugin plugin) { Map knownCommands = getKnownCommands(); if (knownCommands == null) return; @@ -151,12 +221,21 @@ public class PluginManager { }); } + /** + * Retrieves a command from the command map. + * @param command The command string. + * @return The command. + */ public static Command getCommand(String command) { Map knownCommands = getKnownCommands(); if (knownCommands == null) return null; return knownCommands.get(command); } + /** + * Retrieves all file associations, i.e. all plugin loaders. + * @return A map with all pluginloaders. + */ public static Map getFileAssociations() { try { return RSimplePluginManager.getFileAssociations(Bukkit.getPluginManager()); @@ -166,6 +245,11 @@ public class PluginManager { } } + /** + * Retrieves the PluginLoader for the input file. + * @param file The file. + * @return The appropiate PluginLoader. + */ public static PluginLoader getPluginLoader(File file) { Map fileAssociations = getFileAssociations(); if (fileAssociations == null) return null; @@ -179,6 +263,11 @@ public class PluginManager { return null; } + /** + * Retrieves a loaded plugin associated to a jar file. + * @param file The jar file. + * @return The already loaded plugin, or null if none. + */ public static Plugin getLoadedPlugin(File file) { PluginDescriptionFile descriptionFile; try { @@ -190,6 +279,12 @@ public class PluginManager { return Bukkit.getPluginManager().getPlugin(descriptionFile.getName()); } + /** + * Retrieves the PluginDescriptionFile of a jar file. + * @param file The jar file. + * @return The PluginDescriptionFile. + * @throws InvalidDescriptionException Iff the PluginDescriptionFile is invalid. + */ public static PluginDescriptionFile getPluginDescription(File file) throws InvalidDescriptionException { PluginLoader loader = getPluginLoader(file); if (loader == null) return null; diff --git a/src/main/java/net/frankheijden/serverutils/managers/Result.java b/src/main/java/net/frankheijden/serverutils/managers/Result.java index c9b7e15..c8a95a0 100644 --- a/src/main/java/net/frankheijden/serverutils/managers/Result.java +++ b/src/main/java/net/frankheijden/serverutils/managers/Result.java @@ -27,6 +27,13 @@ public enum Result { return this; } + /** + * Retrieves the associated message of the result + * and sends it to a CommandSender. + * @param sender The receiver. + * @param action The action which let to the result. + * @param what An associated variable. + */ public void sendTo(CommandSender sender, String action, String what) { Messenger.sendMessage(sender, "serverutils." + this.name().toLowerCase(), "%action%", action, diff --git a/src/main/java/net/frankheijden/serverutils/managers/VersionManager.java b/src/main/java/net/frankheijden/serverutils/managers/VersionManager.java index d8d844f..e11d5c2 100644 --- a/src/main/java/net/frankheijden/serverutils/managers/VersionManager.java +++ b/src/main/java/net/frankheijden/serverutils/managers/VersionManager.java @@ -9,6 +9,10 @@ public class VersionManager { 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(); diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RCommandMap.java b/src/main/java/net/frankheijden/serverutils/reflection/RCommandMap.java index ebe6188..2f649c6 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RCommandMap.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RCommandMap.java @@ -1,17 +1,20 @@ package net.frankheijden.serverutils.reflection; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.getDeclaredField; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; + import org.bukkit.command.Command; import org.bukkit.command.SimpleCommandMap; -import java.lang.reflect.*; -import java.util.Map; - -import static net.frankheijden.serverutils.reflection.ReflectionUtils.getDeclaredField; - public class RCommandMap { private static Field knownCommands = null; private static Method getKnownCommands = null; + static { try { try { @@ -25,7 +28,8 @@ public class RCommandMap { } @SuppressWarnings("unchecked") - public static Map getKnownCommands(SimpleCommandMap map) throws IllegalAccessException, InvocationTargetException { + public static Map getKnownCommands(SimpleCommandMap map) + throws IllegalAccessException, InvocationTargetException { return (Map) (knownCommands == null ? getKnownCommands.invoke(map) : knownCommands.get(map)); } } diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RCraftServer.java b/src/main/java/net/frankheijden/serverutils/reflection/RCraftServer.java index 4c66ef7..976ce81 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RCraftServer.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RCraftServer.java @@ -1,19 +1,31 @@ package net.frankheijden.serverutils.reflection; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.MethodParam.methodOf; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.max; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.min; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.versionOf; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.get; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllMethods; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.getDeclaredField; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.getDeclaredMethod; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.invoke; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.set; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; + import org.bukkit.Bukkit; import org.bukkit.Warning; -import org.bukkit.command.*; +import org.bukkit.command.Command; +import org.bukkit.command.SimpleCommandMap; import org.bukkit.configuration.file.YamlConfiguration; -import java.io.File; -import java.lang.reflect.*; -import java.util.Map; - -import static net.frankheijden.serverutils.reflection.ReflectionUtils.*; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.MethodParam.methodOf; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.*; - public class RCraftServer { private static Class craftServerClass; @@ -27,10 +39,12 @@ public class RCraftServer { static { try { - craftServerClass = Class.forName(String.format("org.bukkit.craftbukkit.%s.CraftServer", ReflectionUtils.NMS)); + craftServerClass = Class.forName(String.format("org.bukkit.craftbukkit.%s.CraftServer", + ReflectionUtils.NMS)); craftServer = craftServerClass.cast(Bukkit.getServer()); - commandsConfigFile = (File) getDeclaredMethod(craftServerClass, "getCommandsConfigFile").invoke(craftServer); + commandsConfigFile = (File) getDeclaredMethod(craftServerClass, + "getCommandsConfigFile").invoke(craftServer); configFile = (File) getDeclaredMethod(craftServerClass, "getConfigFile").invoke(craftServer); commandMap = (SimpleCommandMap) getDeclaredField(craftServerClass, "commandMap").get(Bukkit.getServer()); @@ -66,6 +80,13 @@ public class RCraftServer { return configFile; } + /** + * Retrieves the options file from a key. + * @param option The option key. + * @return The associated file. + * @throws InvocationTargetException If the method call produced an exception. + * @throws IllegalAccessException When prohibited access to the method. + */ public static File getOptionsFile(String option) throws IllegalAccessException, InvocationTargetException { Object console = get(fields, craftServer, "console"); Object options = get(RDedicatedServer.getFields(), console, "options"); @@ -80,7 +101,11 @@ public class RCraftServer { return commandMap; } - public static void reloadBukkitConfiguration() throws Exception { + /** + * Reloads the bukkit configuration. + * @throws ReflectiveOperationException Iff exception thrown regarding reflection. + */ + public static void reloadBukkitConfiguration() throws ReflectiveOperationException { YamlConfiguration bukkit = YamlConfiguration.loadConfiguration(getConfigFile()); set(fields, craftServer, "configuration", bukkit); @@ -91,7 +116,8 @@ public class RCraftServer { set(fields, craftServer, "animalSpawn", bukkit.getInt("spawn-limits.animals")); set(fields, craftServer, "waterAnimalSpawn", bukkit.getInt("spawn-limits.water-animals")); set(fields, craftServer, "ambientSpawn", bukkit.getInt("spawn-limits.ambient")); - set(fields, craftServer, "warningState", Warning.WarningState.value(bukkit.getString("settings.deprecated-verbose"))); + set(fields, craftServer, "warningState", + Warning.WarningState.value(bukkit.getString("settings.deprecated-verbose"))); set(fields, craftServer, "minimumAPI", bukkit.getString("settings.minimum-api")); set(fields, craftServer, "printSaveWarning", false); @@ -105,25 +131,43 @@ public class RCraftServer { invoke(methods, craftServer, "loadIcon"); } + /** + * Reloads the commands.yml file. + * @throws InvocationTargetException If the method call produced an exception. + * @throws IllegalAccessException When prohibited access to the method. + */ public static void reloadCommandsConfiguration() throws IllegalAccessException, InvocationTargetException { Map map = RCommandMap.getKnownCommands(commandMap); Bukkit.getCommandAliases().keySet().forEach(map::remove); YamlConfiguration commands = YamlConfiguration.loadConfiguration(getCommandsConfigFile()); set(fields, craftServer, "commandsConfiguration", commands); - set(fields, craftServer, "overrideAllCommandBlockCommands", commands.getStringList("command-block-overrides").contains("*")); - set(fields, craftServer, "ignoreVanillaPermissions", commands.getBoolean("ignore-vanilla-permissions")); - set(fields, craftServer, "unrestrictedAdvancements", commands.getBoolean("unrestricted-advancements")); + set(fields, craftServer, "overrideAllCommandBlockCommands", + commands.getStringList("command-block-overrides").contains("*")); + set(fields, craftServer, "ignoreVanillaPermissions", + commands.getBoolean("ignore-vanilla-permissions")); + set(fields, craftServer, "unrestrictedAdvancements", + commands.getBoolean("unrestricted-advancements")); commandMap.registerServerAliases(); } - public static void reloadIPBans() throws IllegalAccessException, InvocationTargetException { + /** + * Reloads the ip-bans file. + * @throws InvocationTargetException If the method call produced an exception. + * @throws IllegalAccessException When prohibited access to the method. + */ + public static void reloadIpBans() throws IllegalAccessException, InvocationTargetException { Object playerList = get(fields, craftServer, "playerList"); Object jsonList = invoke(RPlayerList.getMethods(), playerList, "getIPBans"); RJsonList.load(jsonList); } + /** + * Reloads the profile bans file. + * @throws InvocationTargetException If the method call produced an exception. + * @throws IllegalAccessException When prohibited access to the method. + */ public static void reloadProfileBans() throws IllegalAccessException, InvocationTargetException { Object playerList = get(fields, craftServer, "playerList"); Object jsonList = invoke(RPlayerList.getMethods(), playerList, "getProfileBans"); diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RCraftingManager.java b/src/main/java/net/frankheijden/serverutils/reflection/RCraftingManager.java index 5ba2bf4..61a7fff 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RCraftingManager.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RCraftingManager.java @@ -1,16 +1,21 @@ package net.frankheijden.serverutils.reflection; -import net.frankheijden.serverutils.utils.MapUtils; -import org.bukkit.plugin.Plugin; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.MINOR; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.min; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.get; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.invoke; -import java.lang.reflect.*; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.util.Collection; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Predicate; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.*; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.min; +import net.frankheijden.serverutils.utils.MapUtils; +import org.bukkit.plugin.Plugin; public class RCraftingManager { @@ -19,7 +24,8 @@ public class RCraftingManager { static { try { - craftingManagerClass = Class.forName(String.format("net.minecraft.server.%s.CraftingManager", ReflectionUtils.NMS)); + craftingManagerClass = Class.forName(String.format("net.minecraft.server.%s.CraftingManager", + ReflectionUtils.NMS)); fields = getAllFields(craftingManagerClass, fieldOf("recipes", min(12))); } catch (Exception ex) { @@ -27,6 +33,12 @@ public class RCraftingManager { } } + /** + * Removes all associated recipes of a plugin. + * @param plugin The plugin to remove recipes for. + * @throws IllegalAccessException When prohibited access to the method. + * @throws InvocationTargetException If the method call produced an exception. + */ @SuppressWarnings({"unchecked", "rawtypes"}) public static void removeRecipesFor(Plugin plugin) throws IllegalAccessException, InvocationTargetException { // Cleaning up recipes before MC 1.12 is not possible, @@ -43,11 +55,12 @@ public class RCraftingManager { Object craftingManager = invoke(RMinecraftServer.getMethods(), server, "getCraftingManager"); Map recipes = (Map) recipesField.get(craftingManager); + Predicate predicate = RMinecraftKey.matchingPluginPredicate(new AtomicBoolean(false), plugin); if (MINOR == 13) { - MapUtils.removeKeys(recipes, RMinecraftKey.matchingPluginPredicate(new AtomicBoolean(false), plugin)); + MapUtils.removeKeys(recipes, predicate); } else { Collection list = (Collection) recipes.values(); - list.forEach(map -> MapUtils.removeKeys(map, RMinecraftKey.matchingPluginPredicate(new AtomicBoolean(false), plugin))); + list.forEach(map -> MapUtils.removeKeys(map, predicate)); } } } diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServer.java b/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServer.java index dcf24bb..bf00bcd 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServer.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServer.java @@ -1,14 +1,21 @@ package net.frankheijden.serverutils.reflection; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.MINOR; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.MethodParam.methodOf; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.min; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.get; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllMethods; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.invoke; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.set; + import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.*; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.MethodParam.methodOf; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.*; - public class RDedicatedServer { private static Class dedicatedServerClass; @@ -17,7 +24,8 @@ public class RDedicatedServer { static { try { - dedicatedServerClass = Class.forName(String.format("net.minecraft.server.%s.DedicatedServer", ReflectionUtils.NMS)); + dedicatedServerClass = Class.forName(String.format("net.minecraft.server.%s.DedicatedServer", + ReflectionUtils.NMS)); fields = getAllFields(dedicatedServerClass, fieldOf("propertyManager", ALL_VERSIONS), @@ -47,29 +55,57 @@ public class RDedicatedServer { return fields; } - public static void reload(Object console) throws Exception { + /** + * Reloads the specified console (= DedicatedServer) instance's bukkit config. + * @param console The console to reload. + * @throws ReflectiveOperationException Iff exception thrown regarding reflection. + */ + public static void reload(Object console) throws ReflectiveOperationException { Object options = get(fields, console, "options"); if (MINOR >= 13) { Object propertyManager = RDedicatedServerSettings.newInstance(options); set(fields, console, "propertyManager", propertyManager); Object config = invoke(RDedicatedServerSettings.getMethods(), propertyManager, "getProperties"); - invoke(methods, console, "setSpawnAnimals", get(RDedicatedServerProperties.getFields(), config, "spawnAnimals")); - invoke(methods, console, "setSpawnNPCs", get(RDedicatedServerProperties.getFields(), config, "spawnNpcs")); - invoke(methods, console, "setPVP", get(RDedicatedServerProperties.getFields(), config, "pvp")); - invoke(methods, console, "setAllowFlight", get(RDedicatedServerProperties.getFields(), config, "allowFlight")); - invoke(methods, console, "setResourcePack", get(RDedicatedServerProperties.getFields(), config, "resourcePack"), invoke(methods, console, "aZ")); - invoke(methods, console, "setMotd", get(RDedicatedServerProperties.getFields(), config, "motd")); - invoke(methods, console, "setForceGamemode", get(RDedicatedServerProperties.getFields(), config, "forceGamemode")); - invoke(methods, console, "n", get(RDedicatedServerProperties.getFields(), config, "enforceWhitelist")); - set(fields, console, "o", get(RDedicatedServerProperties.getFields(), config, "gamemode")); + invoke(methods, console, "setSpawnAnimals", getConfigValue(config, "spawnAnimals")); + invoke(methods, console, "setSpawnNPCs", getConfigValue(config, "spawnNpcs")); + invoke(methods, console, "setPVP", getConfigValue(config, "pvp")); + invoke(methods, console, "setAllowFlight", getConfigValue(config, "allowFlight")); + invoke(methods, console, "setResourcePack", getConfigValue(config, "resourcePack"), + invoke(methods, console, "aZ")); + invoke(methods, console, "setMotd", getConfigValue(config, "motd")); + invoke(methods, console, "setForceGamemode", getConfigValue(config, "forceGamemode")); + invoke(methods, console, "n", getConfigValue(config, "enforceWhitelist")); + set(fields, console, "o", getConfigValue(config, "gamemode")); } else { Object config = RPropertyManager.newInstance(options); - set(fields, console, "propertyManager", config); - invoke(methods, console, "setSpawnAnimals", invoke(RPropertyManager.getMethods(), config, "getBoolean", "spawn-animals", invoke(methods, console, "getSpawnAnimals"))); - invoke(methods, console, "setPVP", invoke(RPropertyManager.getMethods(), config, "getBoolean", "pvp", invoke(methods, console, "getPVP"))); - invoke(methods, console, "setAllowFlight", invoke(RPropertyManager.getMethods(), config, "getBoolean", "allow-flight", invoke(methods, console, "getAllowFlight"))); - invoke(methods, console, "setMotd", invoke(RPropertyManager.getMethods(), config, "getString", "motd", invoke(methods, console, "getMotd"))); + setConfigValue(config, console, "getSpawnAnimals", "setSpawnAnimals", "getBoolean", "spawn-animals"); + setConfigValue(config, console, "getPVP", "setPVP", "getBoolean", "pvp"); + setConfigValue(config, console, "getAllowFlight", "setAllowFlight", "getBoolean", "allow-flight"); + setConfigValue(config, console, "getMotd", "setMotd", "getString", "motd"); } } + + public static Object getConfigValue(Object config, String key) throws IllegalAccessException { + return get(RDedicatedServerProperties.getFields(), config, key); + } + + /** + * Sets the specified bukkit config value. + * @param config The config instance (= PropertyManager) + * @param console The console instance (= DedicatedServer) + * @param getMethod The getter method for the config value. + * @param setMethod The setter method for the config value. + * @param configMethod The method which we call the config value upon. + * @param key The config key. + * @throws InvocationTargetException If the method call produced an exception. + * @throws IllegalAccessException When prohibited access to the method. + */ + public static void setConfigValue(Object config, Object console, String getMethod, String setMethod, + String configMethod, String key) + throws InvocationTargetException, IllegalAccessException { + Object defaultValue = invoke(methods, console, getMethod); + Object configValue = invoke(RPropertyManager.getMethods(), config, configMethod, key, defaultValue); + invoke(methods, console, setMethod, configValue); + } } diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServerProperties.java b/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServerProperties.java index 93f370f..38b1178 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServerProperties.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServerProperties.java @@ -1,21 +1,22 @@ package net.frankheijden.serverutils.reflection; -import java.lang.reflect.Field; -import java.util.Map; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields; +import java.lang.reflect.Field; +import java.util.Map; + public class RDedicatedServerProperties { - private static Class dedicatedServerPropertiesClass; + private static Class serverPropertiesClass; private static Map fields; static { try { - dedicatedServerPropertiesClass = Class.forName(String.format("net.minecraft.server.%s.DedicatedServerProperties", ReflectionUtils.NMS)); - fields = getAllFields(dedicatedServerPropertiesClass, + serverPropertiesClass = Class.forName(String.format("net.minecraft.server.%s.DedicatedServerProperties", + ReflectionUtils.NMS)); + fields = getAllFields(serverPropertiesClass, fieldOf("spawnAnimals", ALL_VERSIONS), fieldOf("spawnNpcs", ALL_VERSIONS), fieldOf("pvp", ALL_VERSIONS), diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServerSettings.java b/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServerSettings.java index fe00903..2f7e72d 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServerSettings.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RDedicatedServerSettings.java @@ -1,29 +1,30 @@ package net.frankheijden.serverutils.reflection; -import java.lang.reflect.Method; -import java.util.Map; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.MethodParam.methodOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllMethods; +import java.lang.reflect.Method; +import java.util.Map; + public class RDedicatedServerSettings { - private static Class dedicatedServerSettingsClass; + private static Class serverSettingsClass; private static Map methods; static { try { - dedicatedServerSettingsClass = Class.forName(String.format("net.minecraft.server.%s.DedicatedServerSettings", ReflectionUtils.NMS)); - methods = getAllMethods(dedicatedServerSettingsClass, + serverSettingsClass = Class.forName(String.format("net.minecraft.server.%s.DedicatedServerSettings", + ReflectionUtils.NMS)); + methods = getAllMethods(serverSettingsClass, methodOf("getProperties", ALL_VERSIONS)); } catch (Exception ex) { ex.printStackTrace(); } } - public static Object newInstance(Object options) throws Exception { - return dedicatedServerSettingsClass.getDeclaredConstructor(Class.forName("joptsimple.OptionSet")).newInstance(options); + public static Object newInstance(Object options)throws ReflectiveOperationException { + return serverSettingsClass.getDeclaredConstructor(Class.forName("joptsimple.OptionSet")).newInstance(options); } public static Map getMethods() { diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RJavaPlugin.java b/src/main/java/net/frankheijden/serverutils/reflection/RJavaPlugin.java index 4a1ca9b..b04ea25 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RJavaPlugin.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RJavaPlugin.java @@ -1,16 +1,16 @@ package net.frankheijden.serverutils.reflection; -import org.bukkit.plugin.java.JavaPlugin; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.MethodParam.methodOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllMethods; import static net.frankheijden.serverutils.reflection.ReflectionUtils.invoke; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; + +import org.bukkit.plugin.java.JavaPlugin; + public class RJavaPlugin { private static Class javaPluginClass; diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RJsonList.java b/src/main/java/net/frankheijden/serverutils/reflection/RJsonList.java index feb045d..33018fa 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RJsonList.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RJsonList.java @@ -1,18 +1,19 @@ package net.frankheijden.serverutils.reflection; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Map; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.MethodParam.methodOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllMethods; import static net.frankheijden.serverutils.reflection.ReflectionUtils.invoke; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; + public class RJsonList { private static Class jsonListClass; private static Map methods; + static { try { jsonListClass = Class.forName(String.format("net.minecraft.server.%s.JsonList", ReflectionUtils.NMS)); diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RMinecraftKey.java b/src/main/java/net/frankheijden/serverutils/reflection/RMinecraftKey.java index 4bd90fa..3a6136d 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RMinecraftKey.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RMinecraftKey.java @@ -1,6 +1,10 @@ package net.frankheijden.serverutils.reflection; -import org.bukkit.plugin.Plugin; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.max; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.min; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.get; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields; import java.lang.reflect.Field; import java.util.Locale; @@ -8,10 +12,7 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Predicate; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.*; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.get; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields; +import org.bukkit.plugin.Plugin; public class RMinecraftKey { @@ -20,7 +21,8 @@ public class RMinecraftKey { static { try { - minecraftKeyClass = Class.forName(String.format("net.minecraft.server.%s.MinecraftKey", ReflectionUtils.NMS)); + minecraftKeyClass = Class.forName(String.format("net.minecraft.server.%s.MinecraftKey", + ReflectionUtils.NMS)); fields = getAllFields(minecraftKeyClass, fieldOf("a", max(13)), fieldOf("namespace", min(14))); @@ -29,6 +31,12 @@ public class RMinecraftKey { } } + /** + * Retrieves the namespace of the specified MinecraftKey instance. + * @param instance The MinecraftKey instance. + * @return The namespace. + * @throws IllegalAccessException When prohibited access to the field. + */ public static String getNameSpace(Object instance) throws IllegalAccessException { if (ReflectionUtils.MINOR <= 13) { return (String) get(fields, instance, "a"); @@ -41,6 +49,12 @@ public class RMinecraftKey { return namespace.equalsIgnoreCase(getNameSpace(instance)); } + /** + * Creates a predicate which returns true if a MinecraftKey instance comes from the specified plugin. + * @param errorThrown Requires an atomicboolean to ensure an exception is only thrown once, if any. + * @param plugin The plugin to match the MinecraftKey instance with. + * @return The predicate. + */ public static Predicate matchingPluginPredicate(AtomicBoolean errorThrown, Plugin plugin) { return o -> { try { diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RMinecraftServer.java b/src/main/java/net/frankheijden/serverutils/reflection/RMinecraftServer.java index 4d1e214..f3b17ca 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RMinecraftServer.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RMinecraftServer.java @@ -1,12 +1,12 @@ package net.frankheijden.serverutils.reflection; -import java.lang.reflect.Method; -import java.util.Map; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.MethodParam.methodOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllMethods; +import java.lang.reflect.Method; +import java.util.Map; + public class RMinecraftServer { private static Class minecraftServerClass; @@ -14,7 +14,8 @@ public class RMinecraftServer { static { try { - minecraftServerClass = Class.forName(String.format("net.minecraft.server.%s.MinecraftServer", ReflectionUtils.NMS)); + minecraftServerClass = Class.forName(String.format("net.minecraft.server.%s.MinecraftServer", + ReflectionUtils.NMS)); methods = getAllMethods(minecraftServerClass, methodOf("getServer", ALL_VERSIONS), methodOf("getCraftingManager", ALL_VERSIONS)); diff --git a/src/main/java/net/frankheijden/serverutils/reflection/ROptionSet.java b/src/main/java/net/frankheijden/serverutils/reflection/ROptionSet.java index ef0d326..04f2212 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/ROptionSet.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/ROptionSet.java @@ -1,12 +1,12 @@ package net.frankheijden.serverutils.reflection; -import java.lang.reflect.Method; -import java.util.Map; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.MethodParam.methodOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllMethods; +import java.lang.reflect.Method; +import java.util.Map; + public class ROptionSet { private static Class optionSetClass; diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RPlayerList.java b/src/main/java/net/frankheijden/serverutils/reflection/RPlayerList.java index 811afef..5740925 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RPlayerList.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RPlayerList.java @@ -1,12 +1,12 @@ package net.frankheijden.serverutils.reflection; -import java.lang.reflect.Method; -import java.util.Map; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.MethodParam.methodOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllMethods; +import java.lang.reflect.Method; +import java.util.Map; + public class RPlayerList { private static Class playerListClass; diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RPlugin.java b/src/main/java/net/frankheijden/serverutils/reflection/RPlugin.java index 025a292..adff9f2 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RPlugin.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RPlugin.java @@ -1,17 +1,18 @@ package net.frankheijden.serverutils.reflection; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.java.JavaPlugin; +import static net.frankheijden.serverutils.reflection.ReflectionUtils.getDeclaredMethod; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import static net.frankheijden.serverutils.reflection.ReflectionUtils.getDeclaredMethod; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; public class RPlugin { private static Method getFile; + static { try { getFile = getDeclaredMethod(JavaPlugin.class, "getFile"); diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RPluginClassLoader.java b/src/main/java/net/frankheijden/serverutils/reflection/RPluginClassLoader.java index f02d966..593d553 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RPluginClassLoader.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RPluginClassLoader.java @@ -1,15 +1,15 @@ package net.frankheijden.serverutils.reflection; -import java.io.IOException; -import java.lang.reflect.*; -import java.net.URLClassLoader; -import java.util.Map; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields; import static net.frankheijden.serverutils.reflection.ReflectionUtils.set; +import java.io.IOException; +import java.lang.reflect.Field; +import java.net.URLClassLoader; +import java.util.Map; + public class RPluginClassLoader { private static Class pluginClassLoaderClass; @@ -30,17 +30,32 @@ public class RPluginClassLoader { return pluginClassLoaderClass.isInstance(obj); } + /** + * Clears and closes the provided classloader. + * Remov + * @param loader The classloader instance. + * @throws IOException When closing the loader failed. + * @throws IllegalAccessException When prohibited access to the field. + */ public static void clearClassLoader(ClassLoader loader) throws IOException, IllegalAccessException { if (loader == null) return; if (isInstance(loader)) { - clearURLClassLoader((URLClassLoader) loader); + clearUrlClassLoader(loader); + } + + if (loader instanceof URLClassLoader) { + ((URLClassLoader) loader).close(); } } - public static void clearURLClassLoader(URLClassLoader loader) throws IllegalAccessException, IOException { - if (loader == null) return; - set(fields, loader, "plugin", null); - set(fields, loader, "pluginInit", null); - loader.close(); + /** + * Clears the plugin fields from the specified PluginClassLoader. + * @param pluginLoader The plugin loader instance. + * @throws IllegalAccessException When prohibited access to the field. + */ + public static void clearUrlClassLoader(Object pluginLoader) throws IllegalAccessException { + if (pluginLoader == null) return; + set(fields, pluginLoader, "plugin", null); + set(fields, pluginLoader, "pluginInit", null); } } diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RPropertyManager.java b/src/main/java/net/frankheijden/serverutils/reflection/RPropertyManager.java index d8ccd39..4794044 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RPropertyManager.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RPropertyManager.java @@ -1,12 +1,12 @@ package net.frankheijden.serverutils.reflection; -import java.lang.reflect.Method; -import java.util.Map; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.MethodParam.methodOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllMethods; +import java.lang.reflect.Method; +import java.util.Map; + public class RPropertyManager { private static Class propertyManagerClass; @@ -14,7 +14,8 @@ public class RPropertyManager { static { try { - propertyManagerClass = Class.forName(String.format("net.minecraft.server.%s.PropertyManager", ReflectionUtils.NMS)); + propertyManagerClass = Class.forName(String.format("net.minecraft.server.%s.PropertyManager", + ReflectionUtils.NMS)); methods = getAllMethods(propertyManagerClass, methodOf("getBoolean", ALL_VERSIONS, String.class, boolean.class), methodOf("getString", ALL_VERSIONS, String.class, String.class)); @@ -23,7 +24,7 @@ public class RPropertyManager { } } - public static Object newInstance(Object options) throws Exception { + public static Object newInstance(Object options) throws ReflectiveOperationException { return propertyManagerClass.getDeclaredConstructor(Class.forName("joptsimple.OptionSet")).newInstance(options); } diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RRegistryMaterials.java b/src/main/java/net/frankheijden/serverutils/reflection/RRegistryMaterials.java index 928c81a..91bd6fb 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RRegistryMaterials.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RRegistryMaterials.java @@ -1,17 +1,17 @@ package net.frankheijden.serverutils.reflection; -import net.frankheijden.serverutils.utils.MapUtils; -import org.bukkit.plugin.Plugin; - -import java.lang.reflect.Field; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.get; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields; +import java.lang.reflect.Field; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import net.frankheijden.serverutils.utils.MapUtils; +import org.bukkit.plugin.Plugin; + public class RRegistryMaterials { private static Class registryMaterialsClass; @@ -20,7 +20,8 @@ public class RRegistryMaterials { static { try { - registryMaterialsClass = Class.forName(String.format("net.minecraft.server.%s.RegistryMaterials", ReflectionUtils.NMS)); + registryMaterialsClass = Class.forName(String.format("net.minecraft.server.%s.RegistryMaterials", + ReflectionUtils.NMS)); fields = getAllFields(registryMaterialsClass, fieldOf("b", ALL_VERSIONS)); } catch (Exception ex) { @@ -28,6 +29,12 @@ public class RRegistryMaterials { } } + /** + * Removes all registered keys from an instance associated to the specified plugin. + * @param instance The RegistryMaterials instance. + * @param plugin The plugin to remove keys for. + * @throws IllegalAccessException When prohibited access to the field. + */ @SuppressWarnings("rawtypes") public static void removeKeysFor(Object instance, Plugin plugin) throws IllegalAccessException { Map map = (Map) get(fields, instance, "b"); diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RRegistrySimple.java b/src/main/java/net/frankheijden/serverutils/reflection/RRegistrySimple.java index 4cb12a0..d34faa5 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RRegistrySimple.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RRegistrySimple.java @@ -1,17 +1,17 @@ package net.frankheijden.serverutils.reflection; -import net.frankheijden.serverutils.utils.MapUtils; -import org.bukkit.plugin.Plugin; - -import java.lang.reflect.Field; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.get; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields; +import java.lang.reflect.Field; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import net.frankheijden.serverutils.utils.MapUtils; +import org.bukkit.plugin.Plugin; + public class RRegistrySimple { private static Class registrySimpleClass; @@ -19,7 +19,8 @@ public class RRegistrySimple { static { try { - registrySimpleClass = Class.forName(String.format("net.minecraft.server.%s.RegistrySimple", ReflectionUtils.NMS)); + registrySimpleClass = Class.forName(String.format("net.minecraft.server.%s.RegistrySimple", + ReflectionUtils.NMS)); fields = getAllFields(registrySimpleClass, fieldOf("c", ALL_VERSIONS)); } catch (Exception ex) { @@ -27,6 +28,12 @@ public class RRegistrySimple { } } + /** + * Removes all registered MinecraftKey's from an instance associated to the specified plugin. + * @param instance The RegistrySimple instance. + * @param plugin The plugin to remove keys for. + * @throws IllegalAccessException When prohibited access to the field. + */ @SuppressWarnings("rawtypes") public static void removeKeyFor(Object instance, Plugin plugin) throws IllegalAccessException { Map map = (Map) get(fields, instance, "c"); diff --git a/src/main/java/net/frankheijden/serverutils/reflection/RSimplePluginManager.java b/src/main/java/net/frankheijden/serverutils/reflection/RSimplePluginManager.java index ccb9546..be9fb5c 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/RSimplePluginManager.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/RSimplePluginManager.java @@ -1,16 +1,20 @@ package net.frankheijden.serverutils.reflection; -import org.bukkit.plugin.*; - -import java.lang.reflect.Field; -import java.util.*; -import java.util.regex.Pattern; - import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf; import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS; import static net.frankheijden.serverutils.reflection.ReflectionUtils.get; import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields; +import java.lang.reflect.Field; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.regex.Pattern; + +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginLoader; +import org.bukkit.plugin.SimplePluginManager; + public class RSimplePluginManager { private static Class simplePluginManagerClass; @@ -38,6 +42,13 @@ public class RSimplePluginManager { return (List) get(fields, manager, "plugins"); } + /** + * Removes the lookup name of the plugin. + * This ensures the plugin cannot be found anymore in Bukkit#getPlugin(String name). + * @param manager The SimplePluginManager instance to remove the lookup name from. + * @param name The name of the plugin to remove. + * @throws IllegalAccessException When prohibited access to the field. + */ @SuppressWarnings("unchecked") public static void removeLookupName(Object manager, String name) throws IllegalAccessException { Map lookupNames = (Map) get(fields, manager, "lookupNames"); diff --git a/src/main/java/net/frankheijden/serverutils/reflection/ReflectionUtils.java b/src/main/java/net/frankheijden/serverutils/reflection/ReflectionUtils.java index 038458d..7b542d1 100644 --- a/src/main/java/net/frankheijden/serverutils/reflection/ReflectionUtils.java +++ b/src/main/java/net/frankheijden/serverutils/reflection/ReflectionUtils.java @@ -1,9 +1,12 @@ package net.frankheijden.serverutils.reflection; -import org.bukkit.Bukkit; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; -import java.lang.reflect.*; -import java.util.*; +import org.bukkit.Bukkit; public class ReflectionUtils { @@ -11,6 +14,7 @@ public class ReflectionUtils { public static int MAJOR; public static int MINOR; public static int PATCH; + static { String bukkitPackage = Bukkit.getServer().getClass().getPackage().getName(); NMS = bukkitPackage.substring(bukkitPackage.lastIndexOf('.') + 1); @@ -21,33 +25,71 @@ public class ReflectionUtils { PATCH = Integer.parseInt(split[2].substring(1, 2)); } + /** + * Retrieves a declared field from a class and makes it accessible. + * @param clazz The class of the method. + * @param field The field name. + * @return The specified field. + * @throws NoSuchFieldException iff field doesn't exist. + */ public static Field getDeclaredField(Class clazz, String field) throws NoSuchFieldException { Field f = clazz.getDeclaredField(field); f.setAccessible(true); return f; } + /** + * Retrieves a field from a class and makes it accessible. + * @param clazz The class of the method. + * @param field The field name. + * @return The specified field. + * @throws NoSuchFieldException iff field doesn't exist. + */ public static Field getField(Class clazz, String field) throws NoSuchFieldException { Field f = clazz.getField(field); f.setAccessible(true); return f; } - public static Method getDeclaredMethod(Class clazz, String method, Class... params) throws NoSuchMethodException { + /** + * Retrieves a declared method from a class and makes it accessible. + * @param clazz The class of the method. + * @param method The method name. + * @param params The parameters of the method. + * @return The specified method. + * @throws NoSuchMethodException iff method doesn't exist. + */ + public static Method getDeclaredMethod(Class clazz, String method, Class... params) + throws NoSuchMethodException { Method m = clazz.getDeclaredMethod(method, params); m.setAccessible(true); return m; } - public static Method getMethod(Class clazz, String method, Class... params) throws NoSuchMethodException { + /** + * Retrieves a method from a class and makes it accessible. + * @param clazz The class of the method. + * @param method The method name. + * @param params The parameters of the method. + * @return The specified method. + * @throws NoSuchMethodException iff method doesn't exist. + */ + public static Method getMethod(Class clazz, String method, Class... params) + throws NoSuchMethodException { Method m = clazz.getMethod(method, params); m.setAccessible(true); return m; } - public static Map getAllFields(Class clazz, FieldParam... fields) { + /** + * Retrieves fields from a class based on the specified FieldParams. + * @param clazz The class of the fields. + * @param fieldParams The fields which will be collected. + * @return A map with key the field name and value the actual field. + */ + public static Map getAllFields(Class clazz, FieldParam... fieldParams) { Map map = new HashMap<>(); - for (FieldParam fieldParam : fields) { + for (FieldParam fieldParam : fieldParams) { if (!fieldParam.versionParam.isCompatible()) continue; try { map.put(fieldParam.field, getDeclaredField(clazz, fieldParam.field)); @@ -62,6 +104,12 @@ public class ReflectionUtils { return map; } + /** + * Retrieves methods from a class based on the specified MethodParams. + * @param clazz The class of the methods. + * @param methodParams The methods which will be collected. + * @return A map with key the method name and value the actual method. + */ public static Map getAllMethods(Class clazz, MethodParam... methodParams) { Map map = new HashMap<>(); for (MethodParam methodParam : methodParams) { @@ -79,19 +127,49 @@ public class ReflectionUtils { return map; } - public static Object invoke(Map map, Object instance, String methodName, Object... params) throws InvocationTargetException, IllegalAccessException { + /** + * Invokes a method on an instance. + * Will return null if method not present in map. + * @param map The map with methods. + * @param instance The instance of the class. + * @param methodName The name of the method. + * @param params The parameters of the method. + * @return The object returned by the method, or null if not present in map. + * @throws InvocationTargetException If the method call produced an exception. + * @throws IllegalAccessException When prohibited access to the method. + */ + public static Object invoke(Map map, Object instance, String methodName, Object... params) + throws InvocationTargetException, IllegalAccessException { Method method = map.get(methodName); if (method == null) return null; return method.invoke(instance, params); } + /** + * Retrieves the specified field from an object instance. + * Returns null if the field is not in the map. + * @param map The map with fields. + * @param instance The instance of the class. + * @param fieldName The field name. + * @throws IllegalAccessException When prohibited access to the field. + */ public static Object get(Map map, Object instance, String fieldName) throws IllegalAccessException { Field field = map.get(fieldName); if (field == null) return null; return field.get(instance); } - public static void set(Map map, Object instance, String fieldName, Object value) throws IllegalAccessException { + /** + * Sets the specified field to the specified value. + * Will silently fail if the field is not in the map. + * @param map The map with fields. + * @param instance The instance of the class. + * @param fieldName The field name. + * @param value The value to set the field to. + * @throws IllegalAccessException When prohibited access to the field. + */ + public static void set(Map map, Object instance, String fieldName, Object value) + throws IllegalAccessException { Field field = map.get(fieldName); if (field == null) return; field.set(instance, value); diff --git a/src/main/java/net/frankheijden/serverutils/tasks/UpdateCheckerTask.java b/src/main/java/net/frankheijden/serverutils/tasks/UpdateCheckerTask.java index d773e79..dcf0e05 100644 --- a/src/main/java/net/frankheijden/serverutils/tasks/UpdateCheckerTask.java +++ b/src/main/java/net/frankheijden/serverutils/tasks/UpdateCheckerTask.java @@ -1,10 +1,32 @@ package net.frankheijden.serverutils.tasks; -import com.google.gson.*; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Method; +import java.net.ConnectException; +import java.net.SocketTimeoutException; +import java.net.URL; +import java.net.URLConnection; +import java.net.UnknownHostException; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; +import java.nio.charset.StandardCharsets; +import java.util.logging.Level; + import net.frankheijden.serverutils.ServerUtils; import net.frankheijden.serverutils.config.Config; import net.frankheijden.serverutils.config.Messenger; -import net.frankheijden.serverutils.managers.*; +import net.frankheijden.serverutils.managers.VersionManager; import net.frankheijden.serverutils.utils.VersionUtils; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -12,14 +34,6 @@ import org.bukkit.plugin.InvalidDescriptionException; import org.bukkit.plugin.InvalidPluginException; import org.bukkit.plugin.java.JavaPlugin; -import java.io.*; -import java.lang.reflect.Method; -import java.net.*; -import java.nio.channels.Channels; -import java.nio.channels.ReadableByteChannel; -import java.nio.charset.StandardCharsets; -import java.util.logging.Level; - public class UpdateCheckerTask implements Runnable { private static final ServerUtils plugin = ServerUtils.getInstance(); @@ -29,6 +43,19 @@ public class UpdateCheckerTask implements Runnable { private final boolean startup; private static final String GITHUB_LINK = "https://api.github.com/repos/FrankHeijden/ServerUtils/releases/latest"; + private static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0)" + + "Gecko/20100101" + + "Firefox/77.0"; + + private static final String UPDATE_CHECK_START = "Checking for updates..."; + private static final String GENERAL_ERROR = "Error fetching new version of ServerUtils"; + private static final String CONNECTION_ERROR = GENERAL_ERROR + ": (%s) %s (maybe check your connection?)"; + private static final String UNAVAILABLE = GENERAL_ERROR + ": (%s) %s (no update available)"; + private static final String UPDATE_AVAILABLE = "ServerUtils %s is available!"; + private static final String DOWNLOAD_START = "Started downloading from \"%s\"..."; + private static final String DOWNLOAD_ERROR = "Error downloading a new version of ServerUtils"; + private static final String UPGRADE_SUCCESS = "Successfully upgraded ServerUtils to v%s!"; + private static final String DOWNLOADED_RESTART = "Downloaded ServerUtils version v%s. Restarting plugin now..."; private UpdateCheckerTask(CommandSender sender, boolean startup) { this.sender = sender; @@ -52,22 +79,20 @@ public class UpdateCheckerTask implements Runnable { @Override public void run() { if (isStartupCheck()) { - plugin.getLogger().info("Checking for updates..."); + plugin.getLogger().info(UPDATE_CHECK_START); } JsonObject jsonObject; try { - jsonObject = readJsonFromURL(GITHUB_LINK).getAsJsonObject(); + jsonObject = readJsonFromUrl(GITHUB_LINK).getAsJsonObject(); } catch (ConnectException | UnknownHostException | SocketTimeoutException ex) { - plugin.getLogger().severe(String.format("Error fetching new version of ServerUtils: (%s) %s (maybe check your connection?)", - ex.getClass().getSimpleName(), ex.getMessage())); + plugin.getLogger().severe(String.format(CONNECTION_ERROR, ex.getClass().getSimpleName(), ex.getMessage())); return; } catch (FileNotFoundException ex) { - plugin.getLogger().severe(String.format("Error fetching new version of ServerUtils: (%s) %s (no update available)", - ex.getClass().getSimpleName(), ex.getMessage())); + plugin.getLogger().severe(String.format(UNAVAILABLE, ex.getClass().getSimpleName(), ex.getMessage())); return; } catch (IOException ex) { - plugin.getLogger().log(Level.SEVERE, ex, () -> "Error fetching new version of ServerUtils"); + plugin.getLogger().log(Level.SEVERE, ex, () -> GENERAL_ERROR); return; } String githubVersion = jsonObject.getAsJsonPrimitive("tag_name").getAsString(); @@ -84,12 +109,12 @@ public class UpdateCheckerTask implements Runnable { } if (VersionUtils.isNewVersion(currentVersion, githubVersion)) { if (isStartupCheck()) { - plugin.getLogger().info(String.format("ServerUtils %s is available!", githubVersion)); + plugin.getLogger().info(String.format(UPDATE_AVAILABLE, githubVersion)); plugin.getLogger().info("Release info: " + body); } if (canDownloadPlugin()) { if (isStartupCheck()) { - plugin.getLogger().info("Started downloading from \"" + downloadLink + "\"..."); + plugin.getLogger().info(String.format(DOWNLOAD_START, downloadLink)); } else { Messenger.sendMessage(sender, "serverutils.update.downloading", "%old%", currentVersion, @@ -131,11 +156,11 @@ public class UpdateCheckerTask implements Runnable { download(downloadLink, getPluginFile()); } catch (IOException ex) { broadcastDownloadStatus(githubVersion, true); - throw new RuntimeException("Error downloading a new version of ServerUtils", ex); + throw new RuntimeException(DOWNLOAD_ERROR, ex); } if (isStartupCheck()) { - plugin.getLogger().info(String.format("Downloaded ServerUtils version v%s. Restarting plugin now...", githubVersion)); + plugin.getLogger().info(String.format(DOWNLOADED_RESTART, githubVersion)); Bukkit.getPluginManager().disablePlugin(plugin); try { Bukkit.getPluginManager().enablePlugin(Bukkit.getPluginManager().loadPlugin(getPluginFile())); @@ -143,7 +168,7 @@ public class UpdateCheckerTask implements Runnable { ex.printStackTrace(); return; } - plugin.getLogger().info(String.format("Successfully upgraded ServerUtils to v%s!", githubVersion)); + plugin.getLogger().info(String.format(UPGRADE_SUCCESS, githubVersion)); } else { versionManager.setDownloadedVersion(githubVersion); broadcastDownloadStatus(githubVersion, false); @@ -186,7 +211,7 @@ public class UpdateCheckerTask implements Runnable { return sb.toString(); } - private JsonElement readJsonFromURL(String url) throws IOException { + private JsonElement readJsonFromUrl(String url) throws IOException { try (InputStream is = stream(url)) { BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); String jsonText = readAll(reader); @@ -196,7 +221,7 @@ public class UpdateCheckerTask implements Runnable { private InputStream stream(String url) throws IOException { URLConnection conn = new URL(url).openConnection(); - conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0) Gecko/20100101 Firefox/77.0"); + conn.setRequestProperty("User-Agent", USER_AGENT); conn.setConnectTimeout(10000); return conn.getInputStream(); } diff --git a/src/main/java/net/frankheijden/serverutils/utils/FormatBuilder.java b/src/main/java/net/frankheijden/serverutils/utils/FormatBuilder.java index 469ad69..a7e6fbb 100644 --- a/src/main/java/net/frankheijden/serverutils/utils/FormatBuilder.java +++ b/src/main/java/net/frankheijden/serverutils/utils/FormatBuilder.java @@ -1,11 +1,11 @@ package net.frankheijden.serverutils.utils; -import net.frankheijden.serverutils.config.Messenger; -import org.bukkit.command.CommandSender; - import java.util.ArrayList; import java.util.List; +import net.frankheijden.serverutils.config.Messenger; +import org.bukkit.command.CommandSender; + public class FormatBuilder { private final String format; @@ -39,6 +39,10 @@ public class FormatBuilder { return this; } + /** + * Builds the format and sends it to the CommandSender. + * @param sender The receiver of the list. + */ public void sendTo(CommandSender sender) { valueList.forEach(values -> { int length = Math.min(values.length, orderedKeys.length); diff --git a/src/main/java/net/frankheijden/serverutils/utils/ForwardFilter.java b/src/main/java/net/frankheijden/serverutils/utils/ForwardFilter.java index a1caa0b..3dfd478 100644 --- a/src/main/java/net/frankheijden/serverutils/utils/ForwardFilter.java +++ b/src/main/java/net/frankheijden/serverutils/utils/ForwardFilter.java @@ -1,14 +1,19 @@ package net.frankheijden.serverutils.utils; +import java.util.logging.Level; +import java.util.logging.LogRecord; + import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; -import java.util.logging.*; - public class ForwardFilter extends PredicateFilter { private boolean warnings; + /** + * Creates a filter which forwards all output to the sender. + * @param sender The sender to forward logs to. + */ public ForwardFilter(CommandSender sender) { this.warnings = false; diff --git a/src/main/java/net/frankheijden/serverutils/utils/ListBuilder.java b/src/main/java/net/frankheijden/serverutils/utils/ListBuilder.java index 5fc011b..062bafd 100644 --- a/src/main/java/net/frankheijden/serverutils/utils/ListBuilder.java +++ b/src/main/java/net/frankheijden/serverutils/utils/ListBuilder.java @@ -17,6 +17,11 @@ public class ListBuilder { return new ListBuilder<>(collection); } + /** + * Creates a pre-defined ListBuilder with type String. + * @param collection The collection to be used. + * @return The ListBuilder. + */ @SuppressWarnings("unchecked") public static ListBuilder createStrings(Collection collection) { ListBuilder builder = create((Collection) collection); diff --git a/src/main/java/net/frankheijden/serverutils/utils/MapUtils.java b/src/main/java/net/frankheijden/serverutils/utils/MapUtils.java index f7f04d2..a0f1b1e 100644 --- a/src/main/java/net/frankheijden/serverutils/utils/MapUtils.java +++ b/src/main/java/net/frankheijden/serverutils/utils/MapUtils.java @@ -1,10 +1,17 @@ package net.frankheijden.serverutils.utils; -import java.util.*; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; import java.util.function.Predicate; public class MapUtils { + /** + * Removes keys from a map using a predicate. + * @param map The map. + * @param predicate The predicate used to test removal. + */ @SuppressWarnings({"unchecked", "rawtypes"}) public static void removeKeys(Map map, Predicate predicate) { Set keysToRemove = new HashSet<>(); @@ -16,6 +23,11 @@ public class MapUtils { keysToRemove.forEach(map::remove); } + /** + * Removes values from a map using a predicate. + * @param map The map. + * @param predicate The predicate used to test removal. + */ @SuppressWarnings({"unchecked", "rawtypes"}) public static void removeValues(Map map, Predicate predicate) { Set keysToRemove = new HashSet<>(); diff --git a/src/main/java/net/frankheijden/serverutils/utils/PredicateFilter.java b/src/main/java/net/frankheijden/serverutils/utils/PredicateFilter.java index 6e26c5f..bbca442 100644 --- a/src/main/java/net/frankheijden/serverutils/utils/PredicateFilter.java +++ b/src/main/java/net/frankheijden/serverutils/utils/PredicateFilter.java @@ -1,7 +1,9 @@ package net.frankheijden.serverutils.utils; import java.util.function.Predicate; -import java.util.logging.*; +import java.util.logging.Filter; +import java.util.logging.LogRecord; +import java.util.logging.Logger; public class PredicateFilter implements Filter { diff --git a/src/main/java/net/frankheijden/serverutils/utils/VersionUtils.java b/src/main/java/net/frankheijden/serverutils/utils/VersionUtils.java index a44849b..a25005d 100644 --- a/src/main/java/net/frankheijden/serverutils/utils/VersionUtils.java +++ b/src/main/java/net/frankheijden/serverutils/utils/VersionUtils.java @@ -2,6 +2,13 @@ package net.frankheijden.serverutils.utils; public class VersionUtils { + /** + * Compares two versions in X.X.X format. + * Returns true if version is newer than the old one. + * @param oldVersion The old version. + * @param newVersion The new version. + * @return true iff new version is newer than old version. + */ public static boolean isNewVersion(String oldVersion, String newVersion) { String[] oldVersionSplit = oldVersion.split("\\."); String[] newVersionSplit = newVersion.split("\\."); diff --git a/src/main/java/net/frankheijden/serverutils/utils/YamlUtils.java b/src/main/java/net/frankheijden/serverutils/utils/YamlUtils.java new file mode 100644 index 0000000..6a8055e --- /dev/null +++ b/src/main/java/net/frankheijden/serverutils/utils/YamlUtils.java @@ -0,0 +1,51 @@ +package net.frankheijden.serverutils.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) { + MemorySection section = (MemorySection) defaults.get(root); + if (section == null) return; + for (String key : section.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(key) == null) { + yml.set(key, 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; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9e26dfe..09ca140 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1 +1,5 @@ -{} \ No newline at end of file +settings: + disable-plugins-command: false + check-updates: true + download-updates: false + download-at-startup-and-update: false \ No newline at end of file diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 9e26dfe..aa16d35 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -1 +1,56 @@ -{} \ No newline at end of file +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_changed: "&cAccessing the jar file while %action%ing &4%what%&c went wrong, please load the plugin manually!" + invalid_description: "&cAn error occurred while %action%ing &4%what%&c, plugin doesn't have a valid description!" + 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 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 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 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 Plugins&r &8]&m=-------------" + format: "&3%plugin%" + format_disabled: "&c%plugin%" + seperator: "&b, " + last_seperator: " &band " + version: " &8(&a%version%&8)" + footer: "&8&m-------------------------------------------------" + plugininfo: + header: "&8&m-----------=&r&8[ &b&lServerUtils 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 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." \ No newline at end of file