From 6e3df970fab216d9a660276149a3d38d6f70f6ac Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Wed, 9 Dec 2020 22:31:20 +0100 Subject: [PATCH] Added GlobalEventHandler --- .../net/minestom/server/MinecraftServer.java | 14 +++++++++++ .../server/event/GlobalEventHandler.java | 23 +++++++++++++++++++ .../server/event/handler/EventHandler.java | 22 ++++++++++++++---- 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 src/main/java/net/minestom/server/event/GlobalEventHandler.java diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java index b6e97e810..4c3450394 100644 --- a/src/main/java/net/minestom/server/MinecraftServer.java +++ b/src/main/java/net/minestom/server/MinecraftServer.java @@ -9,6 +9,7 @@ import net.minestom.server.data.SerializableData; import net.minestom.server.entity.EntityManager; import net.minestom.server.entity.EntityType; import net.minestom.server.entity.Player; +import net.minestom.server.event.GlobalEventHandler; import net.minestom.server.extensions.Extension; import net.minestom.server.extensions.ExtensionManager; import net.minestom.server.fluids.Fluid; @@ -133,6 +134,7 @@ public final class MinecraftServer { private static ResponseDataConsumer responseDataConsumer; private static String brandName = "Minestom"; private static Difficulty difficulty = Difficulty.NORMAL; + private static GlobalEventHandler globalEventHandler = new GlobalEventHandler(); private static LootTableManager lootTableManager; private static TagManager tagManager; @@ -282,6 +284,18 @@ public final class MinecraftServer { PacketUtils.sendGroupedPacket(connectionManager.getOnlinePlayers(), serverDifficultyPacket); } + /** + * Gets the global event handler. + *

+ * Used to register event callback at a global scale. + * + * @return the global event handler + */ + @NotNull + public static GlobalEventHandler getGlobalEventListener() { + return globalEventHandler; + } + /** * Gets the manager handling all incoming packets * diff --git a/src/main/java/net/minestom/server/event/GlobalEventHandler.java b/src/main/java/net/minestom/server/event/GlobalEventHandler.java new file mode 100644 index 000000000..dd9da088c --- /dev/null +++ b/src/main/java/net/minestom/server/event/GlobalEventHandler.java @@ -0,0 +1,23 @@ +package net.minestom.server.event; + +import net.minestom.server.event.handler.EventHandler; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Object containing all the global event listeners. + */ +public final class GlobalEventHandler implements EventHandler { + + // Events + private final Map, Collection> eventCallbacks = new ConcurrentHashMap<>(); + + @NotNull + @Override + public Map, Collection> getEventCallbacksMap() { + return eventCallbacks; + } +} diff --git a/src/main/java/net/minestom/server/event/handler/EventHandler.java b/src/main/java/net/minestom/server/event/handler/EventHandler.java index 47080e812..52529252f 100644 --- a/src/main/java/net/minestom/server/event/handler/EventHandler.java +++ b/src/main/java/net/minestom/server/event/handler/EventHandler.java @@ -1,9 +1,11 @@ package net.minestom.server.event.handler; +import net.minestom.server.MinecraftServer; import net.minestom.server.entity.Entity; import net.minestom.server.event.CancellableEvent; import net.minestom.server.event.Event; import net.minestom.server.event.EventCallback; +import net.minestom.server.event.GlobalEventHandler; import net.minestom.server.instance.Instance; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; @@ -89,17 +91,21 @@ public interface EventHandler { * @param the event type */ default void callEvent(@NotNull Class eventClass, @NotNull E event) { - // TODO global event - final Collection eventCallbacks = getEventCallbacks(eventClass); - for (EventCallback eventCallback : eventCallbacks) { - eventCallback.run(event); + + // Global listeners + if (!(this instanceof GlobalEventHandler)) { + MinecraftServer.getGlobalEventListener().callEvent(eventClass, event); } + // Local listeners + final Collection eventCallbacks = getEventCallbacks(eventClass); + runEvent(eventCallbacks, event); + // Call the same event for the current entity instance if (this instanceof Entity) { final Instance instance = ((Entity) this).getInstance(); if (instance != null) { - instance.callEvent(eventClass, event); + runEvent(instance.getEventCallbacks(eventClass), event); } } } @@ -122,4 +128,10 @@ public interface EventHandler { } } + private void runEvent(@NotNull Collection eventCallbacks, @NotNull E event) { + for (EventCallback eventCallback : eventCallbacks) { + eventCallback.run(event); + } + } + }