2012-11-05 20:29:04 +01:00
|
|
|
package mekanism.common;
|
2012-10-02 20:39:40 +02:00
|
|
|
|
2012-12-20 22:53:39 +01:00
|
|
|
import ic2.api.ElectricItem;
|
|
|
|
import ic2.api.IElectricItem;
|
2012-11-09 03:22:18 +01:00
|
|
|
import mekanism.api.IMachineUpgrade;
|
2012-12-20 22:53:39 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.network.INetworkManager;
|
|
|
|
import net.minecraft.network.packet.Packet250CustomPayload;
|
2012-12-21 14:30:40 +01:00
|
|
|
import net.minecraftforge.common.ForgeDirection;
|
2012-11-21 17:38:37 +01:00
|
|
|
import universalelectricity.core.implement.IItemElectric;
|
2012-10-02 20:39:40 +02:00
|
|
|
|
|
|
|
import com.google.common.io.ByteArrayDataInput;
|
|
|
|
|
2012-10-09 22:27:10 +02:00
|
|
|
import dan200.computer.api.IComputerAccess;
|
|
|
|
|
2012-10-02 20:39:40 +02:00
|
|
|
public abstract class TileEntityAdvancedElectricMachine extends TileEntityBasicMachine
|
|
|
|
{
|
|
|
|
/** How much secondary energy (fuel) this machine uses per tick. */
|
|
|
|
public int SECONDARY_ENERGY_PER_TICK;
|
|
|
|
|
|
|
|
/** Maximum amount of secondary energy (fuel) this machine can hold. */
|
|
|
|
public int MAX_SECONDARY_ENERGY;
|
|
|
|
|
|
|
|
/** How much secondary energy (fuel) is stored in this machine. */
|
|
|
|
public int secondaryEnergyStored = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advanced Electric Machine -- a machine like this has a total of 4 slots. Input slot (0), fuel slot (1), output slot (2),
|
2012-10-22 03:29:26 +02:00
|
|
|
* 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
|
2012-10-02 20:39:40 +02:00
|
|
|
* fuel ticks.
|
|
|
|
*
|
2012-10-22 03:29:26 +02:00
|
|
|
* @param soundPath - location of the sound effect
|
2012-10-17 20:46:27 +02:00
|
|
|
* @param name - full name of this machine
|
|
|
|
* @param path - GUI texture path of this machine
|
2012-10-02 20:39:40 +02:00
|
|
|
* @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.
|
|
|
|
* @param maxSecondaryEnergy - maximum amount of secondary energy (fuel) this machine can hold.
|
|
|
|
*/
|
2012-10-22 03:29:26 +02:00
|
|
|
public TileEntityAdvancedElectricMachine(String soundPath, String name, String path, int perTick, int secondaryPerTick, int ticksRequired, int maxEnergy, int maxSecondaryEnergy)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2012-10-24 22:13:24 +02:00
|
|
|
super(soundPath, name, path, perTick, ticksRequired, maxEnergy);
|
|
|
|
inventory = new ItemStack[5];
|
2012-10-02 20:39:40 +02:00
|
|
|
SECONDARY_ENERGY_PER_TICK = secondaryPerTick;
|
|
|
|
MAX_SECONDARY_ENERGY = maxSecondaryEnergy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the amount of ticks the declared itemstack can fuel this machine.
|
|
|
|
* @param itemstack - itemstack to check with
|
|
|
|
* @return fuel ticks
|
|
|
|
*/
|
|
|
|
public abstract int getFuelTicks(ItemStack itemstack);
|
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-10-02 20:39:40 +02:00
|
|
|
public void onUpdate()
|
|
|
|
{
|
2012-10-30 05:14:25 +01:00
|
|
|
super.onUpdate();
|
2012-12-19 21:23:55 +01:00
|
|
|
|
2012-10-02 20:39:40 +02:00
|
|
|
boolean testActive = operatingTicks > 0;
|
|
|
|
|
|
|
|
if(inventory[3] != null)
|
|
|
|
{
|
2012-11-25 16:45:00 +01:00
|
|
|
if(electricityStored < currentMaxElectricity)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2012-11-25 16:45:00 +01:00
|
|
|
if(inventory[3].getItem() instanceof IItemElectric)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2012-11-29 20:56:38 +01:00
|
|
|
IItemElectric electricItem = (IItemElectric)inventory[3].getItem();
|
2012-10-24 03:16:16 +02:00
|
|
|
|
2012-10-24 22:13:24 +02:00
|
|
|
if (electricItem.canProduceElectricity())
|
|
|
|
{
|
2012-11-29 20:56:38 +01:00
|
|
|
double joulesNeeded = currentMaxElectricity-electricityStored;
|
|
|
|
double joulesReceived = 0;
|
|
|
|
|
2013-01-14 19:30:58 +01:00
|
|
|
if(electricItem.getVoltage(inventory[3]) <= joulesNeeded)
|
2012-11-29 20:56:38 +01:00
|
|
|
{
|
2013-01-14 19:30:58 +01:00
|
|
|
joulesReceived = electricItem.onUse(electricItem.getVoltage(inventory[3]), inventory[3]);
|
2012-11-29 20:56:38 +01:00
|
|
|
}
|
2013-01-14 19:30:58 +01:00
|
|
|
else if(electricItem.getVoltage(inventory[3]) > joulesNeeded)
|
2012-11-29 20:56:38 +01:00
|
|
|
{
|
|
|
|
joulesReceived = electricItem.onUse(joulesNeeded, inventory[3]);
|
|
|
|
}
|
|
|
|
|
2012-11-25 16:45:00 +01:00
|
|
|
setJoules(electricityStored + joulesReceived);
|
2012-10-24 22:13:24 +02:00
|
|
|
}
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|
|
|
|
else if(inventory[3].getItem() instanceof IElectricItem)
|
|
|
|
{
|
|
|
|
IElectricItem item = (IElectricItem)inventory[3].getItem();
|
|
|
|
if(item.canProvideEnergy())
|
|
|
|
{
|
2012-12-03 16:10:48 +01:00
|
|
|
double gain = ElectricItem.discharge(inventory[3], (int)((MAX_ELECTRICITY - electricityStored)*Mekanism.TO_IC2), 3, false, false)*Mekanism.FROM_IC2;
|
2012-11-25 16:45:00 +01:00
|
|
|
setJoules(electricityStored + gain);
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-21 02:15:59 +01:00
|
|
|
if(inventory[3].itemID == Item.redstone.itemID && electricityStored <= (MAX_ELECTRICITY-1000))
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2012-11-25 16:45:00 +01:00
|
|
|
setJoules(electricityStored + 1000);
|
2012-10-02 20:39:40 +02:00
|
|
|
--inventory[3].stackSize;
|
|
|
|
|
|
|
|
if (inventory[3].stackSize <= 0)
|
|
|
|
{
|
|
|
|
inventory[3] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 21:42:45 +01:00
|
|
|
if(inventory[1] != null)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
|
|
|
int fuelTicks = getFuelTicks(inventory[1]);
|
2013-01-23 21:42:45 +01:00
|
|
|
int energyNeeded = MAX_SECONDARY_ENERGY - secondaryEnergyStored;
|
|
|
|
if(fuelTicks > 0 && fuelTicks <= energyNeeded)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
|
|
|
if(fuelTicks <= energyNeeded)
|
|
|
|
{
|
|
|
|
setSecondaryEnergy(secondaryEnergyStored + fuelTicks);
|
|
|
|
}
|
|
|
|
else if(fuelTicks > energyNeeded)
|
|
|
|
{
|
|
|
|
setSecondaryEnergy(secondaryEnergyStored + energyNeeded);
|
|
|
|
}
|
|
|
|
--inventory[1].stackSize;
|
|
|
|
|
|
|
|
if(inventory[1].stackSize == 0)
|
|
|
|
{
|
|
|
|
inventory[1] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-09 03:22:18 +01:00
|
|
|
if(inventory[4] != null && inventory[4].getItem() instanceof IMachineUpgrade)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
|
|
|
int energyToAdd = 0;
|
|
|
|
int ticksToRemove = 0;
|
|
|
|
|
2012-11-25 16:45:00 +01:00
|
|
|
if(currentMaxElectricity == MAX_ELECTRICITY)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-11-09 03:22:18 +01:00
|
|
|
energyToAdd = ((IMachineUpgrade)inventory[4].getItem()).getEnergyBoost(inventory[4]);
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
2012-11-09 03:22:18 +01:00
|
|
|
|
|
|
|
if(currentTicksRequired == TICKS_REQUIRED)
|
2012-10-22 03:29:26 +02:00
|
|
|
{
|
2012-11-09 03:22:18 +01:00
|
|
|
ticksToRemove = ((IMachineUpgrade)inventory[4].getItem()).getTickReduction(inventory[4]);
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
|
2012-11-25 16:45:00 +01:00
|
|
|
currentMaxElectricity += energyToAdd;
|
2012-10-22 03:29:26 +02:00
|
|
|
currentTicksRequired -= ticksToRemove;
|
|
|
|
}
|
|
|
|
else if(inventory[4] == null)
|
|
|
|
{
|
|
|
|
currentTicksRequired = TICKS_REQUIRED;
|
2012-11-25 16:45:00 +01:00
|
|
|
currentMaxElectricity = MAX_ELECTRICITY;
|
2012-10-22 03:29:26 +02:00
|
|
|
}
|
|
|
|
|
2013-01-23 21:42:45 +01:00
|
|
|
if(electricityStored >= ENERGY_PER_TICK && secondaryEnergyStored >= SECONDARY_ENERGY_PER_TICK)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2013-01-23 21:42:45 +01:00
|
|
|
if(canOperate() && (operatingTicks+1) < currentTicksRequired && secondaryEnergyStored >= SECONDARY_ENERGY_PER_TICK)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2013-01-23 21:42:45 +01:00
|
|
|
++operatingTicks;
|
|
|
|
secondaryEnergyStored -= SECONDARY_ENERGY_PER_TICK;
|
|
|
|
electricityStored -= ENERGY_PER_TICK;
|
|
|
|
}
|
|
|
|
else if((operatingTicks+1) >= currentTicksRequired)
|
|
|
|
{
|
|
|
|
if(!worldObj.isRemote)
|
|
|
|
{
|
|
|
|
operate();
|
|
|
|
}
|
|
|
|
operatingTicks = 0;
|
|
|
|
secondaryEnergyStored -= SECONDARY_ENERGY_PER_TICK;
|
|
|
|
electricityStored -= ENERGY_PER_TICK;
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!canOperate())
|
|
|
|
{
|
|
|
|
operatingTicks = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!worldObj.isRemote)
|
|
|
|
{
|
2013-01-23 21:42:45 +01:00
|
|
|
if(canOperate() && electricityStored >= ENERGY_PER_TICK && secondaryEnergyStored >= SECONDARY_ENERGY_PER_TICK)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2013-01-23 21:42:45 +01:00
|
|
|
setActive(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setActive(false);
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-10-02 20:39:40 +02:00
|
|
|
public void operate()
|
|
|
|
{
|
2012-12-19 21:23:55 +01:00
|
|
|
ItemStack itemstack = RecipeHandler.getOutput(inventory[0], true, getRecipes());
|
2012-10-02 20:39:40 +02:00
|
|
|
|
|
|
|
if (inventory[0].stackSize <= 0)
|
|
|
|
{
|
|
|
|
inventory[0] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inventory[2] == null)
|
|
|
|
{
|
|
|
|
inventory[2] = itemstack;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inventory[2].stackSize += itemstack.stackSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-10-02 20:39:40 +02:00
|
|
|
public boolean canOperate()
|
|
|
|
{
|
|
|
|
if (inventory[0] == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack itemstack = RecipeHandler.getOutput(inventory[0], false, getRecipes());
|
|
|
|
|
|
|
|
if (itemstack == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inventory[2] == null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inventory[2].isItemEqual(itemstack))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return inventory[2].stackSize + itemstack.stackSize <= inventory[2].getMaxStackSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-21 14:30:40 +01:00
|
|
|
@Override
|
|
|
|
public int getStartInventorySide(ForgeDirection side)
|
|
|
|
{
|
|
|
|
if(side == ForgeDirection.getOrientation(1))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(side == ForgeDirection.getOrientation(0))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if(side == ForgeDirection.getOrientation(facing) || side == ForgeDirection.getOrientation(facing).getOpposite())
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
2012-12-21 21:46:18 +01:00
|
|
|
else if(side == MekanismUtils.getLeft(facing))
|
2012-12-21 14:30:40 +01:00
|
|
|
{
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2012-12-21 21:46:18 +01:00
|
|
|
return 3;
|
2012-12-21 14:30:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSizeInventorySide(ForgeDirection side)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-15 21:04:12 +01:00
|
|
|
@Override
|
2012-10-25 23:55:32 +02:00
|
|
|
public void handlePacketData(INetworkManager network, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
facing = dataStream.readInt();
|
2012-11-15 21:04:12 +01:00
|
|
|
isActive = dataStream.readBoolean();
|
2012-10-02 20:39:40 +02:00
|
|
|
operatingTicks = dataStream.readInt();
|
2012-11-25 16:45:00 +01:00
|
|
|
electricityStored = dataStream.readDouble();
|
2012-10-02 20:39:40 +02:00
|
|
|
secondaryEnergyStored = dataStream.readInt();
|
2012-11-25 16:45:00 +01:00
|
|
|
currentMaxElectricity = dataStream.readDouble();
|
2012-10-22 03:29:26 +02:00
|
|
|
currentTicksRequired = dataStream.readInt();
|
2012-11-21 17:38:37 +01:00
|
|
|
worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord);
|
2013-01-05 05:58:53 +01:00
|
|
|
worldObj.updateAllLightTypes(xCoord, yCoord, zCoord);
|
2012-10-02 20:39:40 +02:00
|
|
|
} catch (Exception e)
|
|
|
|
{
|
2012-11-05 20:29:04 +01:00
|
|
|
System.out.println("[Mekanism] Error while handling tile entity packet.");
|
2012-10-02 20:39:40 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2012-11-15 21:04:12 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendPacket()
|
|
|
|
{
|
2012-11-25 16:45:00 +01:00
|
|
|
PacketHandler.sendTileEntityPacketToClients(this, 0, facing, isActive, operatingTicks, electricityStored, secondaryEnergyStored, currentMaxElectricity, currentTicksRequired);
|
2012-11-15 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendPacketWithRange()
|
|
|
|
{
|
2012-11-25 16:45:00 +01:00
|
|
|
PacketHandler.sendTileEntityPacketToClients(this, 50, facing, isActive, operatingTicks, electricityStored, secondaryEnergyStored, currentMaxElectricity, currentTicksRequired);
|
2012-11-15 21:04:12 +01:00
|
|
|
}
|
2012-10-02 20:39:40 +02:00
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-10-17 20:46:27 +02:00
|
|
|
public void readFromNBT(NBTTagCompound nbtTags)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2012-11-02 02:30:40 +01:00
|
|
|
super.readFromNBT(nbtTags);
|
|
|
|
|
2012-10-17 20:46:27 +02:00
|
|
|
secondaryEnergyStored = nbtTags.getInteger("secondaryEnergyStored");
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-10-17 20:46:27 +02:00
|
|
|
public void writeToNBT(NBTTagCompound nbtTags)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2012-10-17 20:46:27 +02:00
|
|
|
super.writeToNBT(nbtTags);
|
2012-10-24 22:13:24 +02:00
|
|
|
|
2012-10-17 20:46:27 +02:00
|
|
|
nbtTags.setInteger("secondaryEnergyStored", secondaryEnergyStored);
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the secondary energy to a new amount
|
|
|
|
* @param energy - amount to store
|
|
|
|
*/
|
|
|
|
public void setSecondaryEnergy(int energy)
|
|
|
|
{
|
2013-01-23 21:42:45 +01:00
|
|
|
secondaryEnergyStored = Math.max(Math.min(energy, MAX_SECONDARY_ENERGY), 0);
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|
2012-10-23 15:23:23 +02:00
|
|
|
|
2012-10-28 23:18:23 +01:00
|
|
|
/**
|
|
|
|
* Gets the scaled secondary energy level for the GUI.
|
|
|
|
* @param i - multiplier
|
|
|
|
* @return scaled secondary energy
|
|
|
|
*/
|
2012-10-23 15:23:23 +02:00
|
|
|
public int getScaledSecondaryEnergyLevel(int i)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
|
|
|
return secondaryEnergyStored*i / MAX_SECONDARY_ENERGY;
|
|
|
|
}
|
2012-10-09 22:27:10 +02:00
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-10-09 22:27:10 +02:00
|
|
|
public String[] getMethodNames()
|
|
|
|
{
|
2012-10-25 23:55:32 +02:00
|
|
|
return new String[] {"getStored", "getSecondaryStored", "getProgress", "isActive", "facing", "canOperate", "getMaxEnergy", "getEnergyNeeded"};
|
2012-10-09 22:27:10 +02:00
|
|
|
}
|
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-10-09 22:27:10 +02:00
|
|
|
public Object[] callMethod(IComputerAccess computer, int method, Object[] arguments) throws Exception
|
|
|
|
{
|
|
|
|
switch(method)
|
|
|
|
{
|
|
|
|
case 0:
|
2012-11-25 16:45:00 +01:00
|
|
|
return new Object[] {electricityStored};
|
2012-10-09 22:27:10 +02:00
|
|
|
case 1:
|
|
|
|
return new Object[] {secondaryEnergyStored};
|
|
|
|
case 2:
|
|
|
|
return new Object[] {operatingTicks};
|
|
|
|
case 3:
|
|
|
|
return new Object[] {isActive};
|
|
|
|
case 4:
|
|
|
|
return new Object[] {facing};
|
|
|
|
case 5:
|
|
|
|
return new Object[] {canOperate()};
|
2012-10-25 23:55:32 +02:00
|
|
|
case 6:
|
2012-11-25 16:45:00 +01:00
|
|
|
return new Object[] {currentMaxElectricity};
|
2012-10-25 23:55:32 +02:00
|
|
|
case 7:
|
2012-11-25 16:45:00 +01:00
|
|
|
return new Object[] {(currentMaxElectricity-electricityStored)};
|
2012-10-09 22:27:10 +02:00
|
|
|
default:
|
2012-11-05 20:29:04 +01:00
|
|
|
System.err.println("[Mekanism] Attempted to call unknown method with computer ID " + computer.getID());
|
2012-10-25 23:55:32 +02:00
|
|
|
return new Object[] {"Unknown command."};
|
2012-10-09 22:27:10 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|