Fixed Villagers

This commit is contained in:
Auxilor 2020-10-25 21:29:44 +00:00
parent fcb9899bb6
commit 16bbe392fe
4 changed files with 47 additions and 16 deletions

View File

@ -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
*/

View File

@ -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<MerchantRecipe> 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);

View File

@ -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;

View File

@ -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();
}