2012-11-05 20:29:04 +01:00
|
|
|
package mekanism.common;
|
2012-10-28 23:18:23 +01:00
|
|
|
|
2013-02-27 02:21:30 +01:00
|
|
|
import java.util.ArrayList;
|
2013-03-18 17:23:57 +01:00
|
|
|
import java.util.EnumSet;
|
|
|
|
|
|
|
|
import universalelectricity.core.block.IConnector;
|
|
|
|
import universalelectricity.core.block.IElectricityStorage;
|
|
|
|
import universalelectricity.core.block.IVoltage;
|
|
|
|
import universalelectricity.core.electricity.ElectricityNetworkHelper;
|
|
|
|
import universalelectricity.core.electricity.ElectricityPack;
|
2013-02-27 02:21:30 +01:00
|
|
|
|
|
|
|
import com.google.common.io.ByteArrayDataInput;
|
|
|
|
|
2012-10-28 23:18:23 +01:00
|
|
|
import ic2.api.IWrenchable;
|
2012-12-20 22:53:39 +01:00
|
|
|
import ic2.api.energy.event.EnergyTileLoadEvent;
|
|
|
|
import ic2.api.energy.event.EnergyTileUnloadEvent;
|
|
|
|
import ic2.api.energy.tile.IEnergyTile;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2013-03-18 17:23:57 +01:00
|
|
|
import net.minecraftforge.common.ForgeDirection;
|
2012-10-28 23:18:23 +01:00
|
|
|
import net.minecraftforge.common.ISidedInventory;
|
2012-12-19 21:23:55 +01:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
2012-12-20 22:53:39 +01:00
|
|
|
import buildcraft.api.power.IPowerProvider;
|
|
|
|
import buildcraft.api.power.IPowerReceptor;
|
|
|
|
import buildcraft.api.power.PowerFramework;
|
2012-10-28 23:18:23 +01:00
|
|
|
|
2013-04-02 03:06:57 +02:00
|
|
|
public abstract class TileEntityElectricBlock extends TileEntityContainerBlock implements IWrenchable, ITileNetwork, IPowerReceptor, IEnergyTile, IElectricityStorage, IVoltage, IConnector
|
2012-10-28 23:18:23 +01:00
|
|
|
{
|
|
|
|
/** How much energy is stored in this block. */
|
2012-11-25 16:45:00 +01:00
|
|
|
public double electricityStored;
|
2012-10-28 23:18:23 +01:00
|
|
|
|
|
|
|
/** Maximum amount of energy this machine can hold. */
|
2012-11-25 16:45:00 +01:00
|
|
|
public double MAX_ELECTRICITY;
|
2012-10-28 23:18:23 +01:00
|
|
|
|
2012-11-15 21:04:12 +01:00
|
|
|
/** BuildCraft power provider. */
|
|
|
|
public IPowerProvider powerProvider;
|
2012-10-28 23:18:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The base of all blocks that deal with electricity. It has a facing state, initialized state,
|
|
|
|
* and a current amount of stored energy.
|
|
|
|
* @param name - full name of this block
|
|
|
|
* @param maxEnergy - how much energy this block can store
|
|
|
|
*/
|
2013-02-01 01:43:39 +01:00
|
|
|
public TileEntityElectricBlock(String name, double maxEnergy)
|
2012-10-28 23:18:23 +01:00
|
|
|
{
|
2012-11-21 16:14:35 +01:00
|
|
|
super(name);
|
2012-11-25 16:45:00 +01:00
|
|
|
MAX_ELECTRICITY = maxEnergy;
|
2012-12-21 14:30:40 +01:00
|
|
|
|
2012-11-15 21:04:12 +01:00
|
|
|
if(PowerFramework.currentFramework != null)
|
2012-10-28 23:18:23 +01:00
|
|
|
{
|
2013-03-28 18:27:27 +01:00
|
|
|
powerProvider = new LinkedPowerProvider(this);
|
2013-02-01 01:43:39 +01:00
|
|
|
powerProvider.configure(0, 0, 100, 0, (int)(maxEnergy*Mekanism.TO_BC));
|
2012-10-28 23:18:23 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-19 21:23:55 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpdate()
|
|
|
|
{
|
|
|
|
if(!initialized && worldObj != null)
|
|
|
|
{
|
|
|
|
if(Mekanism.hooks.IC2Loaded)
|
|
|
|
{
|
|
|
|
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
}
|
2013-03-18 17:23:57 +01:00
|
|
|
|
|
|
|
if(!worldObj.isRemote)
|
|
|
|
{
|
2013-03-29 17:10:23 +01:00
|
|
|
if(powerProvider != null)
|
|
|
|
{
|
|
|
|
powerProvider.update(this);
|
|
|
|
}
|
|
|
|
|
2013-03-18 17:23:57 +01:00
|
|
|
ElectricityPack electricityPack = ElectricityNetworkHelper.consumeFromMultipleSides(this, getConsumingSides(), getRequest());
|
|
|
|
setJoules(getJoules()+electricityPack.getWatts());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected EnumSet<ForgeDirection> getConsumingSides()
|
|
|
|
{
|
|
|
|
return EnumSet.allOf(ForgeDirection.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canConnect(ForgeDirection direction)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ElectricityPack getRequest()
|
|
|
|
{
|
|
|
|
return new ElectricityPack((getMaxJoules() - getJoules()) / getVoltage(), getVoltage());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getMaxJoules()
|
|
|
|
{
|
|
|
|
return MAX_ELECTRICITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getJoules()
|
|
|
|
{
|
|
|
|
return electricityStored;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setJoules(double joules)
|
|
|
|
{
|
|
|
|
electricityStored = Math.max(Math.min(joules, getMaxJoules()), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getVoltage()
|
|
|
|
{
|
|
|
|
return 120;
|
2012-12-19 21:23:55 +01:00
|
|
|
}
|
|
|
|
|
2013-02-27 02:21:30 +01:00
|
|
|
@Override
|
|
|
|
public void handlePacketData(ByteArrayDataInput dataStream)
|
|
|
|
{
|
|
|
|
super.handlePacketData(dataStream);
|
|
|
|
electricityStored = dataStream.readDouble();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ArrayList getNetworkedData(ArrayList data)
|
|
|
|
{
|
|
|
|
super.getNetworkedData(data);
|
|
|
|
data.add(electricityStored);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2012-12-19 21:23:55 +01:00
|
|
|
@Override
|
|
|
|
public void invalidate()
|
|
|
|
{
|
2013-03-18 17:23:57 +01:00
|
|
|
ElectricityNetworkHelper.invalidate(this);
|
2012-12-19 21:23:55 +01:00
|
|
|
|
|
|
|
if(initialized)
|
|
|
|
{
|
|
|
|
if(Mekanism.hooks.IC2Loaded)
|
|
|
|
{
|
|
|
|
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
|
|
|
|
}
|
|
|
|
}
|
2013-03-18 17:23:57 +01:00
|
|
|
|
|
|
|
super.invalidate();
|
2012-12-19 21:23:55 +01:00
|
|
|
}
|
2012-10-28 23:18:23 +01:00
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-11-02 02:30:40 +01:00
|
|
|
public void readFromNBT(NBTTagCompound nbtTags)
|
|
|
|
{
|
|
|
|
super.readFromNBT(nbtTags);
|
|
|
|
|
2012-11-25 16:45:00 +01:00
|
|
|
electricityStored = nbtTags.getDouble("electricityStored");
|
2012-11-02 02:30:40 +01:00
|
|
|
}
|
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-11-02 02:30:40 +01:00
|
|
|
public void writeToNBT(NBTTagCompound nbtTags)
|
|
|
|
{
|
|
|
|
super.writeToNBT(nbtTags);
|
|
|
|
|
2012-11-25 16:45:00 +01:00
|
|
|
nbtTags.setDouble("electricityStored", electricityStored);
|
2012-11-02 02:30:40 +01:00
|
|
|
}
|
2012-10-28 23:18:23 +01:00
|
|
|
|
2012-12-23 20:46:11 +01:00
|
|
|
@Override
|
2012-10-28 23:18:23 +01:00
|
|
|
public boolean isAddedToEnergyNet()
|
|
|
|
{
|
|
|
|
return initialized;
|
|
|
|
}
|
|
|
|
|
2012-11-15 21:04:12 +01:00
|
|
|
@Override
|
2013-03-28 18:27:27 +01:00
|
|
|
public void setPowerProvider(IPowerProvider provider) {}
|
2012-11-15 21:04:12 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public IPowerProvider getPowerProvider()
|
|
|
|
{
|
|
|
|
return powerProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-03-28 18:27:27 +01:00
|
|
|
public int powerRequest(ForgeDirection side)
|
2012-11-15 21:04:12 +01:00
|
|
|
{
|
2013-03-28 18:27:27 +01:00
|
|
|
return (int)Math.min(((MAX_ELECTRICITY-electricityStored)*Mekanism.TO_BC), 100);
|
2012-11-15 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doWork() {}
|
2012-10-28 23:18:23 +01:00
|
|
|
}
|