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

421 lines
9.2 KiB
Java
Raw Normal View History

package mekanism.common.tile;
import java.util.ArrayList;
import java.util.EnumSet;
import mekanism.api.Coord4D;
import mekanism.api.MekanismConfig.general;
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.Upgrade;
import mekanism.common.base.ITileNetwork;
import mekanism.common.base.IUpgradeTile;
import mekanism.common.util.MekanismUtils;
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 net.minecraft.nbt.NBTTagCompound;
2013-11-27 19:24:13 +01:00
import net.minecraft.tileentity.TileEntity;
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;
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 io.netty.buffer.ByteBuf;
import cofh.api.energy.IEnergyHandler;
import ic2.api.energy.EnergyNet;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergyConductor;
import ic2.api.energy.tile.IEnergySink;
import ic2.api.energy.tile.IEnergySource;
import ic2.api.energy.tile.IEnergyTile;
import ic2.api.tile.IEnergyStorage;
@InterfaceList({
@Interface(iface = "ic2.api.energy.tile.IEnergySink", modid = "IC2"),
2014-11-11 13:50:48 +01:00
@Interface(iface = "ic2.api.energy.tile.IEnergySource", modid = "IC2"),
@Interface(iface = "ic2.api.tile.IEnergyStorage", modid = "IC2"),
@Interface(iface = "cofh.api.energy.IEnergyHandler", modid = "CoFHAPI|energy"),
})
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 abstract class TileEntityElectricBlock extends TileEntityContainerBlock implements ITileNetwork, IStrictEnergyStorage, IEnergyHandler, IEnergySink, IEnergySource, IEnergyStorage, IStrictEnergyAcceptor, ICableOutputter
{
/** How much energy is stored in this block. */
public double electricityStored;
/** Maximum amount of energy this machine can hold. */
public double BASE_MAX_ENERGY;
/** Actual maximum energy storage, including upgrades */
public double maxEnergy;
/** Is this registered with IC2 */
public boolean ic2Registered = false;
/**
* 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 baseMaxEnergy - how much energy this block can store
*/
public TileEntityElectricBlock(String name, double baseMaxEnergy)
{
super(name);
BASE_MAX_ENERGY = baseMaxEnergy;
maxEnergy = BASE_MAX_ENERGY;
}
@Method(modid = "IC2")
2013-11-03 22:54:33 +01:00
public void register()
{
if(!worldObj.isRemote)
{
TileEntity registered = EnergyNet.instance.getTileEntity(worldObj, xCoord, yCoord, zCoord);
if(registered != this)
{
if(registered instanceof IEnergyTile)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent((IEnergyTile)registered));
}
else if(registered == null)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
ic2Registered = true;
}
}
2013-11-03 22:54:33 +01:00
}
}
@Method(modid = "IC2")
2014-03-08 04:00:35 +01:00
public void deregister()
{
if(!worldObj.isRemote)
2014-03-08 04:00:35 +01:00
{
TileEntity registered = EnergyNet.instance.getTileEntity(worldObj, xCoord, yCoord, zCoord);
if(registered instanceof IEnergyTile)
2014-03-08 04:00:35 +01:00
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent((IEnergyTile)registered));
2014-03-08 04:00:35 +01:00
}
}
}
2013-11-03 22:54:33 +01:00
@Override
2013-12-03 17:10:14 +01:00
public void onUpdate()
{
if(!ic2Registered && MekanismUtils.useIC2())
{
register();
}
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 maxEnergy;
}
@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;
}
2014-08-01 06:55:24 +02:00
@Override
public void onAdded()
{
super.onAdded();
if(MekanismUtils.useIC2())
{
register();
}
}
@Override
2013-11-03 22:54:33 +01:00
public void onChunkUnload()
{
if(MekanismUtils.useIC2())
2014-08-01 06:55:24 +02:00
{
deregister();
2014-08-01 06:55:24 +02:00
}
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())
2014-08-01 06:55:24 +02:00
{
deregister();
2014-08-01 06:55:24 +02:00
}
2014-02-15 23:10:13 +01:00
}
@Override
public void readFromNBT(NBTTagCompound nbtTags)
{
super.readFromNBT(nbtTags);
electricityStored = nbtTags.getDouble("electricityStored");
}
@Override
public void writeToNBT(NBTTagCompound nbtTags)
{
super.writeToNBT(nbtTags);
nbtTags.setDouble("electricityStored", getEnergy());
}
/**
* 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* general.FROM_TE);
if(!simulate)
{
setEnergy(getEnergy() + toAdd);
}
return (int)Math.round(toAdd* general.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* general.FROM_TE));
if(!simulate)
{
setEnergy(getEnergy() - toSend);
}
return (int)Math.round(toSend* general.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)
{
return (int)Math.round(getEnergy()* general.TO_TE);
}
@Override
@Method(modid = "CoFHAPI|energy")
public int getMaxEnergyStored(ForgeDirection from)
{
return (int)Math.round(getMaxEnergy()* general.TO_TE);
2013-11-27 19:24:13 +01:00
}
2013-11-27 19:24:13 +01:00
@Override
@Method(modid = "IC2")
public int getSinkTier()
2013-11-27 19:24:13 +01:00
{
return 4;
2013-11-27 19:24:13 +01:00
}
@Override
@Method(modid = "IC2")
public int getSourceTier()
{
return 4;
}
2013-11-27 19:24:13 +01:00
@Override
@Method(modid = "IC2")
2013-11-27 19:24:13 +01:00
public void setStored(int energy)
{
setEnergy(energy*general.FROM_IC2);
2013-11-27 19:24:13 +01:00
}
@Override
@Method(modid = "IC2")
2013-11-27 19:24:13 +01:00
public int addEnergy(int amount)
{
setEnergy(getEnergy() + amount*general.FROM_IC2);
return (int)Math.round(getEnergy()*general.TO_IC2);
2013-11-27 19:24:13 +01:00
}
@Override
@Method(modid = "IC2")
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 = "IC2")
2013-11-27 19:24:13 +01:00
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction)
{
return getConsumingSides().contains(direction);
}
@Override
@Method(modid = "IC2")
public boolean emitsEnergyTo(TileEntity receiver, ForgeDirection direction)
{
2014-10-02 19:42:04 +02:00
return getOutputtingSides().contains(direction) && receiver instanceof IEnergyConductor;
2013-11-27 19:24:13 +01:00
}
@Override
@Method(modid = "IC2")
public int getStored()
2013-11-27 19:24:13 +01:00
{
return (int)Math.round(getEnergy()*general.TO_IC2);
2013-11-27 19:24:13 +01:00
}
@Override
@Method(modid = "IC2")
public int getCapacity()
2013-11-27 19:24:13 +01:00
{
return (int)Math.round(getMaxEnergy()*general.TO_IC2);
2013-11-27 19:24:13 +01:00
}
@Override
@Method(modid = "IC2")
public int getOutput()
2013-11-27 19:24:13 +01:00
{
return (int)Math.round(getMaxOutput()*general.TO_IC2);
2013-11-27 19:24:13 +01:00
}
@Override
@Method(modid = "IC2")
public double getDemandedEnergy()
2013-11-27 19:24:13 +01:00
{
return (getMaxEnergy() - getEnergy())*general.TO_IC2;
}
@Override
@Method(modid = "IC2")
public double getOfferedEnergy()
{
return Math.min(getEnergy(), getMaxOutput())*general.TO_IC2;
2013-11-27 19:24:13 +01:00
}
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 = "IC2")
2013-11-27 19:24:13 +01:00
public double getOutputEnergyUnitsPerTick()
{
return getMaxOutput()*general.TO_IC2;
2013-11-27 19:24:13 +01:00
}
@Override
@Method(modid = "IC2")
public double injectEnergy(ForgeDirection direction, double amount, double voltage)
{
if(Coord4D.get(this).getFromSide(direction).getTileEntity(worldObj) instanceof IGridTransmitter)
2013-11-27 19:24:13 +01:00
{
return amount;
2013-11-27 19:24:13 +01:00
}
return amount-transferEnergyToAcceptor(direction, amount*general.FROM_IC2)*general.TO_IC2;
}
@Override
@Method(modid = "IC2")
public void drawEnergy(double amount)
{
setEnergy(Math.max(getEnergy() - (amount*general.FROM_IC2), 0));
}
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;
}
}