From 2452314fad094101c2cf16978a074384c876e4a8 Mon Sep 17 00:00:00 2001 From: xHyroM Date: Wed, 30 Aug 2023 08:44:54 +0200 Subject: [PATCH] feat: unhook platform hooks --- .../lighteco/bukkit/BukkitLightEcoLoader.java | 3 --- .../lighteco/bukkit/BukkitLightEcoPlugin.java | 12 +++++++++++- .../dev/xhyrom/lighteco/bukkit/hooks/Hooks.java | 17 +++++++++++++++++ .../common/dependencies/DependencyManager.java | 2 +- .../dependencies/DependencyManagerImpl.java | 5 ++++- .../common/plugin/AbstractLightEcoPlugin.java | 16 +++++++++++++++- .../currency/money/bukkit/BukkitMCLoader.java | 11 ++++++++++- .../money/bukkit/hooks/vault/VaultFactory.java | 7 +++++++ 8 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/Hooks.java diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java index 07c5a31..ebf03b4 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java @@ -27,9 +27,6 @@ public class BukkitLightEcoLoader extends JavaPlugin { public void onEnable() { CommandAPI.onEnable(); this.bootstrap.onEnable(); - - if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) - new PlaceholderAPIExpansion(this.bootstrap.getPlugin()).register(); } @Override diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java index 22475ee..acbdbf3 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java @@ -3,6 +3,7 @@ package dev.xhyrom.lighteco.bukkit; import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.manager.ContextManager; import dev.xhyrom.lighteco.api.platform.Platform; +import dev.xhyrom.lighteco.bukkit.hooks.Hooks; import dev.xhyrom.lighteco.bukkit.listeners.BukkitConnectionListener; import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; import dev.xhyrom.lighteco.bukkit.manager.BukkitContextManager; @@ -12,7 +13,6 @@ import dev.xhyrom.lighteco.common.manager.user.StandardUserManager; import lombok.Getter; import org.bukkit.entity.Player; import org.bukkit.plugin.ServicePriority; -import org.bukkit.plugin.java.PluginClassLoader; import org.checkerframework.checker.nullness.qual.NonNull; @Getter @@ -50,6 +50,16 @@ public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin { this.getBootstrap().getServer().getServicesManager().register(LightEco.class, api, this.getBootstrap().getLoader(), ServicePriority.Normal); } + @Override + protected void registerPlatformHooks() { + Hooks.register(); + } + + @Override + protected void removePlatformHooks() { + Hooks.unregister(); + } + @Override public Platform.@NonNull Type getPlatformType() { return Platform.Type.BUKKIT; diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/Hooks.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/Hooks.java new file mode 100644 index 0000000..b90cdb8 --- /dev/null +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/Hooks.java @@ -0,0 +1,17 @@ +package dev.xhyrom.lighteco.bukkit.hooks; + +import org.bukkit.Bukkit; + +public class Hooks { + private static PlaceholderAPIExpansion placeholderAPIExpansion; + + public static void register() { + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) + placeholderAPIExpansion.register(); + } + + public static void unregister() { + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) + placeholderAPIExpansion.unregister(); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManager.java index dc43695..c81b6a1 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManager.java @@ -12,5 +12,5 @@ public interface DependencyManager extends AutoCloseable { ClassLoader obtainClassLoaderWith(Set dependencies); @Override - void close() throws Exception; + void close(); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java index 3ddb62e..1f19284 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java @@ -3,6 +3,7 @@ package dev.xhyrom.lighteco.common.dependencies; import com.google.common.collect.ImmutableSet; import com.google.common.io.MoreFiles; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger; import dev.xhyrom.lighteco.common.util.URLClassLoaderAccess; import dev.xhyrom.lighteco.common.storage.StorageType; @@ -22,11 +23,13 @@ public class DependencyManagerImpl implements DependencyManager { private final EnumMap loaded = new EnumMap<>(Dependency.class); private final Map, IsolatedClassLoader> loaders = new HashMap<>(); + private final PluginLogger logger; private final DependencyRegistry registry; private final Path cacheDirectory; private final URLClassLoaderAccess classLoader; public DependencyManagerImpl(LightEcoPlugin plugin) { + this.logger = plugin.getBootstrap().getLogger(); this.registry = new DependencyRegistry(); this.cacheDirectory = setupCacheDirectory(plugin); this.classLoader = URLClassLoaderAccess.create((URLClassLoader) plugin.getBootstrap().getClass().getClassLoader()); @@ -159,7 +162,7 @@ public class DependencyManagerImpl implements DependencyManager { } if (exception != null) { - throw new RuntimeException(exception); + this.logger.error("Failed to close class loader", exception); } } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java index 26f9258..89c8aa9 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java @@ -53,6 +53,9 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { // setup managers this.setupManagers(); + // register platform hooks + this.registerPlatformHooks(); + // register api this.api = new LightEcoApi(this); LightEcoProvider.set(this.api); @@ -63,13 +66,24 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { } public final void disable() { - this.userSaveTask.run(); // save all users synchronously + // save dirty users synchronously + this.userSaveTask.run(); + // remove platform hooks + this.removePlatformHooks(); + + // shutdown storage this.storage.shutdown(); + + // close isolated class loaders + this.dependencyManager.close(); } protected abstract void registerListeners(); protected abstract void setupManagers(); protected abstract void registerApiOnPlatform(LightEco api); + + protected abstract void registerPlatformHooks(); + protected abstract void removePlatformHooks(); } diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/BukkitMCLoader.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/BukkitMCLoader.java index ff43119..1d531a0 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/BukkitMCLoader.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/BukkitMCLoader.java @@ -10,6 +10,8 @@ import dev.xhyrom.lighteco.currency.money.common.MoneyCurrency; import org.bukkit.plugin.java.JavaPlugin; public class BukkitMCLoader extends JavaPlugin { + private VaultFactory vaultFactory; + @Override public void onEnable() { LightEco provider = LightEcoProvider.get(); @@ -23,8 +25,15 @@ public class BukkitMCLoader extends JavaPlugin { if (getServer().getPluginManager().getPlugin("Vault") != null) { getSLF4JLogger().info("Vault found, hooking..."); - new VaultFactory(this).hook(); + + this.vaultFactory = new VaultFactory(this); + this.vaultFactory.hook(); } } + + @Override + public void onDisable() { + this.vaultFactory.unhook(); + } } diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/VaultFactory.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/VaultFactory.java index 5270eea..88c1f68 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/VaultFactory.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/VaultFactory.java @@ -21,4 +21,11 @@ public class VaultFactory { ServicesManager manager = Bukkit.getServicesManager(); manager.register(Economy.class, vault, this.plugin, ServicePriority.Highest); } + + public void unhook() { + if (this.vault == null) return; + + ServicesManager manager = Bukkit.getServicesManager(); + manager.unregister(Economy.class, vault); + } }