mirror of
https://github.com/HbmMods/Hbm-s-Nuclear-Tech-GIT.git
synced 2026-01-25 10:32:49 +00:00
479 lines
14 KiB
Java
479 lines
14 KiB
Java
package com.hbm.tileentity.machine.oil;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
|
|
import com.hbm.blocks.BlockDummyable;
|
|
import com.hbm.blocks.ModBlocks;
|
|
import com.hbm.handler.MultiblockHandlerXR;
|
|
import com.hbm.interfaces.IControlReceiver;
|
|
import com.hbm.interfaces.IFluidAcceptor;
|
|
import com.hbm.interfaces.IFluidContainer;
|
|
import com.hbm.interfaces.IFluidSource;
|
|
import com.hbm.inventory.FluidStack;
|
|
import com.hbm.inventory.OreDictManager;
|
|
import com.hbm.inventory.RecipesCommon.AStack;
|
|
import com.hbm.inventory.RecipesCommon.ComparableStack;
|
|
import com.hbm.inventory.RecipesCommon.OreDictStack;
|
|
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.items.ModItems;
|
|
import com.hbm.lib.Library;
|
|
import com.hbm.tileentity.IOverpressurable;
|
|
import com.hbm.tileentity.IPersistentNBT;
|
|
import com.hbm.tileentity.IRepairable;
|
|
import com.hbm.tileentity.TileEntityMachineBase;
|
|
import com.hbm.util.ParticleUtil;
|
|
import com.hbm.util.Tuple.Quintet;
|
|
import com.hbm.util.fauxpointtwelve.DirPos;
|
|
|
|
import api.hbm.energy.IEnergyUser;
|
|
import api.hbm.fluid.IFluidStandardTransceiver;
|
|
import cpw.mods.fml.relauncher.Side;
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
import net.minecraft.entity.Entity;
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.tileentity.TileEntity;
|
|
import net.minecraft.util.AxisAlignedBB;
|
|
import net.minecraft.util.Vec3;
|
|
import net.minecraft.world.Explosion;
|
|
import net.minecraft.world.World;
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
public class TileEntityMachineRefinery extends TileEntityMachineBase implements IEnergyUser, IFluidContainer, IFluidAcceptor, IFluidSource, IControlReceiver, IOverpressurable, IPersistentNBT, IRepairable, IFluidStandardTransceiver {
|
|
|
|
public long power = 0;
|
|
public int sulfur = 0;
|
|
public static final int maxSulfur = 100;
|
|
public static final long maxPower = 1000;
|
|
public FluidTank[] tanks;
|
|
public List<IFluidAcceptor> list1 = new ArrayList();
|
|
public List<IFluidAcceptor> list2 = new ArrayList();
|
|
public List<IFluidAcceptor> list3 = new ArrayList();
|
|
public List<IFluidAcceptor> list4 = new ArrayList();
|
|
|
|
public boolean hasExploded = false;
|
|
public Explosion lastExplosion = null;
|
|
|
|
private static final int[] slot_access = new int[] {11};
|
|
|
|
public TileEntityMachineRefinery() {
|
|
super(12);
|
|
tanks = new FluidTank[5];
|
|
tanks[0] = new FluidTank(Fluids.HOTOIL, 64_000, 0);
|
|
tanks[1] = new FluidTank(Fluids.HEAVYOIL, 24_000, 1);
|
|
tanks[2] = new FluidTank(Fluids.NAPHTHA, 24_000, 2);
|
|
tanks[3] = new FluidTank(Fluids.LIGHTOIL, 24_000, 3);
|
|
tanks[4] = new FluidTank(Fluids.PETROLEUM, 24_000, 4);
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "container.machineRefinery";
|
|
}
|
|
|
|
@Override
|
|
public boolean isItemValidForSlot(int i, ItemStack stack) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void readFromNBT(NBTTagCompound nbt) {
|
|
super.readFromNBT(nbt);
|
|
|
|
power = nbt.getLong("power");
|
|
tanks[0].readFromNBT(nbt, "input");
|
|
tanks[1].readFromNBT(nbt, "heavy");
|
|
tanks[2].readFromNBT(nbt, "naphtha");
|
|
tanks[3].readFromNBT(nbt, "light");
|
|
tanks[4].readFromNBT(nbt, "petroleum");
|
|
sulfur = nbt.getInteger("sulfur");
|
|
hasExploded = nbt.getBoolean("exploded");
|
|
}
|
|
|
|
@Override
|
|
public void writeToNBT(NBTTagCompound nbt) {
|
|
super.writeToNBT(nbt);
|
|
|
|
nbt.setLong("power", power);
|
|
tanks[0].writeToNBT(nbt, "input");
|
|
tanks[1].writeToNBT(nbt, "heavy");
|
|
tanks[2].writeToNBT(nbt, "naphtha");
|
|
tanks[3].writeToNBT(nbt, "light");
|
|
tanks[4].writeToNBT(nbt, "petroleum");
|
|
nbt.setInteger("sulfur", sulfur);
|
|
nbt.setBoolean("exploded", hasExploded);
|
|
}
|
|
|
|
@Override
|
|
public int[] getAccessibleSlotsFromSide(int side) {
|
|
return slot_access;
|
|
}
|
|
|
|
@Override
|
|
public boolean canInsertItem(int i, ItemStack itemStack, int j) {
|
|
return this.isItemValidForSlot(i, itemStack);
|
|
}
|
|
|
|
@Override
|
|
public boolean canExtractItem(int i, ItemStack itemStack, int j) {
|
|
return i == 11;
|
|
}
|
|
|
|
@Override
|
|
public void updateEntity() {
|
|
|
|
if(!worldObj.isRemote) {
|
|
|
|
if(this.getBlockMetadata() < 12) {
|
|
ForgeDirection dir = ForgeDirection.getOrientation(this.getBlockMetadata()).getRotation(ForgeDirection.DOWN);
|
|
worldObj.removeTileEntity(xCoord, yCoord, zCoord);
|
|
worldObj.setBlock(xCoord, yCoord, zCoord, ModBlocks.machine_refinery, dir.ordinal() + 10, 3);
|
|
MultiblockHandlerXR.fillSpace(worldObj, xCoord, yCoord, zCoord, ((BlockDummyable) ModBlocks.machine_refinery).getDimensions(), ModBlocks.machine_refinery, dir);
|
|
NBTTagCompound data = new NBTTagCompound();
|
|
this.writeToNBT(data);
|
|
worldObj.getTileEntity(xCoord, yCoord, zCoord).readFromNBT(data);
|
|
return;
|
|
}
|
|
|
|
if(!this.hasExploded) {
|
|
|
|
this.updateConnections();
|
|
|
|
power = Library.chargeTEFromItems(slots, 0, power, maxPower);
|
|
|
|
if(worldObj.getTotalWorldTime() % 10 == 0) {
|
|
fillFluidInit(tanks[1].getTankType());
|
|
fillFluidInit(tanks[2].getTankType());
|
|
fillFluidInit(tanks[3].getTankType());
|
|
fillFluidInit(tanks[4].getTankType());
|
|
}
|
|
|
|
tanks[0].loadTank(1, 2, slots);
|
|
|
|
refine();
|
|
|
|
tanks[1].unloadTank(3, 4, slots);
|
|
tanks[2].unloadTank(5, 6, slots);
|
|
tanks[3].unloadTank(7, 8, slots);
|
|
tanks[4].unloadTank(9, 10, slots);
|
|
|
|
for(DirPos pos : getConPos()) {
|
|
for(int i = 1; i < 5; i++) {
|
|
if(tanks[i].getFill() > 0) {
|
|
this.sendFluid(tanks[i].getTankType(), worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir());
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
|
|
boolean isBurning = false;
|
|
for(int i = 0; i < 5; i++) {
|
|
if(tanks[i].getFill() > 0) {
|
|
tanks[i].setFill(Math.max(tanks[i].getFill() - 10, 0));
|
|
isBurning = true;
|
|
}
|
|
}
|
|
|
|
if(isBurning) {
|
|
List<Entity> affected = worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(xCoord - 1.5, yCoord, zCoord - 1.5, xCoord + 2.5, yCoord + 8, zCoord + 2.5));
|
|
for(Entity e : affected) e.setFire(5);
|
|
Random rand = worldObj.rand;
|
|
ParticleUtil.spawnGasFlame(worldObj, xCoord + rand.nextDouble(), yCoord + 1.5 + rand.nextDouble() * 3, zCoord + rand.nextDouble(), rand.nextGaussian() * 0.05, 0.1, rand.nextGaussian() * 0.05);
|
|
}
|
|
}
|
|
|
|
NBTTagCompound data = new NBTTagCompound();
|
|
data.setLong("power", this.power);
|
|
for(int i = 0; i < 5; i++) tanks[i].writeToNBT(data, "" + i);
|
|
data.setBoolean("exploded", hasExploded);
|
|
this.networkPack(data, 150);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void networkUnpack(NBTTagCompound nbt) {
|
|
this.power = nbt.getLong("power");
|
|
for(int i = 0; i < 5; i++) tanks[i].readFromNBT(nbt, "" + i);
|
|
this.hasExploded = nbt.getBoolean("exploded");
|
|
}
|
|
|
|
private void refine() {
|
|
Quintet<FluidStack, FluidStack, FluidStack, FluidStack, ItemStack> refinery = RefineryRecipes.getRefinery(tanks[0].getTankType());
|
|
|
|
if(refinery == null) //usually not possible
|
|
return;
|
|
|
|
FluidStack[] stacks = new FluidStack[] {refinery.getV(), refinery.getW(), refinery.getX(), refinery.getY()};
|
|
|
|
for(int i = 0; i < stacks.length; i++)
|
|
tanks[i + 1].setTankType(stacks[i].type);
|
|
|
|
if(power < 5 || tanks[0].getFill() < 100)
|
|
return;
|
|
|
|
for(int i = 0; i < stacks.length; i++) {
|
|
if(tanks[i + 1].getFill() + stacks[i].fill > tanks[i + 1].getMaxFill()) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
tanks[0].setFill(tanks[0].getFill() - 100);
|
|
|
|
for(int i = 0; i < stacks.length; i++)
|
|
tanks[i + 1].setFill(tanks[i + 1].getFill() + stacks[i].fill);
|
|
|
|
this.sulfur++;
|
|
|
|
if(this.sulfur >= maxSulfur) {
|
|
this.sulfur -= maxSulfur;
|
|
|
|
ItemStack out = refinery.getZ();
|
|
|
|
if(out != null) {
|
|
|
|
if(slots[11] == null) {
|
|
slots[11] = out.copy();
|
|
} else {
|
|
|
|
if(out.getItem() == slots[11].getItem() && out.getItemDamage() == slots[11].getItemDamage() && slots[11].stackSize + out.stackSize <= slots[11].getMaxStackSize()) {
|
|
slots[11].stackSize += out.stackSize;
|
|
}
|
|
}
|
|
}
|
|
|
|
this.markDirty();
|
|
}
|
|
|
|
this.power -= 5;
|
|
}
|
|
|
|
private void updateConnections() {
|
|
for(DirPos pos : getConPos()) {
|
|
this.trySubscribe(worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir());
|
|
this.trySubscribe(tanks[0].getTankType(), worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir());
|
|
}
|
|
}
|
|
|
|
public DirPos[] getConPos() {
|
|
return new DirPos[] {
|
|
new DirPos(xCoord + 2, yCoord, zCoord + 1, Library.POS_X),
|
|
new DirPos(xCoord + 2, yCoord, zCoord - 1, Library.POS_X),
|
|
new DirPos(xCoord - 2, yCoord, zCoord + 1, Library.NEG_X),
|
|
new DirPos(xCoord - 2, yCoord, zCoord - 1, Library.NEG_X),
|
|
new DirPos(xCoord + 1, yCoord, zCoord + 2, Library.POS_Z),
|
|
new DirPos(xCoord - 1, yCoord, zCoord + 2, Library.POS_Z),
|
|
new DirPos(xCoord + 1, yCoord, zCoord - 2, Library.NEG_Z),
|
|
new DirPos(xCoord - 1, yCoord, zCoord - 2, Library.NEG_Z)
|
|
};
|
|
}
|
|
|
|
public long getPowerScaled(long i) {
|
|
return (power * i) / maxPower;
|
|
}
|
|
|
|
@Override
|
|
public void setPower(long i) {
|
|
power = i;
|
|
}
|
|
|
|
@Override
|
|
public long getPower() {
|
|
return power;
|
|
|
|
}
|
|
|
|
@Override
|
|
public long getMaxPower() {
|
|
return maxPower;
|
|
}
|
|
|
|
@Override
|
|
public void fillFluidInit(FluidType type) {
|
|
fillFluid(this.xCoord + 1, this.yCoord, this.zCoord - 2, getTact(), type);
|
|
fillFluid(this.xCoord + 1, this.yCoord, this.zCoord + 2, getTact(), type);
|
|
fillFluid(this.xCoord - 1, this.yCoord, this.zCoord - 2, getTact(), type);
|
|
fillFluid(this.xCoord - 1, this.yCoord, this.zCoord + 2, getTact(), type);
|
|
|
|
fillFluid(this.xCoord - 2, this.yCoord, this.zCoord + 1, getTact(), type);
|
|
fillFluid(this.xCoord + 2, this.yCoord, this.zCoord + 1, getTact(), type);
|
|
fillFluid(this.xCoord - 2, this.yCoord, this.zCoord - 1, getTact(), type);
|
|
fillFluid(this.xCoord + 2, this.yCoord, this.zCoord - 1, 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 int getFluidFill(FluidType type) {
|
|
|
|
for(int i = 0; i < 5; i++) {
|
|
if(type == tanks[i].getTankType()) {
|
|
return tanks[i].getFill();
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public void setFluidFill(int fill, FluidType type) {
|
|
|
|
for(int i = 0; i < 5; i++) {
|
|
if(type == tanks[i].getTankType()) {
|
|
tanks[i].setFill(fill);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<IFluidAcceptor> getFluidList(FluidType type) {
|
|
if(type == tanks[1].getTankType()) return list1;
|
|
if(type == tanks[2].getTankType()) return list2;
|
|
if(type == tanks[3].getTankType()) return list3;
|
|
if(type == tanks[4].getTankType()) return list4;
|
|
return new ArrayList<IFluidAcceptor>();
|
|
}
|
|
|
|
@Override
|
|
public void clearFluidList(FluidType type) {
|
|
if(type == tanks[1].getTankType()) list1.clear();
|
|
if(type == tanks[2].getTankType()) list2.clear();
|
|
if(type == tanks[3].getTankType()) list3.clear();
|
|
if(type == tanks[4].getTankType()) list4.clear();
|
|
}
|
|
|
|
@Override
|
|
public int getMaxFluidFill(FluidType type) {
|
|
if(type == tanks[0].getTankType())
|
|
return tanks[0].getMaxFill();
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public void setFillForSync(int fill, int index) {
|
|
if(index < 5 && tanks[index] != null)
|
|
tanks[index].setFill(fill);
|
|
}
|
|
|
|
@Override
|
|
public void setTypeForSync(FluidType type, int index) {
|
|
if(index < 5 && tanks[index] != null)
|
|
tanks[index].setTankType(type);
|
|
}
|
|
|
|
@Override
|
|
public AxisAlignedBB getRenderBoundingBox() {
|
|
return TileEntity.INFINITE_EXTENT_AABB;
|
|
}
|
|
|
|
@Override
|
|
@SideOnly(Side.CLIENT)
|
|
public double getMaxRenderDistanceSquared() {
|
|
return 65536.0D;
|
|
}
|
|
|
|
@Override
|
|
public boolean hasPermission(EntityPlayer player) {
|
|
return Vec3.createVectorHelper(xCoord - player.posX, yCoord - player.posY, zCoord - player.posZ).lengthVector() < 25;
|
|
}
|
|
|
|
@Override
|
|
public void receiveControl(NBTTagCompound data) {
|
|
|
|
if(data.hasKey("toggle")) {
|
|
|
|
for(DirPos pos : getConPos()) {
|
|
this.tryUnsubscribe(tanks[0].getTankType(), worldObj, pos.getX(), pos.getY(), pos.getZ());
|
|
}
|
|
|
|
if(tanks[0].getTankType() == Fluids.HOTOIL) {
|
|
tanks[0].setTankType(Fluids.HOTCRACKOIL);
|
|
} else {
|
|
tanks[0].setTankType(Fluids.HOTOIL);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public FluidTank[] getSendingTanks() {
|
|
return new FluidTank[] { tanks[1], tanks[2], tanks[3], tanks[4] };
|
|
}
|
|
|
|
@Override
|
|
public FluidTank[] getReceivingTanks() {
|
|
return new FluidTank[] { tanks[0] };
|
|
}
|
|
|
|
@Override
|
|
public FluidTank[] getAllTanks() {
|
|
return tanks;
|
|
}
|
|
|
|
@Override
|
|
public boolean canConnect(FluidType type, ForgeDirection dir) {
|
|
return dir != ForgeDirection.UNKNOWN && dir != ForgeDirection.DOWN;
|
|
}
|
|
|
|
@Override
|
|
public void explode(World world, int x, int y, int z) {
|
|
|
|
if(this.hasExploded) return;
|
|
|
|
this.hasExploded = true;
|
|
this.markChanged();
|
|
}
|
|
|
|
@Override
|
|
public boolean isDamaged() {
|
|
return this.hasExploded;
|
|
}
|
|
|
|
List<AStack> repair = new ArrayList();
|
|
@Override
|
|
public List<AStack> getRepairMaterials() {
|
|
|
|
if(!repair.isEmpty())
|
|
return repair;
|
|
|
|
repair.add(new OreDictStack(OreDictManager.STEEL.plate(), 8));
|
|
repair.add(new ComparableStack(ModItems.ducttape, 4));
|
|
return repair;
|
|
}
|
|
|
|
@Override
|
|
public void repair() {
|
|
this.hasExploded = false;
|
|
this.markChanged();
|
|
}
|
|
|
|
@Override
|
|
public void writeNBT(NBTTagCompound nbt) {
|
|
if(tanks[0].getFill() == 0 && tanks[1].getFill() == 0 && tanks[2].getFill() == 0 && tanks[3].getFill() == 0 && tanks[4].getFill() == 0 && !this.hasExploded) return;
|
|
NBTTagCompound data = new NBTTagCompound();
|
|
for(int i = 0; i < 5; i++) this.tanks[i].writeToNBT(data, "" + i);
|
|
data.setBoolean("hasExploded", hasExploded);
|
|
nbt.setTag(NBT_PERSISTENT_KEY, data);
|
|
}
|
|
|
|
@Override
|
|
public void readNBT(NBTTagCompound nbt) {
|
|
NBTTagCompound data = nbt.getCompoundTag(NBT_PERSISTENT_KEY);
|
|
for(int i = 0; i < 5; i++) this.tanks[i].readFromNBT(data, "" + i);
|
|
this.hasExploded = data.getBoolean("hasExploded");
|
|
}
|
|
}
|