More abstractions
- Abstracted UpdateCheckerTask - Merged PluginProvider into the PluginManagers
This commit is contained in:
parent
23e8e80191
commit
e67b20dee0
40 changed files with 546 additions and 347 deletions
|
|
@ -1,17 +1,41 @@
|
|||
package net.frankheijden.serverutils.common;
|
||||
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask;
|
||||
|
||||
public class ServerUtilsApp {
|
||||
public class ServerUtilsApp<T> {
|
||||
|
||||
public static final int BSTATS_METRICS_ID = 7790;
|
||||
private static ServerUtilsPlugin plugin;
|
||||
|
||||
public static void init(ServerUtilsPlugin plugin) {
|
||||
ServerUtilsApp.plugin = plugin;
|
||||
private final T platformPlugin;
|
||||
private final ServerUtilsPlugin plugin;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static ServerUtilsApp instance;
|
||||
|
||||
private ServerUtilsApp(T platformPlugin, ServerUtilsPlugin plugin) {
|
||||
this.platformPlugin = platformPlugin;
|
||||
this.plugin = plugin;
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public static <T> void init(T obj, ServerUtilsPlugin plugin) {
|
||||
new ServerUtilsApp<>(obj, plugin);
|
||||
}
|
||||
|
||||
public static void checkForUpdates() {
|
||||
if (Config.getInstance().getConfig().getBoolean("settings.check-updates")) {
|
||||
UpdateCheckerTask.start(getPlugin().getChatProvider().getConsoleSender(), true);
|
||||
}
|
||||
}
|
||||
|
||||
public static ServerUtilsPlugin getPlugin() {
|
||||
return plugin;
|
||||
return instance.plugin;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getPlatformPlugin() {
|
||||
return (T) instance.platformPlugin;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ public class Messenger extends YamlResource {
|
|||
if (message != null) {
|
||||
return StringUtils.apply(message, replacements);
|
||||
} else {
|
||||
instance.plugin.getLogger().severe("Missing locale in messages.yml at path '" + path + "'!");
|
||||
Messenger.plugin.getLogger().severe("Missing locale in messages.yml at path '" + path + "'!");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -44,7 +44,7 @@ public class Messenger extends YamlResource {
|
|||
public static void sendRawMessage(ServerCommandSender sender, String msg, String... replacements) {
|
||||
String message = StringUtils.apply(msg, replacements);
|
||||
if (message != null) {
|
||||
sender.sendMessage(instance.plugin.getColorProvider().apply(message));
|
||||
sender.sendMessage(Messenger.plugin.getChatProvider().color(message));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -57,11 +57,11 @@ public class Messenger extends YamlResource {
|
|||
public static void sendMessage(ServerCommandSender sender, String path, String... replacements) {
|
||||
String message = getMessage(path, replacements);
|
||||
if (message != null) {
|
||||
sender.sendMessage(instance.plugin.getColorProvider().apply(message));
|
||||
sender.sendMessage(Messenger.plugin.getChatProvider().color(message));
|
||||
}
|
||||
}
|
||||
|
||||
public static String color(String str) {
|
||||
return instance.plugin.getColorProvider().apply(str);
|
||||
return Messenger.plugin.getChatProvider().color(str);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,4 +3,6 @@ package net.frankheijden.serverutils.common.entities;
|
|||
public interface ServerCommandSender {
|
||||
|
||||
void sendMessage(String message);
|
||||
|
||||
boolean hasPermission(String permission);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,23 +2,26 @@ package net.frankheijden.serverutils.common.entities;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.frankheijden.serverutils.common.providers.ColorProvider;
|
||||
import net.frankheijden.serverutils.common.providers.PluginProvider;
|
||||
import net.frankheijden.serverutils.common.managers.AbstractPluginManager;
|
||||
import net.frankheijden.serverutils.common.managers.AbstractTaskManager;
|
||||
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
||||
import net.frankheijden.serverutils.common.providers.ChatProvider;
|
||||
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||
import net.frankheijden.serverutils.common.utils.FileUtils;
|
||||
|
||||
public abstract class ServerUtilsPlugin {
|
||||
|
||||
public abstract <T> PluginProvider<T> getPluginProvider();
|
||||
public abstract <T> AbstractPluginManager<T> getPluginManager();
|
||||
|
||||
public abstract AbstractTaskManager getTaskManager();
|
||||
|
||||
public abstract ResourceProvider getResourceProvider();
|
||||
|
||||
public abstract ColorProvider getColorProvider();
|
||||
public abstract ChatProvider getChatProvider();
|
||||
|
||||
public abstract AbstractVersionManager getVersionManager();
|
||||
|
||||
public abstract Logger getLogger();
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
package net.frankheijden.serverutils.common.listeners;
|
||||
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask;
|
||||
|
||||
public class ServerListener {
|
||||
|
||||
private static final YamlConfig config = Config.getInstance().getConfig();
|
||||
|
||||
public static void handleUpdate(ServerCommandSender sender) {
|
||||
if (!config.getBoolean("settings.check-updates-login")) return;
|
||||
|
||||
if (sender.hasPermission("serverutils.notification.update")) {
|
||||
UpdateCheckerTask.start(sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package net.frankheijden.serverutils.common.managers;
|
||||
|
||||
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||
import net.frankheijden.serverutils.common.entities.LoadResult;
|
||||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import net.frankheijden.serverutils.common.providers.PluginProvider;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public abstract class AbstractPluginManager<T> extends PluginProvider<T> {
|
||||
|
||||
public abstract LoadResult<T> loadPlugin(String pluginFile);
|
||||
|
||||
public abstract LoadResult<T> loadPlugin(File file);
|
||||
|
||||
public abstract Result enablePlugin(T plugin);
|
||||
|
||||
public abstract CloseableResult reloadPlugin(String pluginName);
|
||||
|
||||
public abstract CloseableResult reloadPlugin(T plugin);
|
||||
|
||||
public abstract CloseableResult unloadPlugin(String pluginName);
|
||||
|
||||
public abstract CloseableResult unloadPlugin(T plugin);
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package net.frankheijden.serverutils.common.managers;
|
||||
|
||||
public abstract class AbstractTaskManager {
|
||||
|
||||
public abstract void runTaskAsynchronously(Runnable runnable);
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
package net.frankheijden.serverutils.common.managers;
|
||||
|
||||
public abstract class AbstractVersionManager {
|
||||
|
||||
private final String currentVersion;
|
||||
private String downloadedVersion;
|
||||
|
||||
/**
|
||||
* Creates a new VersionManager instance.
|
||||
* Used for automatic updating.
|
||||
* @param currentVersion The current version of the plugin.
|
||||
*/
|
||||
public AbstractVersionManager(String currentVersion) {
|
||||
this.currentVersion = currentVersion;
|
||||
this.downloadedVersion = currentVersion;
|
||||
}
|
||||
|
||||
public String getCurrentVersion() {
|
||||
return currentVersion;
|
||||
}
|
||||
|
||||
public String getDownloadedVersion() {
|
||||
return downloadedVersion;
|
||||
}
|
||||
|
||||
public boolean hasDownloaded() {
|
||||
return !downloadedVersion.equals(currentVersion);
|
||||
}
|
||||
|
||||
public boolean isDownloaded(String version) {
|
||||
return downloadedVersion.equals(version);
|
||||
}
|
||||
|
||||
public void setDownloaded(String version) {
|
||||
this.downloadedVersion = version;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package net.frankheijden.serverutils.common.providers;
|
||||
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
|
||||
public abstract class ChatProvider {
|
||||
|
||||
public abstract ServerCommandSender getConsoleSender();
|
||||
|
||||
public abstract String color(String str);
|
||||
|
||||
public abstract void broadcast(String permission, String message);
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
package net.frankheijden.serverutils.common.providers;
|
||||
|
||||
public interface ColorProvider {
|
||||
|
||||
String apply(String str);
|
||||
}
|
||||
|
|
@ -14,6 +14,8 @@ public abstract class PluginProvider<T> {
|
|||
|
||||
public abstract String getPluginName(T plugin);
|
||||
|
||||
public abstract File getPluginFile(T plugin);
|
||||
|
||||
public List<T> getPluginsSorted() {
|
||||
List<T> plugins = getPlugins();
|
||||
plugins.sort(Comparator.comparing(this::getPluginName));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,189 @@
|
|||
package net.frankheijden.serverutils.common.tasks;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||
import net.frankheijden.serverutils.common.entities.CloseableResult;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.frankheijden.serverutils.common.utils.FileUtils;
|
||||
import net.frankheijden.serverutils.common.utils.VersionUtils;
|
||||
|
||||
public class UpdateCheckerTask implements Runnable {
|
||||
|
||||
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();
|
||||
private static final YamlConfig config = Config.getInstance().getConfig();
|
||||
|
||||
private final AbstractVersionManager versionManager;
|
||||
private final ServerCommandSender sender;
|
||||
private final boolean startup;
|
||||
|
||||
private static final String GITHUB_LINK = "https://api.github.com/repos/FrankHeijden/ServerUtils/releases/latest";
|
||||
|
||||
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 TRY_LATER = GENERAL_ERROR + ", please try again later!";
|
||||
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 static final String UP_TO_DATE = "We are up-to-date!";
|
||||
|
||||
private UpdateCheckerTask(ServerCommandSender sender, boolean startup) {
|
||||
this.versionManager = plugin.getVersionManager();
|
||||
this.sender = sender;
|
||||
this.startup = startup;
|
||||
}
|
||||
|
||||
public static void start(ServerCommandSender sender) {
|
||||
start(sender, false);
|
||||
}
|
||||
|
||||
public static void start(ServerCommandSender sender, boolean startup) {
|
||||
UpdateCheckerTask task = new UpdateCheckerTask(sender, startup);
|
||||
plugin.getTaskManager().runTaskAsynchronously(task);
|
||||
}
|
||||
|
||||
public boolean isStartupCheck() {
|
||||
return this.startup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (isStartupCheck()) {
|
||||
plugin.getLogger().info(UPDATE_CHECK_START);
|
||||
}
|
||||
|
||||
JsonElement jsonElement;
|
||||
try {
|
||||
jsonElement = FileUtils.readJsonFromUrl(GITHUB_LINK);
|
||||
} catch (ConnectException | UnknownHostException | SocketTimeoutException ex) {
|
||||
plugin.getLogger().severe(String.format(CONNECTION_ERROR, ex.getClass().getSimpleName(), ex.getMessage()));
|
||||
return;
|
||||
} catch (FileNotFoundException ex) {
|
||||
plugin.getLogger().severe(String.format(UNAVAILABLE, ex.getClass().getSimpleName(), ex.getMessage()));
|
||||
return;
|
||||
} catch (IOException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, ex, () -> GENERAL_ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
if (jsonElement == null) {
|
||||
plugin.getLogger().warning(TRY_LATER);
|
||||
return;
|
||||
}
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
if (jsonObject.has("message")) {
|
||||
plugin.getLogger().warning(jsonObject.get("message").getAsString());
|
||||
return;
|
||||
}
|
||||
|
||||
String githubVersion = getVersion(jsonObject);
|
||||
String body = jsonObject.getAsJsonPrimitive("body").getAsString();
|
||||
String downloadUrl = getDownloadUrl(jsonObject);
|
||||
|
||||
String downloaded = versionManager.getDownloadedVersion();
|
||||
String current = versionManager.getCurrentVersion();
|
||||
if (VersionUtils.isNewVersion(downloaded, githubVersion)) {
|
||||
if (isStartupCheck()) {
|
||||
plugin.getLogger().info(String.format(UPDATE_AVAILABLE, githubVersion));
|
||||
plugin.getLogger().info("Release info: " + body);
|
||||
}
|
||||
if (canDownloadPlugin()) {
|
||||
if (isStartupCheck()) {
|
||||
plugin.getLogger().info(String.format(DOWNLOAD_START, downloadUrl));
|
||||
} else {
|
||||
Messenger.sendMessage(sender, "serverutils.update.downloading",
|
||||
"%old%", current,
|
||||
"%new%", githubVersion,
|
||||
"%info%", body);
|
||||
}
|
||||
downloadPlugin(githubVersion, downloadUrl);
|
||||
tryReloadPlugin();
|
||||
} else if (!isStartupCheck()) {
|
||||
Messenger.sendMessage(sender, "serverutils.update.available",
|
||||
"%old%", current,
|
||||
"%new%", githubVersion,
|
||||
"%info%", body);
|
||||
}
|
||||
} else if (versionManager.hasDownloaded()) {
|
||||
Messenger.sendMessage(sender, "serverutils.update.success",
|
||||
"%new%", versionManager.getDownloadedVersion());
|
||||
} else if (isStartupCheck()) {
|
||||
plugin.getLogger().info(UP_TO_DATE);
|
||||
}
|
||||
}
|
||||
|
||||
private String getVersion(JsonObject jsonObject) {
|
||||
return jsonObject.getAsJsonPrimitive("tag_name").getAsString().replace("v", "");
|
||||
}
|
||||
|
||||
private String getDownloadUrl(JsonObject jsonObject) {
|
||||
JsonArray assets = jsonObject.getAsJsonArray("assets");
|
||||
if (assets != null && assets.size() > 0) {
|
||||
return assets.get(0).getAsJsonObject().getAsJsonPrimitive("browser_download_url").getAsString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean canDownloadPlugin() {
|
||||
if (isStartupCheck()) return config.getBoolean("settings.download-at-startup-and-update");
|
||||
return config.getBoolean("settings.download-updates");
|
||||
}
|
||||
|
||||
private void downloadPlugin(String githubVersion, String downloadLink) {
|
||||
if (versionManager.isDownloaded(githubVersion)) {
|
||||
broadcastDownloadStatus(githubVersion, false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (downloadLink == null) {
|
||||
broadcastDownloadStatus(githubVersion, true);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
FileUtils.download(downloadLink, plugin.getPluginManager().getPluginFile(ServerUtilsApp.getPlatformPlugin()));
|
||||
} catch (IOException ex) {
|
||||
broadcastDownloadStatus(githubVersion, true);
|
||||
throw new RuntimeException(DOWNLOAD_ERROR, ex);
|
||||
}
|
||||
|
||||
versionManager.setDownloaded(githubVersion);
|
||||
}
|
||||
|
||||
private void tryReloadPlugin() {
|
||||
String downloadedVersion = versionManager.getDownloadedVersion();
|
||||
|
||||
if (isStartupCheck()) {
|
||||
plugin.getLogger().info(String.format(DOWNLOADED_RESTART, downloadedVersion));
|
||||
CloseableResult result = plugin.getPluginManager().reloadPlugin(ServerUtilsApp.getPlatformPlugin());
|
||||
plugin.getLogger().info(String.format(UPGRADE_SUCCESS, downloadedVersion));
|
||||
result.tryClose();
|
||||
} else {
|
||||
broadcastDownloadStatus(downloadedVersion, false);
|
||||
}
|
||||
}
|
||||
|
||||
private void broadcastDownloadStatus(String githubVersion, boolean isError) {
|
||||
final String path = "serverutils.update." + (isError ? "failed" : "success");
|
||||
String message = Messenger.getMessage(path,"%new%", githubVersion);
|
||||
plugin.getChatProvider().broadcast("serverutils.notification.update", message);
|
||||
}
|
||||
}
|
||||
|
|
@ -24,6 +24,7 @@ public class ListBuilder<T> {
|
|||
return new ListBuilder<>(new ArrayList<>(list));
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> ListBuilder<T> create(T... elements) {
|
||||
return new ListBuilder<>(Arrays.asList(elements));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue