Fixed hidden enchants from villagers

This commit is contained in:
Auxilor 2020-10-26 10:49:04 +00:00
parent 6e9edff571
commit c4c26d8175
4 changed files with 42 additions and 12 deletions

View File

@ -45,6 +45,8 @@ public final class EnchantDisplay {
@Deprecated
public static final NamespacedKey KEY_SKIP = new NamespacedKey(EcoEnchantsPlugin.getInstance(), "ecoenchantlore-skip");
public static final NamespacedKey KEY_V = new NamespacedKey(EcoEnchantsPlugin.getInstance(), "ecoenchantlore-v");
/**
* Cached enchantment descriptions and names
*/
@ -143,6 +145,15 @@ public final class EnchantDisplay {
shrinkPerLine = ConfigManager.getConfig().getInt("lore.shrink.maximum-per-line");
}
public static ItemStack addV(ItemStack item) {
if(item == null || item.getItemMeta() == null) return item;
ItemMeta meta = item.getItemMeta();
meta.getPersistentDataContainer().set(KEY_V, PersistentDataType.INTEGER, 1);
item.setItemMeta(meta);
return item;
}
/**
* Revert display
* @param item The item to revert
@ -197,6 +208,11 @@ public final class EnchantDisplay {
if(item == null || item.getItemMeta() == null || !EnchantmentTarget.ALL.getMaterials().contains(item.getType()))
return item;
if(hideEnchants && item.getItemMeta().getPersistentDataContainer().has(KEY_V, PersistentDataType.INTEGER)) {
hideEnchants = false;
item.getItemMeta().getPersistentDataContainer().remove(KEY_V);
}
item = revertDisplay(item);
ItemMeta meta = item.getItemMeta();
@ -231,6 +247,8 @@ public final class EnchantDisplay {
enchantments.forEach((enchantment, level) -> {
boolean isEcoEnchant = EcoEnchants.getFromEnchantment(enchantment) != null;
if(CACHE.get(enchantment) == null) return;
String name = CACHE.get(enchantment).getKey();
if(isEcoEnchant) {

View File

@ -26,31 +26,38 @@ public final class PacketOpenWindowMerchant extends AbstractPacketAdapter {
recipes = recipes.stream().peek(merchantRecipe -> {
try {
// Enables removing final modifier
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
// Bukkit MerchantRecipe result
Field fResult = merchantRecipe.getClass().getSuperclass().getDeclaredField("result");
fResult.setAccessible(true);
fResult.set(merchantRecipe, EnchantDisplay.displayEnchantments(merchantRecipe.getResult()));
ItemStack result = EnchantDisplay.displayEnchantments(merchantRecipe.getResult());
result = EnchantDisplay.addV(result);
fResult.set(merchantRecipe, result);
// Get NMS MerchantRecipe from CraftMerchantRecipe
Field fHandle = merchantRecipe.getClass().getDeclaredField("handle");
fHandle.setAccessible(true);
Object handle = fHandle.get(merchantRecipe);
modifiersField.setInt(fHandle, fHandle.getModifiers() & ~Modifier.FINAL);
Object handle = fHandle.get(merchantRecipe); // NMS Recipe
modifiersField.setInt(fHandle, fHandle.getModifiers() & ~Modifier.FINAL); // Remove final
// NMS MerchantRecipe
Field fSelling = fHandle.get(merchantRecipe).getClass().getDeclaredField("sellingItem");
fSelling.setAccessible(true);
Object selling = fSelling.get(handle);
Object selling = fSelling.get(handle); // NMS Selling ItemStack
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);
// Reflectively access CraftItemStack.class for respective version
Class<?> craftItemStack = Class.forName("org.bukkit.craftbukkit." + EcoEnchantsPlugin.nmsVersion + ".inventory.CraftItemStack");
itemStack = EnchantDisplay.displayEnchantments(itemStack);
// Bukkit Result ItemStack from NMS Result ItemStack
ItemStack nmsSelling = (ItemStack) craftItemStack.getMethod("asBukkitCopy", selling.getClass()).invoke(null, selling);
nmsSelling = EnchantDisplay.displayEnchantments(nmsSelling);
nmsSelling = EnchantDisplay.addV(nmsSelling);
fSelling.set(handle, craftItemStack.getMethod("asNMSCopy", ItemStack.class).invoke(null, nmsSelling));
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();
}

View File

@ -17,7 +17,10 @@ public final class PacketSetSlot extends AbstractPacketAdapter {
packet.getItemModifier().modify(0, (item) -> {
boolean hideEnchants = false;
if(item != null && item.getItemMeta() != null) {
if(item == null)
return item;
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

@ -24,6 +24,8 @@ public class AnticheatManager {
}
public static void unexemptPlayer(Player player) {
anticheats.forEach(anticheat -> anticheat.unexempt(player));
Bukkit.getScheduler().runTaskLater(EcoEnchantsPlugin.getInstance(), () -> {
anticheats.forEach(anticheat -> anticheat.unexempt(player));
}, 1);
}
}