Applied-Energistics-2-tiler.../tile/networking/TileWireless.java

195 lines
4.3 KiB
Java
Raw Normal View History

package appeng.tile.networking;
2014-02-09 02:34:52 +01:00
import io.netty.buffer.ByteBuf;
import java.io.IOException;
import java.util.EnumSet;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
2014-02-09 02:34:52 +01:00
import net.minecraftforge.common.util.ForgeDirection;
2014-03-28 05:31:37 +01:00
import appeng.api.implementations.IPowerChannelState;
2014-03-25 18:13:34 +01:00
import appeng.api.implementations.tiles.IWirelessAccessPoint;
import appeng.api.networking.GridFlags;
2014-03-25 18:13:34 +01:00
import appeng.api.networking.IGrid;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord;
2014-02-09 06:08:27 +01:00
import appeng.core.AEConfig;
import appeng.me.GridAccessException;
import appeng.tile.events.AETileEventHandler;
import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkInvTile;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.InvOperation;
2014-03-28 05:31:37 +01:00
import appeng.util.Platform;
2014-03-28 05:31:37 +01:00
public class TileWireless extends AENetworkInvTile implements IWirelessAccessPoint, IPowerChannelState
{
public static final int POWERED_FLAG = 1;
public static final int CHANNEL_FLAG = 2;
final int sides[] = new int[] { 0 };
AppEngInternalInventory inv = new AppEngInternalInventory( this, 1 );
public int clientFlags = 0;
public TileWireless() {
2014-01-31 22:12:15 +01:00
gridProxy.setFlags( GridFlags.REQUIRE_CHANNEL );
gridProxy.setValidSides( EnumSet.noneOf( ForgeDirection.class ) );
addNewHandler( new TileWirelessHandler() );
}
@Override
public void setOrientation(ForgeDirection inForward, ForgeDirection inUp)
{
super.setOrientation( inForward, inUp );
gridProxy.setValidSides( EnumSet.of( getForward().getOpposite() ) );
}
@MENetworkEventSubscribe
public void chanRender(MENetworkChannelsChanged c)
{
markForUpdate();
}
@MENetworkEventSubscribe
public void powerRender(MENetworkPowerStatusChange c)
{
markForUpdate();
}
class TileWirelessHandler extends AETileEventHandler
{
public TileWirelessHandler() {
2014-01-26 07:46:16 +01:00
super( TileEventType.NETWORK );
}
@Override
2014-02-09 02:34:52 +01:00
public boolean readFromStream(ByteBuf data) throws IOException
{
boolean eh = super.readFromStream( data );
int old = clientFlags;
clientFlags = data.readByte();
return eh || old != clientFlags;
}
@Override
2014-02-09 02:34:52 +01:00
public void writeToStream(ByteBuf data) throws IOException
{
clientFlags = 0;
try
{
if ( gridProxy.getEnergy().isNetworkPowered() )
clientFlags |= POWERED_FLAG;
if ( gridProxy.getNode().meetsChannelRequirements() )
clientFlags |= CHANNEL_FLAG;
}
catch (GridAccessException e)
{
// meh
}
data.writeByte( (byte) clientFlags );
}
}
@Override
public AECableType getCableConnectionType(ForgeDirection dir)
{
return AECableType.SMART;
}
@Override
public DimensionalCoord getLocation()
{
return new DimensionalCoord( this );
}
@Override
public IInventory getInternalInventory()
{
return inv;
}
@Override
public void onReady()
{
updatePower();
super.onReady();
}
@Override
2014-02-09 02:34:52 +01:00
public void markDirty()
{
updatePower();
}
private void updatePower()
{
2014-02-09 06:08:27 +01:00
gridProxy.setIdlePowerUsage( AEConfig.instance.wireless_getPowerDrain( getBoosters() ) );
}
@Override
public int[] getAccessibleSlotsBySide(ForgeDirection side)
{
return sides;
}
2014-03-25 18:13:34 +01:00
@Override
public double getRange()
{
2014-02-09 06:08:27 +01:00
return AEConfig.instance.wireless_getMaxRange( getBoosters() );
}
2014-03-28 05:31:37 +01:00
2014-03-25 18:13:34 +01:00
@Override
public boolean isActive()
{
2014-03-28 05:31:37 +01:00
if ( Platform.isClient() )
return isPowered() && (CHANNEL_FLAG == (clientFlags & CHANNEL_FLAG));
2014-03-25 18:13:34 +01:00
return gridProxy.isActive();
}
2014-03-28 05:31:37 +01:00
2014-03-25 18:13:34 +01:00
@Override
public IGrid getGrid()
{
2014-03-28 05:31:37 +01:00
try
{
2014-03-25 18:13:34 +01:00
return gridProxy.getGrid();
2014-03-28 05:31:37 +01:00
}
catch (GridAccessException e)
{
2014-03-25 18:13:34 +01:00
return null;
}
}
2014-03-28 05:31:37 +01:00
private int getBoosters()
{
ItemStack boosters = inv.getStackInSlot( 0 );
return boosters == null ? 0 : boosters.stackSize;
}
@Override
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added)
{
// :P
}
2014-03-28 05:31:37 +01:00
@Override
public boolean isPowered()
{
return POWERED_FLAG == (clientFlags & POWERED_FLAG);
}
}