diff options
author | Senmori <thesenmori@gmail.com> | 2017-12-31 21:21:03 +1100 |
---|---|---|
committer | md_5 <git@md-5.net> | 2017-12-31 21:21:03 +1100 |
commit | 950006003c4f57ca4ac61280c93fdc5ba747ce20 (patch) | |
tree | 9f3e75bbfe36b772ce52fccd37f49ee8b8a48268 /src | |
parent | e1fb9cb640b9c4df167d3eb988aaa69ea533fc89 (diff) | |
download | craftbukkit-950006003c4f57ca4ac61280c93fdc5ba747ce20.tar craftbukkit-950006003c4f57ca4ac61280c93fdc5ba747ce20.tar.gz craftbukkit-950006003c4f57ca4ac61280c93fdc5ba747ce20.tar.lz craftbukkit-950006003c4f57ca4ac61280c93fdc5ba747ce20.tar.xz craftbukkit-950006003c4f57ca4ac61280c93fdc5ba747ce20.zip |
SPIGOT-355: API for Villager careers
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java | 63 |
1 files changed, 63 insertions, 0 deletions
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<Career, Integer> careerIDMap = new HashMap<>(); private CraftMerchant merchant; public CraftVillager(CraftServer server, EntityVillager entity) { @@ -46,6 +50,31 @@ public class CraftVillager extends CraftAgeable implements Villager, InventoryHo } @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<Career> 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<Career> 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; + } + } } |