Applied-Energistics-2-tiler.../src/main/java/appeng/me/helpers/AENetworkProxy.java

452 lines
9.1 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
2014-11-14 12:02:52 +01:00
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-09-24 02:26:27 +02:00
package appeng.me.helpers;
import java.util.Collections;
2014-09-24 02:26:27 +02:00
import java.util.EnumSet;
import com.mojang.authlib.GameProfile;
2014-09-24 02:26:27 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
2015-12-24 02:07:03 +01:00
2014-09-24 02:26:27 +02: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.crafting.ICraftingGrid;
import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.networking.events.MENetworkPowerIdleChange;
import appeng.api.networking.pathing.IPathingGrid;
import appeng.api.networking.security.ISecurityGrid;
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;
import appeng.core.worlddata.WorldData;
2014-09-24 02:26:27 +02:00
import appeng.hooks.TickHandler;
import appeng.me.GridAccessException;
import appeng.me.cache.P2PCache;
import appeng.parts.networking.PartCable;
import appeng.tile.AEBaseTile;
import appeng.util.Platform;
2014-09-24 02:26:27 +02:00
public class AENetworkProxy implements IGridBlock
{
private final IGridProxyable gp;
private final boolean worldNode;
private final String nbtName; // name
2016-09-17 15:02:51 +02:00
private AEColor myColor = AEColor.TRANSPARENT;
private NBTTagCompound data = null; // input
2014-09-24 02:26:27 +02:00
private ItemStack myRepInstance;
private boolean isReady = false;
private IGridNode node = null;
2015-06-16 02:44:59 +02:00
private EnumSet<EnumFacing> validSides;
2014-09-24 02:26:27 +02:00
private EnumSet<GridFlags> flags = EnumSet.noneOf( GridFlags.class );
private double idleDraw = 1.0;
private EntityPlayer owner;
2015-09-30 14:24:40 +02:00
public AENetworkProxy( final IGridProxyable te, final String nbtName, final ItemStack visual, final boolean inWorld )
2014-09-24 02:26:27 +02:00
{
this.gp = te;
this.nbtName = nbtName;
2014-12-29 15:13:47 +01:00
this.worldNode = inWorld;
this.myRepInstance = visual;
2015-06-16 02:44:59 +02:00
this.validSides = EnumSet.allOf( EnumFacing.class );
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
public void setVisualRepresentation( final ItemStack is )
2014-09-24 02:26:27 +02:00
{
this.myRepInstance = is;
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
public void writeToNBT( final NBTTagCompound tag )
2014-09-24 02:26:27 +02:00
{
if( this.node != null )
2015-04-29 02:30:53 +02:00
{
this.node.saveToNBT( this.nbtName, tag );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
public void setValidSides( final EnumSet<EnumFacing> validSides )
2014-09-24 02:26:27 +02:00
{
this.validSides = validSides;
if( this.node != null )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.node.updateState();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
public void validate()
{
if( this.gp instanceof AEBaseTile )
2015-04-29 02:30:53 +02:00
{
2015-01-01 22:13:10 +01:00
TickHandler.INSTANCE.addInit( (AEBaseTile) this.gp );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
public void onChunkUnload()
{
2014-12-29 15:13:47 +01:00
this.isReady = false;
this.invalidate();
2014-09-24 02:26:27 +02:00
}
public void invalidate()
{
2014-12-29 15:13:47 +01:00
this.isReady = false;
if( this.node != null )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.node.destroy();
this.node = null;
2014-09-24 02:26:27 +02:00
}
}
public void onReady()
{
2014-12-29 15:13:47 +01:00
this.isReady = true;
2014-09-24 02:26:27 +02:00
// send orientation based directionality to the node.
if( this.gp instanceof IOrientable )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final IOrientable ori = (IOrientable) this.gp;
if( ori.canBeRotated() )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
ori.setOrientation( ori.getForward(), ori.getUp() );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
2014-12-29 15:13:47 +01:00
this.getNode();
2014-09-24 02:26:27 +02:00
}
public IGridNode getNode()
2014-09-24 02:26:27 +02:00
{
if( this.node == null && Platform.isServer() && this.isReady )
{
this.node = AEApi.instance().createGridNode( this );
this.readFromNBT( this.data );
this.node.updateState();
}
return this.node;
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
public void readFromNBT( final NBTTagCompound tag )
2014-09-24 02:26:27 +02:00
{
this.data = tag;
if( this.node != null && this.data != null )
{
this.node.loadFromNBT( this.nbtName, this.data );
this.data = null;
}
else if( this.node != null && this.owner != null )
{
final GameProfile profile = this.owner.getGameProfile();
final int playerID = WorldData.instance().playerData().getPlayerID( profile );
this.node.setPlayerID( playerID );
this.owner = null;
}
2014-09-24 02:26:27 +02:00
}
public IPathingGrid getPath() throws GridAccessException
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final IGrid grid = this.getGrid();
if( grid == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final IPathingGrid pg = grid.getCache( IPathingGrid.class );
if( pg == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
return pg;
2014-09-24 02:26:27 +02:00
}
/**
* short cut!
*
* @return grid of node
*
* @throws GridAccessException of node or grid is null
*/
public IGrid getGrid() throws GridAccessException
2014-09-24 02:26:27 +02:00
{
if( this.node == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final IGrid grid = this.node.getGrid();
if( grid == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
return grid;
2014-09-24 02:26:27 +02:00
}
public ITickManager getTick() throws GridAccessException
{
2015-09-30 14:24:40 +02:00
final IGrid grid = this.getGrid();
if( grid == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final ITickManager pg = grid.getCache( ITickManager.class );
if( pg == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
return pg;
}
public IStorageGrid getStorage() throws GridAccessException
{
2015-09-30 14:24:40 +02:00
final IGrid grid = this.getGrid();
if( grid == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final IStorageGrid pg = grid.getCache( IStorageGrid.class );
2014-09-24 02:26:27 +02:00
if( pg == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
return pg;
}
public P2PCache getP2P() throws GridAccessException
{
2015-09-30 14:24:40 +02:00
final IGrid grid = this.getGrid();
if( grid == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final P2PCache pg = grid.getCache( P2PCache.class );
2014-09-24 02:26:27 +02:00
if( pg == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
return pg;
}
public ISecurityGrid getSecurity() throws GridAccessException
{
2015-09-30 14:24:40 +02:00
final IGrid grid = this.getGrid();
if( grid == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final ISecurityGrid sg = grid.getCache( ISecurityGrid.class );
2014-09-24 02:26:27 +02:00
if( sg == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
return sg;
}
public ICraftingGrid getCrafting() throws GridAccessException
{
2015-09-30 14:24:40 +02:00
final IGrid grid = this.getGrid();
if( grid == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final ICraftingGrid sg = grid.getCache( ICraftingGrid.class );
2014-09-24 02:26:27 +02:00
if( sg == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
return sg;
}
@Override
public double getIdlePowerUsage()
2014-09-24 02:26:27 +02:00
{
return this.idleDraw;
2014-09-24 02:26:27 +02:00
}
@Override
public EnumSet<GridFlags> getFlags()
{
2014-12-29 15:13:47 +01:00
return this.flags;
2014-09-24 02:26:27 +02:00
}
@Override
public boolean isWorldAccessible()
2014-09-24 02:26:27 +02:00
{
return this.worldNode;
}
2014-09-24 02:26:27 +02:00
@Override
public DimensionalCoord getLocation()
{
return this.gp.getLocation();
}
2014-09-24 02:26:27 +02:00
@Override
public AEColor getGridColor()
{
return this.getColor();
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void onGridNotification( final GridNotification notification )
2014-09-24 02:26:27 +02:00
{
if( this.gp instanceof PartCable )
2015-04-29 02:30:53 +02:00
{
( (PartCable) this.gp ).markForUpdate();
2015-04-29 02:30:53 +02:00
}
}
@Override
2015-09-30 14:24:40 +02:00
public void setNetworkStatus( final IGrid grid, final int channelsInUse )
{
}
@Override
2015-06-16 02:44:59 +02:00
public EnumSet<EnumFacing> getConnectableSides()
{
return this.validSides;
}
@Override
public IGridHost getMachine()
{
return this.gp;
}
@Override
public void gridChanged()
{
this.gp.gridChanged();
2014-09-24 02:26:27 +02:00
}
@Override
public ItemStack getMachineRepresentation()
{
return this.myRepInstance;
}
2015-09-30 14:24:40 +02:00
public void setFlags( final GridFlags... requireChannel )
{
2015-09-30 14:24:40 +02:00
final EnumSet<GridFlags> flags = EnumSet.noneOf( GridFlags.class );
Collections.addAll( flags, requireChannel );
this.flags = flags;
}
2015-09-30 14:24:40 +02:00
public void setIdlePowerUsage( final double idle )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.idleDraw = idle;
2014-09-24 02:26:27 +02:00
if( this.node != null )
2014-09-24 02:26:27 +02:00
{
try
{
2015-09-30 14:24:40 +02:00
final IGrid g = this.getGrid();
2014-12-29 15:13:47 +01:00
g.postEvent( new MENetworkPowerIdleChange( this.node ) );
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
2014-09-24 02:26:27 +02:00
{
// not ready for this yet..
}
}
}
public boolean isReady()
{
2014-12-29 15:13:47 +01:00
return this.isReady;
2014-09-24 02:26:27 +02:00
}
public boolean isActive()
{
if( this.node == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return false;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
return this.node.isActive();
2014-09-24 02:26:27 +02:00
}
public boolean isPowered()
{
try
{
2014-12-29 15:13:47 +01:00
return this.getEnergy().isNetworkPowered();
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
2014-09-24 02:26:27 +02:00
{
return false;
}
}
public IEnergyGrid getEnergy() throws GridAccessException
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final IGrid grid = this.getGrid();
if( grid == null )
2015-04-29 02:30:53 +02:00
{
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final IEnergyGrid eg = grid.getCache( IEnergyGrid.class );
if( eg == null )
2015-04-29 02:30:53 +02:00
{
throw new GridAccessException();
2015-04-29 02:30:53 +02:00
}
return eg;
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
public void setOwner( final EntityPlayer player )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.owner = player;
2014-09-24 02:26:27 +02:00
}
public AEColor getColor()
{
return this.myColor;
}
public void setColor( final AEColor myColor )
{
this.myColor = myColor;
}
2014-09-24 02:26:27 +02:00
}