Search in sources :

Example 1 with Key

use of com.loohp.interactivechat.libs.net.kyori.adventure.key.Key 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)

Example 2 with Key

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

the class ComponentStringUtils method extractItemStack.

@SuppressWarnings("deprecation")
public static ItemStack extractItemStack(Component component) {
    component = ComponentFlattening.flatten(component);
    List<Component> children = new ArrayList<>(component.children());
    for (int i = 0; i < children.size(); i++) {
        Component child = children.get(i);
        HoverEvent<?> hoverEvent = child.hoverEvent();
        if (hoverEvent != null && hoverEvent.action().equals(HoverEvent.Action.SHOW_ITEM)) {
            ShowItem showItem = (ShowItem) hoverEvent.value();
            Key key = showItem.item();
            int count = showItem.count();
            String simpleNbt = "{id:\"" + key.asString() + "\", Count: " + count + "b}";
            String longNbt = showItem.nbt() == null ? null : showItem.nbt().string();
            ItemStack itemstack = null;
            try {
                itemstack = ItemNBTUtils.getItemFromNBTJson(simpleNbt);
            } catch (Throwable e) {
            }
            if (longNbt != null) {
                try {
                    itemstack = Bukkit.getUnsafe().modifyItemStack(itemstack, longNbt);
                } catch (Throwable e) {
                }
            }
            if (itemstack != null) {
                return itemstack;
            }
        }
        if (child instanceof TranslatableComponent) {
            TranslatableComponent trans = (TranslatableComponent) child;
            List<Component> withs = new ArrayList<>(trans.args());
            for (Component with : withs) {
                ItemStack itemstack = extractItemStack(with);
                if (itemstack != null) {
                    return itemstack;
                }
            }
            trans = trans.args(withs);
            children.set(i, trans);
        }
    }
    return null;
}
Also used : ShowItem(com.loohp.interactivechat.libs.net.kyori.adventure.text.event.HoverEvent.ShowItem) TranslatableComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent) ArrayList(java.util.ArrayList) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) TranslatableComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent) ItemStack(org.bukkit.inventory.ItemStack) Key(com.loohp.interactivechat.libs.net.kyori.adventure.key.Key)

Aggregations

Key (com.loohp.interactivechat.libs.net.kyori.adventure.key.Key)2 Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)2 TextComponent (com.loohp.interactivechat.libs.net.kyori.adventure.text.TextComponent)1 TranslatableComponent (com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent)1 ShowItem (com.loohp.interactivechat.libs.net.kyori.adventure.text.event.HoverEvent.ShowItem)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 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ItemStack (org.bukkit.inventory.ItemStack)1