From 16bbe392feaa16b25673ba979cd75aae73ce29cf Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 25 Oct 2020 21:29:44 +0000 Subject: [PATCH] Fixed Villagers --- .../willfp/ecoenchants/EcoEnchantsPlugin.java | 6 +++ .../packets/PacketOpenWindowMerchant.java | 46 ++++++++++++++----- .../display/packets/PacketWindowItems.java | 5 +- .../willfp/ecoenchants/nms/BlockBreak.java | 6 +-- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java b/Plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java index da488258..44797da5 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java @@ -2,6 +2,7 @@ package com.willfp.ecoenchants; import com.comphenix.protocol.ProtocolManager; import com.willfp.ecoenchants.loader.Loader; +import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; /** @@ -20,6 +21,11 @@ public class EcoEnchantsPlugin extends JavaPlugin { */ public static String newVersion; + /** + * NMS version + */ + public static final String nmsVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; + /** * ProtocolLib */ diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketOpenWindowMerchant.java b/Plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketOpenWindowMerchant.java index eb84bc4c..d2c9386d 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketOpenWindowMerchant.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketOpenWindowMerchant.java @@ -2,10 +2,15 @@ package com.willfp.ecoenchants.display.packets; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.events.PacketContainer; +import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.display.AbstractPacketAdapter; import com.willfp.ecoenchants.display.EnchantDisplay; +import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.MerchantRecipe; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; import java.util.List; import java.util.stream.Collectors; @@ -19,17 +24,36 @@ public final class PacketOpenWindowMerchant extends AbstractPacketAdapter { public void onSend(PacketContainer packet) { List recipes = packet.getMerchantRecipeLists().readSafely(0); - recipes = recipes.stream().map(merchantRecipe -> { - MerchantRecipe newRecipe = new MerchantRecipe( - EnchantDisplay.displayEnchantments(merchantRecipe.getResult()), - merchantRecipe.getUses(), - merchantRecipe.getMaxUses(), - merchantRecipe.hasExperienceReward(), - merchantRecipe.getVillagerExperience(), - merchantRecipe.getPriceMultiplier() - ); - newRecipe.setIngredients(merchantRecipe.getIngredients().stream().map(EnchantDisplay::displayEnchantments).collect(Collectors.toList())); - return newRecipe; + recipes = recipes.stream().peek(merchantRecipe -> { + try { + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + + Field fResult = merchantRecipe.getClass().getSuperclass().getDeclaredField("result"); + fResult.setAccessible(true); + fResult.set(merchantRecipe, EnchantDisplay.displayEnchantments(merchantRecipe.getResult())); + + Field fHandle = merchantRecipe.getClass().getDeclaredField("handle"); + fHandle.setAccessible(true); + Object handle = fHandle.get(merchantRecipe); + + modifiersField.setInt(fHandle, fHandle.getModifiers() & ~Modifier.FINAL); + + Field fSelling = fHandle.get(merchantRecipe).getClass().getDeclaredField("sellingItem"); + fSelling.setAccessible(true); + + Object selling = fSelling.get(handle); + + modifiersField.setInt(fSelling, fSelling.getModifiers() & ~Modifier.FINAL); + + ItemStack itemStack = (ItemStack) Class.forName("org.bukkit.craftbukkit." + EcoEnchantsPlugin.nmsVersion + ".inventory.CraftItemStack").getMethod("asBukkitCopy", selling.getClass()).invoke(null, selling); + + itemStack = EnchantDisplay.displayEnchantments(itemStack); + + fSelling.set(handle, Class.forName("org.bukkit.craftbukkit." + EcoEnchantsPlugin.nmsVersion + ".inventory.CraftItemStack").getMethod("asNMSCopy", ItemStack.class).invoke(null, itemStack)); + } catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) { + e.printStackTrace(); + } }).collect(Collectors.toList()); packet.getMerchantRecipeLists().writeSafely(0, recipes); diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketWindowItems.java b/Plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketWindowItems.java index f64b7bc1..eb70a98a 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketWindowItems.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketWindowItems.java @@ -17,9 +17,12 @@ public final class PacketWindowItems extends AbstractPacketAdapter { packet.getItemListModifier().modify(0, (itemStacks) -> { if(itemStacks == null) return null; itemStacks.forEach(item -> { + if(item == null) + return; + boolean hideEnchants = false; - if(item != null && item.getItemMeta() != null) { + if(item.getItemMeta() != null) { hideEnchants = item.getItemMeta().getItemFlags().contains(ItemFlag.HIDE_ENCHANTS); if(hideEnchants && item.getItemMeta().getPersistentDataContainer().has(EnchantDisplay.KEY, PersistentDataType.INTEGER)) hideEnchants = false; diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/nms/BlockBreak.java b/Plugin/src/main/java/com/willfp/ecoenchants/nms/BlockBreak.java index bd1fda29..6cdbaf91 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/nms/BlockBreak.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/nms/BlockBreak.java @@ -1,18 +1,16 @@ package com.willfp.ecoenchants.nms; +import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.nms.API.BlockBreakWrapper; -import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.entity.Player; public class BlockBreak { private static BlockBreakWrapper blockBreakWrapper; - private static final String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; - public static boolean init() { try { - final Class class2 = Class.forName("com.willfp.ecoenchants." + version + ".BlockBreak"); + final Class class2 = Class.forName("com.willfp.ecoenchants." + EcoEnchantsPlugin.nmsVersion + ".BlockBreak"); if (BlockBreakWrapper.class.isAssignableFrom(class2)) { blockBreakWrapper = (BlockBreakWrapper) class2.getConstructor().newInstance(); }