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()]));
}
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;
}
Aggregations