Skip to content

Commit

Permalink
Adventure utility update
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhigyaKrishna committed Jan 8, 2022
1 parent 5c2bfa2 commit 0cd7477
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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<Component> asAdventure(final List<Object> iChatBaseComponent) {
final ArrayList<Component> adventures = new ArrayList<>(iChatBaseComponent.size());
for (final Object component : iChatBaseComponent) {
adventures.add(asAdventure(component));
}
return adventures;
}

public static ArrayList<Component> asAdventureFromJson(final List<String> jsonStrings) {
final ArrayList<Component> adventures = new ArrayList<>(jsonStrings.size());
for (final String json : jsonStrings) {
adventures.add(GSON.deserialize(json));
}
return adventures;
}

public static List<String> asJson(final List<Component> adventures) {
final List<String> 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<Object> asVanilla(final List<Component> adventures) {
final List<Object> 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<String> toVanillaString(List<Component> components) {
final List<String> 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<Component> fromVanillaString(List<String> texts) {
final List<Component> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -138,7 +138,7 @@ public ItemMetaBuilder withLore(List<String> lore) {
public ItemMetaBuilder lore(List<Component> lore) {
List<String> stringLore = new ArrayList<>();
for (Component component : lore) {
stringLore.add(AdventureUtils.fromComponent(component));
stringLore.add(AdventureUtils.toVanillaString(component));
}
return this.withLore(stringLore);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -136,7 +136,7 @@ public static List<Component> extractLore(ItemStack stack) {
List<Component> 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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ public static ItemStack setLoreComponent(ItemStack itemStack, List<Component> lo

List<String> loreString = new ArrayList<>();
for (Component component : lore) {
loreString.add(AdventureUtils.fromComponent(component));
loreString.add(AdventureUtils.toVanillaString(component));
}

meta.setLore(loreString);
Expand Down

0 comments on commit 0cd7477

Please sign in to comment.