001    package cpw.mods.fml.common.registry;
002    
003    import java.util.Map;
004    
005    import net.minecraft.block.Block;
006    import net.minecraft.item.Item;
007    import net.minecraft.item.ItemBlock;
008    import net.minecraft.nbt.NBTTagCompound;
009    
010    import com.google.common.base.Objects;
011    import com.google.common.collect.HashMultiset;
012    import com.google.common.collect.Maps;
013    import com.google.common.collect.Multiset;
014    
015    import cpw.mods.fml.common.ModContainer;
016    
017    public class ItemData {
018    
019        private static Map<String, Multiset<String>> modOrdinals = Maps.newHashMap();
020        public final String modId;
021        public final String itemType;
022        public final int itemId;
023        public final int ordinal;
024    
025        public ItemData(Item item, ModContainer mc)
026        {
027            this.itemId = item.shiftedIndex;
028            if (item.getClass().equals(ItemBlock.class))
029            {
030                this.itemType =  Block.blocksList[this.itemId].getClass().getName();
031            }
032            else
033            {
034                this.itemType = item.getClass().getName();
035            }
036            this.modId = mc.getModId();
037            if (!modOrdinals.containsKey(mc.getModId()))
038            {
039                modOrdinals.put(mc.getModId(), HashMultiset.<String>create());
040            }
041            this.ordinal = modOrdinals.get(mc.getModId()).add(itemType, 1);
042        }
043    
044        public ItemData(NBTTagCompound tag)
045        {
046            this.modId = tag.getString("ModId");
047            this.itemType = tag.getString("ItemType");
048            this.itemId = tag.getInteger("ItemId");
049            this.ordinal = tag.getInteger("ordinal");
050        }
051    
052        public NBTTagCompound toNBT()
053        {
054            NBTTagCompound tag = new NBTTagCompound();
055            tag.setString("ModId", modId);
056            tag.setString("ItemType", itemType);
057            tag.setInteger("ItemId", itemId);
058            tag.setInteger("ordinal", ordinal);
059            return tag;
060        }
061    
062        @Override
063        public int hashCode()
064        {
065            return Objects.hashCode(modId, itemType, itemId, ordinal);
066        }
067    
068        @Override
069        public boolean equals(Object obj)
070        {
071            try
072            {
073                ItemData other = (ItemData) obj;
074                return Objects.equal(modId, other.modId) && Objects.equal(itemType, other.itemType) && Objects.equal(itemId, other.itemId) && Objects.equal(ordinal, other.ordinal);
075            }
076            catch (ClassCastException cce)
077            {
078                return false;
079            }
080        }
081    
082        @Override
083        public String toString()
084        {
085            return String.format("Item %d, Type %s, owned by %s, ordinal %d", itemId, itemType, modId, ordinal);
086        }
087    
088        public boolean mayDifferByOrdinal(ItemData rightValue)
089        {
090            return Objects.equal(itemType, rightValue.itemType) && Objects.equal(modId, rightValue.modId);
091        }
092    }