mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 01:17:47 +01:00
Force the use of EventListener
This commit is contained in:
parent
ee99c94509
commit
c95c8430b6
@ -20,10 +20,14 @@ public class EventListener<T extends Event> {
|
|||||||
this.executor = executor;
|
this.executor = executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends Event> EventListener.Builder<T> of(@NotNull Class<T> eventType) {
|
public static <T extends Event> EventListener.Builder<T> builder(@NotNull Class<T> eventType) {
|
||||||
return new EventListener.Builder<>(eventType);
|
return new EventListener.Builder<>(eventType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T extends Event> EventListener<T> of(@NotNull Class<T> eventType, @NotNull Consumer<@NotNull T> listener) {
|
||||||
|
return EventListener.builder(eventType).handler(listener).build();
|
||||||
|
}
|
||||||
|
|
||||||
public static class Builder<T extends Event> {
|
public static class Builder<T extends Event> {
|
||||||
|
|
||||||
private final Class<T> eventType;
|
private final Class<T> eventType;
|
||||||
|
@ -4,7 +4,6 @@ import net.minestom.server.event.handler.EventHandler;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public interface EventNode<T extends Event> {
|
public interface EventNode<T extends Event> {
|
||||||
@ -36,10 +35,6 @@ public interface EventNode<T extends Event> {
|
|||||||
|
|
||||||
void removeListener(@NotNull EventListener<? extends T> listener);
|
void removeListener(@NotNull EventListener<? extends T> listener);
|
||||||
|
|
||||||
default <E extends T> void addListener(@NotNull Class<E> eventClass, @NotNull Consumer<@NotNull E> listener) {
|
|
||||||
addListener(EventListener.of(eventClass).handler(listener).build());
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull String getName();
|
@NotNull String getName();
|
||||||
|
|
||||||
@NotNull List<@NotNull EventNode<T>> getChildren();
|
@NotNull List<@NotNull EventNode<T>> getChildren();
|
||||||
|
@ -138,19 +138,19 @@ public class PlayerInit {
|
|||||||
// EVENT REGISTERING
|
// EVENT REGISTERING
|
||||||
|
|
||||||
var node = EventNode.type(EventFilter.PLAYER);
|
var node = EventNode.type(EventFilter.PLAYER);
|
||||||
node.addListener(EventListener.of(PlayerTickEvent.class)
|
node.addListener(EventListener.builder(PlayerTickEvent.class)
|
||||||
.handler(playerTickEvent -> System.out.println("Player tick!"))
|
.handler(playerTickEvent -> System.out.println("Player tick!"))
|
||||||
.expirationCount(2)
|
.expirationCount(2)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
var empty = EventNode.all();
|
var empty = EventNode.all();
|
||||||
empty.addListener(PlayerMoveEvent.class, (event) -> {
|
empty.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> {
|
||||||
});
|
}));
|
||||||
|
|
||||||
var list = EventNode.list(EventFilter.PLAYER);
|
var list = EventNode.list(EventFilter.PLAYER);
|
||||||
list.addListener(PlayerMoveEvent.class, playerMoveEvent -> {
|
list.addListener(EventListener.of(PlayerMoveEvent.class, playerMoveEvent -> {
|
||||||
System.out.println("move");
|
System.out.println("move");
|
||||||
});
|
}));
|
||||||
|
|
||||||
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
|
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
|
||||||
globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {
|
globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {
|
||||||
|
Loading…
Reference in New Issue
Block a user