diff --git a/src/com/massivecraft/massivecore/ItemData.java b/src/com/massivecraft/massivecore/ItemData.java index f65b1a05..1c29b9e5 100644 --- a/src/com/massivecraft/massivecore/ItemData.java +++ b/src/com/massivecraft/massivecore/ItemData.java @@ -3,14 +3,31 @@ package com.massivecraft.massivecore; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.Color; -import org.bukkit.enchantments.Enchantment; +import org.bukkit.block.banner.Pattern; +import org.bukkit.potion.PotionEffect; import com.massivecraft.massivecore.collections.MassiveListDef; -import com.massivecraft.massivecore.collections.MassiveMapDef; +import com.massivecraft.massivecore.collections.MassiveTreeMapDef; +import com.massivecraft.massivecore.collections.MassiveTreeSetDef; +import com.massivecraft.massivecore.comparator.ComparatorHashCode; +import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName; +/** + * This class makes use of primitives, collections and maps only. + * All Bukkit specific enumerations and classes are avoided. + * That means this class itself is compatible with all Bukkit server versions. + * + * We also make sure to only initialize variables with null as value. + * Null means "default" and this way we save database space as well as CPU power on class construction. + * + * This class acts as a safe intermediary for database storage. + * It is mainly used by the ItemStackAdapter and InventoryAdapter. + * It can also be used directly, for example in maps, since it provides a stable equals and hash code method (as opposed to Bukkit). + */ public class ItemData { // -------------------------------------------- // @@ -22,6 +39,7 @@ public class ItemData public static final transient int DEFAULT_COUNT = 1; public static final transient int DEFAULT_DAMAGE = 0; public static final transient boolean DEFAULT_SCALING = false; + public static final transient boolean DEFAULT_UNBREAKABLE = false; // -------------------------------------------- // // FIELDS > BASIC @@ -47,13 +65,14 @@ public class ItemData public String getName() { return this.name; } public ItemData setName(String name) { this.name = name; return this; } - private MassiveListDef lore = new MassiveListDef<>(); + private MassiveListDef lore = null; public List getLore() { return this.lore; } - public ItemData setLore(Collection lore) { this.lore = new MassiveListDef<>(lore); return this;} + public ItemData setLore(Collection lore) { this.lore = (lore == null ? null : new MassiveListDef<>(lore)); return this;} - private MassiveMapDef enchants = new MassiveMapDef<>(); - public Map getEnchants() { return this.enchants; } - public ItemData setEnchants(Map enchants) { this.enchants = new MassiveMapDef<>(enchants); return this; } + // TODO: Can I create a string comparator and use that one instead? HashCode looks ugly. + private MassiveTreeMapDef enchants = null; + public Map getEnchants() { return this.enchants; } + public ItemData setEnchants(Map enchants) { this.enchants = (enchants == null ? null : new MassiveTreeMapDef(ComparatorHashCode.get(), enchants)); return this; } private Integer repaircost = null; public Integer getRepaircost() { return this.repaircost; } @@ -79,6 +98,8 @@ public class ItemData // FIELDS > LEATHER ARMOR // -------------------------------------------- // + // TODO: Color is not a primitive... convert into using a primitive! + // TODO: We must also figure out the int value of the DefaultLeatherColor! private Color color = null; public Color getColor() { return (this.color == null ? Bukkit.getItemFactory().getDefaultLeatherColor() : this.color); } public ItemData setColor(Color color) { this.color = (Bukkit.getItemFactory().getDefaultLeatherColor().equals(color) ? null : color); return this; } @@ -92,10 +113,98 @@ public class ItemData public ItemData setScaling(boolean scaling) { this.scaling = (scaling == DEFAULT_SCALING ? null : scaling); return this; } // -------------------------------------------- // - // FIELDS > ... + // FIELDS > POTION // -------------------------------------------- // - // TODO: Add all the fields + // TODO: Create and use PotionEffectData! + @SerializedName("potion-effects") + private List potionEffects = null; + public List getPotionEffects() { return this.potionEffects; } + public ItemData setPotionEffects(Collection potionEffects) { this.potionEffects = (potionEffects == null ? null : new MassiveListDef<>(potionEffects)); return this; } + + // -------------------------------------------- // + // FIELDS > SKULL + // -------------------------------------------- // + + private String skull = null; + public String getSkull() { return this.skull; } + public ItemData setSkull(String skull) { this.skull = skull; return this; } + + // -------------------------------------------- // + // FIELDS > FIREWORK EFFECT + // -------------------------------------------- // + + // TODO + + // -------------------------------------------- // + // FIELDS > FIREWORK + // -------------------------------------------- // + + // TODO + + // -------------------------------------------- // + // FIELDS > STORED ENCHANTS + // -------------------------------------------- // + + // TODO: Can I create a string comparator and use that one instead? HashCode looks ugly. + @SerializedName("stored-enchants") + private MassiveTreeMapDef storedEnchants = null; + public Map getStoredEnchants() { return this.storedEnchants; } + public ItemData setStoredEnchants(Map storedEnchants) { this.storedEnchants = (storedEnchants == null ? null : new MassiveTreeMapDef(ComparatorHashCode.get(), storedEnchants)); return this; } + + // -------------------------------------------- // + // FIELDS > UNBREAKABLE + // -------------------------------------------- // + // SINCE: 1.8 + + private Boolean unbreakable = null; + public boolean isUnbreakable() { return (this.unbreakable == null ? DEFAULT_UNBREAKABLE : this.unbreakable); } + public ItemData setUnbreakable(boolean unbreakable) { this.unbreakable = (unbreakable == DEFAULT_UNBREAKABLE ? null : unbreakable); return this; } + + // -------------------------------------------- // + // FIELDS > FLAGS + // -------------------------------------------- // + // SINCE: 1.8 + + // TODO: Can I create a string comparator and use that one instead? HashCode looks ugly. + private MassiveTreeSetDef flags = null; + public Set getFlags() { return this.flags; } + public ItemData setFlags(Collection flags) { this.flags = (flags == null ? null : new MassiveTreeSetDef(ComparatorHashCode.get(), flags)); return this; } + + // -------------------------------------------- // + // FIELDS > BANNER BASE + // -------------------------------------------- // + // SINCE: 1.8 + // The integer is the dye color byte representation. + + @SerializedName("banner-base") + private Integer bannerBase = null; + public Integer getBannerBase() { return this.bannerBase; } + public ItemData setBannerBase(Integer bannerBase) { this.bannerBase = bannerBase; return this; } + + // -------------------------------------------- // + // FIELDS > BANNER PATTERNS + // -------------------------------------------- // + // SINCE: 1.8 + // This should really be a list and not a set. + // The order matters and is explicitly assigned. + + // TODO: The Pattern class can not be used here. It breaks 1.8 compatibility. + // TODO: Convert to to use only raw primitiveish data! + // TODO: I actually decided to use a list of integers. That should be mimiced here. + @SerializedName("banner") + private MassiveListDef bannerPatterns = null; + public List getBannerPatterns() { return this.bannerPatterns; } + public ItemData setBannerPatterns(Collection bannerPatterns) { this.bannerPatterns = (bannerPatterns == null ? null : new MassiveListDef<>(bannerPatterns)); return this;} + + // -------------------------------------------- // + // FIELDS > POTION + // -------------------------------------------- // + // SINCE: 1.9 + + private String potion = null; + public String getPotion() { return this.potion; } + public ItemData setPotion(String potion) { this.potion = potion; return this; } // -------------------------------------------- // // CONVERT