Fix ChatUtil stuff
This commit is contained in:
parent
48df97d610
commit
9e50206aee
@ -29,7 +29,6 @@ import net.kyori.adventure.text.format.TextDecoration;
|
|||||||
import net.kyori.adventure.text.format.TextDecoration.State;
|
import net.kyori.adventure.text.format.TextDecoration.State;
|
||||||
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
|
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
|
||||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
|
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import net.md_5.bungee.api.chat.BaseComponent;
|
import net.md_5.bungee.api.chat.BaseComponent;
|
||||||
|
|
||||||
@ -60,8 +59,9 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
|
|||||||
return LegacyComponentSerializer.legacySection().serialize(getAdv());
|
return LegacyComponentSerializer.legacySection().serialize(getAdv());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
public String getPlainText() {
|
public String getPlainText() {
|
||||||
return PlainComponentSerializer.plain().serializeOr(getAdv(), "");
|
return net.kyori.adventure.text.serializer.plain.PlainComponentSerializer.plain().serializeOr(getAdv(), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,8 +4,6 @@ import java.util.Objects;
|
|||||||
|
|
||||||
import fr.pandacube.lib.core.chat.Chat.FormatableChat;
|
import fr.pandacube.lib.core.chat.Chat.FormatableChat;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
|
||||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
|
||||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
import net.md_5.bungee.api.chat.BaseComponent;
|
import net.md_5.bungee.api.chat.BaseComponent;
|
||||||
|
|
||||||
|
@ -15,10 +15,12 @@ import java.util.stream.Collectors;
|
|||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
|
||||||
import fr.pandacube.lib.core.chat.Chat.FormatableChat;
|
import fr.pandacube.lib.core.chat.Chat.FormatableChat;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.TextComponent;
|
||||||
|
import net.kyori.adventure.text.TranslatableComponent;
|
||||||
|
import net.kyori.adventure.text.format.TextDecoration;
|
||||||
|
import net.kyori.adventure.text.format.TextDecoration.State;
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import net.md_5.bungee.api.chat.BaseComponent;
|
|
||||||
import net.md_5.bungee.api.chat.TextComponent;
|
|
||||||
import net.md_5.bungee.api.chat.TranslatableComponent;
|
|
||||||
|
|
||||||
public class ChatUtil {
|
public class ChatUtil {
|
||||||
|
|
||||||
@ -184,7 +186,7 @@ public class ChatUtil {
|
|||||||
public static Chat centerText(Chat text, char repeatedChar, ChatColor decorationColor,
|
public static Chat centerText(Chat text, char repeatedChar, ChatColor decorationColor,
|
||||||
boolean console) {
|
boolean console) {
|
||||||
|
|
||||||
int textWidth = componentWidth(text.get(), console);
|
int textWidth = componentWidth(text.getAdv(), console);
|
||||||
int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH;
|
int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH;
|
||||||
|
|
||||||
if (textWidth > maxWidth)
|
if (textWidth > maxWidth)
|
||||||
@ -212,7 +214,7 @@ public class ChatUtil {
|
|||||||
public static Chat leftText(Chat text, char repeatedChar, ChatColor decorationColor, int nbLeft,
|
public static Chat leftText(Chat text, char repeatedChar, ChatColor decorationColor, int nbLeft,
|
||||||
boolean console) {
|
boolean console) {
|
||||||
|
|
||||||
int textWidth = componentWidth(text.get(), console);
|
int textWidth = componentWidth(text.getAdv(), console);
|
||||||
int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH;
|
int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH;
|
||||||
int repeatedCharWidth = charW(repeatedChar, console, false);
|
int repeatedCharWidth = charW(repeatedChar, console, false);
|
||||||
int leftWidth = nbLeft * repeatedCharWidth;
|
int leftWidth = nbLeft * repeatedCharWidth;
|
||||||
@ -235,7 +237,7 @@ public class ChatUtil {
|
|||||||
public static Chat rightText(Chat text, char repeatedChar, ChatColor decorationColor, int nbRight,
|
public static Chat rightText(Chat text, char repeatedChar, ChatColor decorationColor, int nbRight,
|
||||||
boolean console) {
|
boolean console) {
|
||||||
|
|
||||||
int textWidth = componentWidth(text.get(), console);
|
int textWidth = componentWidth(text.getAdv(), console);
|
||||||
int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH;
|
int maxWidth = (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH;
|
||||||
int repeatedCharWidth = charW(repeatedChar, console, false);
|
int repeatedCharWidth = charW(repeatedChar, console, false);
|
||||||
int rightWidth = nbRight * repeatedCharWidth;
|
int rightWidth = nbRight * repeatedCharWidth;
|
||||||
@ -271,41 +273,41 @@ public class ChatUtil {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static int componentWidth(Component component, boolean console) {
|
||||||
|
return componentWidth(component, console, false);
|
||||||
public static int componentWidth(BaseComponent[] components, boolean console) {
|
|
||||||
if (components == null)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
int count = 0;
|
|
||||||
|
|
||||||
for (BaseComponent c : components)
|
|
||||||
count += componentWidth(c, console);
|
|
||||||
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int componentWidth(BaseComponent component, boolean console) {
|
public static int componentWidth(Component component, boolean console, boolean parentBold) {
|
||||||
if (component == null)
|
if (component == null)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
|
State currentBold = component.style().decoration(TextDecoration.BOLD);
|
||||||
|
boolean actuallyBold = childBold(parentBold, currentBold);
|
||||||
|
|
||||||
if (component instanceof TextComponent) {
|
if (component instanceof TextComponent) {
|
||||||
count += strWidth(((TextComponent)component).getText(), console, component.isBold());
|
count += strWidth(((TextComponent)component).content(), console, actuallyBold);
|
||||||
}
|
}
|
||||||
else if (component instanceof TranslatableComponent) {
|
else if (component instanceof TranslatableComponent) {
|
||||||
for (BaseComponent c : ((TranslatableComponent)component).getWith())
|
for (Component c : ((TranslatableComponent)component).args())
|
||||||
count += componentWidth(c, console);
|
count += componentWidth(c, console, actuallyBold);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (component.getExtra() != null) {
|
if (component.children() != null) {
|
||||||
for (BaseComponent c : component.getExtra())
|
for (Component c : component.children())
|
||||||
count += componentWidth(c, console);
|
count += componentWidth(c, console, actuallyBold);
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean childBold(boolean parent, TextDecoration.State child) {
|
||||||
|
return (parent && child != State.FALSE) || (!parent && child == State.TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
public static int strWidth(String str, boolean console, boolean bold) {
|
public static int strWidth(String str, boolean console, boolean bold) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@ -462,14 +464,6 @@ public class ChatUtil {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static BaseComponent toUniqueBaseComponent(BaseComponent... baseComponents) {
|
|
||||||
if (baseComponents == null || baseComponents.length == 0)
|
|
||||||
return new TextComponent();
|
|
||||||
if (baseComponents.length == 1)
|
|
||||||
return baseComponents[0];
|
|
||||||
return new TextComponent(baseComponents);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -478,18 +472,6 @@ public class ChatUtil {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a tree view based on the tree structure {@code node}.
|
|
||||||
*
|
|
||||||
* Each element in the returned array represent 1 line of the tree view.
|
|
||||||
* Thus, the caller may send each line separately or at once depending of the quantity of data.
|
|
||||||
* @param node
|
|
||||||
* @return A array of component, each element being a single line.
|
|
||||||
*/
|
|
||||||
public static BaseComponent[] treeView(DisplayTreeNode node, boolean console) {
|
|
||||||
List<TextComponent> ret = treeView_(node, console);
|
|
||||||
return ret.toArray(new BaseComponent[ret.size()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String TREE_MIDDLE_CONNECTED = "├";
|
private static final String TREE_MIDDLE_CONNECTED = "├";
|
||||||
private static final String TREE_END_CONNECTED = "└";
|
private static final String TREE_END_CONNECTED = "└";
|
||||||
@ -498,24 +480,30 @@ public class ChatUtil {
|
|||||||
private static final String TREE_MIDDLE_OPEN_CONSOLE = "│";
|
private static final String TREE_MIDDLE_OPEN_CONSOLE = "│";
|
||||||
private static final String TREE_END_OPEN_CONSOLE = " "; // nbsp
|
private static final String TREE_END_OPEN_CONSOLE = " "; // nbsp
|
||||||
|
|
||||||
private static List<TextComponent> treeView_(DisplayTreeNode node, boolean console) {
|
/**
|
||||||
List<TextComponent> ret = new ArrayList<>();
|
* Generate a tree view based on the tree structure {@code node}.
|
||||||
|
*
|
||||||
|
* Each element in the returned list represent 1 line of the tree view.
|
||||||
|
* Thus, the caller may send each line separately or at once depending of the quantity of data.
|
||||||
|
* @param node
|
||||||
|
* @return A array of component, each element being a single line.
|
||||||
|
*/
|
||||||
|
public static List<Chat> treeView(DisplayTreeNode node, boolean console) {
|
||||||
|
List<Chat> ret = new ArrayList<>();
|
||||||
|
|
||||||
TextComponent curr = new TextComponent();
|
ret.add(chat()
|
||||||
curr.addExtra(node.component);
|
.then(node.component));
|
||||||
curr.setText("");
|
|
||||||
|
|
||||||
ret.add(curr);
|
|
||||||
|
|
||||||
for (int i = 0; i < node.children.size(); i++) {
|
for (int i = 0; i < node.children.size(); i++) {
|
||||||
List<TextComponent> childComponents = treeView_(node.children.get(i), console);
|
List<Chat> childComponents = treeView(node.children.get(i), console);
|
||||||
boolean last = i == node.children.size() - 1;
|
boolean last = i == node.children.size() - 1;
|
||||||
for (int j = 0; j < childComponents.size(); j++) {
|
for (int j = 0; j < childComponents.size(); j++) {
|
||||||
TextComponent cComp = childComponents.get(j);
|
|
||||||
String prefix = last ? (j == 0 ? TREE_END_CONNECTED : (console ? TREE_END_OPEN_CONSOLE : TREE_END_OPEN))
|
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));
|
: (j == 0 ? TREE_MIDDLE_CONNECTED : (console ? TREE_MIDDLE_OPEN_CONSOLE : TREE_MIDDLE_OPEN));
|
||||||
cComp.setText(prefix + cComp.getText());
|
|
||||||
ret.add(cComp);
|
ret.add(text(prefix)
|
||||||
|
.then(childComponents.get(j)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -528,10 +516,10 @@ public class ChatUtil {
|
|||||||
|
|
||||||
|
|
||||||
public static class DisplayTreeNode {
|
public static class DisplayTreeNode {
|
||||||
public final BaseComponent component;
|
public final Chat component;
|
||||||
public final List<DisplayTreeNode> children = new ArrayList<>();
|
public final List<DisplayTreeNode> children = new ArrayList<>();
|
||||||
|
|
||||||
public DisplayTreeNode(BaseComponent cmp) {
|
public DisplayTreeNode(Chat cmp) {
|
||||||
component = cmp;
|
component = cmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user