001package net.minecraftforge.liquids; 002 003import static cpw.mods.fml.relauncher.Side.CLIENT; 004 005import com.google.common.base.Objects; 006 007import cpw.mods.fml.relauncher.SideOnly; 008import net.minecraft.block.Block; 009import net.minecraft.block.BlockFluid; 010import net.minecraft.client.renderer.texture.TextureManager; 011import net.minecraft.item.Item; 012import net.minecraft.item.ItemStack; 013import net.minecraft.nbt.NBTTagCompound; 014import net.minecraft.util.Icon; 015 016/** 017 * ItemStack substitute for liquids 018 * @author SirSengir 019 */ 020public class LiquidStack 021{ 022 public int itemID; 023 public int amount; 024 public int itemMeta; 025 026 private LiquidStack(){} 027 028 public LiquidStack(int itemID, int amount) { this(itemID, amount, 0); } 029 public LiquidStack(Item item, int amount) { this(item.itemID, amount, 0); } 030 public LiquidStack(Block block, int amount) { this(block.blockID, amount, 0); } 031 032 public LiquidStack(int itemID, int amount, int itemDamage) 033 { 034 this.itemID = itemID; 035 this.amount = amount; 036 this.itemMeta = itemDamage; 037 } 038 039 public NBTTagCompound writeToNBT(NBTTagCompound nbt) 040 { 041 nbt.setShort("Id", (short)itemID); 042 nbt.setInteger("Amount", amount); 043 nbt.setShort("Meta", (short)itemMeta); 044 nbt.setString("LiquidName", LiquidDictionary.findLiquidName(this)); 045 return nbt; 046 } 047 048 public void readFromNBT(NBTTagCompound nbt) 049 { 050 String liquidName = nbt.getString("LiquidName"); 051 if (liquidName != null) 052 { 053 LiquidStack liquid = LiquidDictionary.getCanonicalLiquid(liquidName); 054 itemID = liquid.itemID; 055 itemMeta = liquid.itemMeta; 056 } 057 else 058 { 059 itemID = nbt.getShort("Id"); 060 itemMeta = nbt.getShort("Meta"); 061 } 062 amount = nbt.getInteger("Amount"); 063 } 064 065 /** 066 * @return A copy of this LiquidStack 067 */ 068 public LiquidStack copy() 069 { 070 return new LiquidStack(itemID, amount, itemMeta); 071 } 072 073 /** 074 * @param other 075 * @return true if this LiquidStack contains the same liquid as the one passed in. 076 */ 077 public boolean isLiquidEqual(LiquidStack other) 078 { 079 return other != null && itemID == other.itemID && itemMeta == other.itemMeta; 080 } 081 082 /** 083 * @param other 084 * @return true if this LiquidStack contains the other liquid (liquids are equal and amount >= other.amount). 085 */ 086 public boolean containsLiquid(LiquidStack other) 087 { 088 return isLiquidEqual(other) && amount >= other.amount; 089 } 090 091 /** 092 * @param other ItemStack containing liquids. 093 * @return true if this LiquidStack contains the same liquid as the one passed in. 094 */ 095 public boolean isLiquidEqual(ItemStack other) 096 { 097 if (other == null) 098 { 099 return false; 100 } 101 102 if (itemID == other.itemID && itemMeta == other.getItemDamage()) 103 { 104 return true; 105 } 106 107 return isLiquidEqual(LiquidContainerRegistry.getLiquidForFilledItem(other)); 108 } 109 110 /** 111 * @return ItemStack representation of this LiquidStack 112 */ 113 public ItemStack asItemStack() 114 { 115 return new ItemStack(itemID, 1, itemMeta); 116 } 117 118 /** 119 * Reads a liquid stack from the passed nbttagcompound and returns it. 120 * 121 * @param nbt 122 * @return the liquid stack 123 */ 124 public static LiquidStack loadLiquidStackFromNBT(NBTTagCompound nbt) 125 { 126 LiquidStack liquidstack = new LiquidStack(); 127 liquidstack.readFromNBT(nbt); 128 return liquidstack.itemID == 0 ? null : liquidstack; 129 } 130 131 @SideOnly(CLIENT) 132 private Icon renderingIcon; 133 134 @SideOnly(CLIENT) 135 public Icon getRenderingIcon() 136 { 137 if (itemID == Block.waterStill.blockID) 138 { 139 return BlockFluid.func_94424_b("water"); 140 } 141 else if (itemID == Block.lavaStill.blockID) 142 { 143 return BlockFluid.func_94424_b("lava"); 144 } 145 return renderingIcon; 146 } 147 148 @SideOnly(CLIENT) 149 public void setRenderingIcon(Icon icon) 150 { 151 this.renderingIcon = icon; 152 } 153 154 @Override 155 public final int hashCode() 156 { 157 return 31 * itemID + itemMeta; 158 } 159 160 @Override 161 public final boolean equals(Object ob) 162 { 163 return ob instanceof LiquidStack && Objects.equal(((LiquidStack)ob).itemID, itemID) && Objects.equal(((LiquidStack)ob).itemMeta, itemMeta); 164 } 165}