diff --git a/src/main/java/net/minestom/server/extensions/Extension.java b/src/main/java/net/minestom/server/extensions/Extension.java index 1272751cc..f3cbac22e 100644 --- a/src/main/java/net/minestom/server/extensions/Extension.java +++ b/src/main/java/net/minestom/server/extensions/Extension.java @@ -1,5 +1,7 @@ package net.minestom.server.extensions; +import net.minestom.server.event.Event; +import net.minestom.server.event.EventNode; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @@ -14,7 +16,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import java.util.*; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; @@ -25,6 +29,9 @@ public abstract class Extension { // Set by reflection @SuppressWarnings("unused") private Logger logger; + // Set by reflection + @SuppressWarnings("unused") + private EventNode eventNode; /** * Observers that will be notified of events related to this extension. @@ -78,6 +85,7 @@ public abstract class Extension { /** * Gets the logger for the extension + * * @return The logger for the extension */ @NotNull @@ -85,6 +93,10 @@ public abstract class Extension { return logger; } + public @NotNull EventNode getEventNode() { + return eventNode; + } + public @NotNull Path getDataDirectory() { return getOrigin().getDataDirectory(); } @@ -209,6 +221,7 @@ public abstract class Extension { /** * Calls some action on all valid observers of this extension + * * @param action code to execute on each observer */ public void triggerChange(Consumer action) { @@ -234,7 +247,7 @@ public abstract class Extension { */ public void cleanupObservers() { Reference ref; - while((ref = observerReferenceQueue.poll()) != null) { + while ((ref = observerReferenceQueue.poll()) != null) { observers.remove(ref); } } diff --git a/src/main/java/net/minestom/server/extensions/ExtensionManager.java b/src/main/java/net/minestom/server/extensions/ExtensionManager.java index 95ca6452e..53ca4e7f7 100644 --- a/src/main/java/net/minestom/server/extensions/ExtensionManager.java +++ b/src/main/java/net/minestom/server/extensions/ExtensionManager.java @@ -5,6 +5,8 @@ import net.minestom.dependencies.DependencyGetter; import net.minestom.dependencies.ResolvedDependency; import net.minestom.dependencies.maven.MavenRepository; import net.minestom.server.MinecraftServer; +import net.minestom.server.event.Event; +import net.minestom.server.event.EventNode; import net.minestom.server.extras.selfmodification.MinestomExtensionClassLoader; import net.minestom.server.extras.selfmodification.MinestomRootClassLoader; import net.minestom.server.ping.ResponseDataConsumer; @@ -25,7 +27,6 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.*; import java.util.stream.Collectors; import java.util.zip.ZipFile; @@ -280,6 +281,22 @@ public class ExtensionManager { LOGGER.error("Main class '{}' in '{}' has no logger field.", mainClass, extensionName, e); } + // Set event node + try { + EventNode eventNode = EventNode.all(extensionName); // Use the extension name + Field loggerField = Extension.class.getDeclaredField("eventNode"); + loggerField.setAccessible(true); + loggerField.set(extension, eventNode); + + MinecraftServer.getGlobalEventHandler().addChild(eventNode); + } catch (IllegalAccessException e) { + // We made it accessible, should not occur + MinecraftServer.getExceptionManager().handleException(e); + } catch (NoSuchFieldException e) { + // This should also not occur + LOGGER.error("Main class '{}' in '{}' has no event node field.", mainClass, extensionName, e); + } + // add dependents to pre-existing extensions, so that they can easily be found during reloading for (String dependencyName : discoveredExtension.getDependencies()) { Extension dependency = extensions.get(dependencyName.toLowerCase()); @@ -660,6 +677,10 @@ public class ExtensionManager { ext.triggerChange(observer -> observer.onExtensionUnload(extensionName)); // TODO: more callback types + // Remove event node + EventNode eventNode = ext.getEventNode(); + MinecraftServer.getGlobalEventHandler().removeChild(eventNode); + ext.postTerminate(); ext.unload();