From 8ec33bb6c8abc3ff5a6a1b8a51eb520a0fe7bc97 Mon Sep 17 00:00:00 2001 From: TheMode Date: Thu, 3 Jun 2021 04:41:08 +0200 Subject: [PATCH] EventNode methods return self --- .../net/minestom/server/event/EventNode.java | 23 ++++++++++------ src/test/java/demo/PlayerInit.java | 26 +++++++++---------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index 019eab90f..2976f1e15 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -108,7 +108,7 @@ public class EventNode { this.children.forEach(eventNode -> eventNode.call(event)); } - public void addChild(@NotNull EventNode child) { + public EventNode addChild(@NotNull EventNode child) { synchronized (GLOBAL_CHILD_LOCK) { final boolean result = this.children.add((EventNode) child); if (result) { @@ -122,9 +122,10 @@ public class EventNode { } } } + return this; } - public void removeChild(@NotNull EventNode child) { + public EventNode removeChild(@NotNull EventNode child) { synchronized (GLOBAL_CHILD_LOCK) { final boolean result = this.children.remove(child); if (result) { @@ -138,9 +139,10 @@ public class EventNode { } } } + return this; } - public void addListener(@NotNull EventListener listener) { + public EventNode addListener(@NotNull EventListener listener) { synchronized (GLOBAL_CHILD_LOCK) { final var eventType = listener.getEventType(); this.listenerMap.computeIfAbsent(eventType, aClass -> new CopyOnWriteArrayList<>()) @@ -151,14 +153,15 @@ public class EventNode { } } } + return this; } - public void removeListener(@NotNull EventListener listener) { + public EventNode removeListener(@NotNull EventListener listener) { synchronized (GLOBAL_CHILD_LOCK) { final var eventType = listener.getEventType(); var listeners = listenerMap.get(eventType); if (listeners == null || listeners.isEmpty()) - return; + return this; final boolean removed = listeners.remove(listener); if (removed && parent != null) { synchronized (parent.lock) { @@ -166,25 +169,29 @@ public class EventNode { } } } + return this; } - public void map(@NotNull EventFilter filter, @NotNull V2 value, @NotNull EventNode node) { + public EventNode map(@NotNull EventFilter filter, @NotNull V2 value, @NotNull EventNode node) { RedirectionEntry entry = new RedirectionEntry<>(); entry.filter = filter; entry.node = node; this.redirectionMap.put(value, (RedirectionEntry) entry); + return this; } - public void removeMap(@NotNull Object value) { + public EventNode removeMap(@NotNull Object value) { this.redirectionMap.remove(value); + return this; } public @NotNull String getName() { return name; } - public void setName(@NotNull String name) { + public EventNode setName(@NotNull String name) { this.name = name; + return this; } public @NotNull Set<@NotNull EventNode> getChildren() { diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index f2616b204..50000bcb0 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -137,22 +137,20 @@ public class PlayerInit { // EVENT REGISTERING - var empty = EventNode.all(); - empty.setName("empty"); - empty.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> { - })); + var empty = EventNode.all() + .setName("empty") + .addListener(EventListener.of(PlayerMoveEvent.class, (event) -> { + })); - var node = EventNode.type(EventFilter.PLAYER); - node.setName("node"); - node.addListener(EventListener.builder(PlayerTickEvent.class) - .handler(playerTickEvent -> System.out.println("Player tick!")) - .expirationCount(50) - .build()); + var node = EventNode.type(EventFilter.PLAYER) + .setName("node") + .addListener(EventListener.builder(PlayerTickEvent.class) + .handler(playerTickEvent -> System.out.println("Player tick!")) + .expirationCount(50) + .build()); - - var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative); - conditional.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> - System.out.println("creative player moved"))); + var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative) + .addListener(EventListener.of(PlayerMoveEvent.class, (event) -> System.out.println("creative player moved"))); GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler(); globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {