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

425 lines
9.6 KiB
Java
Raw Normal View History

package mekanism.common.tile;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
2013-11-27 19:24:13 +01:00
import ic2.api.energy.tile.IEnergySink;
import ic2.api.tile.IEnergyStorage;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import java.util.EnumSet;
import mekanism.api.Coord4D;
2013-11-27 19:24:13 +01:00
import mekanism.api.energy.ICableOutputter;
import mekanism.api.energy.IStrictEnergyAcceptor;
import mekanism.api.energy.IStrictEnergyStorage;
import mekanism.api.transmitters.IGridTransmitter;
import mekanism.common.ITileNetwork;
import mekanism.common.Mekanism;
import mekanism.common.util.MekanismUtils;
import net.minecraft.nbt.NBTTagCompound;
2013-11-27 19:24:13 +01:00
import net.minecraft.tileentity.TileEntity;
2013-12-03 17:10:14 +01:00
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.common.Optional.Interface;
import cpw.mods.fml.common.Optional.InterfaceList;
import cpw.mods.fml.common.Optional.Method;
2013-12-28 04:34:05 +01:00
import buildcraft.api.power.IPowerEmitter;
2013-12-03 17:10:14 +01:00
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerHandler;
import buildcraft.api.power.PowerHandler.PowerReceiver;
import cofh.api.energy.IEnergyHandler;
@InterfaceList({
@Interface(iface = "ic2.api.energy.tile.IEnergySink", modid = "IC2API", striprefs = true),
@Interface(iface = "ic2.api.tile.IEnergyStorage", modid = "IC2API", striprefs = true),
@Interface(iface = "cofh.api.energy.IEnergyHandler", modid = "CoFHAPI|energy"),
@Interface(iface = "buildcraft.api.power.IPowerReceptor", modid = "BuildCraftAPI|power"),
@Interface(iface = "buildcraft.api.power.IPowerEmitter", modid = "BuildCraftAPI|power")
})
public abstract class TileEntityElectricBlock extends TileEntityContainerBlock implements ITileNetwork, IPowerEmitter, IPowerReceptor, IStrictEnergyStorage, IEnergyHandler, IEnergySink, IEnergyStorage, IStrictEnergyAcceptor, ICableOutputter
{
/** How much energy is stored in this block. */
public double electricityStored;
/** Maximum amount of energy this machine can hold. */
public double MAX_ELECTRICITY;
2013-12-03 17:10:14 +01:00
/** BuildCraft power handler. */
public PowerHandler powerHandler;
/**
* 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
*/
public TileEntityElectricBlock(String name, double maxEnergy)
{
super(name);
MAX_ELECTRICITY = maxEnergy;
if(MekanismUtils.useBuildCraft())
configure();
}
@Method(modid = "BuildCraftAPI|power")
public void configure()
{
powerHandler = new PowerHandler(this, PowerHandler.Type.STORAGE);
powerHandler.configurePowerPerdition(0, 0);
powerHandler.configure(0, 0, 0, 0);
}
@Method(modid = "IC2API")
2013-11-03 22:54:33 +01:00
public void register()
{
if(!worldObj.isRemote)
{
if(!Mekanism.ic2Registered.contains(Coord4D.get(this)))
{
Mekanism.ic2Registered.add(Coord4D.get(this));
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
}
2013-11-03 22:54:33 +01:00
}
}
@Method(modid = "IC2API")
2014-03-08 04:00:35 +01:00
public void deregister()
{
if(!worldObj.isRemote)
2014-03-08 04:00:35 +01:00
{
if(Mekanism.ic2Registered.contains(Coord4D.get(this)))
{
Mekanism.ic2Registered.remove(Coord4D.get(this));
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
}
}
}
2013-11-03 22:54:33 +01:00
@Override
2013-12-03 17:10:14 +01:00
public void onUpdate()
{
if(MekanismUtils.useBuildCraft())
reconfigure();
2013-12-03 17:10:14 +01:00
}
2013-11-29 23:17:01 +01:00
public EnumSet<ForgeDirection> getOutputtingSides()
2013-11-02 17:00:29 +01:00
{
return EnumSet.noneOf(ForgeDirection.class);
2013-11-02 17:00:29 +01:00
}
protected EnumSet<ForgeDirection> getConsumingSides()
{
2013-11-30 18:29:49 +01:00
EnumSet set = EnumSet.allOf(ForgeDirection.class);
set.remove(ForgeDirection.UNKNOWN);
return set;
}
2013-11-02 17:00:29 +01:00
public double getMaxOutput()
{
2013-11-02 17:00:29 +01:00
return 0;
}
@Override
public double getEnergy()
{
return electricityStored;
}
@Override
public void setEnergy(double energy)
{
electricityStored = Math.max(Math.min(energy, getMaxEnergy()), 0);
MekanismUtils.saveChunk(this);
}
@Override
public double getMaxEnergy()
{
return MAX_ELECTRICITY;
}
@Override
public void handlePacketData(ByteBuf dataStream)
{
super.handlePacketData(dataStream);
setEnergy(dataStream.readDouble());
}
@Override
public ArrayList getNetworkedData(ArrayList data)
{
super.getNetworkedData(data);
data.add(getEnergy());
return data;
}
@Override
2013-11-03 22:54:33 +01:00
public void onChunkUnload()
{
if(MekanismUtils.useIC2())
deregister();
2013-11-03 22:54:33 +01:00
super.onChunkUnload();
}
2014-02-15 23:10:13 +01:00
@Override
public void invalidate()
{
super.invalidate();
if(MekanismUtils.useIC2())
deregister();
2014-02-15 23:10:13 +01:00
}
@Override
public void readFromNBT(NBTTagCompound nbtTags)
{
super.readFromNBT(nbtTags);
electricityStored = nbtTags.getDouble("electricityStored");
if(MekanismUtils.useBuildCraft())
reconfigure();
}
@Override
public void writeToNBT(NBTTagCompound nbtTags)
{
super.writeToNBT(nbtTags);
nbtTags.setDouble("electricityStored", getEnergy());
}
2013-12-03 17:10:14 +01:00
@Override
@Method(modid = "BuildCraftAPI|power")
public PowerReceiver getPowerReceiver(ForgeDirection side)
2013-12-03 17:10:14 +01:00
{
2013-12-28 04:34:05 +01:00
if(getConsumingSides().contains(side))
{
return powerHandler.getPowerReceiver();
}
2013-12-28 04:34:05 +01:00
return null;
2013-12-03 17:10:14 +01:00
}
@Method(modid = "BuildCraftAPI|power")
2013-12-03 17:10:14 +01:00
protected void reconfigure()
{
powerHandler.configure(0, (float)((getMaxEnergy()-getEnergy())*Mekanism.TO_BC), 0, (float)(getMaxEnergy()*Mekanism.TO_BC));
2013-12-03 17:10:14 +01:00
}
2013-12-03 17:10:14 +01:00
@Override
@Method(modid = "BuildCraftAPI|power")
public void doWork(PowerHandler workProvider)
2013-12-03 17:10:14 +01:00
{
if(powerHandler.getEnergyStored() > 0)
{
if(getEnergy() < getMaxEnergy())
{
setEnergy(getEnergy() + powerHandler.useEnergy(0, (float)((getMaxEnergy()-getEnergy())*Mekanism.TO_BC), true)*Mekanism.FROM_BC);
}
2013-12-03 17:10:14 +01:00
powerHandler.setEnergy(0);
}
2013-12-03 17:10:14 +01:00
reconfigure();
}
2013-12-03 17:10:14 +01:00
@Override
@Method(modid = "BuildCraftAPI|power")
2013-12-03 17:10:14 +01:00
public World getWorld()
{
return worldObj;
}
/**
* Gets the scaled energy level for the GUI.
* @param i - multiplier
* @return scaled energy
*/
public int getScaledEnergyLevel(int i)
{
return (int)(getEnergy()*i / getMaxEnergy());
}
@Override
@Method(modid = "CoFHAPI|energy")
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate)
{
if(getConsumingSides().contains(from))
{
double toAdd = (int)Math.min(getMaxEnergy()-getEnergy(), maxReceive*Mekanism.FROM_TE);
if(!simulate)
{
setEnergy(getEnergy() + toAdd);
}
return (int)Math.round(toAdd*Mekanism.TO_TE);
}
return 0;
}
@Override
@Method(modid = "CoFHAPI|energy")
public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate)
{
2013-11-29 23:17:01 +01:00
if(getOutputtingSides().contains(from))
{
double toSend = Math.min(getEnergy(), Math.min(getMaxOutput(), maxExtract*Mekanism.FROM_TE));
if(!simulate)
{
setEnergy(getEnergy() - toSend);
}
return (int)Math.round(toSend*Mekanism.TO_TE);
}
return 0;
}
@Override
@Method(modid = "CoFHAPI|energy")
2014-06-15 13:35:51 +02:00
public boolean canConnectEnergy(ForgeDirection from)
{
2013-12-21 18:51:55 +01:00
return getConsumingSides().contains(from) || getOutputtingSides().contains(from);
}
@Override
@Method(modid = "CoFHAPI|energy")
public int getEnergyStored(ForgeDirection from)
{
2013-11-27 19:24:13 +01:00
return (int)Math.round(getEnergy()*Mekanism.TO_TE);
}
@Override
@Method(modid = "CoFHAPI|energy")
public int getMaxEnergyStored(ForgeDirection from)
{
2013-11-27 19:24:13 +01:00
return (int)Math.round(getMaxEnergy()*Mekanism.TO_TE);
}
2013-11-27 19:24:13 +01:00
@Override
@Method(modid = "IC2API")
public int getSinkTier()
2013-11-27 19:24:13 +01:00
{
return 4;
2013-11-27 19:24:13 +01:00
}
@Override
@Method(modid = "IC2API")
2013-11-27 19:24:13 +01:00
public void setStored(int energy)
{
setEnergy(energy*Mekanism.FROM_IC2);
}
@Override
@Method(modid = "IC2API")
2013-11-27 19:24:13 +01:00
public int addEnergy(int amount)
{
setEnergy(getEnergy() + amount*Mekanism.FROM_IC2);
return (int)Math.round(getEnergy()*Mekanism.TO_IC2);
}
@Override
@Method(modid = "IC2API")
public boolean isTeleporterCompatible(ForgeDirection side)
2013-11-27 19:24:13 +01:00
{
2013-11-29 23:17:01 +01:00
return getOutputtingSides().contains(side);
2013-11-27 19:24:13 +01:00
}
2013-11-27 19:24:13 +01:00
@Override
public boolean canOutputTo(ForgeDirection side)
{
2013-11-29 23:17:01 +01:00
return getOutputtingSides().contains(side);
2013-11-27 19:24:13 +01:00
}
2013-11-27 19:24:13 +01:00
@Override
@Method(modid = "IC2API")
2013-11-27 19:24:13 +01:00
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction)
{
2013-11-29 23:17:01 +01:00
return !getOutputtingSides().contains(direction);
2013-11-27 19:24:13 +01:00
}
@Override
@Method(modid = "IC2API")
public int getStored()
2013-11-27 19:24:13 +01:00
{
return (int)Math.round(getEnergy()*Mekanism.TO_IC2);
}
@Override
@Method(modid = "IC2API")
public int getCapacity()
2013-11-27 19:24:13 +01:00
{
return (int)Math.round(getMaxEnergy()*Mekanism.TO_IC2);
}
@Override
@Method(modid = "IC2API")
public int getOutput()
2013-11-27 19:24:13 +01:00
{
return (int)Math.round(getMaxOutput()*Mekanism.TO_IC2);
}
@Override
@Method(modid = "IC2API")
public double getDemandedEnergy()
2013-11-27 19:24:13 +01:00
{
return (getMaxEnergy() - getEnergy())*Mekanism.TO_IC2;
}
2013-11-27 19:24:13 +01:00
@Override
public boolean canReceiveEnergy(ForgeDirection side)
{
2013-12-28 04:34:05 +01:00
return getConsumingSides().contains(side);
2013-11-27 19:24:13 +01:00
}
@Override
@Method(modid = "IC2API")
2013-11-27 19:24:13 +01:00
public double getOutputEnergyUnitsPerTick()
{
return getMaxOutput()*Mekanism.TO_IC2;
}
@Override
@Method(modid = "IC2API")
public double injectEnergy(ForgeDirection direction, double i, double v)
{
if(Coord4D.get(this).getFromSide(direction).getTileEntity(worldObj) instanceof IGridTransmitter)
2013-11-27 19:24:13 +01:00
{
return i;
}
return i-transferEnergyToAcceptor(direction, i*Mekanism.FROM_IC2)*Mekanism.TO_IC2;
}
2013-11-27 19:24:13 +01:00
@Override
public double transferEnergyToAcceptor(ForgeDirection side, double amount)
{
if(!(getConsumingSides().contains(side) || side == ForgeDirection.UNKNOWN))
2013-11-27 19:24:13 +01:00
{
2014-01-09 00:28:17 +01:00
return 0;
2013-11-27 19:24:13 +01:00
}
double toUse = Math.min(getMaxEnergy()-getEnergy(), amount);
setEnergy(getEnergy() + toUse);
return toUse;
}
2013-12-28 04:34:05 +01:00
@Override
@Method(modid = "BuildCraftAPI|power")
public boolean canEmitPowerFrom(ForgeDirection side)
2013-12-28 04:34:05 +01:00
{
return getOutputtingSides().contains(side);
}
}