diff options
Diffstat (limited to 'src/Java/miscutil/enderio/conduit/GregTech/ConduitGtTank.java')
-rw-r--r-- | src/Java/miscutil/enderio/conduit/GregTech/ConduitGtTank.java | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/src/Java/miscutil/enderio/conduit/GregTech/ConduitGtTank.java b/src/Java/miscutil/enderio/conduit/GregTech/ConduitGtTank.java new file mode 100644 index 0000000000..d1a81f32d2 --- /dev/null +++ b/src/Java/miscutil/enderio/conduit/GregTech/ConduitGtTank.java @@ -0,0 +1,153 @@ +package miscutil.enderio.conduit.GregTech; + +import mekanism.api.gas.GasStack; +import mekanism.api.gas.GasTank; +import net.minecraft.nbt.NBTTagCompound; + +public class ConduitGtTank extends GasTank { + + private int capacity; + + ConduitGtTank(int capacity) { + super(capacity); + this.capacity = capacity; + } + + public float getFilledRatio() { + if(getStored() <= 0) { + return 0; + } + if(getMaxGas() <= 0) { + return -1; + } + float res = (float) getStored() / getMaxGas(); + return res; + } + + public boolean isFull() { + return getStored() >= getMaxGas(); + } + + public void setAmount(int amount) { + if(stored != null) { + stored.amount = amount; + } + } + + public int getAvailableSpace() { + return getMaxGas() - getStored(); + } + + public void addAmount(int amount) { + setAmount(getStored() + amount); + } + + @Override + public int getMaxGas() { + return this.capacity; + } + + public void setCapacity(int capacity) { + this.capacity = capacity; + if(getStored() > capacity) { + setAmount(capacity); + } + } + + @Override + public int receive(GasStack resource, boolean doReceive) { + if(resource == null || resource.getGas().getID() < 0) { + return 0; + } + + if(stored == null || stored.getGas().getID() < 0) { + if(resource.amount <= capacity) { + if(doReceive) { + setGas(resource.copy()); + } + return resource.amount; + } else { + if(doReceive) { + stored = resource.copy(); + stored.amount = capacity; + } + return capacity; + } + } + + if(!stored.isGasEqual(resource)) { + return 0; + } + + int space = capacity - stored.amount; + if(resource.amount <= space) { + if(doReceive) { + addAmount(resource.amount); + } + return resource.amount; + } else { + if(doReceive) { + stored.amount = capacity; + } + return space; + } + + } + + @Override + public GasStack draw(int maxDrain, boolean doDraw) { + if(stored == null || stored.getGas().getID() < 0) { + return null; + } + if(stored.amount <= 0) { + return null; + } + + int used = maxDrain; + if(stored.amount < used) { + used = stored.amount; + } + + if(doDraw) { + addAmount(-used); + } + + GasStack drained = new GasStack(stored.getGas().getID(), used); + + if(stored.amount < 0) { + stored.amount = 0; + } + return drained; + } + + public String getGasName() { + return stored != null ? stored.getGas().getLocalizedName() : null; + } + + public boolean containsValidGas() { + return GtUtil.isGasValid(stored); + } + + public NBTTagCompound write(NBTTagCompound nbt) { + if(containsValidGas()) { + stored.write(nbt); + } else { + nbt.setBoolean("emptyGasTank", true); + } + return nbt; + } + + public void read(NBTTagCompound nbt) { + if(!nbt.hasKey("emptyGasTank")) { + GasStack gas = GasStack.readFromNBT(nbt); + if(gas != null) { + setGas(gas); + } + } + } + + public boolean isEmpty() { + return stored == null || stored.amount == 0; + } + +} |