Work on TileEntity code of Chemical Formulator

This commit is contained in:
Aidan Brady 2013-12-11 19:53:12 -05:00
parent 7a3c678d76
commit 6114463852
7 changed files with 831 additions and 7 deletions

View file

@ -281,6 +281,8 @@ public class Mekanism
public static double energizedSmelterUsage;
public static double digitalMinerUsage;
public static double rotaryCondensentratorUsage;
public static double chemicalFormulatorUsage;
public static double chemicalInfuserUsage;
/**
* Adds all in-game crafting and smelting recipes.

View file

@ -168,7 +168,7 @@ public abstract class TileEntityAdvancedElectricMachine extends TileEntityBasicM
return getFuelTicks(itemstack) > 0;
}
return true;
return false;
}
@Override

View file

@ -1,9 +1,372 @@
package mekanism.common.tileentity;
public class TileEntityChemicalFormulator extends TileEntityElectricBlock
import java.util.ArrayList;
import mekanism.api.Object3D;
import mekanism.api.gas.GasRegistry;
import mekanism.api.gas.GasStack;
import mekanism.api.gas.GasTransmission;
import mekanism.api.gas.IGasAcceptor;
import mekanism.api.gas.IGasItem;
import mekanism.api.gas.IGasStorage;
import mekanism.api.gas.ITubeConnection;
import mekanism.common.IActiveState;
import mekanism.common.IRedstoneControl;
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.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import com.google.common.io.ByteArrayDataInput;
public class TileEntityChemicalFormulator extends TileEntityElectricBlock implements IActiveState, IGasStorage, ITubeConnection, IRedstoneControl
{
public GasStack gasTank;
public static final int MAX_GAS = 10000;
public int updateDelay;
public int gasOutput = 16;
public boolean isActive;
public boolean clientActive;
public double prevEnergy;
public int operatingTicks = 0;
public int TICKS_REQUIRED = 100;
public final double ENERGY_USAGE = Mekanism.rotaryCondensentratorUsage;
public RedstoneControl controlType = RedstoneControl.DISABLED;
public TileEntityChemicalFormulator()
{
super("ChemicalFormulator", 0 /*TODO*/);
inventory = new ItemStack[3];
}
@Override
public void onUpdate()
{
if(worldObj.isRemote)
{
if(updateDelay > 0)
{
updateDelay--;
if(updateDelay == 0 && clientActive != isActive)
{
isActive = clientActive;
MekanismUtils.updateBlock(worldObj, xCoord, yCoord, zCoord);
}
}
}
if(!worldObj.isRemote)
{
if(updateDelay > 0)
{
updateDelay--;
if(updateDelay == 0 && clientActive != isActive)
{
PacketHandler.sendPacket(Transmission.ALL_CLIENTS, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())));
}
}
ChargeUtils.discharge(1, this);
if(getGas() != null)
{
if(inventory[2] != null)
{
setGas(new GasStack(getGas().getGas(), getGas().amount - GasTransmission.addGas(inventory[2], getGas())));
}
}
if(canOperate() && getEnergy() >= ENERGY_USAGE && MekanismUtils.canFunction(this))
{
setActive(true);
setEnergy(getEnergy() - ENERGY_USAGE);
if(operatingTicks < TICKS_REQUIRED)
{
operatingTicks++;
}
else {
GasStack stack = RecipeHandler.getChemicalFormulatorOutput(inventory[0], true);
setGas(new GasStack(stack.getGas(), getStoredGas()+stack.amount));
operatingTicks = 0;
}
}
else {
if(prevEnergy >= getEnergy())
{
setActive(false);
}
}
prevEnergy = getEnergy();
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.getRight(facing)).getTileEntity(worldObj);
if(tileEntity instanceof IGasAcceptor)
{
if(((IGasAcceptor)tileEntity).canReceiveGas(MekanismUtils.getLeft(facing).getOpposite(), getGas().getGas()))
{
int added = ((IGasAcceptor)tileEntity).receiveGas(new GasStack(getGas().getGas(), Math.min(getGas().amount, gasOutput)));
setGas(new GasStack(getGas().getGas(), getGas().amount - added));
}
}
}
}
}
@Override
public boolean isItemValidForSlot(int slotID, ItemStack itemstack)
{
if(slotID == 0)
{
return RecipeHandler.getChemicalFormulatorOutput(itemstack, false) != null;
}
else if(slotID == 1)
{
return ChargeUtils.canBeDischarged(itemstack);
}
return false;
}
@Override
public boolean canExtractItem(int slotID, ItemStack itemstack, int side)
{
if(slotID == 2)
{
return inventory[2] != null && inventory[2].getItem() instanceof IGasItem && ((IGasItem)inventory[2].getItem()).canProvideGas(inventory[2], null);
}
return false;
}
@Override
public int[] getAccessibleSlotsFromSide(int side)
{
if(side == MekanismUtils.getLeft(facing).ordinal())
{
return new int[] {0};
}
else if(side == 0 || side == 1)
{
return new int[] {1};
}
else if(side == MekanismUtils.getRight(facing).ordinal())
{
return new int[] {2};
}
return new int[0];
}
public int getStoredGas()
{
return gasTank != null ? gasTank.amount : 0;
}
public int getScaledProgress(int i)
{
return operatingTicks*i / TICKS_REQUIRED;
}
public boolean canOperate()
{
if(inventory[0] == null)
{
return false;
}
GasStack stack = RecipeHandler.getChemicalFormulatorOutput(inventory[0], false);
if(stack == null || (getGas() != null && (getGas().getGas() != stack.getGas() || getMaxGas()-getGas().amount < stack.amount)))
{
return false;
}
return true;
}
@Override
public void handlePacketData(ByteArrayDataInput dataStream)
{
super.handlePacketData(dataStream);
isActive = dataStream.readBoolean();
controlType = RedstoneControl.values()[dataStream.readInt()];
operatingTicks = dataStream.readInt();
if(dataStream.readBoolean())
{
gasTank = new GasStack(GasRegistry.getGas(dataStream.readInt()), dataStream.readInt());
}
else {
gasTank = null;
}
MekanismUtils.updateBlock(worldObj, xCoord, yCoord, zCoord);
}
@Override
public ArrayList getNetworkedData(ArrayList data)
{
super.getNetworkedData(data);
data.add(isActive);
data.add(controlType.ordinal());
data.add(operatingTicks);
if(gasTank != null)
{
data.add(true);
data.add(gasTank.getGas().getID());
data.add(gasTank.amount);
}
else {
data.add(false);
}
return data;
}
@Override
public void readFromNBT(NBTTagCompound nbtTags)
{
super.readFromNBT(nbtTags);
isActive = nbtTags.getBoolean("isActive");
controlType = RedstoneControl.values()[nbtTags.getInteger("controlType")];
operatingTicks = nbtTags.getInteger("operatingTicks");
gasTank = GasStack.readFromNBT(nbtTags.getCompoundTag("gasTank"));
}
@Override
public void writeToNBT(NBTTagCompound nbtTags)
{
super.writeToNBT(nbtTags);
nbtTags.setBoolean("isActive", isActive);
nbtTags.setInteger("controlType", controlType.ordinal());
nbtTags.setInteger("operatingTicks", operatingTicks);
if(gasTank != null)
{
nbtTags.setCompoundTag("gasTank", gasTank.write(new NBTTagCompound()));
}
}
@Override
public boolean canSetFacing(int i)
{
return i != 0 && i != 1;
}
public int getScaledGasLevel(int i)
{
return gasTank != null ? gasTank.amount*i / MAX_GAS : 0;
}
@Override
public void setActive(boolean active)
{
isActive = active;
if(clientActive != active && updateDelay == 0)
{
PacketHandler.sendPacket(Transmission.ALL_CLIENTS, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())));
updateDelay = 10;
clientActive = active;
}
}
@Override
public boolean getActive()
{
return isActive;
}
@Override
public boolean renderUpdate()
{
return false;
}
@Override
public boolean lightUpdate()
{
return true;
}
@Override
public boolean canTubeConnect(ForgeDirection side)
{
return side == MekanismUtils.getRight(facing);
}
@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
public RedstoneControl getControlType()
{
return controlType;
}
@Override
public void setControlType(RedstoneControl type)
{
controlType = type;
MekanismUtils.saveChunk(this);
}
}

