From 950006003c4f57ca4ac61280c93fdc5ba747ce20 Mon Sep 17 00:00:00 2001 From: Senmori Date: Sun, 31 Dec 2017 21:21:03 +1100 Subject: SPIGOT-355: API for Villager careers --- .../bukkit/craftbukkit/entity/CraftVillager.java | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'src') diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java index 6f50eb98..503bd066 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java +++ b/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; + } + } } -- cgit v1.2.3