2022-07-30 13:58:16 +02:00
|
|
|
|
package fr.pandacube.lib.chat;
|
|
|
|
|
|
2024-02-14 19:55:56 +01:00
|
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
|
import net.kyori.adventure.text.ComponentLike;
|
|
|
|
|
|
2022-07-30 13:58:16 +02:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
2024-02-14 19:55:56 +01:00
|
|
|
|
* A tree structure of chat {@link Component} intended to be rendered in chat using {@link #render(boolean)}.
|
2022-07-30 13:58:16 +02:00
|
|
|
|
*/
|
|
|
|
|
public class ChatTreeNode {
|
|
|
|
|
|
|
|
|
|
private static final String TREE_MIDDLE_CONNECTED = "├";
|
|
|
|
|
private static final String TREE_END_CONNECTED = "└";
|
|
|
|
|
private static final String TREE_MIDDLE_OPEN = "│§0`§r";
|
|
|
|
|
private static final String TREE_END_OPEN = "§0```§r";
|
|
|
|
|
private static final String TREE_MIDDLE_OPEN_CONSOLE = "│";
|
|
|
|
|
private static final String TREE_END_OPEN_CONSOLE = " "; // nbsp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The component for the current node.
|
|
|
|
|
*/
|
2024-02-14 19:55:56 +01:00
|
|
|
|
public final ComponentLike component;
|
2022-07-30 13:58:16 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Children nodes.
|
|
|
|
|
*/
|
|
|
|
|
public final List<ChatTreeNode> children = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
/**
|
2023-06-20 00:15:46 +02:00
|
|
|
|
* Construct a new {@link ChatTreeNode}.
|
2022-07-30 13:58:16 +02:00
|
|
|
|
* @param cmp the component for the current node.
|
|
|
|
|
*/
|
2024-02-14 19:55:56 +01:00
|
|
|
|
public ChatTreeNode(ComponentLike cmp) {
|
2022-07-30 13:58:16 +02:00
|
|
|
|
component = cmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a child to the current node.
|
|
|
|
|
* @param child the child to add.
|
|
|
|
|
* @return this.
|
|
|
|
|
*/
|
|
|
|
|
public ChatTreeNode addChild(ChatTreeNode child) {
|
|
|
|
|
children.add(child);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a tree view based on this tree structure.
|
|
|
|
|
* <p>
|
|
|
|
|
* Each element in the returned list represent 1 line of this tree view.
|
2023-06-20 00:15:46 +02:00
|
|
|
|
* Thus, the caller may send each line separately or at once, depending on the quantity of data.
|
2022-07-30 13:58:16 +02:00
|
|
|
|
* @param console true to render for console, false otherwise.
|
2024-02-14 19:55:56 +01:00
|
|
|
|
* @return a list of component, each element being a single line.
|
2022-07-30 13:58:16 +02:00
|
|
|
|
*/
|
|
|
|
|
public List<Chat> render(boolean console) {
|
|
|
|
|
List<Chat> ret = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
ret.add(ChatStatic.chat()
|
|
|
|
|
.then(component));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < children.size(); i++) {
|
|
|
|
|
List<Chat> childComponents = children.get(i).render(console);
|
|
|
|
|
boolean last = i == children.size() - 1;
|
|
|
|
|
for (int j = 0; j < childComponents.size(); j++) {
|
|
|
|
|
|
|
|
|
|
String prefix = last ? (j == 0 ? TREE_END_CONNECTED : (console ? TREE_END_OPEN_CONSOLE : TREE_END_OPEN))
|
|
|
|
|
: (j == 0 ? TREE_MIDDLE_CONNECTED : (console ? TREE_MIDDLE_OPEN_CONSOLE : TREE_MIDDLE_OPEN));
|
|
|
|
|
|
|
|
|
|
ret.add(ChatStatic.text(prefix)
|
|
|
|
|
.then(childComponents.get(j)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|