Mekanism-tilera-Edition/src/main/java/mekanism/common/tile/TileEntityAdvancedElectricMachine.java

470 lines
13 KiB
Java
Raw Normal View History

package mekanism.common.tile;
2015-03-05 14:24:00 +01:00
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
2015-03-05 14:24:00 +01:00
import mekanism.api.Coord4D;
2013-12-01 06:03:40 +01:00
import mekanism.api.EnumColor;
2015-03-05 14:24:00 +01:00
import mekanism.api.Range4D;
import mekanism.api.gas.Gas;
import mekanism.api.gas.GasStack;
import mekanism.api.gas.GasTank;
import mekanism.api.gas.IGasHandler;
import mekanism.api.gas.ITubeConnection;
import mekanism.api.transmitters.TransmissionType;
import mekanism.common.Mekanism;
2015-03-05 14:24:00 +01:00
import mekanism.common.MekanismBlocks;
import mekanism.common.MekanismItems;
import mekanism.common.SideData;
import mekanism.common.Upgrade;
2015-03-05 14:24:00 +01:00
import mekanism.common.base.IFactory.RecipeType;
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
import mekanism.common.recipe.RecipeHandler;
2014-09-05 19:32:54 +02:00
import mekanism.common.recipe.inputs.AdvancedMachineInput;
import mekanism.common.recipe.machines.AdvancedMachineRecipe;
import mekanism.common.recipe.outputs.ItemStackOutput;
2015-03-21 19:16:42 +01:00
import mekanism.common.tile.component.TileComponentAdvancedUpgrade;
import mekanism.common.tile.component.TileComponentConfig;
import mekanism.common.tile.component.TileComponentEjector;
import mekanism.common.tile.component.TileComponentUpgrade;
import mekanism.common.util.ChargeUtils;
2013-12-16 05:01:36 +01:00
import mekanism.common.util.InventoryUtils;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.MekanismUtils.ResourceType;
import mekanism.common.util.StatUtils;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
2014-06-26 22:07:46 +02:00
import cpw.mods.fml.common.Optional.Method;
import dan200.computercraft.api.lua.ILuaContext;
Update lots of APIs, most notably BC and an actual 1.7 version of CC Conflicts: build.properties src/api/java/buildcraft/api/core/BlockIndex.java src/api/java/buildcraft/api/core/BuildCraftAPI.java src/api/java/buildcraft/api/fuels/IFuel.java src/api/java/buildcraft/api/mj/BatteryObject.java src/api/java/buildcraft/api/mj/IBatteryIOObject.java src/api/java/buildcraft/api/mj/IBatteryObject.java src/api/java/buildcraft/api/mj/IOMode.java src/api/java/buildcraft/api/mj/MjAPI.java src/api/java/buildcraft/api/mj/MjBattery.java src/api/java/buildcraft/api/power/PowerHandler.java src/api/java/buildcraft/api/recipes/CraftingResult.java src/api/java/buildcraft/api/recipes/IAssemblyRecipeManager.java src/api/java/buildcraft/api/recipes/IFlexibleRecipe.java src/api/java/buildcraft/api/recipes/IRefineryRecipeManager.java src/api/java/buildcraft/api/transport/IPipe.java src/main/java/mekanism/common/CommonProxy.java src/main/java/mekanism/common/EnergyNetwork.java src/main/java/mekanism/common/FuelHandler.java src/main/java/mekanism/common/Mekanism.java src/main/java/mekanism/common/base/IAdvancedBoundingBlock.java src/main/java/mekanism/common/multipart/MultipartMekanism.java src/main/java/mekanism/common/multipart/PartLogisticalTransporter.java src/main/java/mekanism/common/multipart/PartUniversalCable.java src/main/java/mekanism/common/network/PacketConfigSync.java src/main/java/mekanism/common/tile/TileEntityAdvancedBoundingBlock.java src/main/java/mekanism/common/tile/TileEntityElectricBlock.java src/main/java/mekanism/common/util/CableUtils.java src/main/java/mekanism/common/util/MekanismUtils.java src/main/java/mekanism/common/util/TransporterUtils.java src/main/java/mekanism/generators/common/MekanismGenerators.java src/main/java/mekanism/generators/common/tile/TileEntityBioGenerator.java src/main/java/mekanism/generators/common/tile/TileEntityGasGenerator.java src/main/java/mekanism/generators/common/tile/TileEntityGenerator.java src/main/java/mekanism/generators/common/tile/TileEntityHeatGenerator.java src/main/java/mekanism/generators/common/tile/TileEntitySolarGenerator.java src/main/java/mekanism/generators/common/tile/TileEntityWindTurbine.java
2014-11-10 22:51:37 +01:00
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.peripheral.IComputerAccess;
public abstract class TileEntityAdvancedElectricMachine<RECIPE extends AdvancedMachineRecipe<RECIPE>> extends TileEntityBasicMachine<AdvancedMachineInput, ItemStackOutput, RECIPE> implements IGasHandler, ITubeConnection
{
/** How much secondary energy (fuel) this machine uses per tick, not including upgrades. */
public int BASE_SECONDARY_ENERGY_PER_TICK;
/** How much secondary energy this machine uses per tick, including upgrades. */
public double secondaryEnergyPerTick;
2015-03-21 18:21:02 +01:00
public int secondaryEnergyThisTick;
public static int MAX_GAS = 210;
public GasTank gasTank;
2015-03-21 18:21:02 +01:00
public Gas prevGas;
/**
* Advanced Electric Machine -- a machine like this has a total of 4 slots. Input slot (0), fuel slot (1), output slot (2),
* energy slot (3), and the upgrade slot (4). The machine will not run if it does not have enough electricity, or if it doesn't have enough
* fuel ticks.
*
* @param soundPath - location of the sound effect
* @param name - full name of this machine
* @param perTick - how much energy this machine uses per tick.
* @param secondaryPerTick - how much secondary energy (fuel) this machine uses per tick.
* @param ticksRequired - how many ticks it takes to smelt an item.
* @param maxEnergy - maximum amount of energy this machine can hold.
*/
public TileEntityAdvancedElectricMachine(String soundPath, String name, double perTick, int secondaryPerTick, int ticksRequired, double maxEnergy)
{
super(soundPath, name, MekanismUtils.getResource(ResourceType.GUI, "GuiAdvancedMachine.png"), perTick, ticksRequired, maxEnergy);
configComponent = new TileComponentConfig(this, TransmissionType.ITEM, TransmissionType.ENERGY);
2015-03-08 19:27:34 +01:00
configComponent.addOutput(TransmissionType.ITEM, new SideData("None", EnumColor.GREY, InventoryUtils.EMPTY));
configComponent.addOutput(TransmissionType.ITEM, new SideData("Input", EnumColor.DARK_RED, new int[] {0}));
configComponent.addOutput(TransmissionType.ITEM, new SideData("Extra", EnumColor.PURPLE, new int[] {1}));
configComponent.addOutput(TransmissionType.ITEM, new SideData("Output", EnumColor.DARK_BLUE, new int[] {2}));
configComponent.addOutput(TransmissionType.ITEM, new SideData("Energy", EnumColor.DARK_GREEN, new int[] {3}));
2015-03-08 19:27:34 +01:00
configComponent.setConfig(TransmissionType.ITEM, new byte[] {2, 1, 0, 4, 0, 3});
configComponent.setInputEnergyConfig();
2014-01-17 02:50:59 +01:00
gasTank = new GasTank(MAX_GAS);
inventory = new ItemStack[5];
BASE_SECONDARY_ENERGY_PER_TICK = secondaryPerTick;
secondaryEnergyPerTick = secondaryPerTick;
2015-03-21 19:16:42 +01:00
upgradeComponent = upgradeableSecondaryEfficiency() ? new TileComponentAdvancedUpgrade(this, 4) : new TileComponentUpgrade(this, 4);
ejectorComponent = new TileComponentEjector(this, configComponent.getOutputs(TransmissionType.ITEM).get(3));
}
2015-03-05 14:24:00 +01:00
public void upgrade(RecipeType type)
{
worldObj.setBlockToAir(xCoord, yCoord, zCoord);
worldObj.setBlock(xCoord, yCoord, zCoord, MekanismBlocks.MachineBlock, 5, 3);
TileEntityFactory factory = (TileEntityFactory)worldObj.getTileEntity(xCoord, yCoord, zCoord);
//Basic
factory.facing = facing;
factory.clientFacing = clientFacing;
factory.ticker = ticker;
factory.redstone = redstone;
factory.redstoneLastTick = redstoneLastTick;
factory.doAutoSync = doAutoSync;
//Electric
factory.electricityStored = electricityStored;
factory.ic2Registered = ic2Registered;
2015-03-05 14:24:00 +01:00
//Noisy
factory.soundURL = soundURL;
//Machine
factory.progress[0] = operatingTicks;
factory.updateDelay = updateDelay;
factory.isActive = isActive;
factory.clientActive = clientActive;
factory.controlType = controlType;
factory.prevEnergy = prevEnergy;
factory.upgradeComponent = upgradeComponent;
factory.upgradeComponent.setUpgradeSlot(0);
factory.upgradeComponent.tileEntity = factory;
factory.ejectorComponent = ejectorComponent;
2015-03-08 19:27:34 +01:00
factory.ejectorComponent.sideData = factory.configComponent.getOutputs(TransmissionType.ITEM).get(4);
2015-03-05 14:24:00 +01:00
factory.ejectorComponent.tileEntity = factory;
factory.ejectorComponent.trackers = new int[factory.ejectorComponent.sideData.availableSlots.length];
2015-03-05 14:24:00 +01:00
factory.recipeType = type;
2015-03-21 20:07:24 +01:00
factory.upgradeComponent.setSupported(Upgrade.GAS, type.fuelEnergyUpgrades());
2015-03-05 14:24:00 +01:00
//Advanced Machine
factory.gasTank.setGas(gasTank.getGas());
factory.inventory[5] = inventory[0];
factory.inventory[4] = inventory[1];
factory.inventory[5+3] = inventory[2];
factory.inventory[1] = inventory[3];
factory.inventory[0] = inventory[4];
for(Upgrade upgrade : upgradeComponent.getSupportedTypes())
{
factory.recalculateUpgradables(upgrade);
}
factory.upgraded = true;
factory.markDirty();
Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(Coord4D.get(factory), factory.getNetworkedData(new ArrayList())), new Range4D(Coord4D.get(factory)));
}
/**
* Gets the amount of ticks the declared itemstack can fuel this machine.
* @param itemstack - itemstack to check with
* @return fuel ticks
*/
public abstract GasStack getItemGas(ItemStack itemstack);
public abstract boolean isValidGas(Gas gas);
@Override
public void onUpdate()
{
super.onUpdate();
if(!worldObj.isRemote)
{
ChargeUtils.discharge(3, this);
handleSecondaryFuel();
2015-03-21 18:21:02 +01:00
boolean inactive = false;
RECIPE recipe = getRecipe();
2015-03-21 18:21:02 +01:00
secondaryEnergyThisTick = useStatisticalMechanics() ? StatUtils.inversePoisson(secondaryEnergyPerTick) : (int)Math.ceil(secondaryEnergyPerTick);
if(canOperate(recipe) && MekanismUtils.canFunction(this) && getEnergy() >= energyPerTick && gasTank.getStored() >= secondaryEnergyThisTick)
{
setActive(true);
operatingTicks++;
if(operatingTicks >= ticksRequired)
{
operate(recipe);
operatingTicks = 0;
}
2015-03-21 18:21:02 +01:00
gasTank.draw(secondaryEnergyThisTick, true);
electricityStored -= energyPerTick;
}
else {
2015-03-21 18:21:02 +01:00
inactive = true;
setActive(false);
}
2015-03-21 18:21:02 +01:00
if(inactive && getRecipe() == null)
{
operatingTicks = 0;
}
prevEnergy = getEnergy();
2015-03-21 18:21:02 +01:00
if(!(gasTank.getGasType() == null || gasTank.getStored() == 0))
{
prevGas = gasTank.getGasType();
}
}
}
public void handleSecondaryFuel()
{
if(inventory[1] != null && gasTank.getNeeded() > 0)
{
GasStack stack = getItemGas(inventory[1]);
int gasNeeded = gasTank.getNeeded();
if(stack != null && gasTank.canReceive(stack.getGas()) && gasNeeded >= stack.amount)
{
gasTank.receive(stack, true);
inventory[1].stackSize--;
if(inventory[1].stackSize == 0)
{
inventory[1] = null;
}
}
}
}
public boolean upgradeableSecondaryEfficiency()
{
return false;
}
public boolean useStatisticalMechanics()
{
return false;
}
@Override
2013-07-20 18:10:14 +02:00
public boolean isItemValidForSlot(int slotID, ItemStack itemstack)
{
if(slotID == 2)
{
return false;
}
else if(slotID == 4)
{
return itemstack.getItem() == MekanismItems.SpeedUpgrade || itemstack.getItem() == MekanismItems.EnergyUpgrade;
}
else if(slotID == 0)
{
return RecipeHandler.getRecipe(new AdvancedMachineInput(itemstack, gasTank.getGasType()), getRecipes()) != null;
}
else if(slotID == 3)
{
return ChargeUtils.canBeDischarged(itemstack);
}
else if(slotID == 1)
{
return getItemGas(itemstack) != null;
}
return false;
}
@Override
public AdvancedMachineInput getInput()
{
2015-03-21 18:21:02 +01:00
return new AdvancedMachineInput(inventory[0], prevGas);
}
@Override
public RECIPE getRecipe()
{
AdvancedMachineInput input = getInput();
2015-03-21 18:21:02 +01:00
if(cachedRecipe == null || !input.testEquality(cachedRecipe.getInput()))
{
cachedRecipe = RecipeHandler.getRecipe(input, getRecipes());
}
2015-03-21 18:21:02 +01:00
return cachedRecipe;
}
@Override
public void operate(RECIPE recipe)
{
2015-03-21 18:21:02 +01:00
recipe.operate(inventory, 0, 2, gasTank, secondaryEnergyThisTick);
markDirty();
ejectorComponent.onOutput();
}
@Override
public boolean canOperate(RECIPE recipe)
{
2015-03-21 18:21:02 +01:00
return recipe != null && recipe.canOperate(inventory, 0, 2, gasTank, secondaryEnergyThisTick);
}
v5 Beta #9 *Bumped animation textures to 32x32. *Added default parameter to TabProxy.tabMekanism(). *Added additional info to Machine/GeneratorType for better handling of metadata. *Added Reinforced Iron, a stronger version of an Iron Block. *Updated onBlockActivated() code to function correctly. *Added feature for a generator or power unit to be placed facing up or down. *Cleaned up GUI access/handler code. *Fixed electric machine shift-click bug. *Added Diamond Dust. *Fixed Energized Bow continuing to fire after it's energy is depleted. *Added HP information to armor and tools. *Fixed slot parameters. *Overhauled packet system. *Cleaned up tile entity hierarchy. *Added BuildCraft liquid support to Heat Generator to allow energy generation with both BuildCraft's fuel buckets and liquid fuel. *Fixed texture preloads. *Added Electolytic Separator -- a machine that separates hydrogen and oxygen molecules from water. It accepts water from BuildCraft pipes. *Added Hydrogen Generator -- a generator that by default generates 128 u/t, but has boosts by the block's height of up to 512 u/t. *Added Solar Generator, a generator that produces 32 u/t when it can see the sun. *Added Gas API! Simple gas management that allows for both storage of gas in items, blocks, and transfer between themselves. So far implemented gasses are oxygen and hydrogen. *Added LiquidSlot for easy management of liquid in tile entities. *Added Hydrogen Tank and Oxygen Tank items. *Added BuildCraft hooks. *Fixed zombies and skeletons spawning with Obsidian Armor, and lowered chances of spawning with any armor. *More OreDictionary registrations to fix IC2's different dust names. *Fixed some javadocs. *Added 'Solar Panel' item as a crafting element for a Solar Generator. *Minor bugfixes.
2012-11-15 21:04:12 +01:00
@Override
public void handlePacketData(ByteBuf dataStream)
{
super.handlePacketData(dataStream);
if(dataStream.readBoolean())
{
gasTank.setGas(new GasStack(dataStream.readInt(), dataStream.readInt()));
}
else {
gasTank.setGas(null);
}
}
@Override
public ArrayList getNetworkedData(ArrayList data)
{
super.getNetworkedData(data);
if(gasTank.getGas() != null)
{
data.add(true);
data.add(gasTank.getGas().getGas().getID());
data.add(gasTank.getStored());
}
else {
data.add(false);
}
return data;
}
@Override
public void readFromNBT(NBTTagCompound nbtTags)
{
super.readFromNBT(nbtTags);
gasTank.read(nbtTags.getCompoundTag("gasTank"));
2015-03-21 18:21:02 +01:00
gasTank.setMaxGas(MAX_GAS);
}
@Override
public void writeToNBT(NBTTagCompound nbtTags)
{
super.writeToNBT(nbtTags);
nbtTags.setTag("gasTank", gasTank.write(new NBTTagCompound()));
}
/**
* Gets the scaled secondary energy level for the GUI.
* @param i - multiplier
* @return scaled secondary energy
*/
public int getScaledGasLevel(int i)
{
return gasTank.getStored()*i / gasTank.getMaxGas();
}
@Override
public boolean canExtractItem(int slotID, ItemStack itemstack, int side)
{
if(slotID == 3)
{
return ChargeUtils.canBeOutputted(itemstack, false);
}
else if(slotID == 2)
{
return true;
}
return false;
}
@Override
public boolean canTubeConnect(ForgeDirection side)
{
return false;
}
@Override
public int receiveGas(ForgeDirection side, GasStack stack, boolean doTransfer)
{
return 0;
}
@Override
public int receiveGas(ForgeDirection side, GasStack stack)
{
return receiveGas(side, stack, true);
}
@Override
public GasStack drawGas(ForgeDirection side, int amount, boolean doTransfer)
{
return null;
}
@Override
public GasStack drawGas(ForgeDirection side, int amount)
{
return drawGas(side, amount, true);
}
@Override
public boolean canReceiveGas(ForgeDirection side, Gas type)
{
return false;
}
@Override
public boolean canDrawGas(ForgeDirection side, Gas type)
{
return false;
}
@Override
public void recalculateUpgradables(Upgrade upgrade)
{
super.recalculateUpgradables(upgrade);
2015-03-21 20:44:17 +01:00
if(upgrade == Upgrade.SPEED || (upgradeableSecondaryEfficiency() && upgrade == Upgrade.GAS))
{
2015-03-21 19:16:42 +01:00
secondaryEnergyPerTick = MekanismUtils.getSecondaryEnergyPerTickMean(this, BASE_SECONDARY_ENERGY_PER_TICK);
}
}
@Override
2014-06-26 22:07:46 +02:00
@Method(modid = "ComputerCraft")
public String[] getMethodNames()
{
return new String[] {"getStored", "getSecondaryStored", "getProgress", "isActive", "facing", "canOperate", "getMaxEnergy", "getEnergyNeeded"};
}
@Override
2014-06-26 22:07:46 +02:00
@Method(modid = "ComputerCraft")
Update lots of APIs, most notably BC and an actual 1.7 version of CC Conflicts: build.properties src/api/java/buildcraft/api/core/BlockIndex.java src/api/java/buildcraft/api/core/BuildCraftAPI.java src/api/java/buildcraft/api/fuels/IFuel.java src/api/java/buildcraft/api/mj/BatteryObject.java src/api/java/buildcraft/api/mj/IBatteryIOObject.java src/api/java/buildcraft/api/mj/IBatteryObject.java src/api/java/buildcraft/api/mj/IOMode.java src/api/java/buildcraft/api/mj/MjAPI.java src/api/java/buildcraft/api/mj/MjBattery.java src/api/java/buildcraft/api/power/PowerHandler.java src/api/java/buildcraft/api/recipes/CraftingResult.java src/api/java/buildcraft/api/recipes/IAssemblyRecipeManager.java src/api/java/buildcraft/api/recipes/IFlexibleRecipe.java src/api/java/buildcraft/api/recipes/IRefineryRecipeManager.java src/api/java/buildcraft/api/transport/IPipe.java src/main/java/mekanism/common/CommonProxy.java src/main/java/mekanism/common/EnergyNetwork.java src/main/java/mekanism/common/FuelHandler.java src/main/java/mekanism/common/Mekanism.java src/main/java/mekanism/common/base/IAdvancedBoundingBlock.java src/main/java/mekanism/common/multipart/MultipartMekanism.java src/main/java/mekanism/common/multipart/PartLogisticalTransporter.java src/main/java/mekanism/common/multipart/PartUniversalCable.java src/main/java/mekanism/common/network/PacketConfigSync.java src/main/java/mekanism/common/tile/TileEntityAdvancedBoundingBlock.java src/main/java/mekanism/common/tile/TileEntityElectricBlock.java src/main/java/mekanism/common/util/CableUtils.java src/main/java/mekanism/common/util/MekanismUtils.java src/main/java/mekanism/common/util/TransporterUtils.java src/main/java/mekanism/generators/common/MekanismGenerators.java src/main/java/mekanism/generators/common/tile/TileEntityBioGenerator.java src/main/java/mekanism/generators/common/tile/TileEntityGasGenerator.java src/main/java/mekanism/generators/common/tile/TileEntityGenerator.java src/main/java/mekanism/generators/common/tile/TileEntityHeatGenerator.java src/main/java/mekanism/generators/common/tile/TileEntitySolarGenerator.java src/main/java/mekanism/generators/common/tile/TileEntityWindTurbine.java
2014-11-10 22:51:37 +01:00
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws LuaException, InterruptedException
{
switch(method)
{
case 0:
return new Object[] {getEnergy()};
case 1:
return new Object[] {gasTank.getStored()};
case 2:
return new Object[] {operatingTicks};
case 3:
return new Object[] {isActive};
case 4:
return new Object[] {facing};
case 5:
return new Object[] {canOperate(RecipeHandler.getRecipe(getInput(), getRecipes()))};
case 6:
return new Object[] {maxEnergy};
case 7:
return new Object[] {maxEnergy-getEnergy()};
default:
2014-06-03 08:46:03 +02:00
Mekanism.logger.error("Attempted to call unknown method with computer ID " + computer.getID());
return new Object[] {"Unknown command."};
}
}
}