mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-26 19:18:12 +01:00
Added GlobalEventHandler
This commit is contained in:
parent
30d257c847
commit
6e3df970fa
@ -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
|
||||
*
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user