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.FMLLog; 016 import cpw.mods.fml.common.Loader; 017 import cpw.mods.fml.common.LoaderException; 018 import cpw.mods.fml.common.ModContainer; 019 020 public class ItemData { 021 022 private static Map<String, Multiset<String>> modOrdinals = Maps.newHashMap(); 023 private final String modId; 024 private final String itemType; 025 private final int itemId; 026 private final int ordinal; 027 private String forcedModId; 028 private String forcedName; 029 030 public ItemData(Item item, ModContainer mc) 031 { 032 this.itemId = item.itemID; 033 if (item.getClass().equals(ItemBlock.class)) 034 { 035 this.itemType = Block.blocksList[this.getItemId()].getClass().getName(); 036 } 037 else 038 { 039 this.itemType = item.getClass().getName(); 040 } 041 this.modId = mc.getModId(); 042 if (!modOrdinals.containsKey(mc.getModId())) 043 { 044 modOrdinals.put(mc.getModId(), HashMultiset.<String>create()); 045 } 046 this.ordinal = modOrdinals.get(mc.getModId()).add(itemType, 1); 047 } 048 049 public ItemData(NBTTagCompound tag) 050 { 051 this.modId = tag.getString("ModId"); 052 this.itemType = tag.getString("ItemType"); 053 this.itemId = tag.getInteger("ItemId"); 054 this.ordinal = tag.getInteger("ordinal"); 055 this.forcedModId = tag.hasKey("ForcedModId") ? tag.getString("ForcedModId") : null; 056 this.forcedName = tag.hasKey("ForcedName") ? tag.getString("ForcedName") : null; 057 } 058 059 public String getItemType() 060 { 061 return this.forcedName !=null ? forcedName : itemType; 062 } 063 064 public String getModId() 065 { 066 return this.forcedModId != null ? forcedModId : modId; 067 } 068 069 public int getOrdinal() 070 { 071 return ordinal; 072 } 073 074 public int getItemId() 075 { 076 return itemId; 077 } 078 079 public NBTTagCompound toNBT() 080 { 081 NBTTagCompound tag = new NBTTagCompound(); 082 tag.setString("ModId", modId); 083 tag.setString("ItemType", itemType); 084 tag.setInteger("ItemId", itemId); 085 tag.setInteger("ordinal", ordinal); 086 if (forcedModId != null) 087 { 088 tag.setString("ForcedModId", forcedModId); 089 } 090 if (forcedName != null) 091 { 092 tag.setString("ForcedName", forcedName); 093 } 094 return tag; 095 } 096 097 @Override 098 public int hashCode() 099 { 100 return Objects.hashCode(itemId, ordinal); 101 } 102 103 @Override 104 public boolean equals(Object obj) 105 { 106 try 107 { 108 ItemData other = (ItemData) obj; 109 return Objects.equal(getModId(), other.getModId()) && Objects.equal(getItemType(), other.getItemType()) && Objects.equal(itemId, other.itemId) && ( isOveridden() || Objects.equal(ordinal, other.ordinal)); 110 } 111 catch (ClassCastException cce) 112 { 113 return false; 114 } 115 } 116 117 @Override 118 public String toString() 119 { 120 return String.format("Item %d, Type %s, owned by %s, ordinal %d, name %s, claimedModId %s", itemId, itemType, modId, ordinal, forcedName, forcedModId); 121 } 122 123 public boolean mayDifferByOrdinal(ItemData rightValue) 124 { 125 return Objects.equal(getItemType(), rightValue.getItemType()) && Objects.equal(getModId(), rightValue.getModId()); 126 } 127 128 public boolean isOveridden() 129 { 130 return forcedName != null; 131 } 132 133 public void setName(String name, String modId) 134 { 135 if (name == null) 136 { 137 this.forcedName = null; 138 this.forcedModId = null; 139 return; 140 } 141 String localModId = modId; 142 if (modId == null) 143 { 144 localModId = Loader.instance().activeModContainer().getModId(); 145 } 146 if (modOrdinals.get(localModId).count(name)>0) 147 { 148 FMLLog.severe("The mod %s is attempting to redefine the item at id %d with a non-unique name (%s.%s)", Loader.instance().activeModContainer(), itemId, localModId, name); 149 throw new LoaderException(); 150 } 151 modOrdinals.get(localModId).add(name); 152 this.forcedModId = modId; 153 this.forcedName = name; 154 } 155 }