diff --git a/paper-server/nms-patches/EntityVillager.patch b/paper-server/nms-patches/EntityVillager.patch index fa7f585a3a..8693d0ff09 100644 --- a/paper-server/nms-patches/EntityVillager.patch +++ b/paper-server/nms-patches/EntityVillager.patch @@ -15,6 +15,22 @@ public class EntityVillager extends EntityAgeable implements NPC, IMerchant { +@@ -18,13 +26,13 @@ + @Nullable + private EntityHuman tradingPlayer; + @Nullable +- private MerchantRecipeList trades; ++ public MerchantRecipeList trades; // PAIL private -> public + private int bF; + private boolean bG; + private boolean bH; + public int riches; + private String bJ; +- private int bK; ++ public int bK; // PAIL private -> public // PAIL rename careerID + private int bL; + private boolean bM; + private boolean bN; @@ -37,7 +45,7 @@ public EntityVillager(World world, int i) { @@ -40,6 +56,15 @@ } } +@@ -432,7 +447,7 @@ + return this.trades; + } + +- private void dx() { ++ public void dx() { // CraftBukkit private -> public // PAIL rename populateTrades + EntityVillager.IMerchantRecipeOption[][][] aentityvillager_imerchantrecipeoption = EntityVillager.bP[this.getProfession()]; + + if (this.bK != 0 && this.bL != 0) { @@ -460,7 +475,20 @@ for (int l = 0; l < k; ++l) { EntityVillager.IMerchantRecipeOption entityvillager_imerchantrecipeoption = aentityvillager_imerchantrecipeoption3[l]; diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java index 6f50eb9866..503bd0661c 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java @@ -1,6 +1,9 @@ package org.bukkit.craftbukkit.entity; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import javax.annotation.Nullable; import net.minecraft.server.EntityVillager; import org.apache.commons.lang.Validate; import org.bukkit.craftbukkit.CraftServer; @@ -15,6 +18,7 @@ import org.bukkit.inventory.MerchantRecipe; public class CraftVillager extends CraftAgeable implements Villager, InventoryHolder { + private static final Map careerIDMap = new HashMap<>(); private CraftMerchant merchant; public CraftVillager(CraftServer server, EntityVillager entity) { @@ -45,6 +49,31 @@ public class CraftVillager extends CraftAgeable implements Villager, InventoryHo getHandle().setProfession(profession.ordinal() - 1); } + @Override + public Career getCareer() { + return getCareer(getProfession(), getHandle().bK); + } + + @Override + public void setCareer(Career career) { + setCareer(career, true); + } + + @Override + public void setCareer(Career career, boolean resetTrades) { + if (career == null) { + getHandle().bK = 0; // reset career + } else { + Validate.isTrue(career.getProfession() == getProfession(), "Career assignment mismatch. Found (" + getProfession() + ") Required (" + career.getProfession() + ")"); + getHandle().bK = getCareerID(career); + } + + if (resetTrades) { + getHandle().trades = null; + getHandle().dx(); + } + } + @Override public Inventory getInventory() { return new CraftInventory(getHandle().inventory); @@ -98,4 +127,38 @@ public class CraftVillager extends CraftAgeable implements Villager, InventoryHo public void setRiches(int riches) { getHandle().riches = riches; } + + @Nullable + private static Career getCareer(Profession profession, int id) { + Validate.isTrue(id > 0, "Career id must be greater than 0"); + + List careers = profession.getCareers(); + for (Career c : careers) { + if (careerIDMap.containsKey(c) && careerIDMap.get(c) == id) { + return c; + } + } + + return null; + } + + private static int getCareerID(Career career) { + return careerIDMap.getOrDefault(career, 0); + } + + static { + // build Career -> ID map + int id = 0; + for (Profession prof : Profession.values()) { + List careers = prof.getCareers(); + if (!careers.isEmpty()) { + for (Career c : careers) { + careerIDMap.put(c, ++id); + } + } + + Validate.isTrue(id == careers.size(), "Career id registration mismatch"); + id = 0; + } + } }