Add more node children manipulation methods

This commit is contained in:
TheMode 2021-06-08 17:09:24 +02:00
parent eb3cdd1495
commit 34cde8390a
2 changed files with 54 additions and 7 deletions

View File

@ -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");

View File

@ -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));