Provide an event node per extension

This commit is contained in:
TheMode 2021-06-08 16:25:46 +02:00
parent 1e90d67511
commit 8c6d7ad1c3
2 changed files with 37 additions and 3 deletions

View File

@ -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<Event> 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<Event> 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<IExtensionObserver> action) {

View File

@ -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<Event> 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<Event> eventNode = ext.getEventNode();
MinecraftServer.getGlobalEventHandler().removeChild(eventNode);
ext.postTerminate();
ext.unload();