Skip to content

Commit

Permalink
Convert TileCable to use the wired network API
Browse files Browse the repository at this point in the history
There are several important things to note here:

 - The network element is associated with the cable, whilst the
   peripheral (and so packet sender/receiver) is associated with the
   modem. This allows us to have the main element be in the centre of
   the cable block, whilst the modem is in the centre of the adjacent
   computer.

 - Cables will connect to any adjacent network element, not just
   other cables.

 - Rednet messages are now sent on the computer thread, rather than the
   cable tick.
  • Loading branch information
SquidDev committed Feb 21, 2018
1 parent 74f5093 commit 5c7828d
Show file tree
Hide file tree
Showing 5 changed files with 651 additions and 684 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public void drawHighlight( DrawBlockHighlightEvent event )
GlStateManager.depthMask( false );
GlStateManager.pushMatrix();

EnumFacing direction = type != PeripheralType.Cable ? cable.getDirection() : null;

{
EntityPlayer player = event.getPlayer();
double x = player.lastTickPosX + (player.posX - player.lastTickPosX) * event.getPartialTicks();
Expand All @@ -78,7 +76,7 @@ public void drawHighlight( DrawBlockHighlightEvent event )

for( EnumFacing facing : EnumFacing.VALUES )
{
if( direction == facing || BlockCable.isCable( world, pos.offset( facing ) ) )
if( BlockCable.doesConnectVisually( state, world, pos, facing ) )
{
flags |= 1 << facing.ordinal();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.modem.TileCable;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockFaceShape;
Expand Down Expand Up @@ -51,23 +50,6 @@ public static class Properties
public static final PropertyBool DOWN = PropertyBool.create( "down" );
}

public static boolean isCable( IBlockAccess world, BlockPos pos )
{
Block block = world.getBlockState( pos ).getBlock();
if( block == ComputerCraft.Blocks.cable )
{
switch( ComputerCraft.Blocks.cable.getPeripheralType( world, pos ) )
{
case Cable:
case WiredModemWithCable:
{
return true;
}
}
}
return false;
}

// Members

public BlockCable()
Expand Down Expand Up @@ -175,33 +157,30 @@ public IBlockState getDefaultBlockState( PeripheralType type, EnumFacing placedS
}
}

private boolean doesConnect( IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing dir )
public static boolean canConnectIn( IBlockState state, EnumFacing direction )
{
if( state.getValue( Properties.CABLE ) == BlockCableCableVariant.NONE )
{
return false;
}
else if( state.getValue( Properties.MODEM ).getFacing() == dir )
{
return true;
}
else
{
return isCable( world, pos.offset( dir ) );
}
return state.getValue( BlockCable.Properties.CABLE ) != BlockCableCableVariant.NONE
&& state.getValue( BlockCable.Properties.MODEM ).getFacing() != direction;
}

public static boolean doesConnectVisually( IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction )
{
if( state.getValue( Properties.CABLE ) == BlockCableCableVariant.NONE ) return false;
if( state.getValue( Properties.MODEM ).getFacing() == direction ) return true;
return ComputerCraft.getWiredElementAt( world, pos.offset( direction ), direction.getOpposite() ) != null;
}

@Nonnull
@Override
@Deprecated
public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess world, BlockPos pos )
{
state = state.withProperty( Properties.NORTH, doesConnect( state, world, pos, EnumFacing.NORTH ) );
state = state.withProperty( Properties.SOUTH, doesConnect( state, world, pos, EnumFacing.SOUTH ) );
state = state.withProperty( Properties.EAST, doesConnect( state, world, pos, EnumFacing.EAST ) );
state = state.withProperty( Properties.WEST, doesConnect( state, world, pos, EnumFacing.WEST ) );
state = state.withProperty( Properties.UP, doesConnect( state, world, pos, EnumFacing.UP ) );
state = state.withProperty( Properties.DOWN, doesConnect( state, world, pos, EnumFacing.DOWN ) );
state = state.withProperty( Properties.NORTH, doesConnectVisually( state, world, pos, EnumFacing.NORTH ) );
state = state.withProperty( Properties.SOUTH, doesConnectVisually( state, world, pos, EnumFacing.SOUTH ) );
state = state.withProperty( Properties.EAST, doesConnectVisually( state, world, pos, EnumFacing.EAST ) );
state = state.withProperty( Properties.WEST, doesConnectVisually( state, world, pos, EnumFacing.WEST ) );
state = state.withProperty( Properties.UP, doesConnectVisually( state, world, pos, EnumFacing.UP ) );
state = state.withProperty( Properties.DOWN, doesConnectVisually( state, world, pos, EnumFacing.DOWN ) );

if( state.getValue( Properties.CABLE ) != BlockCableCableVariant.NONE )
{
Expand Down Expand Up @@ -345,7 +324,6 @@ public boolean removedByPlayer( @Nonnull IBlockState state, World world, @Nonnul
if( WorldUtil.isVecInsideInclusive( bb, hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) )
{
world.setBlockState( pos, state.withProperty( Properties.MODEM, BlockCableModemVariant.None ), 3 );
cable.modemChanged();
item = PeripheralItemFactory.create( PeripheralType.WiredModem, null, 1 );
}
else
Expand All @@ -365,6 +343,7 @@ public boolean removedByPlayer( @Nonnull IBlockState state, World world, @Nonnul
return super.removedByPlayer( state, world, pos, player, willHarvest );
}

@Nonnull
@Override
public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult hit, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player )
{
Expand All @@ -373,7 +352,7 @@ public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult hit, @
{
TileCable cable = (TileCable) tile;
PeripheralType type = getPeripheralType( state );

if( type == PeripheralType.WiredModemWithCable )
{
if( hit == null || WorldUtil.isVecInsideInclusive( cable.getModemBounds(), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) )
Expand Down
Loading

0 comments on commit 5c7828d

Please sign in to comment.