Search in sources :

Example 1 with ShowItem

use of com.loohp.interactivechat.libs.net.kyori.adventure.text.event.HoverEvent.ShowItem 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)1 Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)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 ArrayList (java.util.ArrayList)1 ItemStack (org.bukkit.inventory.ItemStack)1