mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-22 05:02:25 +00:00
Refactored Tank Classes
This commit is contained in:
1 parent
23a8a8b312
commit
8ecc703cdb
13 files changed
+206
-197
No files matched your search
@@ -1,7 +1,9 @@
|
||||
package crazypants.enderio.machine.generator.zombie;
|
||||
|
||||
import crazypants.enderio.tool.SmartTank;
|
||||
|
||||
public interface IHasNutrientTank {
|
||||
|
||||
NutrientTank getNutrientTank();
|
||||
SmartTank getNutrientTank();
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package crazypants.enderio.machine.generator.zombie;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
import crazypants.enderio.EnderIO;
|
||||
|
||||
public class NutrientTank extends FluidTank {
|
||||
|
||||
public NutrientTank(int capacity) {
|
||||
super(capacity);
|
||||
}
|
||||
|
||||
public boolean canFill(Fluid fluid) {
|
||||
return fluid != null && fluid.getID() == EnderIO.fluidNutrientDistillation.getID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill) {
|
||||
if(resource == null || !canFill(resource.getFluid())) {
|
||||
return 0;
|
||||
}
|
||||
return super.fill(resource, doFill);
|
||||
}
|
||||
|
||||
public float getFilledRatio() {
|
||||
return (float) getFluidAmount() / getCapacity();
|
||||
}
|
||||
|
||||
public void setFluidAmount(int amount) {
|
||||
if(amount > 0) {
|
||||
setFluid(new FluidStack(EnderIO.fluidNutrientDistillation, Math.min(capacity, amount)));
|
||||
} else {
|
||||
setFluid(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeCommon(String name, NBTTagCompound nbtRoot) {
|
||||
if(getFluidAmount() > 0) {
|
||||
NBTTagCompound tankRoot = new NBTTagCompound();
|
||||
writeToNBT(tankRoot);
|
||||
nbtRoot.setTag(name, tankRoot);
|
||||
}
|
||||
}
|
||||
|
||||
public void readCommon(String name, NBTTagCompound nbtRoot) {
|
||||
NBTTagCompound tankRoot = (NBTTagCompound) nbtRoot.getTag(name);
|
||||
if(tankRoot != null) {
|
||||
readFromNBT(tankRoot);
|
||||
} else {
|
||||
setFluid(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import crazypants.enderio.machine.SlotDefinition;
|
||||
import crazypants.enderio.machine.generator.AbstractGeneratorEntity;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import crazypants.enderio.power.PowerDistributor;
|
||||
import crazypants.enderio.tool.SmartTank;
|
||||
import crazypants.util.BlockCoord;
|
||||
import crazypants.util.FluidUtil;
|
||||
import crazypants.util.ITankAccess;
|
||||
@@ -26,7 +27,7 @@ public class TileZombieGenerator extends AbstractGeneratorEntity implements IFlu
|
||||
|
||||
private static int IO_MB_TICK = 250;
|
||||
|
||||
final NutrientTank fuelTank = new NutrientTank(FluidContainerRegistry.BUCKET_VOLUME * 2);
|
||||
final SmartTank fuelTank = new SmartTank(EnderIO.fluidNutrientDistillation, FluidContainerRegistry.BUCKET_VOLUME * 2);
|
||||
|
||||
int outputPerTick = Config.zombieGeneratorRfPerTick;
|
||||
int tickPerBucketOfFuel = Config.zombieGeneratorTicksPerBucketFuel;
|
||||
@@ -274,7 +275,7 @@ public class TileZombieGenerator extends AbstractGeneratorEntity implements IFlu
|
||||
}
|
||||
|
||||
@Override
|
||||
public NutrientTank getNutrientTank() {
|
||||
public SmartTank getNutrientTank() {
|
||||
return fuelTank;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ import crazypants.enderio.gui.MultiIconButtonEIO;
|
||||
import crazypants.enderio.gui.TextFieldEIO;
|
||||
import crazypants.enderio.gui.TooltipAddera;
|
||||
import crazypants.enderio.gui.VScrollbarEIO;
|
||||
import crazypants.enderio.machine.generator.zombie.NutrientTank;
|
||||
import crazypants.enderio.machine.gui.GuiMachineBase;
|
||||
import crazypants.enderio.machine.invpanel.client.DatabaseView;
|
||||
import crazypants.enderio.machine.invpanel.client.ICraftingHelper;
|
||||
@@ -32,6 +31,7 @@ import crazypants.enderio.machine.invpanel.client.InventoryDatabaseClient;
|
||||
import crazypants.enderio.machine.invpanel.client.ItemEntry;
|
||||
import crazypants.enderio.machine.invpanel.client.SortOrder;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import crazypants.enderio.tool.SmartTank;
|
||||
import crazypants.gui.GhostSlot;
|
||||
import crazypants.gui.GuiToolTip;
|
||||
import crazypants.render.RenderUtil;
|
||||
@@ -198,7 +198,7 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
|
||||
fr.drawString(headerInventory, sx + 38, sy + 120, headerColor);
|
||||
|
||||
TileInventoryPanel te = getTileEntity();
|
||||
NutrientTank fuelTank = te.fuelTank;
|
||||
SmartTank fuelTank = te.fuelTank;
|
||||
if(fuelTank.getFluidAmount() > 0) {
|
||||
RenderUtil.renderGuiTank(fuelTank.getFluid(), fuelTank.getCapacity(), fuelTank.getFluidAmount(), sx + 12, sy + 132, zLevel, 16, 47);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
import net.minecraftforge.fluids.FluidTankInfo;
|
||||
import net.minecraftforge.fluids.IFluidHandler;
|
||||
import crazypants.enderio.EnderIO;
|
||||
import crazypants.enderio.ModObject;
|
||||
import crazypants.enderio.conduit.TileConduitBundle;
|
||||
import crazypants.enderio.conduit.item.FilterRegister;
|
||||
@@ -20,12 +21,12 @@ import crazypants.enderio.config.Config;
|
||||
import crazypants.enderio.machine.AbstractMachineEntity;
|
||||
import crazypants.enderio.machine.SlotDefinition;
|
||||
import crazypants.enderio.machine.generator.zombie.IHasNutrientTank;
|
||||
import crazypants.enderio.machine.generator.zombie.NutrientTank;
|
||||
import crazypants.enderio.machine.generator.zombie.PacketNutrientTank;
|
||||
import crazypants.enderio.machine.invpanel.client.ClientDatabaseManager;
|
||||
import crazypants.enderio.machine.invpanel.client.InventoryDatabaseClient;
|
||||
import crazypants.enderio.machine.invpanel.server.InventoryDatabaseServer;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import crazypants.enderio.tool.SmartTank;
|
||||
import crazypants.util.ITankAccess;
|
||||
|
||||
public class TileInventoryPanel extends AbstractMachineEntity implements IFluidHandler, ITankAccess, IHasNutrientTank {
|
||||
@@ -35,7 +36,7 @@ public class TileInventoryPanel extends AbstractMachineEntity implements IFluidH
|
||||
public static final int SLOT_VIEW_FILTER = 10;
|
||||
public static final int SLOT_RETURN_START = 11;
|
||||
|
||||
protected final NutrientTank fuelTank;
|
||||
protected final SmartTank fuelTank;
|
||||
protected boolean tanksDirty;
|
||||
|
||||
private InventoryDatabaseServer dbServer;
|
||||
@@ -48,7 +49,7 @@ public class TileInventoryPanel extends AbstractMachineEntity implements IFluidH
|
||||
|
||||
public TileInventoryPanel() {
|
||||
super(new SlotDefinition(0, 8, 11, 20, 21, 20));
|
||||
this.fuelTank = new NutrientTank(2000);
|
||||
this.fuelTank = new SmartTank(EnderIO.fluidNutrientDistillation, 2000);
|
||||
}
|
||||
|
||||
public InventoryDatabaseServer getDatabaseServer() {
|
||||
@@ -294,7 +295,7 @@ public class TileInventoryPanel extends AbstractMachineEntity implements IFluidH
|
||||
}
|
||||
|
||||
@Override
|
||||
public NutrientTank getNutrientTank() {
|
||||
public SmartTank getNutrientTank() {
|
||||
return fuelTank;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,10 +34,10 @@ import crazypants.enderio.config.Config;
|
||||
import crazypants.enderio.machine.AbstractMachineEntity;
|
||||
import crazypants.enderio.machine.SlotDefinition;
|
||||
import crazypants.enderio.machine.generator.zombie.IHasNutrientTank;
|
||||
import crazypants.enderio.machine.generator.zombie.NutrientTank;
|
||||
import crazypants.enderio.machine.generator.zombie.PacketNutrientTank;
|
||||
import crazypants.enderio.machine.wireless.WirelessChargedLocation;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import crazypants.enderio.tool.SmartTank;
|
||||
import crazypants.enderio.xp.ExperienceContainer;
|
||||
import crazypants.enderio.xp.IHaveExperience;
|
||||
import crazypants.enderio.xp.PacketExperianceContainer;
|
||||
@@ -63,7 +63,7 @@ public class TileKillerJoe extends AbstractMachineEntity implements IFluidHandle
|
||||
|
||||
protected WirelessChargedLocation chargedLocation;
|
||||
|
||||
final NutrientTank fuelTank = new NutrientTank(FluidContainerRegistry.BUCKET_VOLUME * 2);
|
||||
final SmartTank fuelTank = new SmartTank(EnderIO.fluidNutrientDistillation, FluidContainerRegistry.BUCKET_VOLUME * 2);
|
||||
|
||||
int lastFluidLevelUpdate;
|
||||
|
||||
@@ -569,7 +569,7 @@ public class TileKillerJoe extends AbstractMachineEntity implements IFluidHandle
|
||||
}
|
||||
|
||||
@Override
|
||||
public NutrientTank getNutrientTank() {
|
||||
public SmartTank getNutrientTank() {
|
||||
return fuelTank;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
@@ -170,7 +171,7 @@ public class ReservoirRenderer extends TileEntitySpecialRenderer implements IRes
|
||||
|
||||
private IIcon getLiquidTexture() {
|
||||
if(tex == null) {
|
||||
tex = ReservoirTank.WATER.getFluid().getStillIcon();
|
||||
tex = FluidRegistry.WATER.getStillIcon();
|
||||
}
|
||||
return tex;
|
||||
}
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
package crazypants.enderio.machine.reservoir;
|
||||
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
|
||||
public class ReservoirTank extends FluidTank {
|
||||
|
||||
static final FluidStack WATER = FluidRegistry.getFluidStack("water", 0);
|
||||
|
||||
ReservoirTank(int quantity, int capacity) {
|
||||
super(WATER.getFluid(), quantity, capacity);
|
||||
}
|
||||
|
||||
ReservoirTank(int capacity) {
|
||||
this(0, capacity);
|
||||
}
|
||||
|
||||
ReservoirTank(FluidStack liquid, int capacity) {
|
||||
super(liquid, capacity);
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return getFluid().amount;
|
||||
}
|
||||
|
||||
public float getFilledRatio() {
|
||||
return (float) getAmount() / getCapacity();
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
return getAmount() >= getCapacity();
|
||||
}
|
||||
|
||||
public void setAmount(int amount) {
|
||||
FluidStack newLiquid = WATER.copy();
|
||||
newLiquid.amount = Math.min(getCapacity(), amount);
|
||||
setFluid(newLiquid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack getFluid() {
|
||||
FluidStack l = super.getFluid();
|
||||
if(l == null) {
|
||||
l = WATER.copy();
|
||||
setFluid(l);
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
public int getAvailableSpace() {
|
||||
return getCapacity() - getAmount();
|
||||
}
|
||||
|
||||
public void addAmount(int amount) {
|
||||
setAmount(getAmount() + amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCapacity(int capacity) {
|
||||
super.setCapacity(capacity);
|
||||
if(getAmount() > capacity) {
|
||||
setAmount(capacity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import net.minecraftforge.fluids.FluidTankInfo;
|
||||
import net.minecraftforge.fluids.IFluidHandler;
|
||||
import crazypants.enderio.EnderIO;
|
||||
import crazypants.enderio.TileEntityEio;
|
||||
import crazypants.enderio.tool.SmartTank;
|
||||
import crazypants.render.BoundingBox;
|
||||
import crazypants.util.BlockCoord;
|
||||
import crazypants.util.ITankAccess;
|
||||
@@ -66,9 +67,9 @@ public class TileReservoir extends TileEntityEio implements IFluidHandler, ITank
|
||||
// Position within multiblock
|
||||
Pos pos = Pos.UNKNOWN;
|
||||
|
||||
ReservoirTank tank = new ReservoirTank(BUCKET_VOLUME);
|
||||
SmartTank tank = new SmartTank(FluidRegistry.WATER, BUCKET_VOLUME);
|
||||
|
||||
ReservoirTank regenTank = null;
|
||||
SmartTank regenTank = null;
|
||||
|
||||
boolean autoEject;
|
||||
|
||||
@@ -105,8 +106,8 @@ public class TileReservoir extends TileEntityEio implements IFluidHandler, ITank
|
||||
if(autoEject && neighboursDirty) {
|
||||
doUpdateTankNeighbours();
|
||||
}
|
||||
if(autoEject && tankNeighbours != null && !tankNeighbours.isEmpty() && tank.getAmount() > 0) {
|
||||
int ejectable = tank.getAmount();
|
||||
if(autoEject && tankNeighbours != null && !tankNeighbours.isEmpty() && tank.getFluidAmount() > 0) {
|
||||
int ejectable = tank.getFluidAmount();
|
||||
int amountPerNeighbour = ejectable / tankNeighbours.size();
|
||||
FluidStack source = WATER_BUCKET.copy();
|
||||
int used = 0;
|
||||
@@ -256,13 +257,14 @@ public class TileReservoir extends TileEntityEio implements IFluidHandler, ITank
|
||||
if(liquid != null) {
|
||||
tank.setFluid(liquid);
|
||||
} else {
|
||||
tank.setAmount(0);
|
||||
tank.setFluidAmount(0);
|
||||
}
|
||||
|
||||
if(regenLiquid == null) {
|
||||
regenTank = null;
|
||||
} else {
|
||||
regenTank = new ReservoirTank(regenLiquid, BUCKET_VOLUME * 2);
|
||||
regenTank = new SmartTank(FluidRegistry.WATER, BUCKET_VOLUME * 2);
|
||||
regenTank.setFluidAmount(regenLiquid.amount);
|
||||
}
|
||||
|
||||
boolean wasMulti = isMultiblock();
|
||||
@@ -416,19 +418,19 @@ public class TileReservoir extends TileEntityEio implements IFluidHandler, ITank
|
||||
|
||||
if(isMaster()) {
|
||||
|
||||
regenTank = new ReservoirTank(BUCKET_VOLUME * 2);
|
||||
regenTank = new SmartTank(FluidRegistry.WATER, BUCKET_VOLUME * 2);
|
||||
tank.setCapacity(BUCKET_VOLUME * 2);
|
||||
for (BlockCoord bc : multiblock) {
|
||||
TileReservoir res = getReservoir(bc);
|
||||
if(res != null) {
|
||||
FluidStack drained = res.doDrain(ForgeDirection.UNKNOWN, regenTank.getAvailableSpace(), true);
|
||||
if(drained != null) {
|
||||
regenTank.addAmount(drained.amount);
|
||||
regenTank.addFluidAmount(drained.amount);
|
||||
}
|
||||
// incase regen tank is full, add to normal tank
|
||||
drained = res.doDrain(ForgeDirection.UNKNOWN, tank.getAvailableSpace(), true);
|
||||
if(drained != null) {
|
||||
tank.addAmount(drained.amount);
|
||||
tank.addFluidAmount(drained.amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -681,10 +683,10 @@ public class TileReservoir extends TileEntityEio implements IFluidHandler, ITank
|
||||
@Override
|
||||
public void setTanksDirty() {
|
||||
if (isMaster() && regenTank != null && tank != null) {
|
||||
if (!regenTank.isFull() && tank.getAmount() > 0) {
|
||||
int toMove = Math.min(tank.getAmount(), regenTank.getCapacity() - regenTank.getAmount());
|
||||
regenTank.setAmount(regenTank.getAmount() + toMove);
|
||||
tank.setAmount(tank.getAmount() - toMove);
|
||||
if (!regenTank.isFull() && tank.getFluidAmount() > 0) {
|
||||
int toMove = Math.min(tank.getFluidAmount(), regenTank.getCapacity() - regenTank.getFluidAmount());
|
||||
regenTank.setFluidAmount(regenTank.getFluidAmount() + toMove);
|
||||
tank.setFluidAmount(tank.getFluidAmount() - toMove);
|
||||
}
|
||||
}
|
||||
tankDirty = true;
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package crazypants.enderio.machine.tank;
|
||||
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
|
||||
public class FluidTankEio extends FluidTank {
|
||||
|
||||
public FluidTankEio(int capacity) {
|
||||
super(capacity);
|
||||
}
|
||||
|
||||
public float getFilledRatio() {
|
||||
return (float) getFluidAmount() / getCapacity();
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
return getFluidAmount() >= getCapacity();
|
||||
}
|
||||
|
||||
public boolean canDrainFluidType(FluidStack resource) {
|
||||
if(resource == null || resource.getFluid() == null || fluid == null) {
|
||||
return false;
|
||||
}
|
||||
return fluid.isFluidEqual(resource);
|
||||
}
|
||||
|
||||
public boolean canDrainFluidType(Fluid fl) {
|
||||
if(fl == null || fluid == null) {
|
||||
return false;
|
||||
}
|
||||
return fl.getID() == fluid.fluidID;
|
||||
}
|
||||
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain) {
|
||||
if(!canDrainFluidType(resource)) {
|
||||
return null;
|
||||
}
|
||||
return drain(resource.amount, doDrain);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import org.lwjgl.opengl.GL11;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import crazypants.enderio.tool.SmartTank;
|
||||
import crazypants.render.BoundingBox;
|
||||
import crazypants.render.CubeRenderer;
|
||||
import crazypants.render.RenderUtil;
|
||||
@@ -26,7 +27,7 @@ public class TankFluidRenderer extends TileEntitySpecialRenderer {
|
||||
renderTankFluid(tank.tank, (float)x, (float)y, (float)z);
|
||||
}
|
||||
|
||||
public static void renderTankFluid(FluidTankEio tank, float x, float y, float z) {
|
||||
public static void renderTankFluid(SmartTank tank, float x, float y, float z) {
|
||||
if(tank == null || tank.getFluid() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import crazypants.enderio.machine.AbstractMachineEntity;
|
||||
import crazypants.enderio.machine.IoMode;
|
||||
import crazypants.enderio.machine.SlotDefinition;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import crazypants.enderio.tool.SmartTank;
|
||||
import crazypants.util.BlockCoord;
|
||||
import crazypants.util.FluidUtil;
|
||||
import crazypants.util.ITankAccess;
|
||||
@@ -28,7 +29,7 @@ public class TileTank extends AbstractMachineEntity implements IFluidHandler, IT
|
||||
|
||||
private static int IO_MB_TICK = 100;
|
||||
|
||||
protected FluidTankEio tank;// = new FluidTankEio(16000);
|
||||
protected SmartTank tank;// = new FluidTankEio(16000);
|
||||
protected int lastUpdateLevel = -1;
|
||||
|
||||
private boolean tankDirty = false;
|
||||
@@ -37,9 +38,9 @@ public class TileTank extends AbstractMachineEntity implements IFluidHandler, IT
|
||||
public TileTank(int meta) {
|
||||
super(new SlotDefinition(0, 1, 2, 3, -1, -1));
|
||||
if(meta == 1) {
|
||||
tank = new FluidTankEio(32000);
|
||||
tank = new SmartTank(32000);
|
||||
} else {
|
||||
tank = new FluidTankEio(16000);
|
||||
tank = new SmartTank(16000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,9 +347,9 @@ public class TileTank extends AbstractMachineEntity implements IFluidHandler, IT
|
||||
int tankType = nbtRoot.getInteger("tankType");
|
||||
tankType = MathHelper.clamp_int(tankType, 0, 1);
|
||||
if(tankType == 1) {
|
||||
tank = new FluidTankEio(32000);
|
||||
tank = new SmartTank(32000);
|
||||
} else {
|
||||
tank = new FluidTankEio(16000);
|
||||
tank = new SmartTank(16000);
|
||||
}
|
||||
|
||||
if(nbtRoot.hasKey("tankContents")) {
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
package crazypants.enderio.tool;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import crazypants.enderio.EnderIO;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
|
||||
public class SmartTank extends FluidTank {
|
||||
|
||||
protected Fluid restriction;
|
||||
|
||||
public SmartTank(FluidStack liquid, int capacity) {
|
||||
super(liquid, capacity);
|
||||
if (liquid != null) {
|
||||
restriction = liquid.getFluid();
|
||||
} else {
|
||||
restriction = null;
|
||||
}
|
||||
}
|
||||
|
||||
public SmartTank(int capacity) {
|
||||
super(capacity);
|
||||
}
|
||||
|
||||
public SmartTank(Fluid liquid, int capacity) {
|
||||
super(capacity);
|
||||
restriction = liquid;
|
||||
}
|
||||
|
||||
public void setRestriction(Fluid restriction) {
|
||||
this.restriction = restriction;
|
||||
}
|
||||
|
||||
public float getFilledRatio() {
|
||||
return (float) getFluidAmount() / getCapacity();
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
return getFluidAmount() >= getCapacity();
|
||||
}
|
||||
|
||||
public boolean canDrainFluidType(FluidStack resource) {
|
||||
if(resource == null || resource.getFluid() == null || fluid == null) {
|
||||
return false;
|
||||
}
|
||||
return fluid.isFluidEqual(resource);
|
||||
}
|
||||
|
||||
public boolean canDrainFluidType(Fluid fl) {
|
||||
if(fl == null || fluid == null) {
|
||||
return false;
|
||||
}
|
||||
return fl.getID() == fluid.fluidID;
|
||||
}
|
||||
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain) {
|
||||
if(!canDrainFluidType(resource)) {
|
||||
return null;
|
||||
}
|
||||
return drain(resource.amount, doDrain);
|
||||
}
|
||||
|
||||
public boolean canFill(FluidStack resource) {
|
||||
if (fluid != null) {
|
||||
return fluid.isFluidEqual(resource);
|
||||
} else if (restriction != null) {
|
||||
return resource.getFluid() != null && restriction.getID() == resource.getFluid().getID();
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canFill(Fluid fl) {
|
||||
if (fluid != null) {
|
||||
return fluid.getFluid().getID() == fl.getID();
|
||||
} else if (restriction != null) {
|
||||
return restriction.getID() == fl.getID();
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void setFluidAmount(int amount) {
|
||||
if(amount > 0) {
|
||||
if (fluid != null) {
|
||||
fluid.amount = Math.min(capacity, amount);
|
||||
} else if (restriction != null) {
|
||||
setFluid(new FluidStack(restriction, Math.min(capacity, amount)));
|
||||
} else {
|
||||
throw new RuntimeException("Cannot set fluid amount of an empty tank");
|
||||
}
|
||||
} else {
|
||||
setFluid(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill) {
|
||||
if(!canFill(resource)) {
|
||||
return 0;
|
||||
}
|
||||
return super.fill(resource, doFill);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack getFluid() {
|
||||
if (fluid != null) {
|
||||
return fluid;
|
||||
} else if (restriction != null) {
|
||||
return new FluidStack(restriction, 0);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public int getAvailableSpace() {
|
||||
return getCapacity() - getFluidAmount();
|
||||
}
|
||||
|
||||
public void addFluidAmount(int amount) {
|
||||
setFluidAmount(getFluidAmount() + amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCapacity(int capacity) {
|
||||
super.setCapacity(capacity);
|
||||
if(getFluidAmount() > capacity) {
|
||||
setFluidAmount(capacity);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeCommon(String name, NBTTagCompound nbtRoot) {
|
||||
if(getFluidAmount() > 0 || restriction != null) {
|
||||
NBTTagCompound tankRoot = new NBTTagCompound();
|
||||
writeToNBT(tankRoot);
|
||||
if (restriction != null) {
|
||||
tankRoot.setString("FluidRestriction", FluidRegistry.getFluidName(restriction.getID()));
|
||||
}
|
||||
nbtRoot.setTag(name, tankRoot);
|
||||
} else {
|
||||
nbtRoot.removeTag(name);
|
||||
}
|
||||
}
|
||||
|
||||
public void readCommon(String name, NBTTagCompound nbtRoot) {
|
||||
NBTTagCompound tankRoot = (NBTTagCompound) nbtRoot.getTag(name);
|
||||
if(tankRoot != null) {
|
||||
readFromNBT(tankRoot);
|
||||
restriction = null;
|
||||
if (tankRoot.hasKey("FluidRestriction")) {
|
||||
String fluidName = tankRoot.getString("FluidRestriction");
|
||||
if (!Strings.isNullOrEmpty(fluidName)) {
|
||||
restriction = FluidRegistry.getFluid(fluidName);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
setFluid(null);
|
||||
// not reseting 'restriction' here on purpose
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user