use of com.loohp.interactivechat.libs.net.kyori.adventure.text.Component in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class DiscordItemStackUtils method getToolTip.
public static DiscordToolTip getToolTip(ItemStack item, Player player) throws Exception {
String language = InteractiveChatDiscordSrvAddon.plugin.language;
if (!item.getType().equals(Material.AIR) && InteractiveChat.ecoHook) {
item = EcoHook.setEcoLores(item, player);
}
List<Component> prints = new ArrayList<>();
boolean hasCustomName = true;
if (item == null) {
item = new ItemStack(Material.AIR);
}
XMaterial xMaterial = XMaterialUtils.matchXMaterial(item);
Component itemDisplayNameComponent = ItemStackUtils.getDisplayName(item);
prints.add(itemDisplayNameComponent);
boolean hasMeta = item.hasItemMeta();
if (xMaterial.equals(XMaterial.WRITTEN_BOOK) && hasMeta && item.getItemMeta() instanceof BookMeta) {
BookMeta meta = (BookMeta) item.getItemMeta();
String author = meta.getAuthor();
if (author != null) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getBookAuthor(), language).replaceFirst("%1\\$s", author)));
}
Generation generation = meta.getGeneration();
if (generation == null) {
generation = Generation.ORIGINAL;
}
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getBookGeneration(generation), language)));
}
if (xMaterial.equals(XMaterial.SHIELD) && (!hasMeta || (hasMeta && !item.getItemMeta().hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)))) {
if (NBTEditor.contains(item, "BlockEntityTag")) {
List<Pattern> patterns = Collections.emptyList();
if (!(item.getItemMeta() instanceof BannerMeta)) {
if (item.getItemMeta() instanceof BlockStateMeta) {
BlockStateMeta bmeta = (BlockStateMeta) item.getItemMeta();
if (bmeta.hasBlockState()) {
Banner bannerBlockMeta = (Banner) bmeta.getBlockState();
patterns = bannerBlockMeta.getPatterns();
}
}
} else {
BannerMeta meta = (BannerMeta) item.getItemMeta();
patterns = meta.getPatterns();
}
for (Pattern pattern : patterns) {
PatternTypeWrapper type = PatternTypeWrapper.fromPatternType(pattern.getPattern());
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getBannerPatternName(type, pattern.getColor()), language)));
}
}
}
if (xMaterial.isOneOf(Arrays.asList("CONTAINS:Banner")) && (!hasMeta || (hasMeta && !item.getItemMeta().hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)))) {
List<Pattern> patterns = Collections.emptyList();
if (!(item.getItemMeta() instanceof BannerMeta)) {
if (item.getItemMeta() instanceof BlockStateMeta) {
BlockStateMeta bmeta = (BlockStateMeta) item.getItemMeta();
Banner bannerBlockMeta = (Banner) bmeta.getBlockState();
patterns = bannerBlockMeta.getPatterns();
}
} else {
BannerMeta meta = (BannerMeta) item.getItemMeta();
patterns = meta.getPatterns();
}
for (Pattern pattern : patterns) {
PatternTypeWrapper type = PatternTypeWrapper.fromPatternType(pattern.getPattern());
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getBannerPatternName(type, pattern.getColor()), language)));
}
}
if (xMaterial.equals(XMaterial.TROPICAL_FISH_BUCKET)) {
List<String> translations = TranslationKeyUtils.getTropicalFishBucketName(item);
if (translations.size() > 0) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + "" + ChatColor.ITALIC + LanguageUtils.getTranslation(translations.get(0), language)));
if (translations.size() > 1) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + "" + ChatColor.ITALIC + translations.stream().skip(1).map(each -> LanguageUtils.getTranslation(each, language)).collect(Collectors.joining(", "))));
}
}
}
if (xMaterial.isOneOf(Arrays.asList("CONTAINS:Music_Disc"))) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getMusicDiscName(item), language)));
}
if (xMaterial.equals(XMaterial.FIREWORK_ROCKET)) {
if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_12) && NBTEditor.contains(item, "Fireworks", "Flight")) {
int flight = NBTEditor.getByte(item, "Fireworks", "Flight");
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getRocketFlightDuration(), language) + " " + flight));
}
}
if (xMaterial.equals(XMaterial.CROSSBOW)) {
CrossbowMeta meta = (CrossbowMeta) item.getItemMeta();
List<ItemStack> charged = meta.getChargedProjectiles();
if (charged != null && !charged.isEmpty()) {
ItemStack charge = charged.get(0);
Component chargeItemName = getToolTip(charge, player).getComponents().get(0);
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.WHITE + LanguageUtils.getTranslation(TranslationKeyUtils.getCrossbowProjectile(), language) + " [" + InteractiveChatComponentSerializer.bungeecordApiLegacy().serialize(chargeItemName) + ChatColor.WHITE + "]"));
}
}
if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_12) && FilledMapUtils.isFilledMap(item)) {
MapMeta map = (MapMeta) item.getItemMeta();
MapView mapView;
int id;
if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_13_1)) {
mapView = map.getMapView();
id = mapView.getId();
} else if (InteractiveChat.version.equals(MCVersion.V1_13)) {
short shortId = (short) bukkitMapViewClassGetIdMethod.invoke(map);
mapView = (MapView) bukkitBukkitClassGetMapShortMethod.invoke(null, shortId);
id = shortId;
} else {
short shortId = item.getDurability();
mapView = (MapView) bukkitBukkitClassGetMapShortMethod.invoke(null, shortId);
id = shortId;
}
int scale = mapView.getScale().getValue();
if (!InteractiveChat.version.isLegacy()) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getFilledMapId(), language).replaceFirst("%s", id + "")));
} else {
prints.set(0, prints.get(0).children(Arrays.asList(LegacyComponentSerializer.legacySection().deserialize(ChatColor.WHITE + " (#" + id + ")"))));
}
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getFilledMapScale(), language).replaceFirst("%s", (int) Math.pow(2, scale) + "")));
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getFilledMapLevel(), language).replaceFirst("%s", scale + "").replaceFirst("%s", "4")));
}
if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_12) && !hasMeta || (hasMeta && !item.getItemMeta().hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS))) {
if (item.getItemMeta() instanceof PotionMeta) {
PotionMeta meta = (PotionMeta) item.getItemMeta();
List<PotionEffect> effects = new ArrayList<>();
List<PotionEffect> base = PotionUtils.getBasePotionEffect(item);
if (base != null) {
effects.addAll(base);
}
effects.addAll(meta.getCustomEffects());
if (effects.isEmpty()) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(LanguageUtils.getTranslation(TranslationKeyUtils.getNoEffect(), language)));
} else {
for (PotionEffect effect : effects) {
String key = TranslationKeyUtils.getEffect(effect.getType());
String translation = LanguageUtils.getTranslation(key, language);
String description = "";
if (key.equals(translation)) {
description += WordUtils.capitalize(effect.getType().getName().toLowerCase().replace("_", " "));
} else {
description += translation;
}
int amplifier = effect.getAmplifier();
String effectLevelTranslation = LanguageUtils.getTranslation(TranslationKeyUtils.getEffectLevel(amplifier), language);
if (effectLevelTranslation.length() > 0) {
description += " " + effectLevelTranslation;
}
if (!effect.getType().isInstant()) {
if (xMaterial.equals(XMaterial.LINGERING_POTION)) {
description += " (" + TimeUtils.getReadableTimeBetween(0, effect.getDuration() / 4 * 50L, ":", ChronoUnit.MINUTES, ChronoUnit.SECONDS, true) + ")";
} else {
description += " (" + TimeUtils.getReadableTimeBetween(0, effect.getDuration() * 50L, ":", ChronoUnit.MINUTES, ChronoUnit.SECONDS, true) + ")";
}
}
ChatColor color;
try {
color = PotionUtils.getPotionEffectChatColor(effect.getType());
} catch (Throwable e) {
color = ChatColor.BLUE;
}
prints.add(LegacyComponentSerializer.legacySection().deserialize(color + description));
}
}
}
}
if (!hasMeta || (hasMeta && !item.getItemMeta().hasItemFlag(ItemFlag.HIDE_ENCHANTS))) {
if (hasMeta && item.getItemMeta() instanceof EnchantmentStorageMeta) {
for (Entry<Enchantment, Integer> entry : CustomMapUtils.sortMapByValue(((EnchantmentStorageMeta) item.getItemMeta()).getStoredEnchants()).entrySet()) {
Enchantment ench = entry.getKey();
int level = entry.getValue();
String key = TranslationKeyUtils.getEnchantment(ench);
String translation = LanguageUtils.getTranslation(key, language);
String enchName;
if (key.equals(translation)) {
enchName = WordUtils.capitalize(ench.getName().toLowerCase().replace("_", " "));
} else {
enchName = translation;
}
if (enchName != null) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + enchName + (ench.getMaxLevel() == 1 && level == 1 ? "" : " " + LanguageUtils.getTranslation(TranslationKeyUtils.getEnchantmentLevel(level), language))));
}
}
} else {
for (Entry<Enchantment, Integer> entry : CustomMapUtils.sortMapByValue(item.getEnchantments()).entrySet()) {
Enchantment ench = entry.getKey();
int level = entry.getValue();
String key = TranslationKeyUtils.getEnchantment(ench);
String translation = LanguageUtils.getTranslation(key, language);
String enchName;
if (key.equals(translation)) {
enchName = WordUtils.capitalize(ench.getName().toLowerCase().replace("_", " "));
} else {
enchName = translation;
}
if (enchName != null) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + enchName + (ench.getMaxLevel() == 1 && level == 1 ? "" : " " + LanguageUtils.getTranslation(TranslationKeyUtils.getEnchantmentLevel(level), language))));
}
}
}
}
if (hasMeta && item.getItemMeta() instanceof LeatherArmorMeta && !item.getItemMeta().hasItemFlag(ItemFlag.HIDE_DYE)) {
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
if (NBTEditor.contains(item, "display", "color")) {
Color color = new Color(meta.getColor().asRGB());
String hex = ColorUtils.rgb2Hex(color).toUpperCase();
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getDyeColor(), language).replaceFirst("%s", hex)));
}
}
if (hasMeta) {
ItemMeta meta = item.getItemMeta();
if (meta.hasLore()) {
for (String lore : meta.getLore()) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.DARK_PURPLE + "" + ChatColor.ITALIC + lore));
}
}
}
if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_12) && hasMeta && NBTEditor.contains(item, "AttributeModifiers") && NBTEditor.getSize(item, "AttributeModifiers") > 0 && !item.getItemMeta().hasItemFlag(ItemFlag.HIDE_ATTRIBUTES)) {
boolean useMainHand = false;
List<Component> mainHand = new LinkedList<>();
boolean useOffhand = false;
List<Component> offHand = new LinkedList<>();
boolean useFeet = false;
List<Component> feet = new LinkedList<>();
boolean useLegs = false;
List<Component> legs = new LinkedList<>();
boolean useChest = false;
List<Component> chest = new LinkedList<>();
boolean useHead = false;
List<Component> head = new LinkedList<>();
ListTag<CompoundTag> attributeList = (ListTag<CompoundTag>) new SNBTDeserializer().fromString(NBTEditor.getNBTCompound(item, "tag", "AttributeModifiers").toJson());
for (CompoundTag attributeTag : attributeList) {
String attributeName = attributeTag.getString("AttributeName").replace("minecraft:", "");
double amount = attributeTag.getDouble("Amount");
int operation = attributeTag.containsKey("Operation") ? attributeTag.getInt("Operation") : 0;
Component attributeComponent = LegacyComponentSerializer.legacySection().deserialize((amount < 0 ? ChatColor.RED : ChatColor.BLUE) + LanguageUtils.getTranslation(TranslationKeyUtils.getAttributeModifierKey(amount, operation), language).replaceFirst("%s", ATTRIBUTE_FORMAT.format(Math.abs(amount)) + "").replaceFirst("%s", LanguageUtils.getTranslation(TranslationKeyUtils.getAttributeKey(attributeName), language)).replace("%%", "%"));
if (attributeTag.containsKey("Slot")) {
String slot = attributeTag.getString("Slot");
switch(slot) {
case "mainhand":
if (amount != 0) {
mainHand.add(attributeComponent);
}
useMainHand = true;
break;
case "offhand":
if (amount != 0) {
offHand.add(attributeComponent);
}
useOffhand = true;
break;
case "feet":
if (amount != 0) {
feet.add(attributeComponent);
}
useFeet = true;
break;
case "legs":
if (amount != 0) {
legs.add(attributeComponent);
}
useLegs = true;
break;
case "chest":
if (amount != 0) {
chest.add(attributeComponent);
}
useChest = true;
break;
case "head":
if (amount != 0) {
head.add(attributeComponent);
}
useHead = true;
break;
}
} else {
if (amount != 0) {
mainHand.add(attributeComponent);
offHand.add(attributeComponent);
feet.add(attributeComponent);
legs.add(attributeComponent);
chest.add(attributeComponent);
head.add(attributeComponent);
}
useMainHand = true;
useOffhand = true;
useFeet = true;
useLegs = true;
useChest = true;
useHead = true;
}
}
if (useMainHand) {
prints.add(Component.empty());
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getModifierSlotKey(EquipmentSlot.HAND), language)));
prints.addAll(mainHand);
}
if (useOffhand) {
prints.add(Component.empty());
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getModifierSlotKey(EquipmentSlot.OFF_HAND), language)));
prints.addAll(offHand);
}
if (useFeet) {
prints.add(Component.empty());
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getModifierSlotKey(EquipmentSlot.FEET), language)));
prints.addAll(feet);
}
if (useLegs) {
prints.add(Component.empty());
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getModifierSlotKey(EquipmentSlot.LEGS), language)));
prints.addAll(legs);
}
if (useChest) {
prints.add(Component.empty());
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getModifierSlotKey(EquipmentSlot.CHEST), language)));
prints.addAll(chest);
}
if (useHead) {
prints.add(Component.empty());
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getModifierSlotKey(EquipmentSlot.HEAD), language)));
prints.addAll(head);
}
}
if (hasMeta && isUnbreakable(item) && !item.getItemMeta().hasItemFlag(ItemFlag.HIDE_UNBREAKABLE)) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.BLUE + LanguageUtils.getTranslation(TranslationKeyUtils.getUnbreakable(), language)));
}
if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_12) && hasMeta && !item.getItemMeta().hasItemFlag(ItemFlag.HIDE_DESTROYS)) {
if (NBTEditor.contains(item, "CanDestroy") && NBTEditor.getSize(item, "CanDestroy") > 0) {
prints.add(Component.empty());
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getCanDestroy(), language)));
ListTag<StringTag> materialList = (ListTag<StringTag>) new SNBTDeserializer().fromString(NBTEditor.getNBTCompound(item, "tag", "CanDestroy").toJson());
for (StringTag materialTag : materialList) {
XMaterial parsedXMaterial = XMaterialUtils.matchXMaterial(materialTag.getValue().replace("minecraft:", "").toUpperCase());
if (parsedXMaterial == null) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.DARK_GRAY + WordUtils.capitalizeFully(materialTag.getValue().replace("_", " ").toLowerCase())));
} else {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.DARK_GRAY + LanguageUtils.getTranslation(LanguageUtils.getTranslationKey(parsedXMaterial.parseItem()), language)));
}
}
}
}
if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_12) && hasMeta && !item.getItemMeta().hasItemFlag(ItemFlag.HIDE_PLACED_ON)) {
if (NBTEditor.contains(item, "CanPlaceOn") && NBTEditor.getSize(item, "CanPlaceOn") > 0) {
prints.add(Component.empty());
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.GRAY + LanguageUtils.getTranslation(TranslationKeyUtils.getCanPlace(), language)));
ListTag<StringTag> materialList = (ListTag<StringTag>) new SNBTDeserializer().fromString(NBTEditor.getNBTCompound(item, "tag", "CanPlaceOn").toJson());
for (StringTag materialTag : materialList) {
XMaterial parsedXMaterial = XMaterialUtils.matchXMaterial(materialTag.getValue().replace("minecraft:", "").toUpperCase());
if (parsedXMaterial == null) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.DARK_GRAY + WordUtils.capitalizeFully(materialTag.getValue().replace("_", " ").toLowerCase())));
} else {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.DARK_GRAY + LanguageUtils.getTranslation(LanguageUtils.getTranslationKey(parsedXMaterial.parseItem()), language)));
}
}
}
}
if (item.getType().getMaxDurability() > 0) {
int durability = item.getType().getMaxDurability() - (InteractiveChat.version.isLegacy() ? item.getDurability() : ((Damageable) item.getItemMeta()).getDamage());
int maxDur = item.getType().getMaxDurability();
if (durability < maxDur) {
prints.add(LegacyComponentSerializer.legacySection().deserialize(ChatColor.WHITE + LanguageUtils.getTranslation(TranslationKeyUtils.getDurability(), language).replaceFirst("%s", String.valueOf(durability)).replaceFirst("%s", String.valueOf(maxDur))));
}
}
return new DiscordToolTip(prints, !hasCustomName && prints.size() <= 1);
}
use of com.loohp.interactivechat.libs.net.kyori.adventure.text.Component in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class Updater method sendUpdateMessage.
public static void sendUpdateMessage(CommandSender sender, String version, int spigotPluginId, boolean devbuild) {
if (!version.equals("error")) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (!devbuild) {
player.sendMessage(ChatColor.YELLOW + "[ICDiscordSrvAddon] A new version is available on SpigotMC: " + version);
Component url = LegacyComponentSerializer.legacySection().deserialize(ChatColor.GOLD + "https://www.spigotmc.org/resources/" + spigotPluginId);
url = url.hoverEvent(HoverEvent.showText(Component.text("Click me!").color(NamedTextColor.AQUA)));
url = url.clickEvent(ClickEvent.openUrl("https://www.spigotmc.org/resources/" + spigotPluginId));
InteractiveChat.sendMessage(player, url);
} else {
sender.sendMessage(ChatColor.GREEN + "[ICDiscordSrvAddon] You are running the latest release!");
Component url = LegacyComponentSerializer.legacySection().deserialize(ChatColor.YELLOW + "[InteractiveChat] However, a new Development Build is available if you want to try that!");
url = url.hoverEvent(HoverEvent.showText(Component.text("Click me!").color(NamedTextColor.AQUA)));
url = url.clickEvent(ClickEvent.openUrl("https://ci.loohpjames.com/job/" + PLUGIN_NAME));
InteractiveChat.sendMessage(player, url);
}
} else {
if (!devbuild) {
sender.sendMessage(ChatColor.YELLOW + "[ICDiscordSrvAddon] A new version is available on SpigotMC: " + version);
sender.sendMessage(ChatColor.GOLD + "Download: https://www.spigotmc.org/resources/" + spigotPluginId);
} else {
sender.sendMessage(ChatColor.GREEN + "[ICDiscordSrvAddon] You are running the latest release!");
sender.sendMessage(ChatColor.YELLOW + "[ICDiscordSrvAddon] However, a new Development Build is available if you want to try that!");
}
}
}
}
use of com.loohp.interactivechat.libs.net.kyori.adventure.text.Component in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class AchievementUtils method getAdvancementData.
public static AdvancementData getAdvancementData(Object achievementObject) {
try {
Object nmsAchievement = craftStatisticClassGetNMSAchievementMethod.invoke(null, achievementObject);
nmsAdvancementClassNameField.setAccessible(true);
String name = nmsAdvancementClassNameField.get(nmsAchievement).toString();
Component title = Component.translatable(name).color(NamedTextColor.GREEN);
Component description = Component.translatable(name + ".desc");
AdvancementType advancementType = AdvancementType.LEGACY;
nmsAdvancementClassItemStackField.setAccessible(true);
Object nmsItemStack = nmsAdvancementClassItemStackField.get(nmsAchievement);
ItemStack itemStack = ItemStackUtils.toBukkitCopy(nmsItemStack);
return new AdvancementData(title, description, itemStack, advancementType, true);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
use of com.loohp.interactivechat.libs.net.kyori.adventure.text.Component in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class MinecraftFontRenderer method updateTextComponent.
private void updateTextComponent() {
if (resourceManager == null) {
return;
}
try {
if (!updateTextImageLock.tryLock(0, TimeUnit.MILLISECONDS)) {
return;
}
} catch (InterruptedException e) {
}
try {
String text = textAreaInput.getText();
Component component;
if (text.isEmpty()) {
component = Component.empty();
} else {
try {
if (legacyTextCheckBox.isSelected()) {
component = LegacyComponentSerializer.legacySection().deserialize(ChatColorUtils.translateAlternateColorCodes('&', text, false, true, Collections.emptyList()));
} else {
component = GsonComponentSerializer.gson().deserialize(text);
}
} catch (Throwable e) {
component = PlainTextComponentSerializer.plainText().deserialize(e.getLocalizedMessage().replaceAll("\\t", " ")).color(NamedTextColor.RED);
}
}
List<Component> prints = new ArrayList<>(ComponentStyling.splitAtLineBreaks(component));
renderingComponents.set(prints);
} catch (Throwable e) {
e.printStackTrace();
}
updateTextImageLock.unlock();
}
use of com.loohp.interactivechat.libs.net.kyori.adventure.text.Component 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()]));
}
Aggregations