Added GlobalEventHandler

This commit is contained in:
Felix Cravic 2020-12-09 22:31:20 +01:00
parent 30d257c847
commit 6e3df970fa
3 changed files with 54 additions and 5 deletions

View File

@ -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.
* <p>
* 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
*

View File

@ -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<Class<? extends Event>, Collection<EventCallback>> eventCallbacks = new ConcurrentHashMap<>();
@NotNull
@Override
public Map<Class<? extends Event>, Collection<EventCallback>> getEventCallbacksMap() {
return eventCallbacks;
}
}

View File

@ -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 <E> the event type
*/
default <E extends Event> void callEvent(@NotNull Class<E> eventClass, @NotNull E event) {
// TODO global event
final Collection<EventCallback> eventCallbacks = getEventCallbacks(eventClass);
for (EventCallback<E> eventCallback : eventCallbacks) {
eventCallback.run(event);
// Global listeners
if (!(this instanceof GlobalEventHandler)) {
MinecraftServer.getGlobalEventListener().callEvent(eventClass, event);
}
// Local listeners
final Collection<EventCallback> 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 <E extends Event> void runEvent(@NotNull Collection<EventCallback> eventCallbacks, @NotNull E event) {
for (EventCallback<E> eventCallback : eventCallbacks) {
eventCallback.run(event);
}
}
}