From 8658480e5bcd4e7d72cabec005154f39170d8b47 Mon Sep 17 00:00:00 2001 From: Jannyboy11 Date: Sun, 30 Jul 2023 14:26:56 +0200 Subject: [PATCH] Fix reloads --- .../scalaloader/paper/ScalaLoader.java | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/ScalaLoader-Paper/src/main/java/xyz/janboerman/scalaloader/paper/ScalaLoader.java b/ScalaLoader-Paper/src/main/java/xyz/janboerman/scalaloader/paper/ScalaLoader.java index 3b8e81ff..b3fd6347 100644 --- a/ScalaLoader-Paper/src/main/java/xyz/janboerman/scalaloader/paper/ScalaLoader.java +++ b/ScalaLoader-Paper/src/main/java/xyz/janboerman/scalaloader/paper/ScalaLoader.java @@ -75,6 +75,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -103,6 +104,8 @@ public final class ScalaLoader extends JavaPlugin implements IScalaLoader, Liste Migration.addMigrator(PaperPluginTransformer::new); //TODO Paper's ClassloaderBytecodeModifier api gives us the ability to transform bytecode of JavaPlugins. //TODO should I make use of this? Are there any ScalaLoader apis that I broke that can be called by JavaPlugins? + //TODO the only thing that comes to mind right now is ScalaPluginLoader.openUpToJavaPlugin(ScalaPlugin, JavaPlugin). + //TODO the replacement would be to make the ScalaPlugin's classloader accessible to the JavaPlugin's classloader (perhaps through ClassLoader groups). } private EventBus eventBus; @@ -167,13 +170,23 @@ public void onEnable() { initCommands(); getServer().getPluginManager().registerEvents(this, this); enableScalaPlugins(PluginLoadOrder.STARTUP); //Enable ScalaPlugins at STARTUP because ScalaLoader itself enables on STARTUP. + for (ScalaPlugin scalaPlugin : getScalaPlugins()) { + registerScalaPlugin(scalaPlugin); + } ScalaLoaderUtils.initBStats(this); } + @Override + public void onDisable() { + scalaPlugins.clear(); + } + @EventHandler public void onServerLoad(ServerLoadEvent event) { - //enable ScalaPlugin's at POSTWORLD for both load types (STARTUP, RELOAD) - enableScalaPlugins(PluginLoadOrder.POSTWORLD); + if (event.getType() == LoadType.STARTUP) { + enableScalaPlugins(PluginLoadOrder.POSTWORLD); + } + //Don't need to enable ScalaPlugins at LoadType.RELOAD, because the server itself already does this. } private void initCommands() { @@ -363,16 +376,9 @@ private static void bootstrap(PluginBootstrap bootstrapper, ScalaPluginProviderC private void enableScalaPlugins(PluginLoadOrder loadOrder) { for (ScalaPlugin scalaPlugin : getScalaPlugins()) { + //if this is the right moment to load if (scalaPlugin.getPluginMeta().getLoadOrder() == loadOrder) { - - //register the ScalaPlugin with Paper's pluginManager. - PaperPluginManagerImpl paperPluginManager = PaperHacks.getPaperPluginManager(); - if (paperPluginManager.getPlugin(scalaPlugin.getName()) == null) { //ensure idempotency - paperPluginManager.loadPlugin(scalaPlugin); //more like "registerPlugin" since PaperPluginInstanceManager.loadPlugin(Plugin) does not call Plugin.onLoad()! - //note that scalaPlugin.onLoad() has already been called! - } - - //now, enable the scalaPlugin. + //enable the scalaPlugin. if (!scalaPlugin.isEnabled()) { ScalaPluginEnableEvent event = new ScalaPluginEnableEvent(scalaPlugin); getServer().getPluginManager().callEvent(event); @@ -385,6 +391,14 @@ private void enableScalaPlugins(PluginLoadOrder loadOrder) { } } + private static void registerScalaPlugin(ScalaPlugin scalaPlugin) { + PaperPluginManagerImpl paperPluginManager = PaperHacks.getPaperPluginManager(); + if (paperPluginManager.getPlugin(scalaPlugin.getName()) == null) { //ensure idempotency + paperPluginManager.loadPlugin(scalaPlugin); //more like "registerPlugin" since PaperPluginInstanceManager.loadPlugin(Plugin) does not call Plugin.onLoad()! + //note that scalaPlugin.onLoad() has already been called! + } + } + private static Comparator dependencyOrder(MutableGraph dependencies) { return new Comparator() { @Override