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;
}
Aggregations