2013-08-27 00:49:32 +02:00
|
|
|
package mekanism.common.tileentity;
|
2012-10-02 20:39:40 +02:00
|
|
|
|
2013-04-13 16:33:37 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2013-12-01 06:03:40 +01:00
|
|
|
import mekanism.api.EnumColor;
|
2013-08-27 00:49:32 +02:00
|
|
|
import mekanism.common.Mekanism;
|
|
|
|
import mekanism.common.RecipeHandler;
|
2013-11-30 06:28:02 +01:00
|
|
|
import mekanism.common.SideData;
|
2013-10-30 03:15:39 +01:00
|
|
|
import mekanism.common.TileComponentEjector;
|
2013-08-27 00:49:32 +02:00
|
|
|
import mekanism.common.TileComponentUpgrade;
|
2013-08-27 00:57:08 +02:00
|
|
|
import mekanism.common.util.ChargeUtils;
|
2013-12-16 05:01:36 +01:00
|
|
|
import mekanism.common.util.InventoryUtils;
|
2013-08-27 00:57:08 +02:00
|
|
|
import mekanism.common.util.MekanismUtils;
|
2012-12-20 22:53:39 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2013-07-20 18:10:14 +02:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2013-08-25 03:22:45 +02:00
|
|
|
|
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;
|
2013-09-30 23:37:16 +02:00
|
|
|
import dan200.computer.api.ILuaContext;
|
2012-10-09 22:27:10 +02:00
|
|
|
|
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
|
2013-07-20 18:10:14 +02:00
|
|
|
* @param location - 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.
|
|
|
|
*/
|
2013-07-20 18:10:14 +02:00
|
|
|
public TileEntityAdvancedElectricMachine(String soundPath, String name, ResourceLocation location, double perTick, int secondaryPerTick, int ticksRequired, double maxEnergy, int maxSecondaryEnergy)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2013-07-20 18:10:14 +02:00
|
|
|
super(soundPath, name, location, perTick, ticksRequired, maxEnergy);
|
2013-01-25 00:01:59 +01:00
|
|
|
|
2013-12-16 05:01:36 +01:00
|
|
|
sideOutputs.add(new SideData(EnumColor.GREY, InventoryUtils.EMPTY));
|
2013-07-20 18:10:14 +02:00
|
|
|
sideOutputs.add(new SideData(EnumColor.DARK_RED, new int[] {0}));
|
|
|
|
sideOutputs.add(new SideData(EnumColor.PURPLE, new int[] {1}));
|
|
|
|
sideOutputs.add(new SideData(EnumColor.DARK_BLUE, new int[] {2}));
|
|
|
|
sideOutputs.add(new SideData(EnumColor.DARK_GREEN, new int[] {3}));
|
|
|
|
sideOutputs.add(new SideData(EnumColor.ORANGE, new int[] {4}));
|
2013-01-25 00:01:59 +01:00
|
|
|
|
|
|
|
sideConfig = new byte[] {2, 1, 0, 4, 5, 3};
|
|
|
|
|
2012-10-24 22:13:24 +02:00
|
|
|
inventory = new ItemStack[5];
|
2013-08-25 03:22:45 +02:00
|
|
|
|
2012-10-02 20:39:40 +02:00
|
|
|
SECONDARY_ENERGY_PER_TICK = secondaryPerTick;
|
|
|
|
MAX_SECONDARY_ENERGY = maxSecondaryEnergy;
|
2013-08-25 03:22:45 +02:00
|
|
|
|
2013-08-27 00:49:32 +02:00
|
|
|
upgradeComponent = new TileComponentUpgrade(this, 4);
|
2013-10-30 03:15:39 +01:00
|
|
|
ejectorComponent = new TileComponentEjector(this, sideOutputs.get(3));
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2013-04-22 05:43:04 +02:00
|
|
|
if(!worldObj.isRemote)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2013-04-23 21:36:43 +02:00
|
|
|
ChargeUtils.discharge(3, this);
|
2013-04-22 05:43:04 +02:00
|
|
|
|
|
|
|
handleSecondaryFuel();
|
|
|
|
|
2013-11-03 00:06:56 +01:00
|
|
|
if(canOperate() && MekanismUtils.canFunction(this) && getEnergy() >= MekanismUtils.getEnergyPerTick(getSpeedMultiplier(), getEnergyMultiplier(), ENERGY_PER_TICK) && secondaryEnergyStored >= SECONDARY_ENERGY_PER_TICK)
|
2013-01-23 21:42:45 +01:00
|
|
|
{
|
2013-07-24 22:03:35 +02:00
|
|
|
setActive(true);
|
2013-08-02 09:16:38 +02:00
|
|
|
|
2013-07-24 22:03:35 +02:00
|
|
|
operatingTicks++;
|
2013-08-02 09:16:38 +02:00
|
|
|
|
2013-11-02 00:14:02 +01:00
|
|
|
secondaryEnergyStored -= SECONDARY_ENERGY_PER_TICK;
|
2013-08-25 03:22:45 +02:00
|
|
|
electricityStored -= MekanismUtils.getEnergyPerTick(getSpeedMultiplier(), getEnergyMultiplier(), ENERGY_PER_TICK);
|
2013-07-24 22:03:35 +02:00
|
|
|
|
2013-08-25 03:22:45 +02:00
|
|
|
if((operatingTicks) >= MekanismUtils.getTicks(getSpeedMultiplier(), TICKS_REQUIRED))
|
2013-01-23 21:42:45 +01:00
|
|
|
{
|
|
|
|
operate();
|
2013-04-22 05:43:04 +02:00
|
|
|
|
|
|
|
operatingTicks = 0;
|
2013-01-23 21:42:45 +01:00
|
|
|
}
|
2013-04-22 05:43:04 +02:00
|
|
|
}
|
2013-07-24 22:03:35 +02:00
|
|
|
else {
|
2013-11-03 00:06:56 +01:00
|
|
|
if(prevEnergy >= getEnergy())
|
|
|
|
{
|
|
|
|
setActive(false);
|
|
|
|
}
|
2013-07-24 22:03:35 +02:00
|
|
|
}
|
2013-04-22 05:43:04 +02:00
|
|
|
|
|
|
|
if(!canOperate())
|
|
|
|
{
|
2013-01-23 21:42:45 +01:00
|
|
|
operatingTicks = 0;
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|
2013-11-03 00:06:56 +01:00
|
|
|
|
|
|
|
prevEnergy = getEnergy();
|
2012-10-02 20:39:40 +02:00
|
|
|
}
|
|
|
|
}
|
2013-01-30 13:53:36 +01:00
|
|
|
|
|
|
|
public void handleSecondaryFuel()
|
|
|
|
{
|
|
|
|
if(inventory[1] != null)
|
|
|
|
{
|
|
|
|
int fuelTicks = getFuelTicks(inventory[1]);
|
|
|
|
int energyNeeded = MAX_SECONDARY_ENERGY - secondaryEnergyStored;
|
2013-08-02 09:16:38 +02:00
|
|
|
|
2013-01-30 13:53:36 +01:00
|
|
|
if(fuelTicks > 0 && fuelTicks <= energyNeeded)
|
|
|
|
{
|
|
|
|
if(fuelTicks <= energyNeeded)
|
|
|
|
{
|
|
|
|
setSecondaryEnergy(secondaryEnergyStored + fuelTicks);
|
|
|
|
}
|
|
|
|
else if(fuelTicks > energyNeeded)
|
|
|
|
{
|
|
|
|
setSecondaryEnergy(secondaryEnergyStored + energyNeeded);
|
|
|
|
}
|
2013-08-02 09:16:38 +02:00
|
|
|
|
2013-04-02 03:06:57 +02:00
|
|
|
inventory[1].stackSize--;
|
2013-01-30 13:53:36 +01:00
|
|
|
|
|
|
|
if(inventory[1].stackSize == 0)
|
|
|
|
{
|
|
|
|
inventory[1] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-02 03:06:57 +02:00
|
|
|
|
|
|
|
@Override
|
2013-07-20 18:10:14 +02:00
|
|
|
public boolean isItemValidForSlot(int slotID, ItemStack itemstack)
|
2013-04-02 03:06:57 +02:00
|
|
|
{
|
|
|
|
if(slotID == 2)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(slotID == 4)
|
|
|
|
{
|
|
|
|
return itemstack.itemID == Mekanism.SpeedUpgrade.itemID || itemstack.itemID == Mekanism.EnergyUpgrade.itemID;
|
|
|
|
}
|
|
|
|
else if(slotID == 0)
|
|
|
|
{
|
2013-04-03 21:37:26 +02:00
|
|
|
return RecipeHandler.getOutput(itemstack, false, getRecipes()) != null;
|
2013-04-02 03:06:57 +02:00
|
|
|
}
|
|
|
|
else if(slotID == 3)
|
|
|
|
{
|
2013-08-01 03:20:12 +02:00
|
|
|
return ChargeUtils.canBeDischarged(itemstack);
|
2013-04-02 03:06:57 +02:00
|
|
|
}
|
|
|
|
else if(slotID == 1)
|
|
|
|
{
|
2013-11-23 20:36:50 +01:00
|
|
|
return getFuelTicks(itemstack) > 0;
|
2013-04-02 03:06:57 +02:00
|
|
|
}
|
2013-08-02 09:16:38 +02:00
|
|
|
|
2013-12-12 01:53:12 +01:00
|
|
|
return false;
|
2013-04-02 03:06:57 +02:00
|
|
|
}
|
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
|
|
|
|
2013-04-01 01:12:10 +02:00
|
|
|
if(inventory[0].stackSize <= 0)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
|
|
|
inventory[0] = null;
|
|
|
|
}
|
|
|
|
|
2013-04-01 01:12:10 +02:00
|
|
|
if(inventory[2] == null)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
|
|
|
inventory[2] = itemstack;
|
|
|
|
}
|
2013-04-01 01:12:10 +02:00
|
|
|
else {
|
2012-10-02 20:39:40 +02:00
|
|
|
inventory[2].stackSize += itemstack.stackSize;
|
|
|
|
}
|
2013-10-30 03:15:39 +01:00
|
|
|
|
2013-11-11 20:37:56 +01:00
|
|
|
onInventoryChanged();
|
2013-10-30 03:15:39 +01:00
|
|
|
ejectorComponent.onOutput();
|
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 boolean canOperate()
|
|
|
|
{
|
2013-04-01 01:12:10 +02:00
|
|
|
if(inventory[0] == null)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack itemstack = RecipeHandler.getOutput(inventory[0], false, getRecipes());
|
|
|
|
|
2013-04-01 01:12:10 +02:00
|
|
|
if(itemstack == null)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-01 01:12:10 +02:00
|
|
|
if(inventory[2] == null)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-01 01:12:10 +02:00
|
|
|
if(!inventory[2].isItemEqual(itemstack))
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-01 01:12:10 +02:00
|
|
|
else {
|
2012-10-02 20:39:40 +02:00
|
|
|
return inventory[2].stackSize + itemstack.stackSize <= inventory[2].getMaxStackSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 21:04:12 +01:00
|
|
|
@Override
|
2013-02-27 02:21:30 +01:00
|
|
|
public void handlePacketData(ByteArrayDataInput dataStream)
|
2012-10-02 20:39:40 +02:00
|
|
|
{
|
2013-02-27 02:21:30 +01:00
|
|
|
super.handlePacketData(dataStream);
|
|
|
|
secondaryEnergyStored = dataStream.readInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ArrayList getNetworkedData(ArrayList data)
|
|
|
|
{
|
|
|
|
super.getNetworkedData(data);
|
|
|
|
data.add(secondaryEnergyStored);
|
|
|
|
return data;
|
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;
|
|
|
|
}
|
2013-04-03 21:37:26 +02:00
|
|
|
|
|
|
|
@Override
|
2013-05-07 21:57:55 +02:00
|
|
|
public boolean canExtractItem(int slotID, ItemStack itemstack, int side)
|
2013-04-03 21:37:26 +02:00
|
|
|
{
|
|
|
|
if(slotID == 3)
|
|
|
|
{
|
2013-08-01 03:20:12 +02:00
|
|
|
return ChargeUtils.canBeOutputted(itemstack, false);
|
2013-04-03 21:37:26 +02:00
|
|
|
}
|
|
|
|
else if(slotID == 2)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
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
|
2013-09-30 23:37:16 +02:00
|
|
|
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception
|
2012-10-09 22:27:10 +02:00
|
|
|
{
|
|
|
|
switch(method)
|
|
|
|
{
|
|
|
|
case 0:
|
2013-08-25 03:22:45 +02:00
|
|
|
return new Object[] {getEnergy()};
|
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:
|
2013-12-18 18:39:37 +01:00
|
|
|
return new Object[] {MekanismUtils.getMaxEnergy(getEnergyMultiplier(), getMaxEnergy())};
|
2012-10-25 23:55:32 +02:00
|
|
|
case 7:
|
2013-12-18 18:39:37 +01:00
|
|
|
return new Object[] {(MekanismUtils.getMaxEnergy(getEnergyMultiplier(), getMaxEnergy())-getEnergy())};
|
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
|
|
|
}
|