mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-23 16:41:35 +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));
|
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) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
final boolean result = this.children.add((EventNode<T>) child);
|
final boolean result = this.children.add((EventNode<T>) child);
|
||||||
if (result) {
|
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) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
final boolean result = this.children.remove(child);
|
final boolean result = this.children.remove(child);
|
||||||
if (result) {
|
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) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
final var eventType = listener.getEventType();
|
final var eventType = listener.getEventType();
|
||||||
this.listenerMap.computeIfAbsent(eventType, aClass -> new CopyOnWriteArrayList<>())
|
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) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
final var eventType = listener.getEventType();
|
final var eventType = listener.getEventType();
|
||||||
var listeners = listenerMap.get(eventType);
|
var listeners = listenerMap.get(eventType);
|
||||||
if (listeners == null || listeners.isEmpty())
|
if (listeners == null || listeners.isEmpty())
|
||||||
return;
|
return this;
|
||||||
final boolean removed = listeners.remove(listener);
|
final boolean removed = listeners.remove(listener);
|
||||||
if (removed && parent != null) {
|
if (removed && parent != null) {
|
||||||
synchronized (parent.lock) {
|
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<>();
|
RedirectionEntry<E> entry = new RedirectionEntry<>();
|
||||||
entry.filter = filter;
|
entry.filter = filter;
|
||||||
entry.node = node;
|
entry.node = node;
|
||||||
this.redirectionMap.put(value, (RedirectionEntry<T>) entry);
|
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);
|
this.redirectionMap.remove(value);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull String getName() {
|
public @NotNull String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(@NotNull String name) {
|
public EventNode<T> setName(@NotNull String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Set<@NotNull EventNode<T>> getChildren() {
|
public @NotNull Set<@NotNull EventNode<T>> getChildren() {
|
||||||
|
@ -137,22 +137,20 @@ public class PlayerInit {
|
|||||||
|
|
||||||
// EVENT REGISTERING
|
// EVENT REGISTERING
|
||||||
|
|
||||||
var empty = EventNode.all();
|
var empty = EventNode.all()
|
||||||
empty.setName("empty");
|
.setName("empty")
|
||||||
empty.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> {
|
.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
var node = EventNode.type(EventFilter.PLAYER);
|
var node = EventNode.type(EventFilter.PLAYER)
|
||||||
node.setName("node");
|
.setName("node")
|
||||||
node.addListener(EventListener.builder(PlayerTickEvent.class)
|
.addListener(EventListener.builder(PlayerTickEvent.class)
|
||||||
.handler(playerTickEvent -> System.out.println("Player tick!"))
|
.handler(playerTickEvent -> System.out.println("Player tick!"))
|
||||||
.expirationCount(50)
|
.expirationCount(50)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
|
var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative)
|
||||||
var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative);
|
.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> System.out.println("creative player moved")));
|
||||||
conditional.addListener(EventListener.of(PlayerMoveEvent.class, (event) ->
|
|
||||||
System.out.println("creative player moved")));
|
|
||||||
|
|
||||||
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
|
GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler();
|
||||||
globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {
|
globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> {
|
||||||
|
Loading…
Reference in New Issue
Block a user