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.setInteger("Amount", amount); 042 nbt.setShort("Id", (short)itemID); 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 itemID = nbt.getShort("Id"); 052 itemMeta = nbt.getShort("Meta"); 053 LiquidStack liquid = LiquidDictionary.getCanonicalLiquid(liquidName); 054 if(liquid != null) { 055 itemID = liquid.itemID; 056 itemMeta = liquid.itemMeta; 057 } 058 amount = nbt.getInteger("Amount"); 059 } 060 061 /** 062 * @return A copy of this LiquidStack 063 */ 064 public LiquidStack copy() 065 { 066 return new LiquidStack(itemID, amount, itemMeta); 067 } 068 069 /** 070 * @param other 071 * @return true if this LiquidStack contains the same liquid as the one passed in. 072 */ 073 public boolean isLiquidEqual(LiquidStack other) 074 { 075 return other != null && itemID == other.itemID && itemMeta == other.itemMeta; 076 } 077 078 /** 079 * @param other 080 * @return true if this LiquidStack contains the other liquid (liquids are equal and amount >= other.amount). 081 */ 082 public boolean containsLiquid(LiquidStack other) 083 { 084 return isLiquidEqual(other) && amount >= other.amount; 085 } 086 087 /** 088 * @param other ItemStack containing liquids. 089 * @return true if this LiquidStack contains the same liquid as the one passed in. 090 */ 091 public boolean isLiquidEqual(ItemStack other) 092 { 093 if (other == null) 094 { 095 return false; 096 } 097 098 if (itemID == other.itemID && itemMeta == other.getItemDamage()) 099 { 100 return true; 101 } 102 103 return isLiquidEqual(LiquidContainerRegistry.getLiquidForFilledItem(other)); 104 } 105 106 /** 107 * @return ItemStack representation of this LiquidStack 108 */ 109 public ItemStack asItemStack() 110 { 111 return new ItemStack(itemID, 1, itemMeta); 112 } 113 114 /** 115 * Reads a liquid stack from the passed nbttagcompound and returns it. 116 * 117 * @param nbt 118 * @return the liquid stack 119 */ 120 public static LiquidStack loadLiquidStackFromNBT(NBTTagCompound nbt) 121 { 122 LiquidStack liquidstack = new LiquidStack(); 123 liquidstack.readFromNBT(nbt); 124 return liquidstack.itemID == 0 ? null : liquidstack; 125 } 126 127 @SideOnly(CLIENT) 128 private Icon renderingIcon; 129 130 @SideOnly(CLIENT) 131 public Icon getRenderingIcon() 132 { 133 if (itemID == Block.waterStill.blockID) 134 { 135 return BlockFluid.func_94424_b("water"); 136 } 137 else if (itemID == Block.lavaStill.blockID) 138 { 139 return BlockFluid.func_94424_b("lava"); 140 } 141 return renderingIcon; 142 } 143 144 @SideOnly(CLIENT) 145 public void setRenderingIcon(Icon icon) 146 { 147 this.renderingIcon = icon; 148 } 149 150 @Override 151 public final int hashCode() 152 { 153 return 31 * itemID + itemMeta; 154 } 155 156 @Override 157 public final boolean equals(Object ob) 158 { 159 return ob instanceof LiquidStack && Objects.equal(((LiquidStack)ob).itemID, itemID) && Objects.equal(((LiquidStack)ob).itemMeta, itemMeta); 160 } 161}