From 2977bb7b669c21bcaa68b846ab132362a93858b8 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 2 Jun 2021 21:14:48 +0200 Subject: [PATCH] Make EventListener an interface --- .../minestom/server/event/EventListener.java | 73 +++++++++++-------- .../minestom/server/event/EventNodeImpl.java | 6 +- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/minestom/server/event/EventListener.java b/src/main/java/net/minestom/server/event/EventListener.java index 550cd79e2..93497f279 100644 --- a/src/main/java/net/minestom/server/event/EventListener.java +++ b/src/main/java/net/minestom/server/event/EventListener.java @@ -7,28 +7,34 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; -import java.util.function.Function; import java.util.function.Predicate; -public class EventListener { +public interface EventListener { - protected final Class type; - protected final Function executor; + @NotNull Class getEventType(); - private EventListener(@NotNull Class type, @NotNull Function executor) { - this.type = type; - this.executor = executor; - } + @NotNull Result run(@NotNull T event); - public static EventListener.Builder builder(@NotNull Class eventType) { + static EventListener.Builder builder(@NotNull Class eventType) { return new EventListener.Builder<>(eventType); } - public static EventListener of(@NotNull Class eventType, @NotNull Consumer<@NotNull T> listener) { - return EventListener.builder(eventType).handler(listener).build(); + static EventListener of(@NotNull Class eventType, @NotNull Consumer<@NotNull T> listener) { + return new EventListener<>() { + @Override + public @NotNull Class getEventType() { + return eventType; + } + + @Override + public @NotNull Result run(@NotNull T event) { + listener.accept(event); + return Result.SUCCESS; + } + }; } - public static class Builder { + class Builder { private final Class eventType; @@ -61,34 +67,41 @@ public class EventListener { return this; } - public EventListener build() { + public @NotNull EventListener build() { AtomicInteger expirationCount = new AtomicInteger(this.expirationCount); final boolean hasExpirationCount = expirationCount.get() > 0; final var filters = new ArrayList<>(this.filters); final var handler = this.handler; - return new EventListener<>(eventType, event -> { - // Filtering - if (!filters.isEmpty()) { - if (filters.stream().anyMatch(filter -> !filter.test(event))) { - // Cancelled - return Result.INVALID; + return new EventListener<>() { + @Override + public @NotNull Class getEventType() { + return eventType; + } + + @Override + public @NotNull Result run(@NotNull T event) { + // Filtering + if (!filters.isEmpty()) { + if (filters.stream().anyMatch(filter -> !filter.test(event))) { + // Cancelled + return Result.INVALID; + } } + // Handler + if (handler != null) { + handler.accept(event); + } + // Expiration check + if (hasExpirationCount && expirationCount.decrementAndGet() == 0) { + return Result.EXPIRED; + } + return Result.SUCCESS; } - // Handler - if (handler != null) { - handler.accept(event); - } - // Expiration check - if (hasExpirationCount && expirationCount.decrementAndGet() == 0) { - return Result.EXPIRED; - } - return Result.SUCCESS; - }); + }; } } - enum Result { SUCCESS, INVALID, diff --git a/src/main/java/net/minestom/server/event/EventNodeImpl.java b/src/main/java/net/minestom/server/event/EventNodeImpl.java index e6b25b983..a50ff0088 100644 --- a/src/main/java/net/minestom/server/event/EventNodeImpl.java +++ b/src/main/java/net/minestom/server/event/EventNodeImpl.java @@ -45,7 +45,7 @@ class EventNodeImpl implements EventNod final var listeners = listenerMap.get(event.getClass()); if (listeners != null && !listeners.isEmpty()) { listeners.forEach(listener -> { - final EventListener.Result result = listener.executor.apply(event); + final EventListener.Result result = listener.run(event); if (result == EventListener.Result.EXPIRED) { listeners.remove(listener); } @@ -66,13 +66,13 @@ class EventNodeImpl implements EventNod @Override public void addListener(@NotNull EventListener listener) { - this.listenerMap.computeIfAbsent(listener.type, aClass -> new CopyOnWriteArrayList<>()) + this.listenerMap.computeIfAbsent(listener.getEventType(), aClass -> new CopyOnWriteArrayList<>()) .add((EventListener) listener); } @Override public void removeListener(@NotNull EventListener listener) { - var listeners = listenerMap.get(listener.type); + var listeners = listenerMap.get(listener.getEventType()); if (listeners == null || listeners.isEmpty()) return; listeners.remove(listener);