001package net.minecraftforge.liquids; 002 003import net.minecraft.nbt.NBTTagCompound; 004import net.minecraft.tileentity.TileEntity; 005 006/** 007 * Reference implementation of ILiquidTank. Use this or implement your own. 008 */ 009public class LiquidTank implements ILiquidTank { 010 private LiquidStack liquid; 011 private int capacity; 012 private int tankPressure; 013 private TileEntity tile; 014 015 public LiquidTank(int capacity) 016 { 017 this(null, capacity); 018 } 019 020 public LiquidTank(int liquidId, int quantity, int capacity) 021 { 022 this(new LiquidStack(liquidId, quantity), capacity); 023 } 024 025 public LiquidTank(int liquidId, int quantity, int capacity, TileEntity tile) 026 { 027 this(liquidId, quantity, capacity); 028 this.tile = tile; 029 } 030 031 public LiquidTank(LiquidStack liquid, int capacity) 032 { 033 this.liquid = liquid; 034 this.capacity = capacity; 035 } 036 037 public LiquidTank(LiquidStack liquid, int capacity, TileEntity tile) 038 { 039 this(liquid, capacity); 040 this.tile = tile; 041 } 042 043 @Override 044 public LiquidStack getLiquid() 045 { 046 return this.liquid; 047 } 048 049 @Override 050 public int getCapacity() 051 { 052 return this.capacity; 053 } 054 055 public void setLiquid(LiquidStack liquid) 056 { 057 this.liquid = liquid; 058 } 059 060 public void setCapacity(int capacity) 061 { 062 this.capacity = capacity; 063 } 064 065 @Override 066 public int fill(LiquidStack resource, boolean doFill) 067 { 068 if (resource == null || resource.itemID <= 0) return 0; 069 070 if (liquid == null || liquid.itemID <= 0) 071 { 072 if (resource.amount <= capacity) 073 { 074 if (doFill) this.liquid = resource.copy(); 075 return resource.amount; 076 } 077 else 078 { 079 if (doFill) 080 { 081 this.liquid = resource.copy(); 082 this.liquid.amount = capacity; 083 if (tile != null) 084 LiquidEvent.fireEvent(new LiquidEvent.LiquidFillingEvent(liquid, tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord, this)); 085 } 086 return capacity; 087 } 088 } 089 090 if (!liquid.isLiquidEqual(resource)) return 0; 091 092 int space = capacity - liquid.amount; 093 if (resource.amount <= space) 094 { 095 if (doFill) this.liquid.amount += resource.amount; 096 return resource.amount; 097 } 098 else 099 { 100 101 if (doFill) this.liquid.amount = capacity; 102 return space; 103 } 104 105 } 106 107 @Override 108 public LiquidStack drain(int maxDrain, boolean doDrain) 109 { 110 if (liquid == null || liquid.itemID <= 0) return null; 111 if (liquid.amount <= 0) return null; 112 113 int used = maxDrain; 114 if (liquid.amount < used) used = liquid.amount; 115 116 if (doDrain) 117 { 118 liquid.amount -= used; 119 } 120 121 LiquidStack drained = new LiquidStack(liquid.itemID, used, liquid.itemMeta); 122 123 // Reset liquid if emptied 124 if (liquid.amount <= 0) liquid = null; 125 126 if (doDrain && tile != null) 127 LiquidEvent.fireEvent(new LiquidEvent.LiquidDrainingEvent(drained, tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord, this)); 128 129 return drained; 130 } 131 132 @Override 133 public int getTankPressure() 134 { 135 return tankPressure; 136 } 137 138 public void setTankPressure(int pressure) 139 { 140 this.tankPressure = pressure; 141 } 142 143 144 public String getLiquidName() 145 { 146 return liquid!= null ? LiquidDictionary.findLiquidName(liquid) : null; 147 } 148 149 public boolean containsValidLiquid() 150 { 151 return LiquidDictionary.findLiquidName(liquid) != null; 152 } 153 154 155 public NBTTagCompound writeToNBT(NBTTagCompound nbt) 156 { 157 if (containsValidLiquid()) 158 { 159 liquid.writeToNBT(nbt); 160 } 161 else 162 { 163 nbt.setString("emptyTank", ""); 164 } 165 return nbt; 166 } 167 168 public LiquidTank readFromNBT(NBTTagCompound nbt) 169 { 170 if (!nbt.hasKey("emptyTank")) 171 { 172 LiquidStack liquid = LiquidStack.loadLiquidStackFromNBT(nbt); 173 if (liquid != null) 174 { 175 setLiquid(liquid); 176 } 177 } 178 return this; 179 } 180}