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

@ -62,6 +62,11 @@ public class BukkitPlugin extends ServerUtilsPlugin<
return Platform.BUKKIT; return Platform.BUKKIT;
} }
@Override
public Plugin getPlugin() {
return plugin;
}
@Override @Override
public BukkitPluginManager getPluginManager() { public BukkitPluginManager getPluginManager() {
return pluginManager; return pluginManager;

View file

@ -7,8 +7,11 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;
public class BukkitPlayerListener extends PlayerListener<BukkitPlugin, BukkitCommandSender> implements Listener { public class BukkitPlayerListener
extends PlayerListener<BukkitPlugin, Plugin, BukkitCommandSender>
implements Listener {
public BukkitPlayerListener(BukkitPlugin plugin) { public BukkitPlayerListener(BukkitPlugin plugin) {
super(plugin); super(plugin);

View file

@ -437,6 +437,11 @@ public class BukkitPluginManager implements AbstractPluginManager<Plugin> {
return Bukkit.getPluginManager().getPlugin(pluginName); return Bukkit.getPluginManager().getPlugin(pluginName);
} }
@Override
public Plugin getInstance(Plugin plugin) {
return plugin;
}
@Override @Override
public Set<String> getCommands() { public Set<String> getCommands() {
Map<String, Command> knownCommands = getKnownCommands(); Map<String, Command> knownCommands = getKnownCommands();

View file

@ -55,6 +55,11 @@ public class BungeePlugin extends ServerUtilsPlugin<
return Platform.BUNGEE; return Platform.BUNGEE;
} }
@Override
public Plugin getPlugin() {
return plugin;
}
@Override @Override
public BungeePluginManager getPluginManager() { public BungeePluginManager getPluginManager() {
return pluginManager; return pluginManager;

View file

@ -5,9 +5,12 @@ import net.frankheijden.serverutils.bungee.entities.BungeePlugin;
import net.frankheijden.serverutils.common.listeners.PlayerListener; import net.frankheijden.serverutils.common.listeners.PlayerListener;
import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
public class BungeePlayerListener extends PlayerListener<BungeePlugin, BungeeCommandSender> implements Listener { public class BungeePlayerListener
extends PlayerListener<BungeePlugin, Plugin, BungeeCommandSender>
implements Listener {
public BungeePlayerListener(BungeePlugin plugin) { public BungeePlayerListener(BungeePlugin plugin) {
super(plugin); super(plugin);

View file

@ -229,6 +229,11 @@ public class BungeePluginManager implements AbstractPluginManager<Plugin> {
return proxy.getPluginManager().getPlugin(pluginName); return proxy.getPluginManager().getPlugin(pluginName);
} }
@Override
public Plugin getInstance(Plugin plugin) {
return plugin;
}
@Override @Override
public Set<String> getCommands() { public Set<String> getCommands() {
return proxy.getPluginManager().getCommands().stream() return proxy.getPluginManager().getCommands().stream()

View file

@ -1,42 +1,89 @@
package net.frankheijden.serverutils.common; 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.entities.ServerUtilsPlugin;
import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask; 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 int BSTATS_METRICS_ID = 7790;
public static final String VERSION = "{version}"; public static final String VERSION = "{version}";
private final T platformPlugin; private final Object platformPlugin;
private final ServerUtilsPlugin plugin; private final U plugin;
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private static ServerUtilsApp instance; private static ServerUtilsApp instance;
private ServerUtilsApp(T platformPlugin, ServerUtilsPlugin plugin) { private ServerUtilsApp(Object platformPlugin, U plugin) {
this.platformPlugin = platformPlugin; this.platformPlugin = platformPlugin;
this.plugin = plugin; this.plugin = plugin;
instance = this;
} }
public static <T> void init(T obj, ServerUtilsPlugin plugin) { public static <U extends ServerUtilsPlugin<P, T, C, S>, P, T, C extends ServerCommandSender<S>, S> void init(
new ServerUtilsApp<>(obj, plugin); Object platformPlugin,
U plugin
) {
instance = new ServerUtilsApp<>(platformPlugin, plugin);
} }
/** /**
* Tries checking for updates if enabled by the config. * Tries checking for updates if enabled by the config.
*/ */
public static void tryCheckForUpdates() { 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") @SuppressWarnings("unchecked")
public static <T> T getPlatformPlugin() { public static <U extends ServerUtilsPlugin<P, T, C, S>, P, T, C extends ServerCommandSender<S>, S>
return (T) instance.platformPlugin; 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 Platform getPlatform();
public abstract P getPlugin();
public CommandsResource getCommandsResource() { public CommandsResource getCommandsResource() {
return commandsResource; return commandsResource;
} }
@ -94,6 +96,7 @@ public abstract class ServerUtilsPlugin<P, T, C extends ServerCommandSender<S>,
reload(); reload();
enablePlugin(); enablePlugin();
ServerUtilsApp.tryCheckForUpdates(); ServerUtilsApp.tryCheckForUpdates();
ServerUtilsApp.unloadServerUtilsUpdater();
} }
protected void enablePlugin() { 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.entities.ServerUtilsPlugin;
import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask; 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> { extends ServerUtilsListener<U, C> {
protected PlayerListener(U plugin) { protected PlayerListener(U plugin) {
@ -17,7 +17,7 @@ public abstract class PlayerListener<U extends ServerUtilsPlugin<?, ?, C, ?>, C
*/ */
protected void handleUpdate(C sender) { protected void handleUpdate(C sender) {
if (sender.hasPermission("serverutils.notification.update")) { 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); P getPlugin(String pluginName);
Object getInstance(P plugin);
Set<String> getCommands(); 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.serverutils.common.utils.VersionUtils;
import net.frankheijden.serverutilsupdater.common.Updater; import net.frankheijden.serverutilsupdater.common.Updater;
public class UpdateCheckerTask implements Runnable { public class UpdateCheckerTask<U extends ServerUtilsPlugin<P, ?, ?, ?>, P> implements Runnable {
@SuppressWarnings("unchecked")
private static final ServerUtilsPlugin<Object, ?, ?, ?> plugin = ServerUtilsApp.getPlugin();
private static final ServerUtilsConfig config = plugin.getConfigResource().getConfig();
private final U plugin;
private final ServerCommandSender<?> sender; private final ServerCommandSender<?> sender;
private final boolean download; private final boolean download;
private final boolean install; 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 UPDATER_ENABLE_ERROR = "Failed to enable ServerUtilsUpdater: {0}";
private static final String UP_TO_DATE = "We are up-to-date!"; private static final String UP_TO_DATE = "We are up-to-date!";
private UpdateCheckerTask(ServerCommandSender<?> sender, boolean download, boolean install) { private UpdateCheckerTask(U plugin, ServerCommandSender<?> sender, boolean download, boolean install) {
this.plugin = plugin;
this.sender = sender; this.sender = sender;
this.download = download; this.download = download;
this.install = install; this.install = install;
@ -62,9 +60,14 @@ public class UpdateCheckerTask implements Runnable {
* Checks for updates if enabled per config for the specific action. * Checks for updates if enabled per config for the specific action.
* Action must be 'login' or 'boot'. * 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)) { 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. * Checks for updates and downloads/installs if configured.
* Action must be 'login' or 'boot'. * Action must be 'login' or 'boot'.
*/ */
public static void start(ServerCommandSender<?> sender, String action) { public static <U extends ServerUtilsPlugin<P, ?, ?, ?>, P> void start(
plugin.getTaskManager().runTaskAsynchronously(new UpdateCheckerTask( U plugin,
ServerCommandSender<?> sender,
String action
) {
ServerUtilsConfig config = ServerUtilsApp.getPlugin().getConfigResource().getConfig();
ServerUtilsApp.getPlugin().getTaskManager().runTaskAsynchronously(new UpdateCheckerTask<>(
plugin,
sender, sender,
config.getBoolean("settings.download-updates-" + action), config.getBoolean("settings.download-updates-" + action),
config.getBoolean("settings.install-updates-" + action) config.getBoolean("settings.install-updates-" + action)
@ -227,25 +236,26 @@ public class UpdateCheckerTask implements Runnable {
} }
private void deletePlugin() { private void deletePlugin() {
plugin.getPluginManager().getPluginFile(ServerUtilsApp.getPlatformPlugin()).delete(); plugin.getPluginManager().getPluginFile(plugin.getPlugin()).delete();
} }
private void tryReloadPlugin(File pluginFile, File updaterFile) { private void tryReloadPlugin(File pluginFile, File updaterFile) {
plugin.getTaskManager().runTask(() -> { plugin.getTaskManager().runTask(() -> {
LoadResult<?> loadResult = plugin.getPluginManager().loadPlugin(updaterFile); LoadResult<P> loadResult = plugin.getPluginManager().loadPlugin(updaterFile);
Updater updater = (Updater) plugin.getPluginManager().getPlugin("ServerUtilsUpdater"); if (!loadResult.isSuccess()) {
if (!loadResult.isSuccess() && updater == null) {
plugin.getLogger().log(Level.INFO, UPDATER_LOAD_ERROR, plugin.getLogger().log(Level.INFO, UPDATER_LOAD_ERROR,
loadResult.getResult().name()); loadResult.getResult().name());
return; 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) { if (result != Result.SUCCESS && result != Result.ALREADY_ENABLED) {
plugin.getLogger().log(Level.INFO, UPDATER_ENABLE_ERROR, result.name()); plugin.getLogger().log(Level.INFO, UPDATER_ENABLE_ERROR, result.name());
return; return;
} }
Updater updater = (Updater) plugin.getPluginManager().getInstance(updaterPlugin);
updater.update(pluginFile); updater.update(pluginFile);
updaterFile.delete(); updaterFile.delete();
}); });

View file

@ -34,7 +34,11 @@ public class VelocityPlugin extends ServerUtilsPlugin<
*/ */
public VelocityPlugin(ServerUtils plugin) { public VelocityPlugin(ServerUtils plugin) {
this.plugin = plugin; this.plugin = plugin;
this.pluginManager = new VelocityPluginManager(); this.pluginManager = new VelocityPluginManager(
plugin.getProxy(),
plugin.getLogger(),
plugin.getPluginCommandManager()
);
this.taskManager = new VelocityTaskManager(plugin); this.taskManager = new VelocityTaskManager(plugin);
this.resourceProvider = new VelocityResourceProvider(plugin); this.resourceProvider = new VelocityResourceProvider(plugin);
this.chatProvider = new VelocityChatProvider(plugin); this.chatProvider = new VelocityChatProvider(plugin);
@ -66,6 +70,11 @@ public class VelocityPlugin extends ServerUtilsPlugin<
return Platform.VELOCITY; return Platform.VELOCITY;
} }
@Override
public PluginContainer getPlugin() {
return plugin.getPluginContainer();
}
@Override @Override
public VelocityResourceProvider getResourceProvider() { public VelocityResourceProvider getResourceProvider() {
return this.resourceProvider; return this.resourceProvider;

View file

@ -2,11 +2,13 @@ package net.frankheijden.serverutils.velocity.listeners;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.PlayerChooseInitialServerEvent; import com.velocitypowered.api.event.player.PlayerChooseInitialServerEvent;
import com.velocitypowered.api.plugin.PluginContainer;
import net.frankheijden.serverutils.common.listeners.PlayerListener; import net.frankheijden.serverutils.common.listeners.PlayerListener;
import net.frankheijden.serverutils.velocity.entities.VelocityCommandSender; import net.frankheijden.serverutils.velocity.entities.VelocityCommandSender;
import net.frankheijden.serverutils.velocity.entities.VelocityPlugin; import net.frankheijden.serverutils.velocity.entities.VelocityPlugin;
public class VelocityPlayerListener extends PlayerListener<VelocityPlugin, VelocityCommandSender> { public class VelocityPlayerListener
extends PlayerListener<VelocityPlugin, PluginContainer, VelocityCommandSender> {
public VelocityPlayerListener(VelocityPlugin plugin) { public VelocityPlayerListener(VelocityPlugin plugin) {
super(plugin); super(plugin);

View file

@ -30,7 +30,6 @@ import net.frankheijden.serverutils.common.entities.CloseableResult;
import net.frankheijden.serverutils.common.entities.Result; import net.frankheijden.serverutils.common.entities.Result;
import net.frankheijden.serverutils.common.events.PluginEvent; import net.frankheijden.serverutils.common.events.PluginEvent;
import net.frankheijden.serverutils.common.managers.AbstractPluginManager; import net.frankheijden.serverutils.common.managers.AbstractPluginManager;
import net.frankheijden.serverutils.velocity.ServerUtils;
import net.frankheijden.serverutils.velocity.entities.VelocityLoadResult; import net.frankheijden.serverutils.velocity.entities.VelocityLoadResult;
import net.frankheijden.serverutils.velocity.events.VelocityPluginDisableEvent; import net.frankheijden.serverutils.velocity.events.VelocityPluginDisableEvent;
import net.frankheijden.serverutils.velocity.events.VelocityPluginEnableEvent; import net.frankheijden.serverutils.velocity.events.VelocityPluginEnableEvent;
@ -43,15 +42,23 @@ import net.frankheijden.serverutils.velocity.reflection.RVelocityEventManager;
import net.frankheijden.serverutils.velocity.reflection.RVelocityPluginContainer; import net.frankheijden.serverutils.velocity.reflection.RVelocityPluginContainer;
import net.frankheijden.serverutils.velocity.reflection.RVelocityPluginManager; import net.frankheijden.serverutils.velocity.reflection.RVelocityPluginManager;
import net.frankheijden.serverutils.velocity.reflection.RVelocityScheduler; import net.frankheijden.serverutils.velocity.reflection.RVelocityScheduler;
import org.slf4j.Logger;
public class VelocityPluginManager implements AbstractPluginManager<PluginContainer> { public class VelocityPluginManager implements AbstractPluginManager<PluginContainer> {
private static VelocityPluginManager instance; private static VelocityPluginManager instance;
private final ProxyServer proxy; private final ProxyServer proxy;
private final Logger logger;
private final VelocityPluginCommandManager pluginCommandManager;
public VelocityPluginManager() { /**
* Constructs a new VelocityPluginManager.
*/
public VelocityPluginManager(ProxyServer proxy, Logger logger, VelocityPluginCommandManager pluginCommandManager) {
instance = this; instance = this;
this.proxy = ServerUtils.getInstance().getProxy(); this.proxy = proxy;
this.logger = logger;
this.pluginCommandManager = pluginCommandManager;
} }
public static VelocityPluginManager get() { public static VelocityPluginManager get() {
@ -73,7 +80,7 @@ public class VelocityPluginManager implements AbstractPluginManager<PluginContai
for (PluginDependency dependency : candidate.getDependencies()) { for (PluginDependency dependency : candidate.getDependencies()) {
if (!dependency.isOptional() && !proxy.getPluginManager().isLoaded(dependency.getId())) { if (!dependency.isOptional() && !proxy.getPluginManager().isLoaded(dependency.getId())) {
ServerUtils.getInstance().getLogger().error( logger.error(
"Can't load plugin {} due to missing dependency {}", "Can't load plugin {} due to missing dependency {}",
candidate.getId(), candidate.getId(),
dependency.getId() dependency.getId()
@ -123,14 +130,14 @@ public class VelocityPluginManager implements AbstractPluginManager<PluginContai
try { try {
RJavaPluginLoader.createPlugin(javaPluginLoader, container, module, commonModule); RJavaPluginLoader.createPlugin(javaPluginLoader, container, module, commonModule);
} catch (Exception ex) { } catch (Exception ex) {
ServerUtils.getInstance().getLogger().error( logger.error(
String.format("Can't create plugin %s", container.getDescription().getId()), String.format("Can't create plugin %s", container.getDescription().getId()),
ex ex
); );
return Result.ERROR; return Result.ERROR;
} }
ServerUtils.getInstance().getLogger().info( logger.info(
"Loaded plugin {} {} by {}", "Loaded plugin {} {} by {}",
realPlugin.getId(), realPlugin.getId(),
realPlugin.getVersion().orElse("<UNKNOWN>"), realPlugin.getVersion().orElse("<UNKNOWN>"),
@ -158,7 +165,7 @@ public class VelocityPluginManager implements AbstractPluginManager<PluginContai
).join().createFunction(console); ).join().createFunction(console);
if (permissionFunction == null) { if (permissionFunction == null) {
ServerUtils.getInstance().getLogger().error( logger.error(
"A plugin permission provider {} provided an invalid permission function for the console." "A plugin permission provider {} provided an invalid permission function for the console."
+ " This is a bug in the plugin, not in Velocity." + " This is a bug in the plugin, not in Velocity."
+ " Falling back to the default permission function.", + " Falling back to the default permission function.",
@ -232,7 +239,6 @@ public class VelocityPluginManager implements AbstractPluginManager<PluginContai
} }
String pluginId = container.getDescription().getId(); String pluginId = container.getDescription().getId();
VelocityPluginCommandManager pluginCommandManager = ServerUtils.getInstance().getPluginCommandManager();
for (String alias : pluginCommandManager.getPluginCommands().removeAll(pluginId)) { for (String alias : pluginCommandManager.getPluginCommands().removeAll(pluginId)) {
proxy.getCommandManager().unregister(alias); proxy.getCommandManager().unregister(alias);
} }
@ -287,6 +293,11 @@ public class VelocityPluginManager implements AbstractPluginManager<PluginContai
return proxy.getPluginManager().getPlugin(pluginName).orElse(null); return proxy.getPluginManager().getPlugin(pluginName).orElse(null);
} }
@Override
public Object getInstance(PluginContainer plugin) {
return plugin.getInstance().orElse(null);
}
@Override @Override
public Set<String> getCommands() { public Set<String> getCommands() {
return RVelocityCommandManager.getDispatcher(proxy.getCommandManager()).getRoot().getChildren().stream() return RVelocityCommandManager.getDispatcher(proxy.getCommandManager()).getRoot().getChildren().stream()