diff --git a/build.gradle b/build.gradle index dd1a20e0..428d4aa5 100644 --- a/build.gradle +++ b/build.gradle @@ -49,7 +49,7 @@ allprojects { } dependencies { - compileOnly 'com.willfp:eco:3.0.0' + compileOnly 'com.willfp:eco:3.1.0' compileOnly 'org.jetbrains:annotations:19.0.0' diff --git a/eco-core/core-nms/v1_15_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_15_R1/ChatComponent.java b/eco-core/core-nms/v1_15_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_15_R1/ChatComponent.java deleted file mode 100644 index 5a0c32be..00000000 --- a/eco-core/core-nms/v1_15_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_15_R1/ChatComponent.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.willfp.ecoenchants.proxy.v1_15_R1; - -import com.willfp.ecoenchants.proxy.proxies.ChatComponentProxy; -import org.jetbrains.annotations.NotNull; - -public final class ChatComponent implements ChatComponentProxy { - @Override - public Object modifyComponent(@NotNull final Object object) { - return object; - } -} diff --git a/eco-core/core-nms/v1_15_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_15_R1/VillagerTrade.java b/eco-core/core-nms/v1_15_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_15_R1/VillagerTrade.java deleted file mode 100644 index 20c2666c..00000000 --- a/eco-core/core-nms/v1_15_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_15_R1/VillagerTrade.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.willfp.ecoenchants.proxy.v1_15_R1; - -import com.willfp.ecoenchants.proxy.proxies.VillagerTradeProxy; -import com.willfp.ecoenchants.display.EnchantDisplay; -import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack; -import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftMerchantRecipe; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.MerchantRecipe; -import org.jetbrains.annotations.NotNull; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; - -public final class VillagerTrade implements VillagerTradeProxy { - @Override - public void displayTradeEnchantments(@NotNull final MerchantRecipe merchantRecipe) { - try { - // Enables removing final modifier - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - - // Bukkit MerchantRecipe result - Field fResult = MerchantRecipe.class.getDeclaredField("result"); - fResult.setAccessible(true); - ItemStack result = EnchantDisplay.displayEnchantments(merchantRecipe.getResult()); - EnchantDisplay.addV(result); - fResult.set(merchantRecipe, result); - - // Get NMS MerchantRecipe from CraftMerchantRecipe - Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle"); - fHandle.setAccessible(true); - net.minecraft.server.v1_15_R1.MerchantRecipe handle = (net.minecraft.server.v1_15_R1.MerchantRecipe) fHandle.get(merchantRecipe); // NMS Recipe - modifiersField.setInt(fHandle, fHandle.getModifiers() & ~Modifier.FINAL); // Remove final - - Field fSelling = net.minecraft.server.v1_15_R1.MerchantRecipe.class.getDeclaredField("sellingItem"); - fSelling.setAccessible(true); - modifiersField.setInt(fSelling, fSelling.getModifiers() & ~Modifier.FINAL); - - ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem); - EnchantDisplay.displayEnchantments(selling); - EnchantDisplay.addV(selling); - - fSelling.set(handle, CraftItemStack.asNMSCopy(selling)); - } catch (IllegalAccessException | NoSuchFieldException e) { - e.printStackTrace(); - } - } -} diff --git a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/ChatComponent.java b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/ChatComponent.java deleted file mode 100644 index 80247374..00000000 --- a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/ChatComponent.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.willfp.ecoenchants.proxy.v1_16_R1; - -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.display.EnchantDisplay; -import com.willfp.ecoenchants.proxy.proxies.ChatComponentProxy; -import net.minecraft.server.v1_16_R1.ChatBaseComponent; -import net.minecraft.server.v1_16_R1.ChatHoverable; -import net.minecraft.server.v1_16_R1.ChatMessage; -import net.minecraft.server.v1_16_R1.ChatModifier; -import net.minecraft.server.v1_16_R1.IChatBaseComponent; -import net.minecraft.server.v1_16_R1.MojangsonParser; -import org.bukkit.Material; -import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack; -import org.bukkit.inventory.ItemFlag; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; - -public final class ChatComponent implements ChatComponentProxy { - @Override - public Object modifyComponent(@NotNull final Object object) { - if (!(object instanceof IChatBaseComponent)) { - return object; - } - - IChatBaseComponent chatComponent = (IChatBaseComponent) object; - chatComponent.stream().forEach(this::modifyBaseComponent); - - return chatComponent; - } - - private void modifyBaseComponent(@NotNull final 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); - - ItemMeta meta = itemStack.getItemMeta(); - assert meta != null; - boolean hideEnchants = false; - if (meta.hasItemFlag(ItemFlag.HIDE_ENCHANTS) || meta.hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)) { - hideEnchants = true; - } - - itemStack = EnchantDisplay.displayEnchantments(itemStack, hideEnchants); - EnchantDisplay.addV(itemStack); - - 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(@NotNull final String jsonTag, - @NotNull final String id) { - String processedId = id; - String processedJsonTag = jsonTag; - processedId = processedId.replace("minecraft:", ""); - processedId = processedId.toUpperCase(); - processedId = processedId.replace("\"", ""); - processedJsonTag = processedJsonTag.substring(1, processedJsonTag.length() - 1); - processedJsonTag = processedJsonTag.replace("id:", "\"id\":"); - processedJsonTag = processedJsonTag.replace("\\", ""); - Material material = Material.getMaterial(processedId); - - assert material != null; - ItemStack itemStack = new ItemStack(material); - net.minecraft.server.v1_16_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack); - - try { - nmsStack.setTag(MojangsonParser.parse(processedJsonTag)); - } catch (CommandSyntaxException e) { - e.printStackTrace(); - } - return CraftItemStack.asBukkitCopy(nmsStack); - } - - private static String toJson(@NotNull final ItemStack itemStack) { - return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString(); - } -} diff --git a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/VillagerTrade.java b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/VillagerTrade.java deleted file mode 100644 index faa70436..00000000 --- a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R1/VillagerTrade.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.willfp.ecoenchants.proxy.v1_16_R1; - -import com.willfp.ecoenchants.proxy.proxies.VillagerTradeProxy; -import com.willfp.ecoenchants.display.EnchantDisplay; -import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack; -import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftMerchantRecipe; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.MerchantRecipe; -import org.jetbrains.annotations.NotNull; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; - -public final class VillagerTrade implements VillagerTradeProxy { - @Override - public void displayTradeEnchantments(@NotNull final MerchantRecipe merchantRecipe) { - try { - // Enables removing final modifier - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - - // Bukkit MerchantRecipe result - Field fResult = MerchantRecipe.class.getDeclaredField("result"); - fResult.setAccessible(true); - ItemStack result = EnchantDisplay.displayEnchantments(merchantRecipe.getResult()); - EnchantDisplay.addV(result); - fResult.set(merchantRecipe, result); - - // Get NMS MerchantRecipe from CraftMerchantRecipe - Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle"); - fHandle.setAccessible(true); - net.minecraft.server.v1_16_R1.MerchantRecipe handle = (net.minecraft.server.v1_16_R1.MerchantRecipe) fHandle.get(merchantRecipe); // NMS Recipe - modifiersField.setInt(fHandle, fHandle.getModifiers() & ~Modifier.FINAL); // Remove final - - Field fSelling = net.minecraft.server.v1_16_R1.MerchantRecipe.class.getDeclaredField("sellingItem"); - fSelling.setAccessible(true); - modifiersField.setInt(fSelling, fSelling.getModifiers() & ~Modifier.FINAL); - - ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem); - EnchantDisplay.displayEnchantments(selling); - EnchantDisplay.addV(selling); - - fSelling.set(handle, CraftItemStack.asNMSCopy(selling)); - } catch (IllegalAccessException | NoSuchFieldException e) { - e.printStackTrace(); - } - } -} diff --git a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/ChatComponent.java b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/ChatComponent.java deleted file mode 100644 index 9068209b..00000000 --- a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/ChatComponent.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.willfp.ecoenchants.proxy.v1_16_R2; - -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.display.EnchantDisplay; -import com.willfp.ecoenchants.proxy.proxies.ChatComponentProxy; -import net.minecraft.server.v1_16_R2.ChatBaseComponent; -import net.minecraft.server.v1_16_R2.ChatHoverable; -import net.minecraft.server.v1_16_R2.ChatMessage; -import net.minecraft.server.v1_16_R2.ChatModifier; -import net.minecraft.server.v1_16_R2.IChatBaseComponent; -import net.minecraft.server.v1_16_R2.MojangsonParser; -import org.bukkit.Material; -import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack; -import org.bukkit.inventory.ItemFlag; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; - -public final class ChatComponent implements ChatComponentProxy { - @Override - public Object modifyComponent(@NotNull final Object object) { - if (!(object instanceof IChatBaseComponent)) { - return object; - } - - IChatBaseComponent chatComponent = (IChatBaseComponent) object; - chatComponent.stream().forEach(this::modifyBaseComponent); - - return chatComponent; - } - - private void modifyBaseComponent(@NotNull final 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); - - ItemMeta meta = itemStack.getItemMeta(); - assert meta != null; - boolean hideEnchants = false; - if (meta.hasItemFlag(ItemFlag.HIDE_ENCHANTS) || meta.hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)) { - hideEnchants = true; - } - - itemStack = EnchantDisplay.displayEnchantments(itemStack, hideEnchants); - EnchantDisplay.addV(itemStack); - - 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(@NotNull final String jsonTag, - @NotNull final String id) { - String processedId = id; - String processedJsonTag = jsonTag; - processedId = processedId.replace("minecraft:", ""); - processedId = processedId.toUpperCase(); - processedId = processedId.replace("\"", ""); - processedJsonTag = processedJsonTag.substring(1, processedJsonTag.length() - 1); - processedJsonTag = processedJsonTag.replace("id:", "\"id\":"); - processedJsonTag = processedJsonTag.replace("\\", ""); - Material material = Material.getMaterial(processedId); - - assert material != null; - ItemStack itemStack = new ItemStack(material); - net.minecraft.server.v1_16_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack); - - try { - nmsStack.setTag(MojangsonParser.parse(processedJsonTag)); - } catch (CommandSyntaxException e) { - e.printStackTrace(); - } - return CraftItemStack.asBukkitCopy(nmsStack); - } - - private static String toJson(@NotNull final ItemStack itemStack) { - return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString(); - } -} diff --git a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/VillagerTrade.java b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/VillagerTrade.java deleted file mode 100644 index 57b05390..00000000 --- a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R2/VillagerTrade.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.willfp.ecoenchants.proxy.v1_16_R2; - -import com.willfp.ecoenchants.proxy.proxies.VillagerTradeProxy; -import com.willfp.ecoenchants.display.EnchantDisplay; -import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack; -import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftMerchantRecipe; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.MerchantRecipe; -import org.jetbrains.annotations.NotNull; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; - -public final class VillagerTrade implements VillagerTradeProxy { - @Override - public void displayTradeEnchantments(@NotNull final MerchantRecipe merchantRecipe) { - try { - // Enables removing final modifier - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - - // Bukkit MerchantRecipe result - Field fResult = MerchantRecipe.class.getDeclaredField("result"); - fResult.setAccessible(true); - ItemStack result = EnchantDisplay.displayEnchantments(merchantRecipe.getResult()); - EnchantDisplay.addV(result); - fResult.set(merchantRecipe, result); - - // Get NMS MerchantRecipe from CraftMerchantRecipe - Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle"); - fHandle.setAccessible(true); - net.minecraft.server.v1_16_R2.MerchantRecipe handle = (net.minecraft.server.v1_16_R2.MerchantRecipe) fHandle.get(merchantRecipe); // NMS Recipe - modifiersField.setInt(fHandle, fHandle.getModifiers() & ~Modifier.FINAL); // Remove final - - Field fSelling = net.minecraft.server.v1_16_R2.MerchantRecipe.class.getDeclaredField("sellingItem"); - fSelling.setAccessible(true); - modifiersField.setInt(fSelling, fSelling.getModifiers() & ~Modifier.FINAL); - - ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem); - EnchantDisplay.displayEnchantments(selling); - EnchantDisplay.addV(selling); - - fSelling.set(handle, CraftItemStack.asNMSCopy(selling)); - } catch (IllegalAccessException | NoSuchFieldException e) { - e.printStackTrace(); - } - } -} diff --git a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/ChatComponent.java b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/ChatComponent.java deleted file mode 100644 index bd4e4015..00000000 --- a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/ChatComponent.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.willfp.ecoenchants.proxy.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.proxy.proxies.ChatComponentProxy; -import com.willfp.ecoenchants.display.EnchantDisplay; -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.ItemFlag; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; - -public final class ChatComponent implements ChatComponentProxy { - @Override - public Object modifyComponent(@NotNull final Object object) { - if (!(object instanceof IChatBaseComponent)) { - return object; - } - - IChatBaseComponent chatComponent = (IChatBaseComponent) object; - chatComponent.stream().forEach(this::modifyBaseComponent); - - return chatComponent; - } - - private void modifyBaseComponent(@NotNull final 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); - - ItemMeta meta = itemStack.getItemMeta(); - assert meta != null; - boolean hideEnchants = false; - if (meta.hasItemFlag(ItemFlag.HIDE_ENCHANTS) || meta.hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)) { - hideEnchants = true; - } - - itemStack = EnchantDisplay.displayEnchantments(itemStack, hideEnchants); - EnchantDisplay.addV(itemStack); - - 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(@NotNull final String jsonTag, - @NotNull final String id) { - String processedId = id; - String processedJsonTag = jsonTag; - processedId = processedId.replace("minecraft:", ""); - processedId = processedId.toUpperCase(); - processedId = processedId.replace("\"", ""); - processedJsonTag = processedJsonTag.substring(1, processedJsonTag.length() - 1); - processedJsonTag = processedJsonTag.replace("id:", "\"id\":"); - processedJsonTag = processedJsonTag.replace("\\", ""); - Material material = Material.getMaterial(processedId); - - assert material != null; - ItemStack itemStack = new ItemStack(material); - net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack); - - try { - nmsStack.setTag(MojangsonParser.parse(processedJsonTag)); - } catch (CommandSyntaxException e) { - e.printStackTrace(); - } - return CraftItemStack.asBukkitCopy(nmsStack); - } - - private static String toJson(@NotNull final ItemStack itemStack) { - return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString(); - } -} diff --git a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/VillagerTrade.java b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/VillagerTrade.java deleted file mode 100644 index 7c7d7c5d..00000000 --- a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/ecoenchants/proxy/v1_16_R3/VillagerTrade.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.willfp.ecoenchants.proxy.v1_16_R3; - -import com.willfp.ecoenchants.proxy.proxies.VillagerTradeProxy; -import com.willfp.ecoenchants.display.EnchantDisplay; -import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack; -import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftMerchantRecipe; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.MerchantRecipe; -import org.jetbrains.annotations.NotNull; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; - -public final class VillagerTrade implements VillagerTradeProxy { - @Override - public void displayTradeEnchantments(@NotNull final MerchantRecipe merchantRecipe) { - try { - // Enables removing final modifier - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - - // Bukkit MerchantRecipe result - Field fResult = MerchantRecipe.class.getDeclaredField("result"); - fResult.setAccessible(true); - ItemStack result = EnchantDisplay.displayEnchantments(merchantRecipe.getResult()); - EnchantDisplay.addV(result); - fResult.set(merchantRecipe, result); - - // Get NMS MerchantRecipe from CraftMerchantRecipe - Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle"); - fHandle.setAccessible(true); - net.minecraft.server.v1_16_R3.MerchantRecipe handle = (net.minecraft.server.v1_16_R3.MerchantRecipe) fHandle.get(merchantRecipe); // NMS Recipe - modifiersField.setInt(fHandle, fHandle.getModifiers() & ~Modifier.FINAL); // Remove final - - Field fSelling = net.minecraft.server.v1_16_R3.MerchantRecipe.class.getDeclaredField("sellingItem"); - fSelling.setAccessible(true); - modifiersField.setInt(fSelling, fSelling.getModifiers() & ~Modifier.FINAL); - - ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem); - EnchantDisplay.displayEnchantments(selling); - EnchantDisplay.addV(selling); - - fSelling.set(handle, CraftItemStack.asNMSCopy(selling)); - } catch (IllegalAccessException | NoSuchFieldException e) { - e.printStackTrace(); - } - } -} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java index 8da1b0fd..4edf41ee 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java @@ -1,6 +1,8 @@ package com.willfp.ecoenchants; import com.willfp.eco.util.command.AbstractCommand; +import com.willfp.eco.util.display.Display; +import com.willfp.eco.util.display.DisplayModule; import com.willfp.eco.util.drops.telekinesis.TelekinesisUtils; import com.willfp.eco.util.integrations.IntegrationLoader; import com.willfp.eco.util.interfaces.EcoRunnable; @@ -13,11 +15,6 @@ import com.willfp.ecoenchants.command.tabcompleters.TabCompleterEnchantinfo; import com.willfp.ecoenchants.config.EcoEnchantsConfigs; import com.willfp.ecoenchants.display.EnchantDisplay; import com.willfp.ecoenchants.display.EnchantmentCache; -import com.willfp.ecoenchants.display.packets.PacketChat; -import com.willfp.ecoenchants.display.packets.PacketOpenWindowMerchant; -import com.willfp.ecoenchants.display.packets.PacketSetCreativeSlot; -import com.willfp.ecoenchants.display.packets.PacketSetSlot; -import com.willfp.ecoenchants.display.packets.PacketWindowItems; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity; import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget; @@ -42,6 +39,8 @@ import org.bukkit.Bukkit; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.generator.BlockPopulator; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; import java.util.Arrays; @@ -68,6 +67,21 @@ public class EcoEnchantsPlugin extends AbstractEcoPlugin { */ @Override public void enable() { + Display.registerDisplayModule(new DisplayModule(itemStack -> { + ItemMeta meta = itemStack.getItemMeta(); + if (meta == null) { + return itemStack; + } + boolean hideEnchants = false; + if (meta.hasItemFlag(ItemFlag.HIDE_ENCHANTS) || meta.hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)) { + hideEnchants = true; + } + + return EnchantDisplay.displayEnchantments(itemStack, hideEnchants); + }, 500, this.getPluginName())); + Display.registerRevertModule(EnchantDisplay::revertDisplay); + Display.registerFinalizeModule(EnchantDisplay::addV); + this.getExtensionLoader().loadExtensions(); if (this.getExtensionLoader().getLoadedExtensions().isEmpty()) { @@ -96,7 +110,6 @@ public class EcoEnchantsPlugin extends AbstractEcoPlugin { })); }); - this.getExtensionLoader().unloadExtensions(); } @@ -176,13 +189,7 @@ public class EcoEnchantsPlugin extends AbstractEcoPlugin { */ @Override public List getPacketAdapters() { - return Arrays.asList( - new PacketChat(this), - new PacketOpenWindowMerchant(this), - new PacketSetCreativeSlot(this), - new PacketSetSlot(this), - new PacketWindowItems(this) - ); + return new ArrayList<>(); } /** diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketChat.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketChat.java deleted file mode 100644 index 89213ad1..00000000 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketChat.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.willfp.ecoenchants.display.packets; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.events.ListenerPriority; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.wrappers.WrappedChatComponent; -import com.willfp.eco.util.plugin.AbstractEcoPlugin; -import com.willfp.eco.util.protocollib.AbstractPacketAdapter; -import com.willfp.ecoenchants.proxy.proxies.ChatComponentProxy; -import com.willfp.ecoenchants.util.ProxyUtils; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -public class PacketChat extends AbstractPacketAdapter { - /** - * Instantiate a new listener for {@link PacketType.Play.Server#CHAT}. - * - * @param plugin The plugin to listen through. - */ - public PacketChat(@NotNull final AbstractEcoPlugin plugin) { - super(plugin, PacketType.Play.Server.CHAT, ListenerPriority.MONITOR, true); - } - - @Override - public void onSend(@NotNull final PacketContainer packet, - @NotNull final Player player) { - for (int i = 0; i < packet.getChatComponents().size(); i++) { - WrappedChatComponent component = packet.getChatComponents().read(i); - if (component == null) { - continue; - } - - if (component.getHandle() == null) { - return; - } - - WrappedChatComponent newComponent = WrappedChatComponent.fromHandle(ProxyUtils.getProxy(ChatComponentProxy.class).modifyComponent(component.getHandle())); - packet.getChatComponents().write(i, newComponent); - } - } -} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketOpenWindowMerchant.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketOpenWindowMerchant.java deleted file mode 100644 index 2fecf3cb..00000000 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketOpenWindowMerchant.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.willfp.ecoenchants.display.packets; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.events.PacketContainer; -import com.willfp.eco.util.plugin.AbstractEcoPlugin; -import com.willfp.eco.util.protocollib.AbstractPacketAdapter; -import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget; -import com.willfp.ecoenchants.proxy.proxies.VillagerTradeProxy; -import com.willfp.ecoenchants.util.ProxyUtils; -import org.bukkit.entity.Player; -import org.bukkit.inventory.MerchantRecipe; -import org.jetbrains.annotations.NotNull; - -import java.util.List; -import java.util.stream.Collectors; - -public class PacketOpenWindowMerchant extends AbstractPacketAdapter { - /** - * Instantiate a new listener for {@link PacketType.Play.Server#OPEN_WINDOW_MERCHANT}. - * - * @param plugin The plugin to listen through. - */ - public PacketOpenWindowMerchant(@NotNull final AbstractEcoPlugin plugin) { - super(plugin, PacketType.Play.Server.OPEN_WINDOW_MERCHANT, false); - } - - @Override - public void onSend(@NotNull final PacketContainer packet, - @NotNull final Player player) { - List recipes = packet.getMerchantRecipeLists().readSafely(0); - - recipes = recipes.stream().peek(merchantRecipe -> { - if (!EnchantmentTarget.ALL.getMaterials().contains(merchantRecipe.getResult().getType())) { - return; - } - - ProxyUtils.getProxy(VillagerTradeProxy.class).displayTradeEnchantments(merchantRecipe); - }).collect(Collectors.toList()); - - packet.getMerchantRecipeLists().writeSafely(0, recipes); - } -} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetCreativeSlot.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetCreativeSlot.java deleted file mode 100644 index d0ec0c6a..00000000 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetCreativeSlot.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.willfp.ecoenchants.display.packets; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.events.PacketContainer; -import com.willfp.eco.util.plugin.AbstractEcoPlugin; -import com.willfp.eco.util.protocollib.AbstractPacketAdapter; -import com.willfp.ecoenchants.display.EnchantDisplay; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -public class PacketSetCreativeSlot extends AbstractPacketAdapter { - /** - * Instantiate a new listener for {@link PacketType.Play.Client#SET_CREATIVE_SLOT}. - * - * @param plugin The plugin to listen through. - */ - public PacketSetCreativeSlot(@NotNull final AbstractEcoPlugin plugin) { - super(plugin, PacketType.Play.Client.SET_CREATIVE_SLOT, false); - } - - @Override - public void onReceive(@NotNull final PacketContainer packet, - @NotNull final Player player) { - packet.getItemModifier().modify(0, EnchantDisplay::revertDisplay); - } -} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetSlot.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetSlot.java deleted file mode 100644 index 3af365ef..00000000 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetSlot.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.willfp.ecoenchants.display.packets; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.events.PacketContainer; -import com.willfp.eco.util.plugin.AbstractEcoPlugin; -import com.willfp.eco.util.protocollib.AbstractPacketAdapter; -import com.willfp.ecoenchants.display.EnchantDisplay; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemFlag; -import org.jetbrains.annotations.NotNull; - -public class PacketSetSlot extends AbstractPacketAdapter { - /** - * Instantiate a new listener for {@link PacketType.Play.Server#SET_SLOT}. - * - * @param plugin The plugin to listen through. - */ - public PacketSetSlot(@NotNull final AbstractEcoPlugin plugin) { - super(plugin, PacketType.Play.Server.SET_SLOT, false); - } - - @Override - public void onSend(@NotNull final PacketContainer packet, - @NotNull final Player player) { - packet.getItemModifier().modify(0, item -> { - boolean hideEnchants = false; - - if (item == null) { - return null; - } - - if (item.getItemMeta() != null) { - hideEnchants = item.getItemMeta().getItemFlags().contains(ItemFlag.HIDE_ENCHANTS); - } - - item = EnchantDisplay.displayEnchantments(item, hideEnchants); - return item; - }); - } -} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketWindowItems.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketWindowItems.java deleted file mode 100644 index 5d8a693d..00000000 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketWindowItems.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.willfp.ecoenchants.display.packets; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.events.PacketContainer; -import com.willfp.eco.util.plugin.AbstractEcoPlugin; -import com.willfp.eco.util.protocollib.AbstractPacketAdapter; -import com.willfp.ecoenchants.display.EnchantDisplay; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemFlag; -import org.jetbrains.annotations.NotNull; - -public class PacketWindowItems extends AbstractPacketAdapter { - /** - * Instantiate a new listener for {@link PacketType.Play.Server#WINDOW_ITEMS}. - * - * @param plugin The plugin to listen through. - */ - public PacketWindowItems(@NotNull final AbstractEcoPlugin plugin) { - super(plugin, PacketType.Play.Server.WINDOW_ITEMS, false); - } - - @Override - public void onSend(@NotNull final PacketContainer packet, - @NotNull final Player player) { - packet.getItemListModifier().modify(0, itemStacks -> { - if (itemStacks == null) { - return null; - } - itemStacks.forEach(item -> { - if (item == null) { - return; - } - - boolean hideEnchants = false; - - if (item.getItemMeta() != null) { - hideEnchants = item.getItemMeta().getItemFlags().contains(ItemFlag.HIDE_ENCHANTS); - } - - EnchantDisplay.displayEnchantments(item, hideEnchants); - }); - return itemStacks; - }); - } -} diff --git a/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/ChatComponentProxy.java b/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/ChatComponentProxy.java deleted file mode 100644 index 52e4fef4..00000000 --- a/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/ChatComponentProxy.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.willfp.ecoenchants.proxy.proxies; - - -import com.willfp.eco.util.proxy.AbstractProxy; -import org.jetbrains.annotations.NotNull; - -public interface ChatComponentProxy extends AbstractProxy { - /** - * Modify hover {@link org.bukkit.inventory.ItemStack}s using EnchantDisplay#displayEnchantments. - * @param object The NMS ChatComponent to modify. - * @return The modified ChatComponent. - */ - Object modifyComponent(@NotNull Object object); -} diff --git a/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/VillagerTradeProxy.java b/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/VillagerTradeProxy.java deleted file mode 100644 index a4743b13..00000000 --- a/eco-core/core-proxy/src/main/java/com/willfp/ecoenchants/proxy/proxies/VillagerTradeProxy.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.willfp.ecoenchants.proxy.proxies; - -import com.willfp.eco.util.proxy.AbstractProxy; -import org.bukkit.inventory.MerchantRecipe; - -public interface VillagerTradeProxy extends AbstractProxy { - /** - * Apply enchant display to the result of trades. - * - * @param merchantRecipe The recipe to modify. - */ - void displayTradeEnchantments(MerchantRecipe merchantRecipe); -}