mirror of
https://github.com/HbmMods/Hbm-s-Nuclear-Tech-GIT.git
synced 2026-01-25 10:32:49 +00:00
276 lines
7.2 KiB
Java
276 lines
7.2 KiB
Java
package com.hbm.tileentity.machine.oil;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import com.hbm.interfaces.IFluidAcceptor;
|
|
import com.hbm.interfaces.IFluidSource;
|
|
import com.hbm.inventory.FluidStack;
|
|
import com.hbm.inventory.fluid.FluidType;
|
|
import com.hbm.inventory.fluid.Fluids;
|
|
import com.hbm.inventory.fluid.tank.FluidTank;
|
|
import com.hbm.inventory.recipes.RefineryRecipes;
|
|
import com.hbm.lib.Library;
|
|
import com.hbm.tileentity.INBTPacketReceiver;
|
|
import com.hbm.util.Tuple.Pair;
|
|
import com.hbm.util.fauxpointtwelve.DirPos;
|
|
|
|
import api.hbm.fluid.IFluidStandardTransceiver;
|
|
import cpw.mods.fml.relauncher.Side;
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.tileentity.TileEntity;
|
|
import net.minecraft.util.AxisAlignedBB;
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
public class TileEntityMachineFractionTower extends TileEntity implements IFluidSource, IFluidAcceptor, INBTPacketReceiver, IFluidStandardTransceiver {
|
|
|
|
public FluidTank[] tanks;
|
|
public List<IFluidAcceptor> list1 = new ArrayList();
|
|
public List<IFluidAcceptor> list2 = new ArrayList();
|
|
|
|
public TileEntityMachineFractionTower() {
|
|
tanks = new FluidTank[3];
|
|
tanks[0] = new FluidTank(Fluids.HEAVYOIL, 4000, 0);
|
|
tanks[1] = new FluidTank(Fluids.BITUMEN, 4000, 1);
|
|
tanks[2] = new FluidTank(Fluids.SMEAR, 4000, 2);
|
|
}
|
|
|
|
@Override
|
|
public void updateEntity() {
|
|
|
|
if(!worldObj.isRemote) {
|
|
|
|
TileEntity stack = worldObj.getTileEntity(xCoord, yCoord + 3, zCoord);
|
|
|
|
if(stack instanceof TileEntityMachineFractionTower) {
|
|
TileEntityMachineFractionTower frac = (TileEntityMachineFractionTower) stack;
|
|
|
|
//make types equal
|
|
for(int i = 0; i < 3; i++) {
|
|
frac.tanks[i].setTankType(tanks[i].getTankType());
|
|
}
|
|
|
|
//calculate transfer
|
|
int oil = Math.min(tanks[0].getFill(), frac.tanks[0].getMaxFill() - frac.tanks[0].getFill());
|
|
int left = Math.min(frac.tanks[1].getFill(), tanks[1].getMaxFill() - tanks[1].getFill());
|
|
int right = Math.min(frac.tanks[2].getFill(), tanks[2].getMaxFill() - tanks[2].getFill());
|
|
|
|
//move oil up, pull fractions down
|
|
tanks[0].setFill(tanks[0].getFill() - oil);
|
|
tanks[1].setFill(tanks[1].getFill() + left);
|
|
tanks[2].setFill(tanks[2].getFill() + right);
|
|
frac.tanks[0].setFill(frac.tanks[0].getFill() + oil);
|
|
frac.tanks[1].setFill(frac.tanks[1].getFill() - left);
|
|
frac.tanks[2].setFill(frac.tanks[2].getFill() - right);
|
|
}
|
|
|
|
setupTanks();
|
|
this.updateConnections();
|
|
|
|
if(worldObj.getTotalWorldTime() % 20 == 0)
|
|
fractionate();
|
|
|
|
if(worldObj.getTotalWorldTime() % 10 == 0) {
|
|
fillFluidInit(tanks[1].getTankType());
|
|
fillFluidInit(tanks[2].getTankType());
|
|
}
|
|
|
|
this.sendFluid();
|
|
|
|
NBTTagCompound data = new NBTTagCompound();
|
|
|
|
for(int i = 0; i < 3; i++)
|
|
tanks[i].writeToNBT(data, "tank" + i);
|
|
|
|
INBTPacketReceiver.networkPack(this, data, 50);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void networkUnpack(NBTTagCompound nbt) {
|
|
for(int i = 0; i < 3; i++)
|
|
tanks[i].readFromNBT(nbt, "tank" + i);
|
|
}
|
|
|
|
private void updateConnections() {
|
|
|
|
for(DirPos pos : getConPos()) {
|
|
this.trySubscribe(tanks[0].getTankType(), worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir());
|
|
}
|
|
}
|
|
|
|
private void sendFluid() {
|
|
|
|
for(DirPos pos : getConPos()) {
|
|
this.sendFluid(tanks[1].getTankType(), worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir());
|
|
this.sendFluid(tanks[2].getTankType(), worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir());
|
|
}
|
|
}
|
|
|
|
private DirPos[] getConPos() {
|
|
return new DirPos[] {
|
|
new DirPos(xCoord + 2, yCoord, zCoord, Library.POS_X),
|
|
new DirPos(xCoord - 2, yCoord, zCoord, Library.NEG_X),
|
|
new DirPos(xCoord, yCoord, zCoord + 2, Library.POS_Z),
|
|
new DirPos(xCoord, yCoord, zCoord - 2, Library.NEG_Z)
|
|
};
|
|
}
|
|
|
|
private void setupTanks() {
|
|
|
|
Pair<FluidStack, FluidStack> quart = RefineryRecipes.getFractions(tanks[0].getTankType());
|
|
|
|
if(quart != null) {
|
|
tanks[1].setTankType(quart.getKey().type);
|
|
tanks[2].setTankType(quart.getValue().type);
|
|
} else {
|
|
tanks[0].setTankType(Fluids.NONE);
|
|
tanks[1].setTankType(Fluids.NONE);
|
|
tanks[2].setTankType(Fluids.NONE);
|
|
}
|
|
}
|
|
|
|
private void fractionate() {
|
|
|
|
Pair<FluidStack, FluidStack> quart = RefineryRecipes.getFractions(tanks[0].getTankType());
|
|
|
|
if(quart != null) {
|
|
|
|
int left = quart.getKey().fill;
|
|
int right = quart.getValue().fill;
|
|
|
|
if(tanks[0].getFill() >= 100 && hasSpace(left, right)) {
|
|
tanks[0].setFill(tanks[0].getFill() - 100);
|
|
tanks[1].setFill(tanks[1].getFill() + left);
|
|
tanks[2].setFill(tanks[2].getFill() + right);
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean hasSpace(int left, int right) {
|
|
return tanks[1].getFill() + left <= tanks[1].getMaxFill() && tanks[2].getFill() + right <= tanks[2].getMaxFill();
|
|
}
|
|
|
|
@Override
|
|
public void readFromNBT(NBTTagCompound nbt) {
|
|
super.readFromNBT(nbt);
|
|
|
|
for(int i = 0; i < 3; i++)
|
|
tanks[i].readFromNBT(nbt, "tank" + i);
|
|
}
|
|
|
|
@Override
|
|
public void writeToNBT(NBTTagCompound nbt) {
|
|
super.writeToNBT(nbt);
|
|
|
|
for(int i = 0; i < 3; i++)
|
|
tanks[i].writeToNBT(nbt, "tank" + i);
|
|
}
|
|
|
|
@Override
|
|
public void setFillForSync(int fill, int index) {
|
|
if(index < 3 && tanks[index] != null)
|
|
tanks[index].setFill(fill);
|
|
}
|
|
|
|
@Override
|
|
public void setFluidFill(int fill, FluidType type) {
|
|
for(FluidTank tank : tanks) {
|
|
if(tank.getTankType() == type) {
|
|
tank.setFill(fill);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void setTypeForSync(FluidType type, int index) {
|
|
this.tanks[index].setTankType(type);
|
|
}
|
|
|
|
@Override
|
|
public int getFluidFill(FluidType type) {
|
|
for(FluidTank tank : tanks) {
|
|
if(tank.getTankType() == type) {
|
|
return tank.getFill();
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public int getMaxFluidFill(FluidType type) {
|
|
if(type == tanks[0].getTankType())
|
|
return tanks[0].getMaxFill();
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public void fillFluidInit(FluidType type) {
|
|
|
|
for(int i = 2; i < 6; i++) {
|
|
ForgeDirection dir = ForgeDirection.getOrientation(i);
|
|
fillFluid(xCoord + dir.offsetX * 2, yCoord, zCoord + dir.offsetZ * 2, this.getTact(), type);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void fillFluid(int x, int y, int z, boolean newTact, FluidType type) {
|
|
Library.transmitFluid(x, y, z, newTact, this, worldObj, type);
|
|
}
|
|
|
|
@Override
|
|
public boolean getTact() {
|
|
return worldObj.getTotalWorldTime() % 20 < 10;
|
|
}
|
|
|
|
@Override
|
|
public List<IFluidAcceptor> getFluidList(FluidType type) {
|
|
if(type == tanks[1].getTankType()) return list1;
|
|
if(type == tanks[2].getTankType()) return list2;
|
|
return new ArrayList<IFluidAcceptor>();
|
|
}
|
|
|
|
@Override
|
|
public void clearFluidList(FluidType type) {
|
|
if(type == tanks[1].getTankType()) list1.clear();
|
|
if(type == tanks[2].getTankType()) list2.clear();
|
|
}
|
|
|
|
AxisAlignedBB bb = null;
|
|
|
|
@Override
|
|
public AxisAlignedBB getRenderBoundingBox() {
|
|
|
|
if(bb == null) {
|
|
bb = AxisAlignedBB.getBoundingBox(
|
|
xCoord - 1,
|
|
yCoord,
|
|
zCoord - 1,
|
|
xCoord + 2,
|
|
yCoord + 3,
|
|
zCoord + 2
|
|
);
|
|
}
|
|
|
|
return bb;
|
|
}
|
|
|
|
@Override
|
|
@SideOnly(Side.CLIENT)
|
|
public double getMaxRenderDistanceSquared() {
|
|
return 65536.0D;
|
|
}
|
|
|
|
@Override
|
|
public FluidTank[] getSendingTanks() {
|
|
return new FluidTank[] { tanks[1], tanks[2] };
|
|
}
|
|
|
|
@Override
|
|
public FluidTank[] getReceivingTanks() {
|
|
return new FluidTank[] { tanks[0] };
|
|
}
|
|
}
|