View file

@ -1,14 +1,473 @@
package mekanism.common.tileentity;
import mekanism.api.gas.GasStack;
import java.util.ArrayList;
public class TileEntityChemicalInfuser extends TileEntityElectricBlock
import mekanism.api.Object3D;
import mekanism.api.gas.Gas;
import mekanism.api.gas.GasRegistry;
import mekanism.api.gas.GasStack;
import mekanism.api.gas.GasTransmission;
import mekanism.api.gas.IGasAcceptor;
import mekanism.common.IRedstoneControl.RedstoneControl;
import mekanism.common.Mekanism;
import mekanism.common.PacketHandler;
import mekanism.common.PacketHandler.Transmission;
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.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import com.google.common.io.ByteArrayDataInput;
public class TileEntityChemicalInfuser extends TileEntityElectricBlock //implements IActiveState, IGasStorage, IGasAcceptor, ITubeConnection, IRedstoneControl
{
public GasStack leftStack;
public GasStack rightStack;
public GasStack centerStack;
public static final int MAX_GAS = 10000;
public int updateDelay;
public int gasOutput = 16;
public boolean isActive;
public boolean clientActive;
public double prevEnergy;
public final double ENERGY_USAGE = Mekanism.chemicalInfuserUsage;
/** This machine's current RedstoneControl type. */
public RedstoneControl controlType = RedstoneControl.DISABLED;
public TileEntityChemicalInfuser()
{
super("ChemicalInfuser", 0 /*TODO*/);
inventory = new ItemStack[4];
}
/*@Override
public void onUpdate()
{
if(worldObj.isRemote)
{
if(updateDelay > 0)
{
updateDelay--;
if(updateDelay == 0 && clientActive != isActive)
{
isActive = clientActive;
MekanismUtils.updateBlock(worldObj, xCoord, yCoord, zCoord);
}
}
}
if(!worldObj.isRemote)
{
if(updateDelay > 0)
{
updateDelay--;
if(updateDelay == 0 && clientActive != isActive)
{
PacketHandler.sendPacket(Transmission.ALL_CLIENTS, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())));
}
}
ChargeUtils.discharge(4, this);
if(mode == 0)
{
if(inventory[1] != null && (getGas() == null || getGas().amount < getMaxGas()))
{
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()))))
{
setActive(true);
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(getGas() != null)
{
if(inventory[0] != null)
{
setGas(new GasStack(getGas().getGas(), getGas().amount - GasTransmission.addGas(inventory[0], getGas())));
}
}
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 IGasAcceptor)
{
if(((IGasAcceptor)tileEntity).canReceiveGas(MekanismUtils.getLeft(facing).getOpposite(), getGas().getGas()))
{
int added = ((IGasAcceptor)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.drain(1, true);
setEnergy(getEnergy() - ENERGY_USAGE);
}
else {
if(prevEnergy >= getEnergy())
{
setActive(false);
}
}
}
prevEnergy = getEnergy();
}
}
public boolean isValidGas(GasStack g)
{
if(g == null)
{
return false;
}
return g.getGas().hasFluid();
}
public boolean gasEquals(GasStack gas, FluidStack fluid)
{
if(fluid == null || gas == null || !gas.getGas().hasFluid())
{
return false;
}
return gas.getGas().getFluid() == fluid.getFluid();
}
public boolean isValidFluid(FluidStack f)
{
if(f == null)
{
return false;
}
return GasRegistry.getGas(f.getFluid()) != null;
}
@Override
public void handlePacketData(ByteArrayDataInput dataStream)
{
if(!worldObj.isRemote)
{
int type = dataStream.readInt();
if(type == 0)
{
mode = mode == 0 ? 1 : 0;
}
for(EntityPlayer player : playersUsing)
{
PacketHandler.sendPacket(Transmission.SINGLE_CLIENT, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())), player);
}
return;
}
super.handlePacketData(dataStream);
mode = dataStream.readInt();
isActive = dataStream.readBoolean();
controlType = RedstoneControl.values()[dataStream.readInt()];
if(dataStream.readBoolean())
{
fluidTank.setFluid(new FluidStack(dataStream.readInt(), dataStream.readInt()));
}
else {
fluidTank.setFluid(null);
}
if(dataStream.readBoolean())
{
gasTank = new GasStack(GasRegistry.getGas(dataStream.readInt()), dataStream.readInt());
}
else {
gasTank = null;
}
MekanismUtils.updateBlock(worldObj, xCoord, yCoord, zCoord);
}
@Override
public ArrayList getNetworkedData(ArrayList data)
{
super.getNetworkedData(data);
data.add(mode);
data.add(isActive);
data.add(controlType.ordinal());
if(fluidTank.getFluid() != null)
{
data.add(true);
data.add(fluidTank.getFluid().fluidID);
data.add(fluidTank.getFluid().amount);
}
else {
data.add(false);
}
if(gasTank != null)
{
data.add(true);
data.add(gasTank.getGas().getID());
data.add(gasTank.amount);
}
else {
data.add(false);
}
return data;
}
@Override
public void readFromNBT(NBTTagCompound nbtTags)
{
super.readFromNBT(nbtTags);
mode = nbtTags.getInteger("mode");
isActive = nbtTags.getBoolean("isActive");
controlType = RedstoneControl.values()[nbtTags.getInteger("controlType")];
gasTank = GasStack.readFromNBT(nbtTags.getCompoundTag("gasTank"));
if(nbtTags.hasKey("fluidTank"))
{
fluidTank.readFromNBT(nbtTags.getCompoundTag("fluidTank"));
}
}
@Override
public void writeToNBT(NBTTagCompound nbtTags)
{
super.writeToNBT(nbtTags);
nbtTags.setInteger("mode", mode);
nbtTags.setBoolean("isActive", isActive);
nbtTags.setInteger("controlType", controlType.ordinal());
if(gasTank != null)
{
nbtTags.setCompoundTag("gasTank", gasTank.write(new NBTTagCompound()));
}
if(fluidTank.getFluid() != null)
{
nbtTags.setTag("fluidTank", fluidTank.writeToNBT(new NBTTagCompound()));
}
}
@Override
public boolean canSetFacing(int i)
{
return i != 0 && i != 1;
}
public int getScaledFluidLevel(int i)
{
return fluidTank.getFluid() != null ? fluidTank.getFluid().amount*i / 10000 : 0;
}
public int getScaledGasLevel(int i)
{
return gasTank != null ? gasTank.amount*i / MAX_GAS : 0;
}
@Override
public void setActive(boolean active)
{
isActive = active;
if(clientActive != active && updateDelay == 0)
{
PacketHandler.sendPacket(Transmission.ALL_CLIENTS, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())));
updateDelay = 10;
clientActive = active;
}
}
@Override
public boolean getActive()
{
return isActive;
}
@Override
public boolean renderUpdate()
{
return false;
}
@Override
public boolean lightUpdate()
{
return true;
}
@Override
public boolean canTubeConnect(ForgeDirection side)
{
return side == MekanismUtils.getLeft(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
public boolean canReceiveGas(ForgeDirection side, Gas type)
{
return mode == 0 && (getGas() == null || getGas().getGas() == type) && side == MekanismUtils.getLeft(facing);
}
@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
public RedstoneControl getControlType()
{
return controlType;
}
@Override
public void setControlType(RedstoneControl type)
{
controlType = type;
MekanismUtils.saveChunk(this);
}*/
}

View file

@ -106,7 +106,7 @@ public abstract class TileEntityElectricMachine extends TileEntityBasicMachine
return ChargeUtils.canBeDischarged(itemstack);
}
return true;
return false;
}
@Override

View file

@ -449,7 +449,7 @@ public class TileEntityFactory extends TileEntityElectricBlock implements IPerip
return RecipeType.values()[recipeType].getFuelTicks(itemstack) > 0;
}
return true;
return false;
}
@Override

View file

@ -247,7 +247,7 @@ public class TileEntityMetallurgicInfuser extends TileEntityElectricBlock implem
return ChargeUtils.canBeDischarged(itemstack);
}
return true;
return false;
}
public void operate()