Applied-Energistics-2-tiler.../parts/layers/LayerIEnergySource.java

168 lines
3.7 KiB
Java
Raw Normal View History

2014-05-27 19:15:49 +02:00
package appeng.parts.layers;
import ic2.api.energy.tile.IEnergyEmitter;
import ic2.api.energy.tile.IEnergySink;
import ic2.api.energy.tile.IEnergySource;
import ic2.api.energy.tile.IEnergyTile;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.parts.IPart;
import appeng.api.parts.IPartHost;
import appeng.api.parts.LayerBase;
import appeng.api.parts.LayerFlags;
import appeng.util.Platform;
public class LayerIEnergySource extends LayerBase implements IEnergySource
{
private boolean isInIC2()
{
return getLayerFlags().contains( LayerFlags.IC2_ENET );
}
private TileEntity getEnergySourceTile()
{
IPartHost host = (IPartHost) this;
return host.getTile();
}
private World getEnergySourceWorld()
{
if ( getEnergySourceTile() == null )
return null;
return getEnergySourceTile().getWorldObj();
}
private boolean isTileValid()
{
TileEntity te = getEnergySourceTile();
if ( te == null )
return false;
return !te.isInvalid();
}
final private void addToENet()
{
if ( getEnergySourceWorld() == null )
return;
// re-add
removeFromENet();
if ( !isInIC2() && Platform.isServer() && isTileValid() )
{
getLayerFlags().add( LayerFlags.IC2_ENET );
MinecraftForge.EVENT_BUS.post( new ic2.api.energy.event.EnergyTileLoadEvent( (IEnergySink) getEnergySourceTile() ) );
}
}
final private void removeFromENet()
{
if ( getEnergySourceWorld() == null )
return;
if ( isInIC2() && Platform.isServer() )
{
getLayerFlags().remove( LayerFlags.IC2_ENET );
MinecraftForge.EVENT_BUS.post( new ic2.api.energy.event.EnergyTileUnloadEvent( (IEnergySink) getEnergySourceTile() ) );
}
}
final private boolean interestedInIC2()
{
if ( !((IPartHost) this).isInWorld() )
return false;
int interested = 0;
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
IPart part = getPart( dir );
if ( part instanceof IEnergyTile )
{
interested++;
}
}
return interested == 1;// if more then one tile is interested we need to abandonship...
}
@Override
public void partChanged()
{
super.partChanged();
if ( interestedInIC2() )
addToENet();
else
removeFromENet();
}
@Override
public boolean emitsEnergyTo(TileEntity receiver, ForgeDirection direction)
{
if ( !isInIC2() )
return false;
IPart part = getPart( direction );
if ( part instanceof IEnergySink )
return ((IEnergyEmitter) part).emitsEnergyTo( receiver, direction );
return false;
}
@Override
public double getOfferedEnergy()
{
if ( !isInIC2() )
return 0;
// this is a flawed implementation, that requires a change to the IC2 API.
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
IPart part = getPart( dir );
if ( part instanceof IEnergySource )
{
2014-09-21 02:17:42 +02:00
// use lower number cause ic2 deletes power it sends that isn't received.
return ((IEnergySource) part).getOfferedEnergy();
}
}
return 0;
}
@Override
public void drawEnergy(double amount)
{
// this is a flawed implementation, that requires a change to the IC2 API.
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
IPart part = getPart( dir );
if ( part instanceof IEnergySource )
{
((IEnergySource) part).drawEnergy( amount );
return;
}
}
}
2014-06-29 00:32:40 +02:00
@Override
public int getSourceTier()
{
// this is a flawed implementation, that requires a change to the IC2 API.
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
IPart part = getPart( dir );
if ( part instanceof IEnergySource )
{
return ((IEnergySource) part).getSourceTier();
}
}
return 0;
}
}