mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-23 00:21:26 +01:00
Add more node children manipulation methods
This commit is contained in:
parent
eb3cdd1495
commit
34cde8390a
@ -174,15 +174,13 @@ public class EventNode<T extends Event> {
|
|||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
public <E extends T> @NotNull List<EventNode<E>> findChildren(@NotNull String name, Class<E> eventType) {
|
public <E extends T> @NotNull List<EventNode<E>> findChildren(@NotNull String name, Class<E> eventType) {
|
||||||
|
if (children.isEmpty()) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
synchronized (GLOBAL_CHILD_LOCK) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
if (children.isEmpty()) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
List<EventNode<E>> result = new ArrayList<>();
|
List<EventNode<E>> result = new ArrayList<>();
|
||||||
this.children.forEach(child -> {
|
this.children.forEach(child -> {
|
||||||
final boolean nameCheck = child.getName().equals(name);
|
if (EventNode.equals(child, name, eventType)) {
|
||||||
final boolean typeCheck = child.eventType.isAssignableFrom(eventType);
|
|
||||||
if (nameCheck && typeCheck) {
|
|
||||||
result.add((EventNode<E>) child);
|
result.add((EventNode<E>) child);
|
||||||
}
|
}
|
||||||
result.addAll(child.findChildren(name, eventType));
|
result.addAll(child.findChildren(name, eventType));
|
||||||
@ -196,6 +194,43 @@ public class EventNode<T extends Event> {
|
|||||||
return findChildren(name, eventType);
|
return findChildren(name, eventType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <E extends T> void replaceChildren(@NotNull String name, @NotNull Class<E> eventType, @NotNull EventNode<E> eventNode) {
|
||||||
|
if (children.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
|
this.children.forEach(child -> {
|
||||||
|
if (EventNode.equals(child, name, eventType)) {
|
||||||
|
removeChild(child);
|
||||||
|
addChild(eventNode);
|
||||||
|
}
|
||||||
|
child.replaceChildren(name, eventType, eventNode);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void replaceChildren(@NotNull String name, @NotNull EventNode<T> eventNode) {
|
||||||
|
replaceChildren(name, eventType, eventNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends T> void removeChildren(@NotNull String name, @NotNull Class<E> eventType) {
|
||||||
|
if (children.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
|
this.children.forEach(child -> {
|
||||||
|
if (EventNode.equals(child, name, eventType)) {
|
||||||
|
removeChild(child);
|
||||||
|
}
|
||||||
|
child.removeChildren(name, eventType);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeChildren(@NotNull String name) {
|
||||||
|
removeChildren(name, eventType);
|
||||||
|
}
|
||||||
|
|
||||||
@Contract(value = "_ -> this")
|
@Contract(value = "_ -> this")
|
||||||
public @NotNull EventNode<T> addChild(@NotNull EventNode<? extends T> child) {
|
public @NotNull EventNode<T> addChild(@NotNull EventNode<? extends T> child) {
|
||||||
synchronized (GLOBAL_CHILD_LOCK) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
@ -303,6 +338,12 @@ public class EventNode<T extends Event> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean equals(EventNode<?> node, String name, Class<?> eventType) {
|
||||||
|
final boolean nameCheck = node.getName().equals(name);
|
||||||
|
final boolean typeCheck = node.eventType.isAssignableFrom(eventType);
|
||||||
|
return nameCheck && typeCheck;
|
||||||
|
}
|
||||||
|
|
||||||
private static class ListenerEntry<T extends Event> {
|
private static class ListenerEntry<T extends Event> {
|
||||||
private static final AtomicIntegerFieldUpdater<ListenerEntry> CHILD_UPDATER =
|
private static final AtomicIntegerFieldUpdater<ListenerEntry> CHILD_UPDATER =
|
||||||
AtomicIntegerFieldUpdater.newUpdater(ListenerEntry.class, "childCount");
|
AtomicIntegerFieldUpdater.newUpdater(ListenerEntry.class, "childCount");
|
||||||
|
@ -154,7 +154,13 @@ public class PlayerInit {
|
|||||||
private static AtomicReference<TickMonitor> LAST_TICK = new AtomicReference<>();
|
private static AtomicReference<TickMonitor> LAST_TICK = new AtomicReference<>();
|
||||||
|
|
||||||
public static void init() {
|
public static void init() {
|
||||||
MinecraftServer.getGlobalEventHandler().addChild(DEMO_NODE);
|
var eventHandler = MinecraftServer.getGlobalEventHandler();
|
||||||
|
eventHandler.addChild(DEMO_NODE);
|
||||||
|
var children = eventHandler.findChildren("demo", Event.class);
|
||||||
|
eventHandler.replaceChildren("demo", Event.class, EventNode.all("new_demo"));
|
||||||
|
eventHandler.replaceChildren("new_demo", EventNode.all("new_demo2"));
|
||||||
|
eventHandler.removeChildren("new_demo2");
|
||||||
|
|
||||||
MinecraftServer.getUpdateManager().addTickMonitor(tickMonitor ->
|
MinecraftServer.getUpdateManager().addTickMonitor(tickMonitor ->
|
||||||
LAST_TICK.set(tickMonitor));
|
LAST_TICK.set(tickMonitor));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user