diff --git a/src/main/java/net/minestom/server/event/EventListener.java b/src/main/java/net/minestom/server/event/EventListener.java index 6bc1a9fe4..63c100c43 100644 --- a/src/main/java/net/minestom/server/event/EventListener.java +++ b/src/main/java/net/minestom/server/event/EventListener.java @@ -1,5 +1,6 @@ package net.minestom.server.event; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -14,11 +15,13 @@ public interface EventListener { @NotNull Result run(@NotNull T event); - static EventListener.Builder builder(@NotNull Class eventType) { + @Contract(pure = true) + static EventListener.@NotNull Builder builder(@NotNull Class eventType) { return new EventListener.Builder<>(eventType); } - static EventListener of(@NotNull Class eventType, @NotNull Consumer<@NotNull T> listener) { + @Contract(pure = true) + static @NotNull EventListener of(@NotNull Class eventType, @NotNull Consumer<@NotNull T> listener) { return new EventListener<>() { @Override public @NotNull Class getEventType() { @@ -34,9 +37,7 @@ public interface EventListener { } class Builder { - private final Class eventType; - private final List> filters = new ArrayList<>(); private int expireCount; private Predicate expireWhen; @@ -46,30 +47,37 @@ public interface EventListener { this.eventType = eventType; } - public EventListener.Builder filter(Predicate filter) { + @Contract(value = "_ -> this") + public @NotNull EventListener.Builder filter(Predicate filter) { this.filters.add(filter); return this; } - public EventListener.Builder expireCount(int expireCount) { + @Contract(value = "_ -> this") + public @NotNull EventListener.Builder expireCount(int expireCount) { this.expireCount = expireCount; return this; } - public EventListener.Builder expireWhen(Predicate expireWhen) { + @Contract(value = "_ -> this") + public @NotNull EventListener.Builder expireWhen(Predicate expireWhen) { this.expireWhen = expireWhen; return this; } - public EventListener.Builder handler(Consumer handler) { + @Contract(value = "_ -> this") + public @NotNull EventListener.Builder handler(Consumer handler) { this.handler = handler; return this; } + @Contract(value = "-> new", pure = true) public @NotNull EventListener build() { AtomicInteger expirationCount = new AtomicInteger(this.expireCount); final boolean hasExpirationCount = expirationCount.get() > 0; + final Predicate expireWhen = this.expireWhen; + final var filters = new ArrayList<>(this.filters); final var handler = this.handler; return new EventListener<>() { diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index 910c4953d..f0ed8415d 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -3,6 +3,7 @@ package net.minestom.server.event; import net.minestom.server.tag.Tag; import net.minestom.server.tag.TagReadable; import net.minestom.server.utils.validate.Check; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -17,43 +18,50 @@ import java.util.function.Predicate; public class EventNode { - public static EventNode all(@NotNull String name) { + @Contract(value = "_ -> new", pure = true) + public static @NotNull EventNode all(@NotNull String name) { return type(name, EventFilter.ALL); } - public static EventNode type(@NotNull String name, - @NotNull EventFilter filter, - @NotNull BiPredicate predicate) { + @Contract(value = "_, _, _ -> new", pure = true) + public static @NotNull EventNode type(@NotNull String name, + @NotNull EventFilter filter, + @NotNull BiPredicate predicate) { return create(name, filter, predicate); } - public static EventNode type(@NotNull String name, - @NotNull EventFilter filter) { + @Contract(value = "_, _ -> new", pure = true) + public static @NotNull EventNode type(@NotNull String name, + @NotNull EventFilter filter) { return create(name, filter, null); } - public static EventNode event(@NotNull String name, - @NotNull EventFilter filter, - @NotNull Predicate predicate) { + @Contract(value = "_, _, _ -> new", pure = true) + public static @NotNull EventNode event(@NotNull String name, + @NotNull EventFilter filter, + @NotNull Predicate predicate) { return create(name, filter, (e, h) -> predicate.test(e)); } - public static EventNode value(@NotNull String name, - @NotNull EventFilter filter, - @NotNull Predicate predicate) { + @Contract(value = "_, _, _ -> new", pure = true) + public static @NotNull EventNode value(@NotNull String name, + @NotNull EventFilter filter, + @NotNull Predicate predicate) { return create(name, filter, (e, h) -> predicate.test(h)); } - public static EventNode tag(@NotNull String name, - @NotNull EventFilter filter, - @NotNull Tag tag) { + @Contract(value = "_, _, _ -> new", pure = true) + public static @NotNull EventNode tag(@NotNull String name, + @NotNull EventFilter filter, + @NotNull Tag tag) { return create(name, filter, (e, h) -> h.hasTag(tag)); } - public static EventNode tag(@NotNull String name, - @NotNull EventFilter filter, - @NotNull Tag tag, - @NotNull Predicate<@Nullable V> consumer) { + @Contract(value = "_, _, _, _ -> new", pure = true) + public static @NotNull EventNode tag(@NotNull String name, + @NotNull EventFilter filter, + @NotNull Tag tag, + @NotNull Predicate<@Nullable V> consumer) { return create(name, filter, (e, h) -> consumer.test(h.getTag(tag))); } @@ -138,23 +146,28 @@ public class EventNode { } } + @Contract(pure = true) public @NotNull String getName() { return name; } + @Contract(pure = true) public int getPriority() { return priority; } - public EventNode setPriority(int priority) { + @Contract(value = "_ -> this") + public @NotNull EventNode setPriority(int priority) { this.priority = priority; return this; } + @Contract(pure = true) public @Nullable EventNode getParent() { return parent; } + @Contract(pure = true) public @NotNull Set<@NotNull EventNode> getChildren() { return Collections.unmodifiableSet(children); } @@ -167,7 +180,8 @@ public class EventNode { return findChild(name, eventType); } - public EventNode addChild(@NotNull EventNode child) { + @Contract(value = "_ -> this") + public @NotNull EventNode addChild(@NotNull EventNode child) { synchronized (GLOBAL_CHILD_LOCK) { Check.stateCondition(child.parent != null, "Node already has a parent"); Check.stateCondition(Objects.equals(parent, child), "Cannot have a child as parent"); @@ -189,7 +203,8 @@ public class EventNode { return this; } - public EventNode removeChild(@NotNull EventNode child) { + @Contract(value = "_ -> this") + public @NotNull EventNode removeChild(@NotNull EventNode child) { synchronized (GLOBAL_CHILD_LOCK) { final boolean result = this.children.remove(child); if (result) { @@ -209,15 +224,18 @@ public class EventNode { return this; } - public EventNode addListener(@NotNull EventListener listener) { + @Contract(value = "_ -> this") + public @NotNull EventNode addListener(@NotNull EventListener listener) { return addListener0(listener); } - public EventNode addListener(@NotNull Class eventType, @NotNull Consumer<@NotNull E> listener) { + @Contract(value = "_, _ -> this") + public @NotNull EventNode addListener(@NotNull Class eventType, @NotNull Consumer<@NotNull E> listener) { return addListener0(EventListener.of(eventType, listener)); } - public EventNode removeListener(@NotNull EventListener listener) { + @Contract(value = "_ -> this") + public @NotNull EventNode removeListener(@NotNull EventListener listener) { synchronized (GLOBAL_CHILD_LOCK) { final var eventType = listener.getEventType(); var entry = listenerMap.get(eventType);