diff --git a/adventure-utils/src/main/java/com/pepedevs/corelib/adventure/AdventureUtils.java b/adventure-utils/src/main/java/com/pepedevs/corelib/adventure/AdventureUtils.java index f7dda06..05463ff 100644 --- a/adventure-utils/src/main/java/com/pepedevs/corelib/adventure/AdventureUtils.java +++ b/adventure-utils/src/main/java/com/pepedevs/corelib/adventure/AdventureUtils.java @@ -1,6 +1,8 @@ package com.pepedevs.corelib.adventure; +import com.google.gson.Gson; import com.google.gson.JsonParseException; +import com.pepedevs.corelib.utils.reflection.resolver.FieldResolver; import com.pepedevs.corelib.utils.reflection.resolver.MethodResolver; import com.pepedevs.corelib.utils.reflection.resolver.ResolverQuery; import com.pepedevs.corelib.utils.reflection.resolver.minecraft.CraftClassResolver; @@ -10,22 +12,23 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.kyori.adventure.translation.GlobalTranslator; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Locale; public class AdventureUtils { private static final ClassWrapper I_CHAT_BASE_COMPONENT_CLASS; private static final ClassWrapper I_CHAT_BASE_COMPONENT_CHAT_SERIALIZER_INNER_CLASS; private static final ClassWrapper CRAFT_CHAT_MESSAGE_CLASS; - private static final MethodWrapper CHAT_SERIALIZER_A_METHOD_STRING; - private static final MethodWrapper CHAT_SERIALIZER_A_METHOD_COMPONENT; private static final MethodWrapper CRAFT_CHAT_MESSAGE_FROM_COMPONENT_METHOD; private static final MethodWrapper CRAFT_CHAT_MESSAGE_FROM_STRING_METHOD; + private static final Gson NMS_GSON; private static final GsonComponentSerializer GSON = GsonComponentSerializer.gson(); static { @@ -34,37 +37,104 @@ public class AdventureUtils { I_CHAT_BASE_COMPONENT_CLASS = NMS_CLASS_RESOLVER.resolveWrapper("IChatBaseComponent", "net.minecraft.network.chat.IChatBaseComponent"); I_CHAT_BASE_COMPONENT_CHAT_SERIALIZER_INNER_CLASS = NMS_CLASS_RESOLVER.resolveWrapper("IChatBaseComponent$ChatSerializer", "net.minecraft.network.chat.IChatBaseComponent$ChatSerializer"); CRAFT_CHAT_MESSAGE_CLASS = CRAFT_CLASS_RESOLVER.resolveWrapper("util.CraftChatMessage"); - CHAT_SERIALIZER_A_METHOD_STRING = new MethodResolver(I_CHAT_BASE_COMPONENT_CHAT_SERIALIZER_INNER_CLASS.getClazz()).resolveWrapper( - ResolverQuery.builder().with("a", String.class).build()); - CHAT_SERIALIZER_A_METHOD_COMPONENT = new MethodResolver(I_CHAT_BASE_COMPONENT_CHAT_SERIALIZER_INNER_CLASS.getClazz()).resolveWrapper( - ResolverQuery.builder().with("a", I_CHAT_BASE_COMPONENT_CLASS.getClazz()).build()); CRAFT_CHAT_MESSAGE_FROM_COMPONENT_METHOD = new MethodResolver(CRAFT_CHAT_MESSAGE_CLASS.getClazz()).resolveWrapper( ResolverQuery.builder().with("fromComponent", I_CHAT_BASE_COMPONENT_CLASS.getClazz()).build()); CRAFT_CHAT_MESSAGE_FROM_STRING_METHOD = new MethodResolver(CRAFT_CHAT_MESSAGE_CLASS.getClazz()).resolveWrapper( ResolverQuery.builder().with("fromString", String.class).build()); + NMS_GSON = new FieldResolver(I_CHAT_BASE_COMPONENT_CHAT_SERIALIZER_INNER_CLASS.getClazz()).resolveAccessor("a").get(null); } - public static String fromComponent(Component component) { - String jsonMessage = GSON.serialize(component); + public static Component asAdventure(final Object iChatBaseComponent) { + return iChatBaseComponent == null ? null : GSON.serializer().fromJson(NMS_GSON.toJsonTree(iChatBaseComponent), Component.class); + } + + public static ArrayList asAdventure(final List iChatBaseComponent) { + final ArrayList adventures = new ArrayList<>(iChatBaseComponent.size()); + for (final Object component : iChatBaseComponent) { + adventures.add(asAdventure(component)); + } + return adventures; + } + + public static ArrayList asAdventureFromJson(final List jsonStrings) { + final ArrayList adventures = new ArrayList<>(jsonStrings.size()); + for (final String json : jsonStrings) { + adventures.add(GSON.deserialize(json)); + } + return adventures; + } + + public static List asJson(final List adventures) { + final List jsons = new ArrayList<>(adventures.size()); + for (final Component component : adventures) { + jsons.add(GSON.serialize(component)); + } + return jsons; + } + + public static Object asVanilla(final Component component) { + return NMS_GSON.fromJson(GSON.serializer().toJsonTree(component), I_CHAT_BASE_COMPONENT_CLASS.getClazz()); + } + + public static List asVanilla(final List adventures) { + final List vanillas = new ArrayList<>(adventures.size()); + for (final Component adventure : adventures) { + vanillas.add(asVanilla(adventure)); + } + return vanillas; + } + + public static String toVanillaString(Component component) { try { - Object chatComponent = CHAT_SERIALIZER_A_METHOD_STRING.invoke(null, jsonMessage); + Object chatComponent = asVanilla(component); return (String) CRAFT_CHAT_MESSAGE_FROM_COMPONENT_METHOD.invoke(null, chatComponent); } catch (JsonParseException ignored) { return null; } } - public static Component toComponent(String text) { + public static List toVanillaString(List components) { + final List vanillas = new ArrayList<>(components.size()); + for (final Component component : components) { + vanillas.add(toVanillaString(component)); + } + return vanillas; + } + + public static Component fromVanillaString(String text) { if (text == null || text.isEmpty()) return null; try { Object chatComponent = Array.get(CRAFT_CHAT_MESSAGE_FROM_STRING_METHOD.invoke(null, text), 0); - return GSON.deserialize((String) CHAT_SERIALIZER_A_METHOD_COMPONENT.invoke(null, chatComponent)); + return asAdventure(chatComponent); } catch (JsonParseException ignored) { return null; } } + public static List fromVanillaString(List texts) { + final List components = new ArrayList<>(texts.size()); + for (final String text : texts) { + components.add(fromVanillaString(text)); + } + return components; + } + + public static String asJsonString(final Component component, final Locale locale) { + return GSON.serialize( + GlobalTranslator.render( + component, + locale != null + ? locale + : Locale.US + ) + ); + } + + public static String asJsonString(final Object iChatBaseComponent) { + return NMS_GSON.toJson(iChatBaseComponent); + } + public static Component fromLegacyText(String text) { if (text.contains("§")) { return fromLegacyText('§', text); diff --git a/inventory-gui/src/main/java/com/pepedevs/corelib/gui/inventory/ItemMenu.java b/inventory-gui/src/main/java/com/pepedevs/corelib/gui/inventory/ItemMenu.java index 9acc3d4..a127f2e 100644 --- a/inventory-gui/src/main/java/com/pepedevs/corelib/gui/inventory/ItemMenu.java +++ b/inventory-gui/src/main/java/com/pepedevs/corelib/gui/inventory/ItemMenu.java @@ -15,10 +15,8 @@ import org.bukkit.plugin.Plugin; import java.util.Arrays; -import java.util.List; import java.util.TreeSet; import java.util.function.Predicate; -import java.util.stream.Collectors; import java.util.stream.Stream; /** Class for creating custom Inventory menus. */ diff --git a/utils/src/main/java/com/pepedevs/corelib/utils/itemstack/ItemMetaBuilder.java b/utils/src/main/java/com/pepedevs/corelib/utils/itemstack/ItemMetaBuilder.java index c115138..b7d71f7 100644 --- a/utils/src/main/java/com/pepedevs/corelib/utils/itemstack/ItemMetaBuilder.java +++ b/utils/src/main/java/com/pepedevs/corelib/utils/itemstack/ItemMetaBuilder.java @@ -111,7 +111,7 @@ public ItemMetaBuilder withDisplayName(String display_name) { * @return This Object, for chaining */ public ItemMetaBuilder displayName(Component display_name) { - return this.withDisplayName(AdventureUtils.fromComponent(display_name)); + return this.withDisplayName(AdventureUtils.toVanillaString(display_name)); } /** @@ -138,7 +138,7 @@ public ItemMetaBuilder withLore(List lore) { public ItemMetaBuilder lore(List lore) { List stringLore = new ArrayList<>(); for (Component component : lore) { - stringLore.add(AdventureUtils.fromComponent(component)); + stringLore.add(AdventureUtils.toVanillaString(component)); } return this.withLore(stringLore); } @@ -166,7 +166,7 @@ public ItemMetaBuilder withLore(String... lore) { public ItemMetaBuilder lore(Component... lore) { String[] str = new String[lore.length]; for (int i = 0; i < lore.length; i++) { - str[i] = AdventureUtils.fromComponent(lore[i]); + str[i] = AdventureUtils.toVanillaString(lore[i]); } return this.withLore(str); } @@ -198,7 +198,7 @@ public ItemMetaBuilder appendToLore(String line) { * @return This Object, for chaining */ public ItemMetaBuilder appendLore(Component line) { - return this.appendToLore(AdventureUtils.fromComponent(line)); + return this.appendToLore(AdventureUtils.toVanillaString(line)); } /** @@ -227,7 +227,7 @@ public ItemMetaBuilder removeFromLore(String line) { * @return This Object, for chaining */ public ItemMetaBuilder removeFromLore(Component line) { - return this.removeFromLore(AdventureUtils.fromComponent(line)); + return this.removeFromLore(AdventureUtils.toVanillaString(line)); } /** diff --git a/utils/src/main/java/com/pepedevs/corelib/utils/itemstack/ItemStackUtils.java b/utils/src/main/java/com/pepedevs/corelib/utils/itemstack/ItemStackUtils.java index 9c447bb..58bc1cf 100644 --- a/utils/src/main/java/com/pepedevs/corelib/utils/itemstack/ItemStackUtils.java +++ b/utils/src/main/java/com/pepedevs/corelib/utils/itemstack/ItemStackUtils.java @@ -99,7 +99,7 @@ public static Component extractName(ItemStack stack) { String displayName = stack.getItemMeta().getDisplayName(); return displayName == null ? Component.empty() - : AdventureUtils.toComponent(displayName); + : AdventureUtils.fromVanillaString(displayName); } /** @@ -136,7 +136,7 @@ public static List extractLore(ItemStack stack) { List lore = new ArrayList<>(); if (stack != null && stack.getItemMeta() != null && stack.getItemMeta().getLore() != null) { for (String str : stack.getItemMeta().getLore()) { - lore.add(AdventureUtils.toComponent(str)); + lore.add(AdventureUtils.fromVanillaString(str)); } } return lore; @@ -221,7 +221,7 @@ public static ItemStack setName(ItemStack itemStack, Component name) { return itemStack; } - meta.setDisplayName(AdventureUtils.fromComponent(name)); + meta.setDisplayName(AdventureUtils.toVanillaString(name)); itemStack.setItemMeta(meta); return itemStack; } @@ -283,7 +283,7 @@ public static ItemStack setLoreComponent(ItemStack itemStack, List lo List loreString = new ArrayList<>(); for (Component component : lore) { - loreString.add(AdventureUtils.fromComponent(component)); + loreString.add(AdventureUtils.toVanillaString(component)); } meta.setLore(loreString);