From ac5e815b3e2434e0884a32efc9611b5436d1b8db Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Sat, 7 Sep 2024 18:14:09 +1000 Subject: [PATCH] SPIGOT-7867: Merchant instanceof AbstractVillager always returns false By: Miles Holder --- .../entity/npc/EntityVillagerAbstract.patch | 12 ++--- .../entity/CraftAbstractVillager.java | 50 +++---------------- .../craftbukkit/inventory/CraftMerchant.java | 48 ++++++------------ .../inventory/CraftMerchantCustom.java | 8 +-- 4 files changed, 32 insertions(+), 86 deletions(-) diff --git a/paper-server/nms-patches/net/minecraft/world/entity/npc/EntityVillagerAbstract.patch b/paper-server/nms-patches/net/minecraft/world/entity/npc/EntityVillagerAbstract.patch index 20b08c4326..dbdfbb05dc 100644 --- a/paper-server/nms-patches/net/minecraft/world/entity/npc/EntityVillagerAbstract.patch +++ b/paper-server/nms-patches/net/minecraft/world/entity/npc/EntityVillagerAbstract.patch @@ -1,6 +1,6 @@ --- a/net/minecraft/world/entity/npc/EntityVillagerAbstract.java +++ b/net/minecraft/world/entity/npc/EntityVillagerAbstract.java -@@ -40,8 +40,24 @@ +@@ -40,8 +40,22 @@ import net.minecraft.world.phys.Vec3D; import org.slf4j.Logger; @@ -15,17 +15,15 @@ public abstract class EntityVillagerAbstract extends EntityAgeable implements InventoryCarrier, NPC, IMerchant { + // CraftBukkit start -+ private CraftMerchant craftMerchant; -+ + @Override + public CraftMerchant getCraftMerchant() { -+ return (craftMerchant == null) ? craftMerchant = new CraftMerchant(this) : craftMerchant; ++ return (org.bukkit.craftbukkit.entity.CraftAbstractVillager) getBukkitEntity(); + } + // CraftBukkit end private static final DataWatcherObject DATA_UNHAPPY_COUNTER = DataWatcher.defineId(EntityVillagerAbstract.class, DataWatcherRegistry.INT); private static final Logger LOGGER = LogUtils.getLogger(); public static final int VILLAGER_SLOT_OFFSET = 300; -@@ -50,7 +66,7 @@ +@@ -50,7 +64,7 @@ private EntityHuman tradingPlayer; @Nullable protected MerchantRecipeList offers; @@ -34,7 +32,7 @@ public EntityVillagerAbstract(EntityTypes entitytypes, World world) { super(entitytypes, world); -@@ -179,7 +195,7 @@ +@@ -179,7 +193,7 @@ public void readAdditionalSaveData(NBTTagCompound nbttagcompound) { super.readAdditionalSaveData(nbttagcompound); if (nbttagcompound.contains("Offers")) { @@ -43,7 +41,7 @@ Logger logger = EntityVillagerAbstract.LOGGER; Objects.requireNonNull(logger); -@@ -246,7 +262,16 @@ +@@ -246,7 +260,16 @@ MerchantRecipe merchantrecipe = ((VillagerTrades.IMerchantRecipeOption) arraylist.remove(this.random.nextInt(arraylist.size()))).getOffer(this, this.random); if (merchantrecipe != null) { diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftAbstractVillager.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftAbstractVillager.java index 01285d555e..7d72133545 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftAbstractVillager.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftAbstractVillager.java @@ -1,18 +1,16 @@ package org.bukkit.craftbukkit.entity; -import java.util.List; import net.minecraft.world.entity.npc.EntityVillager; import net.minecraft.world.entity.npc.EntityVillagerAbstract; +import net.minecraft.world.item.trading.IMerchant; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.inventory.CraftInventory; import org.bukkit.craftbukkit.inventory.CraftMerchant; import org.bukkit.entity.AbstractVillager; -import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; -import org.bukkit.inventory.MerchantRecipe; -public class CraftAbstractVillager extends CraftAgeable implements AbstractVillager, InventoryHolder { +public class CraftAbstractVillager extends CraftAgeable implements CraftMerchant, AbstractVillager, InventoryHolder { public CraftAbstractVillager(CraftServer server, EntityVillagerAbstract entity) { super(server, entity); @@ -23,6 +21,11 @@ public class CraftAbstractVillager extends CraftAgeable implements AbstractVilla return (EntityVillager) entity; } + @Override + public IMerchant getMerchant() { + return getHandle(); + } + @Override public String toString() { return "CraftAbstractVillager"; @@ -32,43 +35,4 @@ public class CraftAbstractVillager extends CraftAgeable implements AbstractVilla public Inventory getInventory() { return new CraftInventory(getHandle().getInventory()); } - - private CraftMerchant getMerchant() { - return getHandle().getCraftMerchant(); - } - - @Override - public List getRecipes() { - return getMerchant().getRecipes(); - } - - @Override - public void setRecipes(List recipes) { - this.getMerchant().setRecipes(recipes); - } - - @Override - public MerchantRecipe getRecipe(int i) { - return getMerchant().getRecipe(i); - } - - @Override - public void setRecipe(int i, MerchantRecipe merchantRecipe) { - getMerchant().setRecipe(i, merchantRecipe); - } - - @Override - public int getRecipeCount() { - return getMerchant().getRecipeCount(); - } - - @Override - public boolean isTrading() { - return getTrader() != null; - } - - @Override - public HumanEntity getTrader() { - return getMerchant().getTrader(); - } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchant.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchant.java index 6bb9ee0171..df3b721905 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchant.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchant.java @@ -11,21 +11,13 @@ import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.Merchant; import org.bukkit.inventory.MerchantRecipe; -public class CraftMerchant implements Merchant { +public interface CraftMerchant extends Merchant { - protected final IMerchant merchant; - - public CraftMerchant(IMerchant merchant) { - this.merchant = merchant; - } - - public IMerchant getMerchant() { - return merchant; - } + IMerchant getMerchant(); @Override - public List getRecipes() { - return Collections.unmodifiableList(Lists.transform(merchant.getOffers(), new Function() { + default List getRecipes() { + return Collections.unmodifiableList(Lists.transform(getMerchant().getOffers(), new Function() { @Override public MerchantRecipe apply(net.minecraft.world.item.trading.MerchantRecipe recipe) { return recipe.asBukkit(); @@ -34,8 +26,8 @@ public class CraftMerchant implements Merchant { } @Override - public void setRecipes(List recipes) { - MerchantRecipeList recipesList = merchant.getOffers(); + default void setRecipes(List recipes) { + MerchantRecipeList recipesList = getMerchant().getOffers(); recipesList.clear(); for (MerchantRecipe recipe : recipes) { recipesList.add(CraftMerchantRecipe.fromBukkit(recipe).toMinecraft()); @@ -43,38 +35,28 @@ public class CraftMerchant implements Merchant { } @Override - public MerchantRecipe getRecipe(int i) { - return merchant.getOffers().get(i).asBukkit(); + default MerchantRecipe getRecipe(int i) { + return getMerchant().getOffers().get(i).asBukkit(); } @Override - public void setRecipe(int i, MerchantRecipe merchantRecipe) { - merchant.getOffers().set(i, CraftMerchantRecipe.fromBukkit(merchantRecipe).toMinecraft()); + default void setRecipe(int i, MerchantRecipe merchantRecipe) { + getMerchant().getOffers().set(i, CraftMerchantRecipe.fromBukkit(merchantRecipe).toMinecraft()); } @Override - public int getRecipeCount() { - return merchant.getOffers().size(); + default int getRecipeCount() { + return getMerchant().getOffers().size(); } @Override - public boolean isTrading() { + default boolean isTrading() { return getTrader() != null; } @Override - public HumanEntity getTrader() { - EntityHuman eh = merchant.getTradingPlayer(); + default HumanEntity getTrader() { + EntityHuman eh = getMerchant().getTradingPlayer(); return eh == null ? null : eh.getBukkitEntity(); } - - @Override - public int hashCode() { - return merchant.hashCode(); - } - - @Override - public boolean equals(final Object obj) { - return obj instanceof CraftMerchant && ((CraftMerchant) obj).merchant.equals(this.merchant); - } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchantCustom.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchantCustom.java index 87325cc415..34e49c538f 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchantCustom.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchantCustom.java @@ -11,10 +11,12 @@ import net.minecraft.world.item.trading.MerchantRecipe; import net.minecraft.world.item.trading.MerchantRecipeList; import org.bukkit.craftbukkit.util.CraftChatMessage; -public class CraftMerchantCustom extends CraftMerchant { +public class CraftMerchantCustom implements CraftMerchant { + + private MinecraftMerchant merchant; public CraftMerchantCustom(String title) { - super(new MinecraftMerchant(title)); + this.merchant = new MinecraftMerchant(title); getMerchant().craftMerchant = this; } @@ -25,7 +27,7 @@ public class CraftMerchantCustom extends CraftMerchant { @Override public MinecraftMerchant getMerchant() { - return (MinecraftMerchant) super.getMerchant(); + return this.merchant; } public static class MinecraftMerchant implements IMerchant {