diff --git a/src/main/java/net/minestom/server/event/EventNodeImpl.java b/src/main/java/net/minestom/server/event/EventNodeImpl.java index 261a31ab4..c6ffa92c0 100644 --- a/src/main/java/net/minestom/server/event/EventNodeImpl.java +++ b/src/main/java/net/minestom/server/event/EventNodeImpl.java @@ -218,6 +218,46 @@ non-sealed class EventNodeImpl implements EventNode { return parent; } + @Override + public String toString() { + return createStringGraph(createGraph()); + } + + Graph createGraph() { + synchronized (GLOBAL_CHILD_LOCK) { + List children = this.children.stream().map(EventNodeImpl::createGraph).toList(); + return new Graph(getName(), getEventType().getSimpleName(), getPriority(), children); + } + } + + static String createStringGraph(Graph graph) { + StringBuilder buffer = new StringBuilder(); + genToStringTree(buffer, "", "", graph); + return buffer.toString(); + } + + private static void genToStringTree(StringBuilder buffer, String prefix, String childrenPrefix, Graph graph) { + buffer.append(prefix); + buffer.append(String.format("%s - EventType: %s - Priority: %d", graph.name(), graph.eventType(), graph.priority())); + buffer.append('\n'); + var nextNodes = graph.children(); + for (Iterator iterator = nextNodes.iterator(); iterator.hasNext(); ) { + Graph next = iterator.next(); + if (iterator.hasNext()) { + genToStringTree(buffer, childrenPrefix + '\u251C' + '\u2500' + " ", childrenPrefix + '\u2502' + " ", next); + } else { + genToStringTree(buffer, childrenPrefix + '\u2514' + '\u2500' + " ", childrenPrefix + " ", next); + } + } + } + + record Graph(String name, String eventType, int priority, + List children) { + public Graph { + children = children.stream().sorted(Comparator.comparingInt(Graph::priority)).toList(); + } + } + private void invalidateEventsFor(EventNodeImpl node) { for (Class eventType : listenerMap.keySet()) { node.invalidateEvent(eventType); diff --git a/src/test/java/net/minestom/server/event/EventNodeGraphTest.java b/src/test/java/net/minestom/server/event/EventNodeGraphTest.java new file mode 100644 index 000000000..c9f650b9e --- /dev/null +++ b/src/test/java/net/minestom/server/event/EventNodeGraphTest.java @@ -0,0 +1,55 @@ +package net.minestom.server.event; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class EventNodeGraphTest { + + @Test + public void single() { + EventNode node = EventNode.all("main"); + verifyGraph(node, new EventNodeImpl.Graph("main", "Event", 0, List.of())); + } + + @Test + public void singleChild() { + EventNode node = EventNode.all("main"); + node.addChild(EventNode.all("child")); + verifyGraph(node, new EventNodeImpl.Graph("main", "Event", 0, + List.of(new EventNodeImpl.Graph("child", "Event", 0, List.of()) + ))); + } + + @Test + public void childrenPriority() { + { + EventNode node = EventNode.all("main"); + node.addChild(EventNode.all("child1").setPriority(5)); + node.addChild(EventNode.all("child2").setPriority(10)); + verifyGraph(node, new EventNodeImpl.Graph("main", "Event", 0, + List.of(new EventNodeImpl.Graph("child1", "Event", 5, List.of()), + new EventNodeImpl.Graph("child2", "Event", 10, List.of()) + ))); + } + { + EventNode node = EventNode.all("main"); + node.addChild(EventNode.all("child2").setPriority(10)); + node.addChild(EventNode.all("child1").setPriority(5)); + verifyGraph(node, new EventNodeImpl.Graph("main", "Event", 0, + List.of(new EventNodeImpl.Graph("child1", "Event", 5, List.of()), + new EventNodeImpl.Graph("child2", "Event", 10, List.of()) + ))); + } + } + + void verifyGraph(EventNode n, EventNodeImpl.Graph graph) { + EventNodeImpl node = (EventNodeImpl) n; + var nodeGraph = node.createGraph(); + assertEquals(graph, nodeGraph, "Graphs are not equals"); + assertEquals(EventNodeImpl.createStringGraph(graph), EventNodeImpl.createStringGraph(nodeGraph), "String graphs are not equals"); + assertEquals(n.toString(), EventNodeImpl.createStringGraph(nodeGraph), "The node does not use createStringGraph"); + } +}