Search in sources :

Example 1 with TextComponent

use of com.loohp.interactivechat.libs.net.kyori.adventure.text.TextComponent in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class CharacterDataArray method fromComponent.

public static CharacterDataArray fromComponent(Component component, boolean legacyRGB) {
    List<CharacterData> data = new LinkedList<>();
    StringBuilder sb = new StringBuilder();
    component = ComponentFlattening.flatten(component);
    for (Component each : component.children()) {
        Key font = each.style().font();
        if (font == null) {
            font = Key.key("minecraft:default");
        }
        TextColor color = each.color();
        if (color == null) {
            color = NamedTextColor.WHITE;
        }
        List<TextDecoration> decorations = each.decorations().entrySet().stream().filter(entry -> entry.getValue().equals(State.TRUE)).map(entry -> entry.getKey()).collect(Collectors.toList());
        String content;
        if (each instanceof TextComponent) {
            content = ChatColorUtils.filterIllegalColorCodes(((TextComponent) each).content(), legacyRGB);
        } else {
            content = ChatColorUtils.filterIllegalColorCodes(PlainTextComponentSerializer.plainText().serialize(each), legacyRGB);
        }
        if (content.isEmpty()) {
            continue;
        }
        CharacterData characterData = new CharacterData(font, color, decorations);
        for (char c : content.toCharArray()) {
            data.add(characterData);
            sb.append(c);
        }
    }
    return new CharacterDataArray(sb.toString().toCharArray(), data.toArray(new CharacterData[data.size()]));
}
Also used : TextDecoration(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration) ComponentFlattening(com.loohp.interactivechat.utils.ComponentFlattening) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) State(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration.State) Collectors(java.util.stream.Collectors) Key(com.loohp.interactivechat.libs.net.kyori.adventure.key.Key) List(java.util.List) TextComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TextComponent) PlainTextComponentSerializer(com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer) TextDecoration(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration) ChatColorUtils(com.loohp.interactivechat.utils.ChatColorUtils) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) NamedTextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor) LinkedList(java.util.LinkedList) TextComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TextComponent) LinkedList(java.util.LinkedList) TextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor) NamedTextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) TextComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TextComponent) Key(com.loohp.interactivechat.libs.net.kyori.adventure.key.Key)

Aggregations

Key (com.loohp.interactivechat.libs.net.kyori.adventure.key.Key)1 Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)1 TextComponent (com.loohp.interactivechat.libs.net.kyori.adventure.text.TextComponent)1 NamedTextColor (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor)1 TextColor (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextColor)1 TextDecoration (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration)1 State (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration.State)1 PlainTextComponentSerializer (com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer)1 ChatColorUtils (com.loohp.interactivechat.utils.ChatColorUtils)1 ComponentFlattening (com.loohp.interactivechat.utils.ComponentFlattening)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1