diff --git a/src/main/java/net/minestom/server/event/EventInterface.java b/src/main/java/net/minestom/server/event/EventInterface.java new file mode 100644 index 000000000..e9c9fde39 --- /dev/null +++ b/src/main/java/net/minestom/server/event/EventInterface.java @@ -0,0 +1,35 @@ +package net.minestom.server.event; + +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.BiConsumer; + +public final class EventInterface { + + public static @NotNull Builder builder(@NotNull Class type) { + return new Builder<>(); + } + + final Map, BiConsumer> mapped; + + EventInterface(Map, BiConsumer> map) { + this.mapped = map; + } + + public static class Builder { + private final Map, BiConsumer> mapped = new HashMap<>(); + + @SuppressWarnings("unchecked") + public Builder map(@NotNull Class eventType, + @NotNull BiConsumer<@NotNull T, @NotNull E> consumer) { + this.mapped.put(eventType, (BiConsumer) consumer); + return this; + } + + public @NotNull EventInterface build() { + return new EventInterface<>(Map.copyOf(mapped)); + } + } +} diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index c6b359564..ed96aac9e 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -514,6 +514,13 @@ public class EventNode { return this; } + public void addInter(@NotNull EventInterface inter, @NotNull I value) { + inter.mapped.forEach((eventType, consumer) -> { + // TODO cache so listeners can be removed from the EventInterface + addListener((EventListener) EventListener.builder(eventType).handler(event -> consumer.accept(value, event)).build()); + }); + } + private void increaseChildListenerCount(Class eventClass, int count) { var entry = listenerMap.computeIfAbsent(eventClass, aClass -> new ListenerEntry<>()); ListenerEntry.addAndGet(entry, count);