Work on Chemical Infuser

This commit is contained in:
Aidan Brady 2013-12-12 18:33:56 -05:00
parent 338da4d92c
commit 43d3050b40
8 changed files with 209 additions and 227 deletions

View file

@ -20,7 +20,7 @@ public class GasTank
stored = stack; stored = stack;
} }
public GasStack draw(int amount, boolean doDrain) public GasStack draw(int amount, boolean doDraw)
{ {
if(stored == null || amount <= 0) if(stored == null || amount <= 0)
{ {
@ -31,7 +31,7 @@ public class GasTank
if(ret.amount > 0) if(ret.amount > 0)
{ {
if(doDrain) if(doDraw)
{ {
stored.amount -= ret.amount; stored.amount -= ret.amount;
@ -39,8 +39,6 @@ public class GasTank
{ {
stored = null; stored = null;
} }
} }
return ret; return ret;
@ -49,7 +47,7 @@ public class GasTank
return null; return null;
} }
public int fill(GasStack amount, boolean doFill) public int receive(GasStack amount, boolean doReceive)
{ {
if(amount == null || (stored != null && stored.amount == maxGas)) if(amount == null || (stored != null && stored.amount == maxGas))
{ {
@ -58,7 +56,7 @@ public class GasTank
int toFill = Math.min(maxGas-getStored(), amount.amount); int toFill = Math.min(maxGas-getStored(), amount.amount);
if(doFill) if(doReceive)
{ {
if(stored == null) if(stored == null)
{ {

View file

@ -22,9 +22,23 @@ public class ChemicalInput
@Override @Override
public boolean equals(Object obj) public boolean equals(Object obj)
{ {
return obj instanceof ChemicalInput && if(!(obj instanceof ChemicalInput))
((ChemicalInput)obj).leftGas == leftGas && {
((ChemicalInput)obj).rightGas == rightGas; return false;
}
ChemicalInput compare = (ChemicalInput)obj;
if(leftGas == compare.leftGas && rightGas == compare.rightGas)
{
return true;
}
else if(leftGas == compare.rightGas && rightGas == compare.leftGas)
{
return true;
}
return false;
} }
@Override @Override

View file

@ -145,7 +145,16 @@ public final class RecipeHandler
if(input != null && input.isValid()) if(input != null && input.isValid())
{ {
HashMap<ChemicalInput, GasStack> recipes = Recipe.CHEMICAL_INFUSER.get(); HashMap<ChemicalInput, GasStack> recipes = Recipe.CHEMICAL_INFUSER.get();
return recipes.get(input);
for(Map.Entry<ChemicalInput, GasStack> entry : recipes.entrySet())
{
ChemicalInput key = (ChemicalInput)entry.getKey();
if(key.equals(input))
{
return entry.getValue().copy();
}
}
} }
return null; return null;

View file

@ -104,7 +104,7 @@ public class TileEntityChemicalFormulator extends TileEntityElectricBlock implem
else { else {
GasStack stack = RecipeHandler.getChemicalFormulatorOutput(inventory[0], true); GasStack stack = RecipeHandler.getChemicalFormulatorOutput(inventory[0], true);
gasTank.fill(stack, true); gasTank.receive(stack, true);
operatingTicks = 0; operatingTicks = 0;
} }
} }

View file

@ -1,16 +1,39 @@
package mekanism.common.tileentity; package mekanism.common.tileentity;
import java.util.ArrayList;
import mekanism.api.Object3D;
import mekanism.api.gas.Gas;
import mekanism.api.gas.GasRegistry;
import mekanism.api.gas.GasStack; import mekanism.api.gas.GasStack;
import mekanism.common.IRedstoneControl.RedstoneControl; import mekanism.api.gas.GasTank;
import mekanism.api.gas.GasTransmission;
import mekanism.api.gas.IGasHandler;
import mekanism.api.gas.ITubeConnection;
import mekanism.common.ChemicalInput;
import mekanism.common.IActiveState;
import mekanism.common.IRedstoneControl;
import mekanism.common.Mekanism; import mekanism.common.Mekanism;
import mekanism.common.PacketHandler;
import mekanism.common.PacketHandler.Transmission;
import mekanism.common.RecipeHandler;
import mekanism.common.network.PacketTileEntity;
import mekanism.common.util.ChargeUtils;
import mekanism.common.util.MekanismUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;
public class TileEntityChemicalInfuser extends TileEntityElectricBlock //implements IActiveState, IGasStorage, IGasAcceptor, ITubeConnection, IRedstoneControl import com.google.common.io.ByteArrayDataInput;
public class TileEntityChemicalInfuser extends TileEntityElectricBlock implements IActiveState, IGasHandler, ITubeConnection, IRedstoneControl
{ {
public GasStack leftStack; public GasTank leftTank = new GasTank(MAX_GAS);
public GasStack rightStack; public GasTank rightTank = new GasTank(MAX_GAS);
public GasTank centerTank = new GasTank(MAX_GAS);
public GasStack centerStack;
public static final int MAX_GAS = 10000; public static final int MAX_GAS = 10000;
@ -35,7 +58,7 @@ public class TileEntityChemicalInfuser extends TileEntityElectricBlock //impleme
inventory = new ItemStack[4]; inventory = new ItemStack[4];
} }
/*@Override @Override
public void onUpdate() public void onUpdate()
{ {
if(worldObj.isRemote) if(worldObj.isRemote)
@ -66,123 +89,51 @@ public class TileEntityChemicalInfuser extends TileEntityElectricBlock //impleme
ChargeUtils.discharge(4, this); ChargeUtils.discharge(4, this);
if(mode == 0) if(inventory[0] != null && (leftTank.getGas() == null || leftTank.getStored() < leftTank.getMaxGas()))
{ {
if(inventory[1] != null && (getGas() == null || getGas().amount < getMaxGas())) leftTank.receive(GasTransmission.removeGas(inventory[0], null, leftTank.getNeeded()), true);
{ }
if(getGas() == null)
{
setGas(GasTransmission.removeGas(inventory[1], null, getMaxGas()));
}
else {
GasStack removed = GasTransmission.removeGas(inventory[1], getGas().getGas(), getMaxGas()-getGas().amount);
setGas(new GasStack(getGas().getGas(), getGas().amount + (removed != null ? removed.amount : 0)));
}
}
if(getEnergy() >= ENERGY_USAGE && MekanismUtils.canFunction(this) && isValidGas(gasTank) && (fluidTank.getFluid() == null || (fluidTank.getFluid().amount < 10000 && gasEquals(gasTank, fluidTank.getFluid())))) if(inventory[2] != null && (rightTank.getGas() == null || rightTank.getStored() < rightTank.getMaxGas()))
{
rightTank.receive(GasTransmission.removeGas(inventory[2], null, rightTank.getNeeded()), true);
}
if(inventory[1] != null && centerTank.getGas() != null)
{
centerTank.draw(GasTransmission.addGas(inventory[1], centerTank.getGas()), true);
}
if(canOperate() && getEnergy() >= ENERGY_USAGE && MekanismUtils.canFunction(this))
{
setActive(true);
GasStack stack = RecipeHandler.getChemicalInfuserOutput(new ChemicalInput(leftTank.getGas().getGas(), rightTank.getGas().getGas()));
leftTank.draw(1, true);
rightTank.draw(1, true);
centerTank.receive(stack, true);
setEnergy(getEnergy() - ENERGY_USAGE);
}
else {
if(prevEnergy >= getEnergy())
{ {
setActive(true); setActive(false);
fluidTank.fill(new FluidStack(getGas().getGas().getFluid(), 1), true);
setGas(new GasStack(getGas().getGas(), getGas().amount-1));
setEnergy(getEnergy() - ENERGY_USAGE);
}
else {
if(prevEnergy >= getEnergy())
{
setActive(false);
}
} }
} }
else if(mode == 1)
if(centerTank.getGas() != null)
{ {
if(getGas() != null) GasStack toSend = new GasStack(centerTank.getGas().getGas(), Math.min(centerTank.getStored(), gasOutput));
centerTank.draw(GasTransmission.emitGasToNetwork(toSend, this, ForgeDirection.getOrientation(facing)), true);
TileEntity tileEntity = Object3D.get(this).getFromSide(ForgeDirection.getOrientation(facing)).getTileEntity(worldObj);
if(tileEntity instanceof IGasHandler)
{ {
if(inventory[0] != null) if(((IGasHandler)tileEntity).canReceiveGas(ForgeDirection.getOrientation(facing).getOpposite(), centerTank.getGas().getGas()))
{ {
setGas(new GasStack(getGas().getGas(), getGas().amount - GasTransmission.addGas(inventory[0], getGas()))); centerTank.draw(((IGasHandler)tileEntity).receiveGas(ForgeDirection.getOrientation(facing).getOpposite(), toSend), true);
}
}
if(getGas() != null)
{
GasStack toSend = new GasStack(getGas().getGas(), Math.min(getGas().amount, gasOutput));
setGas(new GasStack(getGas().getGas(), getGas().amount - GasTransmission.emitGasToNetwork(toSend, this, MekanismUtils.getLeft(facing))));
TileEntity tileEntity = Object3D.get(this).getFromSide(MekanismUtils.getLeft(facing)).getTileEntity(worldObj);
if(tileEntity instanceof IGasHandler)
{
if(((IGasHandler)tileEntity).canReceiveGas(MekanismUtils.getLeft(facing).getOpposite(), getGas().getGas()))
{
int added = ((IGasHandler)tileEntity).receiveGas(new GasStack(getGas().getGas(), Math.min(getGas().amount, gasOutput)));
setGas(new GasStack(getGas().getGas(), getGas().amount - added));
}
}
}
if(FluidContainerRegistry.isFilledContainer(inventory[2]))
{
FluidStack itemFluid = FluidContainerRegistry.getFluidForFilledItem(inventory[2]);
if((fluidTank.getFluid() == null && itemFluid.amount <= 10000) || fluidTank.getFluid().amount+itemFluid.amount <= 10000)
{
if(fluidTank.getFluid() != null && !fluidTank.getFluid().isFluidEqual(itemFluid))
{
return;
}
ItemStack containerItem = inventory[2].getItem().getContainerItemStack(inventory[2]);
boolean filled = false;
if(containerItem != null)
{
if(inventory[3] == null || (inventory[3].isItemEqual(containerItem) && inventory[3].stackSize+1 <= containerItem.getMaxStackSize()))
{
inventory[2] = null;
if(inventory[3] == null)
{
inventory[3] = containerItem;
}
else {
inventory[3].stackSize++;
}
filled = true;
}
}
else {
inventory[2].stackSize--;
if(inventory[2].stackSize == 0)
{
inventory[2] = null;
}
filled = true;
}
if(filled)
{
fluidTank.fill(itemFluid, true);
}
}
}
if(getEnergy() >= ENERGY_USAGE && MekanismUtils.canFunction(this) && isValidFluid(fluidTank.getFluid()) && (gasTank == null || (gasTank.amount < MAX_GAS && gasEquals(gasTank, fluidTank.getFluid()))))
{
setActive(true);
setGas(new GasStack(GasRegistry.getGas(fluidTank.getFluid().getFluid()), getGas() != null ? getGas().amount+1 : 1));
fluidTank.draw(1, true);
setEnergy(getEnergy() - ENERGY_USAGE);
}
else {
if(prevEnergy >= getEnergy())
{
setActive(false);
} }
} }
} }
@ -191,34 +142,26 @@ public class TileEntityChemicalInfuser extends TileEntityElectricBlock //impleme
} }
} }
public boolean isValidGas(GasStack g) public boolean canOperate()
{ {
if(g == null) if(leftTank.getGas() == null || rightTank.getGas() == null || centerTank.getNeeded() == 0)
{ {
return false; return false;
} }
return g.getGas().hasFluid(); GasStack out = RecipeHandler.getChemicalInfuserOutput(new ChemicalInput(leftTank.getGas().getGas(), rightTank.getGas().getGas()));
}
public boolean gasEquals(GasStack gas, FluidStack fluid) if(out == null)
{
if(fluid == null || gas == null || !gas.getGas().hasFluid())
{ {
return false; return false;
} }
return gas.getGas().getFluid() == fluid.getFluid(); if(centerTank.getNeeded() < out.amount)
}
public boolean isValidFluid(FluidStack f)
{
if(f == null)
{ {
return false; return false;
} }
return GasRegistry.getGas(f.getFluid()) != null; return true;
} }
@Override @Override
@ -230,7 +173,11 @@ public class TileEntityChemicalInfuser extends TileEntityElectricBlock //impleme
if(type == 0) if(type == 0)
{ {
mode = mode == 0 ? 1 : 0; leftTank.setGas(null);
}
else if(type == 1)
{
rightTank.setGas(null);
} }
for(EntityPlayer player : playersUsing) for(EntityPlayer player : playersUsing)
@ -243,24 +190,31 @@ public class TileEntityChemicalInfuser extends TileEntityElectricBlock //impleme
super.handlePacketData(dataStream); super.handlePacketData(dataStream);
mode = dataStream.readInt();
isActive = dataStream.readBoolean(); isActive = dataStream.readBoolean();
controlType = RedstoneControl.values()[dataStream.readInt()]; controlType = RedstoneControl.values()[dataStream.readInt()];
if(dataStream.readBoolean()) if(dataStream.readBoolean())
{ {
fluidTank.setFluid(new FluidStack(dataStream.readInt(), dataStream.readInt())); leftTank.setGas(new GasStack(GasRegistry.getGas(dataStream.readInt()), dataStream.readInt()));
} }
else { else {
fluidTank.setFluid(null); leftTank.setGas(null);
} }
if(dataStream.readBoolean()) if(dataStream.readBoolean())
{ {
gasTank = new GasStack(GasRegistry.getGas(dataStream.readInt()), dataStream.readInt()); rightTank.setGas(new GasStack(GasRegistry.getGas(dataStream.readInt()), dataStream.readInt()));
} }
else { else {
gasTank = null; rightTank.setGas(null);
}
if(dataStream.readBoolean())
{
centerTank.setGas(new GasStack(GasRegistry.getGas(dataStream.readInt()), dataStream.readInt()));
}
else {
centerTank.setGas(null);
} }
@ -272,25 +226,34 @@ public class TileEntityChemicalInfuser extends TileEntityElectricBlock //impleme
{ {
super.getNetworkedData(data); super.getNetworkedData(data);
data.add(mode);
data.add(isActive); data.add(isActive);
data.add(controlType.ordinal()); data.add(controlType.ordinal());
if(fluidTank.getFluid() != null) if(leftTank != null)
{ {
data.add(true); data.add(true);
data.add(fluidTank.getFluid().fluidID); data.add(leftTank.getGas().getGas().getID());
data.add(fluidTank.getFluid().amount); data.add(leftTank.getStored());
} }
else { else {
data.add(false); data.add(false);
} }
if(gasTank != null) if(rightTank != null)
{ {
data.add(true); data.add(true);
data.add(gasTank.getGas().getID()); data.add(rightTank.getGas().getGas().getID());
data.add(gasTank.amount); data.add(rightTank.getStored());
}
else {
data.add(false);
}
if(centerTank != null)
{
data.add(true);
data.add(centerTank.getGas().getGas().getID());
data.add(centerTank.getStored());
} }
else { else {
data.add(false); data.add(false);
@ -304,16 +267,12 @@ public class TileEntityChemicalInfuser extends TileEntityElectricBlock //impleme
{ {
super.readFromNBT(nbtTags); super.readFromNBT(nbtTags);
mode = nbtTags.getInteger("mode");
isActive = nbtTags.getBoolean("isActive"); isActive = nbtTags.getBoolean("isActive");
controlType = RedstoneControl.values()[nbtTags.getInteger("controlType")]; controlType = RedstoneControl.values()[nbtTags.getInteger("controlType")];
gasTank = GasStack.readFromNBT(nbtTags.getCompoundTag("gasTank")); leftTank = GasTank.readFromNBT(nbtTags.getCompoundTag("leftTank"));
rightTank = GasTank.readFromNBT(nbtTags.getCompoundTag("rightTank"));
if(nbtTags.hasKey("fluidTank")) centerTank = GasTank.readFromNBT(nbtTags.getCompoundTag("centerTank"));
{
fluidTank.readFromNBT(nbtTags.getCompoundTag("fluidTank"));
}
} }
@Override @Override
@ -321,19 +280,12 @@ public class TileEntityChemicalInfuser extends TileEntityElectricBlock //impleme
{ {
super.writeToNBT(nbtTags); super.writeToNBT(nbtTags);
nbtTags.setInteger("mode", mode);
nbtTags.setBoolean("isActive", isActive); nbtTags.setBoolean("isActive", isActive);
nbtTags.setInteger("controlType", controlType.ordinal()); nbtTags.setInteger("controlType", controlType.ordinal());
if(gasTank != null) nbtTags.setCompoundTag("leftTank", leftTank.write(new NBTTagCompound()));
{ nbtTags.setCompoundTag("rightTank", rightTank.write(new NBTTagCompound()));
nbtTags.setCompoundTag("gasTank", gasTank.write(new NBTTagCompound())); nbtTags.setCompoundTag("centerTank", centerTank.write(new NBTTagCompound()));
}
if(fluidTank.getFluid() != null)
{
nbtTags.setTag("fluidTank", fluidTank.writeToNBT(new NBTTagCompound()));
}
} }
@Override @Override
@ -342,14 +294,37 @@ public class TileEntityChemicalInfuser extends TileEntityElectricBlock //impleme
return i != 0 && i != 1; return i != 0 && i != 1;
} }
public int getScaledFluidLevel(int i) public GasTank getTank(ForgeDirection side)
{ {
return fluidTank.getFluid() != null ? fluidTank.getFluid().amount*i / 10000 : 0; if(side == MekanismUtils.getLeft(facing))
{
return leftTank;
}
else if(side == MekanismUtils.getRight(facing))
{
return rightTank;
}
else if(side == ForgeDirection.getOrientation(facing))
{
return centerTank;
}
return null;
} }
public int getScaledGasLevel(int i) public int getScaledLeftGasLevel(int i)
{ {
return gasTank != null ? gasTank.amount*i / MAX_GAS : 0; return leftTank != null ? leftTank.getStored()*i / MAX_GAS : 0;
}
public int getScaledRightGasLevel(int i)
{
return leftTank != null ? rightTank.getStored()*i / MAX_GAS : 0;
}
public int getScaledCenterGasLevel(int i)
{
return leftTank != null ? centerTank.getStored()*i / MAX_GAS : 0;
} }
@Override @Override
@ -387,55 +362,13 @@ public class TileEntityChemicalInfuser extends TileEntityElectricBlock //impleme
@Override @Override
public boolean canTubeConnect(ForgeDirection side) public boolean canTubeConnect(ForgeDirection side)
{ {
return side == MekanismUtils.getLeft(facing); return side == MekanismUtils.getLeft(facing) || side == MekanismUtils.getRight(facing) || side == ForgeDirection.getOrientation(facing);
}
@Override
public int receiveGas(GasStack stack)
{
if(gasTank == null || (gasTank != null && gasTank.getGas() == stack.getGas()))
{
int stored = getGas() != null ? getGas().amount : 0;
int toUse = Math.min(getMaxGas()-stored, stack.amount);
setGas(new GasStack(stack.getGas(), stored + toUse));
return toUse;
}
return 0;
} }
@Override @Override
public boolean canReceiveGas(ForgeDirection side, Gas type) public boolean canReceiveGas(ForgeDirection side, Gas type)
{ {
return mode == 0 && (getGas() == null || getGas().getGas() == type) && side == MekanismUtils.getLeft(facing); return getTank(side) != null && getTank(side) != centerTank ? getTank(side).canReceive(type) : false;
}
@Override
public GasStack getGas(Object... data)
{
return gasTank;
}
@Override
public void setGas(GasStack stack, Object... data)
{
if(stack == null || stack.amount == 0)
{
gasTank = null;
}
else {
gasTank = new GasStack(stack.getGas(), Math.max(Math.min(stack.amount, getMaxGas()), 0));
}
MekanismUtils.saveChunk(this);
}
@Override
public int getMaxGas(Object... data)
{
return MAX_GAS;
} }
@Override @Override
@ -449,5 +382,33 @@ public class TileEntityChemicalInfuser extends TileEntityElectricBlock //impleme
{ {
controlType = type; controlType = type;
MekanismUtils.saveChunk(this); MekanismUtils.saveChunk(this);
}*/ }
@Override
public int receiveGas(ForgeDirection side, GasStack stack)
{
if(canReceiveGas(side, stack != null ? stack.getGas() : null))
{
return getTank(side).receive(stack, true);
}
return 0;
}
@Override
public GasStack drawGas(ForgeDirection side, int amount)
{
if(canDrawGas(side, null))
{
return getTank(side).draw(amount, true);
}
return null;
}
@Override
public boolean canDrawGas(ForgeDirection side, Gas type)
{
return getTank(side) != null && getTank(side) == centerTank ? getTank(side).canDraw(type) : false;
}
} }

View file

@ -50,7 +50,7 @@ public class TileEntityGasTank extends TileEntityContainerBlock implements IGasH
if(inventory[1] != null && (gasTank.getGas() == null || gasTank.getGas().amount < gasTank.getMaxGas())) if(inventory[1] != null && (gasTank.getGas() == null || gasTank.getGas().amount < gasTank.getMaxGas()))
{ {
gasTank.fill(GasTransmission.removeGas(inventory[1], null, gasTank.getNeeded()), true); gasTank.receive(GasTransmission.removeGas(inventory[1], null, gasTank.getNeeded()), true);
} }
if(!worldObj.isRemote && gasTank.getGas() != null && MekanismUtils.canFunction(this)) if(!worldObj.isRemote && gasTank.getGas() != null && MekanismUtils.canFunction(this))
@ -110,7 +110,7 @@ public class TileEntityGasTank extends TileEntityContainerBlock implements IGasH
@Override @Override
public int receiveGas(ForgeDirection side, GasStack stack) public int receiveGas(ForgeDirection side, GasStack stack)
{ {
return gasTank.fill(stack, true); return gasTank.receive(stack, true);
} }
@Override @Override

View file

@ -102,7 +102,7 @@ public class TileEntityRotaryCondensentrator extends TileEntityElectricBlock imp
{ {
if(inventory[1] != null && (gasTank.getGas() == null || gasTank.getStored() < gasTank.getMaxGas())) if(inventory[1] != null && (gasTank.getGas() == null || gasTank.getStored() < gasTank.getMaxGas()))
{ {
gasTank.fill(GasTransmission.removeGas(inventory[1], null, gasTank.getNeeded()), true); gasTank.receive(GasTransmission.removeGas(inventory[1], null, gasTank.getNeeded()), true);
} }
if(inventory[2] != null) if(inventory[2] != null)
@ -229,7 +229,7 @@ public class TileEntityRotaryCondensentrator extends TileEntityElectricBlock imp
if(getEnergy() >= ENERGY_USAGE && MekanismUtils.canFunction(this) && isValidFluid(fluidTank.getFluid()) && (gasTank == null || (gasTank.getStored() < MAX_GAS && gasEquals(gasTank.getGas(), fluidTank.getFluid())))) if(getEnergy() >= ENERGY_USAGE && MekanismUtils.canFunction(this) && isValidFluid(fluidTank.getFluid()) && (gasTank == null || (gasTank.getStored() < MAX_GAS && gasEquals(gasTank.getGas(), fluidTank.getFluid()))))
{ {
setActive(true); setActive(true);
gasTank.fill(new GasStack(GasRegistry.getGas(fluidTank.getFluid().getFluid()), 1), true); gasTank.receive(new GasStack(GasRegistry.getGas(fluidTank.getFluid().getFluid()), 1), true);
fluidTank.drain(1, true); fluidTank.drain(1, true);
setEnergy(getEnergy() - ENERGY_USAGE); setEnergy(getEnergy() - ENERGY_USAGE);
} }
@ -443,7 +443,7 @@ public class TileEntityRotaryCondensentrator extends TileEntityElectricBlock imp
@Override @Override
public int receiveGas(ForgeDirection side, GasStack stack) public int receiveGas(ForgeDirection side, GasStack stack)
{ {
return gasTank.fill(stack, true); return gasTank.receive(stack, true);
} }
@Override @Override

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.4 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB