From cd9ff42711daecb059640673530cf6cc41916f54 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 2 Dec 2020 22:11:46 +0000 Subject: [PATCH] Added ChatComponent nms API --- .../ecoenchants/v1_16_R3/ChatComponent.java | 98 +++++++++++++++++++ .../ecoenchants/v1_16_R3/JsonStack.java | 32 ------ .../willfp/ecoenchants/nms/ChatComponent.java | 35 +++++++ .../com/willfp/ecoenchants/nms/JsonStack.java | 50 ---------- 4 files changed, 133 insertions(+), 82 deletions(-) create mode 100644 NMS/v1_16_R3/src/main/java/com/willfp/ecoenchants/v1_16_R3/ChatComponent.java delete mode 100644 NMS/v1_16_R3/src/main/java/com/willfp/ecoenchants/v1_16_R3/JsonStack.java create mode 100644 Plugin/src/main/java/com/willfp/ecoenchants/nms/ChatComponent.java delete mode 100644 Plugin/src/main/java/com/willfp/ecoenchants/nms/JsonStack.java diff --git a/NMS/v1_16_R3/src/main/java/com/willfp/ecoenchants/v1_16_R3/ChatComponent.java b/NMS/v1_16_R3/src/main/java/com/willfp/ecoenchants/v1_16_R3/ChatComponent.java new file mode 100644 index 00000000..f1be7b65 --- /dev/null +++ b/NMS/v1_16_R3/src/main/java/com/willfp/ecoenchants/v1_16_R3/ChatComponent.java @@ -0,0 +1,98 @@ +package com.willfp.ecoenchants.v1_16_R3; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.willfp.ecoenchants.nms.API.ChatComponentWrapper; +import net.minecraft.server.v1_16_R3.ChatBaseComponent; +import net.minecraft.server.v1_16_R3.ChatHoverable; +import net.minecraft.server.v1_16_R3.ChatMessage; +import net.minecraft.server.v1_16_R3.ChatModifier; +import net.minecraft.server.v1_16_R3.IChatBaseComponent; +import net.minecraft.server.v1_16_R3.MojangsonParser; +import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack; +import org.bukkit.inventory.ItemStack; + +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; + +public class ChatComponent implements ChatComponentWrapper { + @Override + public Object modifyComponent(Object object) { + if(!(object instanceof IChatBaseComponent)) { + return object; + } + + IChatBaseComponent chatComponent = (IChatBaseComponent) object; + chatComponent.stream().forEach(this::modifyBaseComponent); + + return chatComponent; + } + + public void modifyBaseComponent(IChatBaseComponent component) { + component.getSiblings().forEach(this::modifyBaseComponent); + if(component instanceof ChatMessage) { + Arrays.stream(((ChatMessage) component).getArgs()) + .filter(o -> o instanceof IChatBaseComponent) + .map(o -> (IChatBaseComponent) o) + .forEach(this::modifyBaseComponent); + } + + ChatHoverable hoverable = component.getChatModifier().getHoverEvent(); + + if(hoverable == null) + return; + + JsonObject jsonObject = hoverable.b(); + JsonElement json = hoverable.b().get("contents"); + if(json.getAsJsonObject().get("id") == null) return; + if(json.getAsJsonObject().get("tag") == null) return; + String id = json.getAsJsonObject().get("id").toString(); + String tag = json.getAsJsonObject().get("tag").toString(); + ItemStack itemStack = getFromTag(tag, id); + try { + itemStack = (ItemStack) Class.forName("com.willfp.ecoenchants.display.EnchantDisplay").getMethod("displayEnchantments", ItemStack.class).invoke(null, itemStack); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException e) { + e.printStackTrace(); + } + + json.getAsJsonObject().remove("tag"); + String newTag = toJson(itemStack); + json.getAsJsonObject().add("tag", new JsonPrimitive(newTag)); + + jsonObject.remove("contents"); + jsonObject.add("contents", json); + ChatHoverable newHoverable = ChatHoverable.a(jsonObject); + ChatModifier modifier = component.getChatModifier(); + modifier = modifier.setChatHoverable(newHoverable); + + ((ChatBaseComponent) component).setChatModifier(modifier); + } + + private static ItemStack getFromTag(String jsonTag, String id) { + id = id.replaceAll("minecraft:", ""); + id = id.toUpperCase(); + id = id.replaceAll("\"", ""); + jsonTag = jsonTag.substring( 1, jsonTag.length() - 1 ); + jsonTag = jsonTag.replaceAll("id:", "\"id\":"); + jsonTag = jsonTag.replace("\\", ""); + Material material = Material.getMaterial(id); + + assert material != null; + ItemStack itemStack = new ItemStack(material); + net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack); + + try { + nmsStack.setTag(MojangsonParser.parse(jsonTag)); + } catch (CommandSyntaxException e) { + e.printStackTrace(); + } + return CraftItemStack.asBukkitCopy(nmsStack); + } + + private static String toJson(ItemStack itemStack) { + return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString(); + } +} diff --git a/NMS/v1_16_R3/src/main/java/com/willfp/ecoenchants/v1_16_R3/JsonStack.java b/NMS/v1_16_R3/src/main/java/com/willfp/ecoenchants/v1_16_R3/JsonStack.java deleted file mode 100644 index d82c3d52..00000000 --- a/NMS/v1_16_R3/src/main/java/com/willfp/ecoenchants/v1_16_R3/JsonStack.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.willfp.ecoenchants.v1_16_R3; - -import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.willfp.ecoenchants.nms.API.JsonStackWrapper; -import net.minecraft.server.v1_16_R3.MojangsonParser; -import org.bukkit.Material; -import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack; -import org.bukkit.inventory.ItemStack; - -public class JsonStack implements JsonStackWrapper { - @Override - public ItemStack getFromTag(String jsonTag, String id) { - id = id.replaceAll("minecraft:", "").toUpperCase(); - Material material = Material.getMaterial(id); - - assert material != null; - ItemStack itemStack = new ItemStack(material); - net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack); - - try { - nmsStack.setTag(MojangsonParser.parse(jsonTag)); - } catch (CommandSyntaxException e) { - e.printStackTrace(); - } - return CraftItemStack.asBukkitCopy(nmsStack); - } - - @Override - public String toJson(ItemStack itemStack) { - return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString(); - } -} diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/nms/ChatComponent.java b/Plugin/src/main/java/com/willfp/ecoenchants/nms/ChatComponent.java new file mode 100644 index 00000000..d9a5e8b4 --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/nms/ChatComponent.java @@ -0,0 +1,35 @@ +package com.willfp.ecoenchants.nms; + + +import com.willfp.ecoenchants.EcoEnchantsPlugin; +import com.willfp.ecoenchants.nms.API.ChatComponentWrapper; +import org.jetbrains.annotations.ApiStatus; + +/** + * Utility class to manage chat components + */ +public class ChatComponent { + private static ChatComponentWrapper chatComponentWrapper; + + @ApiStatus.Internal + public static boolean init() { + try { + final Class class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".ChatComponent"); + if (ChatComponentWrapper.class.isAssignableFrom(class2)) { + chatComponentWrapper = (ChatComponentWrapper) class2.getConstructor().newInstance(); + } + } catch (Exception e) { + e.printStackTrace(); + chatComponentWrapper = null; + } + return chatComponentWrapper != null; + } + + /** + * Modify chat component + */ + public static Object modifyComponent(Object object) { + assert chatComponentWrapper != null; + return chatComponentWrapper.modifyComponent(object); + } +} diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/nms/JsonStack.java b/Plugin/src/main/java/com/willfp/ecoenchants/nms/JsonStack.java deleted file mode 100644 index f9fa4313..00000000 --- a/Plugin/src/main/java/com/willfp/ecoenchants/nms/JsonStack.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.willfp.ecoenchants.nms; - - -import com.willfp.ecoenchants.EcoEnchantsPlugin; -import com.willfp.ecoenchants.nms.API.JsonStackWrapper; -import org.bukkit.inventory.ItemStack; -import org.jetbrains.annotations.ApiStatus; - -/** - * Utility class to read/write NBTTagCompounds through json - */ -public class JsonStack { - private static JsonStackWrapper jsonStackWrapper; - - @ApiStatus.Internal - public static boolean init() { - try { - final Class class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.NMS_VERSION + ".JsonStack"); - if (JsonStackWrapper.class.isAssignableFrom(class2)) { - jsonStackWrapper = (JsonStackWrapper) class2.getConstructor().newInstance(); - } - } catch (Exception e) { - e.printStackTrace(); - jsonStackWrapper = null; - } - return jsonStackWrapper != null; - } - - /** - * Get the NBTTagCompound of an item as json - * - * @param itemStack The item to query - * @return The NBTTagCompound of the item as json - */ - public static String toJson(ItemStack itemStack) { - assert jsonStackWrapper != null; - return jsonStackWrapper.toJson(itemStack); - } - - /** - * Create an ItemStack from NBTTagCompound json - * @param jsonTag The tag of the item - * @param id The fully qualified material ID - * @return The reconstructed ItemStack - */ - public static ItemStack fromJson(String jsonTag, String id) { - assert jsonStackWrapper != null; - return jsonStackWrapper.getFromTag(jsonTag, id); - } -}