From ae0005a82afae77c397f41a417468a3e4ff41504 Mon Sep 17 00:00:00 2001 From: Frank van der Heijden Date: Wed, 4 Aug 2021 00:42:38 +0200 Subject: [PATCH] Add /su restart --- .../common/commands/CommandServerUtils.java | 41 ++++++++++++- .../common/config/CommandsResource.java | 17 ++++++ .../serverutils/common/config/MessageKey.java | 4 +- .../common/tasks/PluginWatcherTask.java | 2 +- .../common/tasks/UpdateCheckerTask.java | 59 +++++++++++-------- Common/src/main/resources/commands.json | 16 +++++ Common/src/main/resources/messages.json | 6 +- 7 files changed, 118 insertions(+), 27 deletions(-) diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/commands/CommandServerUtils.java b/Common/src/main/java/net/frankheijden/serverutils/common/commands/CommandServerUtils.java index 7c9ce36..de81bae 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/commands/CommandServerUtils.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/commands/CommandServerUtils.java @@ -7,6 +7,7 @@ import cloud.commandframework.context.CommandContext; import java.io.File; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.function.Consumer; @@ -26,6 +27,7 @@ import net.frankheijden.serverutils.common.entities.results.Result; import net.frankheijden.serverutils.common.entities.ServerUtilsAudience; import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin; import net.frankheijden.serverutils.common.managers.AbstractPluginManager; +import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask; import net.frankheijden.serverutils.common.utils.ListComponentBuilder; import net.frankheijden.serverutils.common.utils.KeyValueComponentBuilder; import net.kyori.adventure.text.Component; @@ -58,6 +60,8 @@ public abstract class CommandServerUtils context) { + C sender = context.getSender(); + + if (checkDependingPlugins(context, sender, Collections.singletonList(plugin.getPlugin()), "restart")) { + return; + } + + UpdateCheckerTask.restart(sender); + } + private void handleLoadPlugin(CommandContext context) { C sender = context.getSender(); List jarFiles = Arrays.asList(context.get("jarFiles")); @@ -214,8 +228,12 @@ public abstract class CommandServerUtils reloadResults = plugin.getPluginManager().reloadPlugins(plugins); - reloadResults.sendTo(sender, MessageKey.RELOADPLUGIN); + reloadResults.sendTo(sender, MessageKey.RELOADPLUGIN_SUCCESS); } protected boolean checkDependingPlugins(CommandContext context, C sender, List

plugins, String subcommand) { @@ -260,6 +278,23 @@ public abstract class CommandServerUtils context, C sender, List

plugins) { + for (P loadedPlugin : plugins) { + if (plugin.getPlugin() == loadedPlugin) { + String restartCommand = plugin.getCommandsResource().getAllAliases(getRawPath("restart")).stream() + .min(Comparator.comparingInt(String::length)) + .orElse("restart"); + Component component = plugin.getMessagesResource().get(MessageKey.RELOADPLUGIN_SERVERUTILS).toComponent( + Template.of("command", context.getRawInput().peekFirst() + " " + restartCommand) + ); + sender.sendMessage(component); + return true; + } + } + + return false; + } + private void handleWatchPlugin(CommandContext context) { C sender = context.getSender(); List

plugins = Arrays.asList(context.get("plugins")); @@ -268,6 +303,10 @@ public abstract class CommandServerUtils getAllAliases(String path) { + Object object = getConfig().get(path); + if (object instanceof ServerUtilsConfig) { + ServerUtilsConfig config = (ServerUtilsConfig) object; + + Set aliases = new HashSet<>(); + aliases.add(config.getString("main")); + aliases.addAll(config.getStringList("aliases")); + return aliases; + } + + return Collections.emptySet(); + } + @Override public void migrate(int currentConfigVersion) { diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/config/MessageKey.java b/Common/src/main/java/net/frankheijden/serverutils/common/config/MessageKey.java index c7448a7..52362ce 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/config/MessageKey.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/config/MessageKey.java @@ -7,7 +7,9 @@ public enum MessageKey implements PlaceholderConfigKey { RELOAD("reload", false), LOADPLUGIN("loadplugin"), UNLOADPLUGIN("unloadplugin"), - RELOADPLUGIN("reloadplugin"), + SERVERUTILS_UPDATER("serverutils-updater", false), + RELOADPLUGIN_SUCCESS("reloadplugin.success"), + RELOADPLUGIN_SERVERUTILS("reloadplugin.serverutils"), GENERIC_PREFIX("generic.prefix", false), GENERIC_ERROR("generic.error", false), GENERIC_NOT_EXISTS("generic.not-exists"), diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java b/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java index 4fa1a56..26b084f 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java @@ -167,7 +167,7 @@ public class PluginWatcherTask extends AbstractTask { fileNameToWatchEntryMap.clear(); PluginResults

reloadResults = pluginManager.reloadPlugins(plugins); - reloadResults.sendTo(sender, MessageKey.RELOADPLUGIN); + reloadResults.sendTo(sender, MessageKey.RELOADPLUGIN_SUCCESS); for (PluginResult

reloadResult : reloadResults) { if (!reloadResult.isSuccess()) continue; diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/tasks/UpdateCheckerTask.java b/Common/src/main/java/net/frankheijden/serverutils/common/tasks/UpdateCheckerTask.java index f016811..5986a03 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/tasks/UpdateCheckerTask.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/tasks/UpdateCheckerTask.java @@ -39,18 +39,14 @@ public class UpdateCheckerTask, P> im private static final String UPDATE_CHECK_START = "Checking for updates..."; private static final String RATE_LIMIT = "Received ratelimit from GitHub."; - private static final String GENERAL_ERROR = "Error fetching new version of ServerUtils"; + private static final String GENERAL_ERROR = "Error fetching GitHub"; private static final String TRY_LATER = GENERAL_ERROR + ", please try again later!"; private static final String CONNECTION_ERROR = GENERAL_ERROR + ": ({0}) {1} (maybe check your connection?)"; private static final String UNAVAILABLE = GENERAL_ERROR + ": ({0}) {1} (no update available)"; private static final String UPDATE_AVAILABLE = "ServerUtils {0} is available!"; private static final String RELEASE_INFO = "Release info: {0}"; private static final String DOWNLOAD_START = "Started downloading from \"{0}\"..."; - private static final String DOWNLOAD_ERROR = "Error downloading a new version of ServerUtils"; - private static final String DOWNLOADED = "Downloaded ServerUtils version v{0}."; - private static final String DOWNLOADED_RESTART = DOWNLOADED + " Restarting plugin now..."; - private static final String UPDATER_LOAD_ERROR = "Failed to load ServerUtilsUpdater: {0}"; - private static final String UPDATER_ENABLE_ERROR = "Failed to enable ServerUtilsUpdater: {0}"; + private static final String DOWNLOADED = "Downloaded {0} version v{1}."; private static final String UP_TO_DATE = "We are up-to-date!"; private UpdateCheckerTask(U plugin, ServerUtilsAudience sender, boolean download, boolean install) { @@ -93,6 +89,16 @@ public class UpdateCheckerTask, P> im )); } + /** + * Restarts the plugin. + */ + public static

void restart(ServerUtilsAudience sender) { + ServerUtilsApp.getPlugin().getTaskManager().runTaskAsynchronously(() -> { + UpdateCheckerTask task = new UpdateCheckerTask<>(ServerUtilsApp.getPlugin(), sender, true, true); + task.downloadUpdaterAndReload(task.plugin.getPluginManager().getPluginFile(task.plugin.getPlugin())); + }); + } + @Override public void run() { UpdateManager updateManager = plugin.getUpdateManager(); @@ -146,18 +152,25 @@ public class UpdateCheckerTask, P> im } File pluginTarget = new File(plugin.getPluginManager().getPluginsFolder(), pluginAsset.getName()); - download(githubVersion, pluginAsset.getDownloadUrl(), pluginTarget); + download(pluginAsset.getDownloadUrl(), pluginTarget); updateManager.setDownloadedVersion(githubVersion); if (!install) { deletePlugin(); if (sender.isPlayer()) { broadcastDownloadStatus(githubVersion, false); } else { - plugin.getLogger().log(Level.INFO, DOWNLOADED, githubVersion); + plugin.getLogger().log(Level.INFO, DOWNLOADED, new Object[]{ "ServerUtils", githubVersion }); } return; } + downloadUpdaterAndReload(pluginTarget); + } + + /** + * Downloads the updater and restarts the plugin. + */ + public void downloadUpdaterAndReload(File pluginTarget) { GitHubResponse updaterResponse = getResponse(GITHUB_UPDATER_LINK); if (updaterResponse == null) return; @@ -169,10 +182,12 @@ public class UpdateCheckerTask, P> im plugin.getLogger().log(Level.INFO, DOWNLOAD_START, updaterAsset.getDownloadUrl()); File updaterTarget = new File(plugin.getPluginManager().getPluginsFolder(), updaterAsset.getName()); - download(githubVersion, updaterAsset.getDownloadUrl(), updaterTarget); - plugin.getLogger().log(Level.INFO, DOWNLOADED_RESTART, githubVersion); + download(updaterAsset.getDownloadUrl(), updaterTarget); + plugin.getLogger().log(Level.INFO, DOWNLOADED, new Object[]{ "ServerUtilsUpdater", getVersion(updaterJson) }); - deletePlugin(); + if (!pluginTarget.equals(getPluginFile())) { + deletePlugin(); + } tryReloadPlugin(pluginTarget, updaterTarget); } @@ -223,12 +238,7 @@ public class UpdateCheckerTask, P> im return jsonObject.getAsJsonPrimitive("tag_name").getAsString().replace("v", ""); } - private void download(String githubVersion, String downloadLink, File target) { - if (downloadLink == null) { - broadcastDownloadStatus(githubVersion, true); - return; - } - + private void download(String downloadLink, File target) { try { GitHubResponse response = GitHubUtils.stream(downloadLink); if (response.getRateLimit().isRateLimited()) { @@ -238,32 +248,35 @@ public class UpdateCheckerTask, P> im GitHubUtils.download(response, target); } catch (IOException ex) { - broadcastDownloadStatus(githubVersion, true); - throw new RuntimeException(DOWNLOAD_ERROR, ex); + ex.printStackTrace(); } } + private File getPluginFile() { + return plugin.getPluginManager().getPluginFile(plugin.getPlugin()); + } + private void deletePlugin() { - plugin.getPluginManager().getPluginFile(plugin.getPlugin()).delete(); + getPluginFile().delete(); } private void tryReloadPlugin(File pluginFile, File updaterFile) { plugin.getTaskManager().runTask(() -> { PluginResult

loadResult = plugin.getPluginManager().loadPlugin(updaterFile); if (!loadResult.isSuccess()) { - plugin.getLogger().log(Level.INFO, UPDATER_LOAD_ERROR, loadResult.getResult().name()); + loadResult.sendTo(sender, null); return; } PluginResult

enableResult = plugin.getPluginManager().enablePlugin(loadResult.getPlugin()); if (!enableResult.isSuccess() && enableResult.getResult() != Result.ALREADY_ENABLED) { - plugin.getLogger().log(Level.INFO, UPDATER_ENABLE_ERROR, enableResult.getResult().name()); + loadResult.sendTo(sender, null); return; } Updater updater = (Updater) plugin.getPluginManager().getInstance(enableResult.getPlugin()); + sender.sendMessage(plugin.getMessagesResource().get(MessageKey.SERVERUTILS_UPDATER).toComponent()); updater.update(pluginFile); - updaterFile.delete(); }); } diff --git a/Common/src/main/resources/commands.json b/Common/src/main/resources/commands.json index ffecd21..59f4d11 100644 --- a/Common/src/main/resources/commands.json +++ b/Common/src/main/resources/commands.json @@ -37,6 +37,22 @@ "description": "Reloads the ServerUtils plugin.", "display-in-help": true }, + "restart": { + "main": "restart", + "aliases": [], + "permission": "serverutils.restart", + "description": "Restart the ServerUtils plugin.", + "display-in-help": true, + "flags": { + "force": { + "main": "force", + "aliases": ["f"], + "permission": "serverutils.restart", + "description": "Force restart of the ServerUtils plugin.", + "display-in-help": false + } + } + }, "loadplugin": { "main": "loadplugin", "aliases": ["lp"], diff --git a/Common/src/main/resources/messages.json b/Common/src/main/resources/messages.json index 91aac66..455e15c 100644 --- a/Common/src/main/resources/messages.json +++ b/Common/src/main/resources/messages.json @@ -15,9 +15,13 @@ "file-deleted": "File of plugin has been deleted!" }, "reload": "Successfully reloaded ServerUtils configurations!", + "serverutils-updater": "Loaded and enabled ServerUtilsUpdater. Completion can be monitored from the console, attempting restart now...", "loadplugin": "Successfully loaded !", "unloadplugin": "Successfully unloaded !", - "reloadplugin": "Successfully reloaded !", + "reloadplugin": { + "success": "Successfully reloaded !", + "serverutils": "Sorry, but you can't reload ServerUtils this way. Please restart using /." + }, "depending-plugins": { "prefix": "Plugin has depending plugins: ", "format": "",