mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
EventNode methods return self
This commit is contained in:
parent
d5cce11562
commit
8ec33bb6c8
@ -108,7 +108,7 @@ public class EventNode<T extends Event> {
|
||||
this.children.forEach(eventNode -> eventNode.call(event));
|
||||
}
|
||||
|
||||
public void addChild(@NotNull EventNode<? extends T> child) {
|
||||
public EventNode<T> addChild(@NotNull EventNode<? extends T> child) {
|
||||
synchronized (GLOBAL_CHILD_LOCK) {
|
||||
final boolean result = this.children.add((EventNode<T>) child);
|
||||
if (result) {
|
||||
@ -122,9 +122,10 @@ public class EventNode<T extends Event> {
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void removeChild(@NotNull EventNode<? extends T> child) {
|
||||
public EventNode<T> removeChild(@NotNull EventNode<? extends T> child) {
|
||||
synchronized (GLOBAL_CHILD_LOCK) {
|
||||
final boolean result = this.children.remove(child);
|
||||
if (result) {
|
||||
@ -138,9 +139,10 @@ public class EventNode<T extends Event> {
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void addListener(@NotNull EventListener<? extends T> listener) {
|
||||
public EventNode<T> addListener(@NotNull EventListener<? extends T> listener) {
|
||||
synchronized (GLOBAL_CHILD_LOCK) {
|
||||
final var eventType = listener.getEventType();
|
||||
this.listenerMap.computeIfAbsent(eventType, aClass -> new CopyOnWriteArrayList<>())
|
||||
@ -151,14 +153,15 @@ public class EventNode<T extends Event> {
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void removeListener(@NotNull EventListener<? extends T> listener) {
|
||||
public EventNode<T> removeListener(@NotNull EventListener<? extends T> 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<T extends Event> {
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public <E extends T, V2> void map(@NotNull EventFilter<E, V2> filter, @NotNull V2 value, @NotNull EventNode<E> node) {
|
||||
public <E extends T, V2> EventNode<T> map(@NotNull EventFilter<E, V2> filter, @NotNull V2 value, @NotNull EventNode<E> node) {
|
||||
RedirectionEntry<E> entry = new RedirectionEntry<>();
|
||||
entry.filter = filter;
|
||||
entry.node = node;
|
||||
this.redirectionMap.put(value, (RedirectionEntry<T>) entry);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void removeMap(@NotNull Object value) {
|
||||
public EventNode<T> removeMap(@NotNull Object value) {
|
||||
this.redirectionMap.remove(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public @NotNull String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(@NotNull String name) {
|
||||
public EventNode<T> setName(@NotNull String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public @NotNull Set<@NotNull EventNode<T>> getChildren() {
|
||||
|
@ -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 -> {
|
||||
|
Loading…
Reference in New Issue
Block a user