Added EventHandler#getEventCallbacks

This commit is contained in:
themode 2020-09-20 20:11:46 +02:00
parent b6fc3ee978
commit 403e0c897f
3 changed files with 21 additions and 0 deletions

View File

@ -37,6 +37,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Stream;
public abstract class Entity implements Viewable, EventHandler, DataContainer {
@ -558,6 +559,11 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
return eventCallbacks.computeIfAbsent(eventClass, clazz -> new CopyOnWriteArrayList<>());
}
@Override
public Stream<EventCallback> getEventCallbacks() {
return eventCallbacks.values().stream().flatMap(Collection::stream);
}
/**
* Each entity has an unique id which will change after a restart
* All entities can be retrieved by calling {@link Entity#getEntity(int)}

View File

@ -5,6 +5,7 @@ import net.minestom.server.event.Event;
import net.minestom.server.event.EventCallback;
import java.util.List;
import java.util.stream.Stream;
public interface EventHandler {
@ -35,6 +36,14 @@ public interface EventHandler {
*/
<E extends Event> List<EventCallback> getEventCallbacks(Class<E> eventClass);
/**
* Get a {@link Stream} containing all the added callbacks, no matter to which event they are linked
*
* @return a {@link Stream} containing all the added callbacks
*/
Stream<EventCallback> getEventCallbacks();
/**
* Call the specified event type using the Event object parameter
*

View File

@ -36,6 +36,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.Consumer;
import java.util.stream.Stream;
/**
* Instances are what are called "worlds" in Minecraft
@ -754,6 +755,11 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
return eventCallbacks.computeIfAbsent(eventClass, clazz -> new CopyOnWriteArrayList<>());
}
@Override
public Stream<EventCallback> getEventCallbacks() {
return eventCallbacks.values().stream().flatMap(Collection::stream);
}
// UNSAFE METHODS (need most of time to be synchronized)
/**