2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me.helpers;
|
|
|
|
|
|
|
|
import java.util.EnumSet;
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2014-01-23 17:28:12 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.networking.GridFlags;
|
|
|
|
import appeng.api.networking.GridNotification;
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridBlock;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.energy.IEnergyGrid;
|
|
|
|
import appeng.api.networking.events.MENetworkPowerIdleChange;
|
|
|
|
import appeng.api.networking.pathing.IPathingGrid;
|
2014-01-31 07:33:31 +01:00
|
|
|
import appeng.api.networking.security.ISecurityGrid;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.storage.IStorageGrid;
|
|
|
|
import appeng.api.networking.ticking.ITickManager;
|
|
|
|
import appeng.api.util.AEColor;
|
|
|
|
import appeng.api.util.DimensionalCoord;
|
|
|
|
import appeng.api.util.IOrientable;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.core.WorldSettings;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.helpers.TickHandler;
|
|
|
|
import appeng.me.GridAccessException;
|
2014-01-20 17:41:37 +01:00
|
|
|
import appeng.me.cache.P2PCache;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.parts.networking.PartCable;
|
|
|
|
import appeng.tile.AEBaseTile;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
|
|
|
|
public class AENetworkProxy implements IGridBlock
|
|
|
|
{
|
|
|
|
|
|
|
|
final private IGridProxyable gp;
|
|
|
|
final private boolean worldNode;
|
|
|
|
|
2014-01-23 17:28:12 +01:00
|
|
|
final private ItemStack myRepInstance;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
private boolean isReady = false;
|
|
|
|
private IGridNode node = null;
|
|
|
|
|
|
|
|
private EnumSet<ForgeDirection> validSides;
|
|
|
|
public AEColor myColor = AEColor.Transparent;
|
|
|
|
|
|
|
|
private EnumSet<GridFlags> flags = EnumSet.noneOf( GridFlags.class );
|
|
|
|
private double idleDraw = 1.0;
|
|
|
|
|
|
|
|
final private String nbtName; // name
|
|
|
|
NBTTagCompound data = null; // input
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
private EntityPlayer owner;
|
|
|
|
|
2014-01-23 17:28:12 +01:00
|
|
|
@Override
|
|
|
|
public ItemStack getMachineRepresentation()
|
|
|
|
{
|
|
|
|
return myRepInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public AENetworkProxy(IGridProxyable te, String nbtName, ItemStack visual, boolean inWorld) {
|
2013-12-27 23:59:59 +01:00
|
|
|
this.gp = te;
|
|
|
|
this.nbtName = nbtName;
|
|
|
|
worldNode = inWorld;
|
2014-01-23 17:28:12 +01:00
|
|
|
myRepInstance = visual;
|
2013-12-27 23:59:59 +01:00
|
|
|
validSides = EnumSet.allOf( ForgeDirection.class );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeToNBT(NBTTagCompound tag)
|
|
|
|
{
|
|
|
|
if ( node != null )
|
|
|
|
node.saveToNBT( nbtName, tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void readFromNBT(NBTTagCompound tag)
|
|
|
|
{
|
|
|
|
data = tag;
|
|
|
|
if ( node != null && data != null )
|
|
|
|
{
|
|
|
|
node.loadFromNBT( nbtName, data );
|
|
|
|
data = null;
|
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
else if ( node != null && owner != null )
|
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
node.setPlayerID( WorldSettings.getInstance().getPlayerID( owner.getCommandSenderName() ) );
|
2014-01-27 05:00:36 +01:00
|
|
|
owner = null;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DimensionalCoord getLocation()
|
|
|
|
{
|
|
|
|
return gp.getLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AEColor getGridColor()
|
|
|
|
{
|
|
|
|
return myColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onGridNotification(GridNotification notification)
|
|
|
|
{
|
|
|
|
if ( gp instanceof PartCable )
|
|
|
|
((PartCable) gp).markForUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setNetworkStatus(IGrid grid, int channelsInUse)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumSet<ForgeDirection> getConnectableSides()
|
|
|
|
{
|
|
|
|
return validSides;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setValidSides(EnumSet<ForgeDirection> validSides)
|
|
|
|
{
|
|
|
|
this.validSides = validSides;
|
|
|
|
if ( node != null )
|
|
|
|
node.updateState();
|
|
|
|
}
|
|
|
|
|
|
|
|
public IGridNode getNode()
|
|
|
|
{
|
|
|
|
if ( node == null && Platform.isServer() && isReady )
|
|
|
|
{
|
|
|
|
node = AEApi.instance().createGridNode( this );
|
|
|
|
readFromNBT( data );
|
|
|
|
node.updateState();
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void validate()
|
|
|
|
{
|
|
|
|
if ( gp instanceof AEBaseTile )
|
|
|
|
TickHandler.instance.addInit( (AEBaseTile) gp );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onChunkUnload()
|
|
|
|
{
|
|
|
|
isReady = false;
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void invalidate()
|
|
|
|
{
|
|
|
|
isReady = false;
|
|
|
|
if ( node != null )
|
|
|
|
{
|
|
|
|
node.destroy();
|
|
|
|
node = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onReady()
|
|
|
|
{
|
|
|
|
isReady = true;
|
|
|
|
|
|
|
|
// send orientation based directionality to the node.
|
|
|
|
if ( gp instanceof IOrientable )
|
|
|
|
{
|
|
|
|
IOrientable ori = (IOrientable) gp;
|
|
|
|
if ( ori.canBeRotated() )
|
|
|
|
ori.setOrientation( ori.getForward(), ori.getUp() );
|
|
|
|
}
|
|
|
|
|
|
|
|
getNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IGridHost getMachine()
|
|
|
|
{
|
|
|
|
return gp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* short cut!
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* @throws GridAccessException
|
|
|
|
*/
|
|
|
|
public IGrid getGrid() throws GridAccessException
|
|
|
|
{
|
|
|
|
if ( node == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
IGrid grid = node.getGrid();
|
|
|
|
if ( grid == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
return grid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnergyGrid getEnergy() throws GridAccessException
|
|
|
|
{
|
|
|
|
IGrid grid = getGrid();
|
|
|
|
if ( grid == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
IEnergyGrid eg = grid.getCache( IEnergyGrid.class );
|
|
|
|
if ( eg == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
return eg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IPathingGrid getPath() throws GridAccessException
|
|
|
|
{
|
|
|
|
IGrid grid = getGrid();
|
|
|
|
if ( grid == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
IPathingGrid pg = grid.getCache( IPathingGrid.class );
|
|
|
|
if ( pg == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
return pg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ITickManager getTick() throws GridAccessException
|
|
|
|
{
|
|
|
|
IGrid grid = getGrid();
|
|
|
|
if ( grid == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
ITickManager pg = grid.getCache( ITickManager.class );
|
|
|
|
if ( pg == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
return pg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IStorageGrid getStorage() throws GridAccessException
|
|
|
|
{
|
|
|
|
IGrid grid = getGrid();
|
|
|
|
if ( grid == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
|
|
|
|
IStorageGrid pg = grid.getCache( IStorageGrid.class );
|
|
|
|
|
|
|
|
if ( pg == null )
|
|
|
|
throw new GridAccessException();
|
2014-01-20 17:41:37 +01:00
|
|
|
|
|
|
|
return pg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public P2PCache getP2P() throws GridAccessException
|
|
|
|
{
|
|
|
|
IGrid grid = getGrid();
|
|
|
|
if ( grid == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
|
|
|
|
P2PCache pg = grid.getCache( P2PCache.class );
|
|
|
|
|
|
|
|
if ( pg == null )
|
|
|
|
throw new GridAccessException();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
return pg;
|
|
|
|
}
|
|
|
|
|
2014-01-31 07:33:31 +01:00
|
|
|
public ISecurityGrid getSecurity() throws GridAccessException
|
|
|
|
{
|
|
|
|
IGrid grid = getGrid();
|
|
|
|
if ( grid == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
|
|
|
|
ISecurityGrid sg = grid.getCache( ISecurityGrid.class );
|
|
|
|
|
|
|
|
if ( sg == null )
|
|
|
|
throw new GridAccessException();
|
|
|
|
|
|
|
|
return sg;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public boolean isWorldAccessable()
|
|
|
|
{
|
|
|
|
return worldNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumSet<GridFlags> getFlags()
|
|
|
|
{
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFlags(GridFlags... requreChannel)
|
|
|
|
{
|
|
|
|
EnumSet<GridFlags> flags = EnumSet.noneOf( GridFlags.class );
|
|
|
|
|
|
|
|
for (GridFlags gf : requreChannel)
|
|
|
|
flags.add( gf );
|
|
|
|
|
|
|
|
this.flags = flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getIdlePowerUsage()
|
|
|
|
{
|
|
|
|
return idleDraw;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setIdlePowerUsage(double idle)
|
|
|
|
{
|
|
|
|
idleDraw = idle;
|
|
|
|
|
|
|
|
if ( node != null )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
IGrid g = getGrid();
|
|
|
|
g.postEvent( new MENetworkPowerIdleChange( node ) );
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// not ready for this yet..
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isReady()
|
|
|
|
{
|
|
|
|
return isReady;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isActive()
|
|
|
|
{
|
|
|
|
if ( node == null )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return node.isActive();
|
|
|
|
}
|
|
|
|
|
2014-02-03 03:30:52 +01:00
|
|
|
public boolean isPowered()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return getEnergy().isNetworkPowered();
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public void gridChanged()
|
|
|
|
{
|
|
|
|
gp.gridChanged();
|
|
|
|
}
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
public void setOwner(EntityPlayer player)
|
|
|
|
{
|
|
|
|
owner = player;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|