diff --git a/Essentials/src/com/earth2me/essentials/items/FlatItemDb.java b/Essentials/src/com/earth2me/essentials/items/FlatItemDb.java index 5871f4822..e901f3a1c 100644 --- a/Essentials/src/com/earth2me/essentials/items/FlatItemDb.java +++ b/Essentials/src/com/earth2me/essentials/items/FlatItemDb.java @@ -8,11 +8,11 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import net.ess3.api.IEssentials; import org.bukkit.Material; +import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.PotionMeta; -import org.bukkit.inventory.meta.Repairable; import org.bukkit.potion.PotionData; import java.util.*; @@ -24,10 +24,8 @@ import static com.earth2me.essentials.I18n.tl; public class FlatItemDb extends AbstractItemDb { protected static final Logger LOGGER = Logger.getLogger("Essentials"); - private final transient IEssentials ess; - private static Gson gson = new Gson(); - + private final transient IEssentials ess; // Maps primary name to ItemData private final transient Map items = new HashMap<>(); @@ -57,9 +55,9 @@ public class FlatItemDb extends AbstractItemDb { this.reset(); String json = file.getLines().stream() - .filter(line -> !line.startsWith("#")) - .collect(Collectors.joining()); - + .filter(line -> !line.startsWith("#")) + .collect(Collectors.joining()); + this.loadJSON(String.join("\n", json)); } @@ -108,12 +106,12 @@ public class FlatItemDb extends AbstractItemDb { throw new Exception(tl("unknownItemName", id)); } - PotionData potionData = data.getPotionData(); Material material = data.getMaterial(); ItemStack stack = new ItemStack(material); stack.setAmount(material.getMaxStackSize()); + PotionData potionData = data.getPotionData(); ItemMeta meta = stack.getItemMeta(); if (potionData != null && meta instanceof PotionMeta) { @@ -130,6 +128,13 @@ public class FlatItemDb extends AbstractItemDb { stack.setItemMeta(meta); + // The spawner provider will update the meta again, so we need to call it after + // setItemMeta to prevent a race condition + EntityType entity = data.getEntity(); + if (entity != null && material.toString().contains("SPAWNER")) { + ess.getSpawnerProvider().setEntityType(stack, entity); + } + return stack; } @@ -161,14 +166,7 @@ public class FlatItemDb extends AbstractItemDb { @Override public String name(ItemStack item) { - Material type = item.getType(); - PotionData potion = null; - - if (MaterialUtil.isPotion(type) && item.getItemMeta() instanceof PotionMeta) { - potion = ((PotionMeta) item.getItemMeta()).getBasePotionData(); - } - - ItemData data = new ItemData(type, potion); + ItemData data = lookup(item); for (Map.Entry entry : items.entrySet()) { if (entry.getValue().equals(data)) { @@ -185,20 +183,44 @@ public class FlatItemDb extends AbstractItemDb { throw new UnsupportedOperationException("Legacy IDs aren't supported on this version."); } + public ItemData lookup(ItemStack item) { + Material type = item.getType(); + + if (MaterialUtil.isPotion(type) && item.getItemMeta() instanceof PotionMeta) { + PotionData potion = ((PotionMeta) item.getItemMeta()).getBasePotionData(); + return new ItemData(type, potion); + } else if (type.toString().contains("SPAWNER")) { + EntityType entity = ess.getSpawnerProvider().getEntityType(item); + return new ItemData(type, entity); + } else { + return new ItemData(type); + } + } + @Override public Collection listNames() { return Collections.unmodifiableSet(allAliases); } public static class ItemData { - private Material material; - private PotionData potionData; + private final Material material; + private PotionData potionData = null; + private EntityType entity = null; + + public ItemData(Material material) { + this.material = material; + } public ItemData(Material material, PotionData potionData) { this.material = material; this.potionData = potionData; } + public ItemData(Material material, EntityType entity) { + this.material = material; + this.entity = entity; + } + @Override public int hashCode() { return (31 * material.hashCode()) ^ potionData.hashCode(); @@ -213,7 +235,7 @@ public class FlatItemDb extends AbstractItemDb { return false; } ItemData that = (ItemData) o; - return this.material == that.getMaterial() && potionDataEquals(that); + return this.material == that.getMaterial() && potionDataEquals(that) && entityEquals(that); } public Material getMaterial() { @@ -224,6 +246,10 @@ public class FlatItemDb extends AbstractItemDb { return this.potionData; } + public EntityType getEntity() { + return this.entity; + } + private boolean potionDataEquals(ItemData o) { if (this.potionData == null && o.getPotionData() == null) { return true; @@ -233,5 +259,15 @@ public class FlatItemDb extends AbstractItemDb { return false; } } + + private boolean entityEquals(ItemData o) { + if (this.entity == null && o.getEntity() == null) { // neither have an entity + return true; + } else if (this.entity != null && o.getEntity() != null) { // both have an entity; check if it's the same one + return this.entity.equals(o.getEntity()); + } else { // one has an entity but the other doesn't, so they can't be equal + return false; + } + } } }