2014-05-27 19:15:49 +02:00
|
|
|
package appeng.parts.layers;
|
2014-05-27 19:06:55 +02:00
|
|
|
|
|
|
|
import ic2.api.energy.tile.IEnergySink;
|
|
|
|
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 LayerIEnergySink extends LayerBase implements IEnergySink
|
|
|
|
{
|
|
|
|
|
|
|
|
private boolean isInIC2()
|
|
|
|
{
|
|
|
|
return getLayerFlags().contains( LayerFlags.IC2_ENET );
|
|
|
|
}
|
|
|
|
|
|
|
|
private TileEntity getEnergySinkTile()
|
|
|
|
{
|
|
|
|
IPartHost host = (IPartHost) this;
|
|
|
|
return host.getTile();
|
|
|
|
}
|
|
|
|
|
|
|
|
private World getEnergySinkWorld()
|
|
|
|
{
|
|
|
|
if ( getEnergySinkTile() == null )
|
|
|
|
return null;
|
2014-06-09 01:10:19 +02:00
|
|
|
|
2014-05-27 19:06:55 +02:00
|
|
|
return getEnergySinkTile().getWorldObj();
|
|
|
|
}
|
|
|
|
|
2014-06-09 01:10:19 +02:00
|
|
|
private boolean isTileValid()
|
|
|
|
{
|
|
|
|
TileEntity te = getEnergySinkTile();
|
|
|
|
|
|
|
|
if ( te == null )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !te.isInvalid() && te.getWorldObj().blockExists( te.xCoord, te.yCoord, te.zCoord );
|
|
|
|
}
|
|
|
|
|
2014-05-27 19:06:55 +02:00
|
|
|
final private void addToENet()
|
|
|
|
{
|
|
|
|
if ( getEnergySinkWorld() == null )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// re-add
|
|
|
|
removeFromENet();
|
|
|
|
|
2014-06-09 01:10:19 +02:00
|
|
|
if ( !isInIC2() && Platform.isServer() && isTileValid() )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
|
|
|
getLayerFlags().add( LayerFlags.IC2_ENET );
|
|
|
|
MinecraftForge.EVENT_BUS.post( new ic2.api.energy.event.EnergyTileLoadEvent( (IEnergySink) getEnergySinkTile() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final private void removeFromENet()
|
|
|
|
{
|
|
|
|
if ( getEnergySinkWorld() == null )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isInIC2() && Platform.isServer() )
|
|
|
|
{
|
|
|
|
getLayerFlags().remove( LayerFlags.IC2_ENET );
|
|
|
|
MinecraftForge.EVENT_BUS.post( new ic2.api.energy.event.EnergyTileUnloadEvent( (IEnergySink) getEnergySinkTile() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final private boolean interestedInIC2()
|
|
|
|
{
|
2014-06-09 01:10:19 +02:00
|
|
|
if ( !((IPartHost) this).isInWorld() )
|
|
|
|
return false;
|
|
|
|
|
2014-05-27 19:06:55 +02:00
|
|
|
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 acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction)
|
|
|
|
{
|
|
|
|
if ( !isInIC2() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
IPart part = getPart( direction );
|
|
|
|
if ( part instanceof IEnergySink )
|
|
|
|
return ((IEnergySink) part).acceptsEnergyFrom( emitter, direction );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-29 00:32:40 +02:00
|
|
|
public double getDemandedEnergy()
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
|
|
|
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 IEnergySink )
|
|
|
|
{
|
|
|
|
// use lower number cause ic2 deletes power it sends that isn't recieved.
|
2014-06-29 00:32:40 +02:00
|
|
|
return ((IEnergySink) part).getDemandedEnergy();
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-29 00:32:40 +02:00
|
|
|
public double injectEnergy(ForgeDirection directionFrom, double amount, double voltage)
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
|
|
|
if ( !isInIC2() )
|
|
|
|
return amount;
|
|
|
|
|
|
|
|
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
{
|
|
|
|
IPart part = getPart( dir );
|
|
|
|
if ( part instanceof IEnergySink )
|
|
|
|
{
|
2014-06-29 00:32:40 +02:00
|
|
|
return ((IEnergySink) part).injectEnergy( directionFrom, amount, voltage );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-29 00:32:40 +02:00
|
|
|
public int getSinkTier()
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
|
|
|
return Integer.MAX_VALUE; // no real options here...
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|