Add Velocity Updater support

This commit is contained in:
Frank van der Heijden 2021-07-25 00:08:56 +02:00
parent 483e91bad1
commit cb78112cb3
No known key found for this signature in database
GPG key ID: B808721C2DD5B5B8
14 changed files with 151 additions and 41 deletions

View file

@ -1,42 +1,89 @@
package net.frankheijden.serverutils.common;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import net.frankheijden.serverutils.common.entities.CloseableResult;
import net.frankheijden.serverutils.common.entities.Result;
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask;
public class ServerUtilsApp<T> {
public class ServerUtilsApp<U extends ServerUtilsPlugin<P, T, C, S>, P, T, C extends ServerCommandSender<S>, S> {
public static final int BSTATS_METRICS_ID = 7790;
public static final String VERSION = "{version}";
private final T platformPlugin;
private final ServerUtilsPlugin plugin;
private final Object platformPlugin;
private final U plugin;
@SuppressWarnings("rawtypes")
private static ServerUtilsApp instance;
private ServerUtilsApp(T platformPlugin, ServerUtilsPlugin plugin) {
private ServerUtilsApp(Object platformPlugin, U plugin) {
this.platformPlugin = platformPlugin;
this.plugin = plugin;
instance = this;
}
public static <T> void init(T obj, ServerUtilsPlugin plugin) {
new ServerUtilsApp<>(obj, plugin);
public static <U extends ServerUtilsPlugin<P, T, C, S>, P, T, C extends ServerCommandSender<S>, S> void init(
Object platformPlugin,
U plugin
) {
instance = new ServerUtilsApp<>(platformPlugin, plugin);
}
/**
* Tries checking for updates if enabled by the config.
*/
public static void tryCheckForUpdates() {
UpdateCheckerTask.tryStart(getPlugin().getChatProvider().getConsoleSender(), "boot");
UpdateCheckerTask.tryStart(getPlugin(), getPlugin().getChatProvider().getConsoleSender(), "boot");
}
public static ServerUtilsPlugin getPlugin() {
return instance.plugin;
/**
* Unloads the ServerUtilsUpdater and deletes the file.
*/
public static <U extends ServerUtilsPlugin<P, T, C, S>, P, T, C extends ServerCommandSender<S>, S>
void unloadServerUtilsUpdater() {
U plugin = getPlugin();
plugin.getTaskManager().runTaskLater(() -> {
String updaterName = plugin.getPlatform() == ServerUtilsPlugin.Platform.VELOCITY
? "serverutilsupdater"
: "ServerUtilsUpdater";
P updaterPlugin = plugin.getPluginManager().getPlugin(updaterName);
if (updaterPlugin == null) return;
@SuppressWarnings("VariableDeclarationUsageDistance")
File file = plugin.getPluginManager().getPluginFile(updaterPlugin);
Result result = plugin.getPluginManager().disablePlugin(updaterPlugin);
if (result != Result.SUCCESS) {
result.sendTo(plugin.getChatProvider().getConsoleSender(), "disabl", updaterName);
return;
}
CloseableResult closeableResult = plugin.getPluginManager().unloadPlugin(updaterName);
if (closeableResult.getResult() != Result.SUCCESS) {
closeableResult.getResult().sendTo(plugin.getChatProvider().getConsoleSender(), "unload", updaterName);
}
closeableResult.tryClose();
if (Files.exists(file.toPath())) {
try {
Files.delete(file.toPath());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}, 10);
}
public static Object getPlatformPlugin() {
return instance.platformPlugin;
}
@SuppressWarnings("unchecked")
public static <T> T getPlatformPlugin() {
return (T) instance.platformPlugin;
public static <U extends ServerUtilsPlugin<P, T, C, S>, P, T, C extends ServerCommandSender<S>, S>
U getPlugin() {
return (U) instance.plugin;
}
}

View file

@ -27,6 +27,8 @@ public abstract class ServerUtilsPlugin<P, T, C extends ServerCommandSender<S>,
public abstract Platform getPlatform();
public abstract P getPlugin();
public CommandsResource getCommandsResource() {
return commandsResource;
}
@ -94,6 +96,7 @@ public abstract class ServerUtilsPlugin<P, T, C extends ServerCommandSender<S>,
reload();
enablePlugin();
ServerUtilsApp.tryCheckForUpdates();
ServerUtilsApp.unloadServerUtilsUpdater();
}
protected void enablePlugin() {

View file

@ -4,7 +4,7 @@ import net.frankheijden.serverutils.common.entities.ServerCommandSender;
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask;
public abstract class PlayerListener<U extends ServerUtilsPlugin<?, ?, C, ?>, C extends ServerCommandSender<?>>
public abstract class PlayerListener<U extends ServerUtilsPlugin<P, ?, C, ?>, P, C extends ServerCommandSender<?>>
extends ServerUtilsListener<U, C> {
protected PlayerListener(U plugin) {
@ -17,7 +17,7 @@ public abstract class PlayerListener<U extends ServerUtilsPlugin<?, ?, C, ?>, C
*/
protected void handleUpdate(C sender) {
if (sender.hasPermission("serverutils.notification.update")) {
UpdateCheckerTask.tryStart(sender, "login");
UpdateCheckerTask.tryStart(plugin, sender, "login");
}
}
}

View file

@ -24,6 +24,8 @@ public interface PluginProvider<P> {
P getPlugin(String pluginName);
Object getInstance(P plugin);
Set<String> getCommands();
/**

View file

@ -22,12 +22,9 @@ import net.frankheijden.serverutils.common.utils.GitHubUtils;
import net.frankheijden.serverutils.common.utils.VersionUtils;
import net.frankheijden.serverutilsupdater.common.Updater;
public class UpdateCheckerTask implements Runnable {
@SuppressWarnings("unchecked")
private static final ServerUtilsPlugin<Object, ?, ?, ?> plugin = ServerUtilsApp.getPlugin();
private static final ServerUtilsConfig config = plugin.getConfigResource().getConfig();
public class UpdateCheckerTask<U extends ServerUtilsPlugin<P, ?, ?, ?>, P> implements Runnable {
private final U plugin;
private final ServerCommandSender<?> sender;
private final boolean download;
private final boolean install;
@ -52,7 +49,8 @@ public class UpdateCheckerTask implements Runnable {
private static final String UPDATER_ENABLE_ERROR = "Failed to enable ServerUtilsUpdater: {0}";
private static final String UP_TO_DATE = "We are up-to-date!";
private UpdateCheckerTask(ServerCommandSender<?> sender, boolean download, boolean install) {
private UpdateCheckerTask(U plugin, ServerCommandSender<?> sender, boolean download, boolean install) {
this.plugin = plugin;
this.sender = sender;
this.download = download;
this.install = install;
@ -62,9 +60,14 @@ public class UpdateCheckerTask implements Runnable {
* Checks for updates if enabled per config for the specific action.
* Action must be 'login' or 'boot'.
*/
public static void tryStart(ServerCommandSender<?> sender, String action) {
public static <U extends ServerUtilsPlugin<P, ?, ?, ?>, P> void tryStart(
U plugin,
ServerCommandSender<?> sender,
String action
) {
ServerUtilsConfig config = ServerUtilsApp.getPlugin().getConfigResource().getConfig();
if (config.getBoolean("settings.check-updates-" + action)) {
start(sender, action);
start(plugin, sender, action);
}
}
@ -72,8 +75,14 @@ public class UpdateCheckerTask implements Runnable {
* Checks for updates and downloads/installs if configured.
* Action must be 'login' or 'boot'.
*/
public static void start(ServerCommandSender<?> sender, String action) {
plugin.getTaskManager().runTaskAsynchronously(new UpdateCheckerTask(
public static <U extends ServerUtilsPlugin<P, ?, ?, ?>, P> void start(
U plugin,
ServerCommandSender<?> sender,
String action
) {
ServerUtilsConfig config = ServerUtilsApp.getPlugin().getConfigResource().getConfig();
ServerUtilsApp.getPlugin().getTaskManager().runTaskAsynchronously(new UpdateCheckerTask<>(
plugin,
sender,
config.getBoolean("settings.download-updates-" + action),
config.getBoolean("settings.install-updates-" + action)
@ -227,25 +236,26 @@ public class UpdateCheckerTask implements Runnable {
}
private void deletePlugin() {
plugin.getPluginManager().getPluginFile(ServerUtilsApp.getPlatformPlugin()).delete();
plugin.getPluginManager().getPluginFile(plugin.getPlugin()).delete();
}
private void tryReloadPlugin(File pluginFile, File updaterFile) {
plugin.getTaskManager().runTask(() -> {
LoadResult<?> loadResult = plugin.getPluginManager().loadPlugin(updaterFile);
Updater updater = (Updater) plugin.getPluginManager().getPlugin("ServerUtilsUpdater");
if (!loadResult.isSuccess() && updater == null) {
LoadResult<P> loadResult = plugin.getPluginManager().loadPlugin(updaterFile);
if (!loadResult.isSuccess()) {
plugin.getLogger().log(Level.INFO, UPDATER_LOAD_ERROR,
loadResult.getResult().name());
return;
}
Result result = plugin.getPluginManager().enablePlugin(updater);
P updaterPlugin = loadResult.get();
Result result = plugin.getPluginManager().enablePlugin(updaterPlugin);
if (result != Result.SUCCESS && result != Result.ALREADY_ENABLED) {
plugin.getLogger().log(Level.INFO, UPDATER_ENABLE_ERROR, result.name());
return;
}
Updater updater = (Updater) plugin.getPluginManager().getInstance(updaterPlugin);
updater.update(pluginFile);
updaterFile.delete();
});