Add Velocity Updater support
This commit is contained in:
parent
483e91bad1
commit
cb78112cb3
14 changed files with 151 additions and 41 deletions
|
|
@ -62,6 +62,11 @@ public class BukkitPlugin extends ServerUtilsPlugin<
|
|||
return Platform.BUKKIT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Plugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BukkitPluginManager getPluginManager() {
|
||||
return pluginManager;
|
||||
|
|
|
|||
|
|
@ -7,8 +7,11 @@ import org.bukkit.event.EventHandler;
|
|||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
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) {
|
||||
super(plugin);
|
||||
|
|
|
|||
|
|
@ -437,6 +437,11 @@ public class BukkitPluginManager implements AbstractPluginManager<Plugin> {
|
|||
return Bukkit.getPluginManager().getPlugin(pluginName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Plugin getInstance(Plugin plugin) {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getCommands() {
|
||||
Map<String, Command> knownCommands = getKnownCommands();
|
||||
|
|
|
|||
|
|
@ -55,6 +55,11 @@ public class BungeePlugin extends ServerUtilsPlugin<
|
|||
return Platform.BUNGEE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Plugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BungeePluginManager getPluginManager() {
|
||||
return pluginManager;
|
||||
|
|
|
|||
|
|
@ -5,9 +5,12 @@ import net.frankheijden.serverutils.bungee.entities.BungeePlugin;
|
|||
import net.frankheijden.serverutils.common.listeners.PlayerListener;
|
||||
import net.md_5.bungee.api.event.ServerConnectEvent;
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
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) {
|
||||
super(plugin);
|
||||
|
|
|
|||
|
|
@ -229,6 +229,11 @@ public class BungeePluginManager implements AbstractPluginManager<Plugin> {
|
|||
return proxy.getPluginManager().getPlugin(pluginName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Plugin getInstance(Plugin plugin) {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getCommands() {
|
||||
return proxy.getPluginManager().getCommands().stream()
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@ public interface PluginProvider<P> {
|
|||
|
||||
P getPlugin(String pluginName);
|
||||
|
||||
Object getInstance(P plugin);
|
||||
|
||||
Set<String> getCommands();
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -34,7 +34,11 @@ public class VelocityPlugin extends ServerUtilsPlugin<
|
|||
*/
|
||||
public VelocityPlugin(ServerUtils plugin) {
|
||||
this.plugin = plugin;
|
||||
this.pluginManager = new VelocityPluginManager();
|
||||
this.pluginManager = new VelocityPluginManager(
|
||||
plugin.getProxy(),
|
||||
plugin.getLogger(),
|
||||
plugin.getPluginCommandManager()
|
||||
);
|
||||
this.taskManager = new VelocityTaskManager(plugin);
|
||||
this.resourceProvider = new VelocityResourceProvider(plugin);
|
||||
this.chatProvider = new VelocityChatProvider(plugin);
|
||||
|
|
@ -66,6 +70,11 @@ public class VelocityPlugin extends ServerUtilsPlugin<
|
|||
return Platform.VELOCITY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PluginContainer getPlugin() {
|
||||
return plugin.getPluginContainer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityResourceProvider getResourceProvider() {
|
||||
return this.resourceProvider;
|
||||
|
|
|
|||
|
|
@ -2,11 +2,13 @@ package net.frankheijden.serverutils.velocity.listeners;
|
|||
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.player.PlayerChooseInitialServerEvent;
|
||||
import com.velocitypowered.api.plugin.PluginContainer;
|
||||
import net.frankheijden.serverutils.common.listeners.PlayerListener;
|
||||
import net.frankheijden.serverutils.velocity.entities.VelocityCommandSender;
|
||||
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) {
|
||||
super(plugin);
|
||||
|
|
|
|||
|
|
@ -30,7 +30,6 @@ import net.frankheijden.serverutils.common.entities.CloseableResult;
|
|||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import net.frankheijden.serverutils.common.events.PluginEvent;
|
||||
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.events.VelocityPluginDisableEvent;
|
||||
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.RVelocityPluginManager;
|
||||
import net.frankheijden.serverutils.velocity.reflection.RVelocityScheduler;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class VelocityPluginManager implements AbstractPluginManager<PluginContainer> {
|
||||
|
||||
private static VelocityPluginManager instance;
|
||||
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;
|
||||
this.proxy = ServerUtils.getInstance().getProxy();
|
||||
this.proxy = proxy;
|
||||
this.logger = logger;
|
||||
this.pluginCommandManager = pluginCommandManager;
|
||||
}
|
||||
|
||||
public static VelocityPluginManager get() {
|
||||
|
|
@ -73,7 +80,7 @@ public class VelocityPluginManager implements AbstractPluginManager<PluginContai
|
|||
|
||||
for (PluginDependency dependency : candidate.getDependencies()) {
|
||||
if (!dependency.isOptional() && !proxy.getPluginManager().isLoaded(dependency.getId())) {
|
||||
ServerUtils.getInstance().getLogger().error(
|
||||
logger.error(
|
||||
"Can't load plugin {} due to missing dependency {}",
|
||||
candidate.getId(),
|
||||
dependency.getId()
|
||||
|
|
@ -123,14 +130,14 @@ public class VelocityPluginManager implements AbstractPluginManager<PluginContai
|
|||
try {
|
||||
RJavaPluginLoader.createPlugin(javaPluginLoader, container, module, commonModule);
|
||||
} catch (Exception ex) {
|
||||
ServerUtils.getInstance().getLogger().error(
|
||||
logger.error(
|
||||
String.format("Can't create plugin %s", container.getDescription().getId()),
|
||||
ex
|
||||
);
|
||||
return Result.ERROR;
|
||||
}
|
||||
|
||||
ServerUtils.getInstance().getLogger().info(
|
||||
logger.info(
|
||||
"Loaded plugin {} {} by {}",
|
||||
realPlugin.getId(),
|
||||
realPlugin.getVersion().orElse("<UNKNOWN>"),
|
||||
|
|
@ -158,7 +165,7 @@ public class VelocityPluginManager implements AbstractPluginManager<PluginContai
|
|||
).join().createFunction(console);
|
||||
|
||||
if (permissionFunction == null) {
|
||||
ServerUtils.getInstance().getLogger().error(
|
||||
logger.error(
|
||||
"A plugin permission provider {} provided an invalid permission function for the console."
|
||||
+ " This is a bug in the plugin, not in Velocity."
|
||||
+ " Falling back to the default permission function.",
|
||||
|
|
@ -232,7 +239,6 @@ public class VelocityPluginManager implements AbstractPluginManager<PluginContai
|
|||
}
|
||||
|
||||
String pluginId = container.getDescription().getId();
|
||||
VelocityPluginCommandManager pluginCommandManager = ServerUtils.getInstance().getPluginCommandManager();
|
||||
for (String alias : pluginCommandManager.getPluginCommands().removeAll(pluginId)) {
|
||||
proxy.getCommandManager().unregister(alias);
|
||||
}
|
||||
|
|
@ -287,6 +293,11 @@ public class VelocityPluginManager implements AbstractPluginManager<PluginContai
|
|||
return proxy.getPluginManager().getPlugin(pluginName).orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getInstance(PluginContainer plugin) {
|
||||
return plugin.getInstance().orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getCommands() {
|
||||
return RVelocityCommandManager.getDispatcher(proxy.getCommandManager()).getRoot().getChildren().stream()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue