mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-29 21:02:59 +00:00 
			
		
		
		
	Several miscellaneous changes
- Merge BlockPeripheralBase and BlockPeripheral, as no other classes extended the former. - Make BlockPeripheral use ITilePeripheral instead of TilePeripheralBase. This allows us to use other, non-ticking tiles instead. - Convert advanced and normal modems to extend from a generic TileWirelessModemBase class, and thus neither now tick.
This commit is contained in:
		| @@ -7,6 +7,8 @@ | |||||||
| package dan200.computercraft.shared.peripheral.common; | package dan200.computercraft.shared.peripheral.common; | ||||||
|  |  | ||||||
| import dan200.computercraft.ComputerCraft; | import dan200.computercraft.ComputerCraft; | ||||||
|  | import dan200.computercraft.shared.common.BlockDirectional; | ||||||
|  | import dan200.computercraft.shared.common.TileGeneric; | ||||||
| import dan200.computercraft.shared.peripheral.PeripheralType; | import dan200.computercraft.shared.peripheral.PeripheralType; | ||||||
| import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive; | import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive; | ||||||
| import dan200.computercraft.shared.peripheral.modem.ModemBounds; | import dan200.computercraft.shared.peripheral.modem.ModemBounds; | ||||||
| @@ -15,18 +17,21 @@ import dan200.computercraft.shared.peripheral.monitor.TileMonitor; | |||||||
| import dan200.computercraft.shared.peripheral.printer.TilePrinter; | import dan200.computercraft.shared.peripheral.printer.TilePrinter; | ||||||
| import dan200.computercraft.shared.peripheral.speaker.TileSpeaker; | import dan200.computercraft.shared.peripheral.speaker.TileSpeaker; | ||||||
| import dan200.computercraft.shared.util.DirectionUtil; | import dan200.computercraft.shared.util.DirectionUtil; | ||||||
|  | import net.minecraft.block.material.Material; | ||||||
| import net.minecraft.block.properties.PropertyDirection; | import net.minecraft.block.properties.PropertyDirection; | ||||||
| import net.minecraft.block.properties.PropertyEnum; | import net.minecraft.block.properties.PropertyEnum; | ||||||
| import net.minecraft.block.state.BlockFaceShape; | import net.minecraft.block.state.BlockFaceShape; | ||||||
| import net.minecraft.block.state.BlockStateContainer; | import net.minecraft.block.state.BlockStateContainer; | ||||||
| import net.minecraft.block.state.IBlockState; | import net.minecraft.block.state.IBlockState; | ||||||
| import net.minecraft.entity.EntityLivingBase; | import net.minecraft.entity.EntityLivingBase; | ||||||
|  | import net.minecraft.entity.player.EntityPlayer; | ||||||
| import net.minecraft.item.ItemStack; | import net.minecraft.item.ItemStack; | ||||||
| import net.minecraft.tileentity.TileEntity; | import net.minecraft.tileentity.TileEntity; | ||||||
| import net.minecraft.util.BlockRenderLayer; | import net.minecraft.util.BlockRenderLayer; | ||||||
| import net.minecraft.util.EnumFacing; | import net.minecraft.util.EnumFacing; | ||||||
| import net.minecraft.util.math.AxisAlignedBB; | import net.minecraft.util.math.AxisAlignedBB; | ||||||
| import net.minecraft.util.math.BlockPos; | import net.minecraft.util.math.BlockPos; | ||||||
|  | import net.minecraft.util.math.RayTraceResult; | ||||||
| import net.minecraft.world.IBlockAccess; | import net.minecraft.world.IBlockAccess; | ||||||
| import net.minecraft.world.World; | import net.minecraft.world.World; | ||||||
| import net.minecraftforge.fml.relauncher.Side; | import net.minecraftforge.fml.relauncher.Side; | ||||||
| @@ -34,7 +39,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; | |||||||
|  |  | ||||||
| import javax.annotation.Nonnull; | import javax.annotation.Nonnull; | ||||||
|  |  | ||||||
| public class BlockPeripheral extends BlockPeripheralBase | public class BlockPeripheral extends BlockDirectional | ||||||
| { | { | ||||||
|     public static class Properties |     public static class Properties | ||||||
|     { |     { | ||||||
| @@ -44,6 +49,7 @@ public class BlockPeripheral extends BlockPeripheralBase | |||||||
|  |  | ||||||
|     public BlockPeripheral() |     public BlockPeripheral() | ||||||
|     { |     { | ||||||
|  |         super( Material.ROCK ); | ||||||
|         setHardness( 2.0f ); |         setHardness( 2.0f ); | ||||||
|         setTranslationKey( "computercraft:peripheral" ); |         setTranslationKey( "computercraft:peripheral" ); | ||||||
|         setCreativeTab( ComputerCraft.mainCreativeTab ); |         setCreativeTab( ComputerCraft.mainCreativeTab ); | ||||||
| @@ -187,202 +193,107 @@ public class BlockPeripheral extends BlockPeripheralBase | |||||||
|     @Deprecated |     @Deprecated | ||||||
|     public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess world, BlockPos pos ) |     public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess world, BlockPos pos ) | ||||||
|     { |     { | ||||||
|         int anim; |  | ||||||
|         EnumFacing dir; |  | ||||||
|         TileEntity tile = world.getTileEntity( pos ); |         TileEntity tile = world.getTileEntity( pos ); | ||||||
|         if( tile instanceof TilePeripheralBase ) |  | ||||||
|         { |  | ||||||
|             TilePeripheralBase peripheral = (TilePeripheralBase) tile; |  | ||||||
|             anim = peripheral.getAnim(); |  | ||||||
|             dir = peripheral.getDirection(); |  | ||||||
|         } |  | ||||||
|         else |  | ||||||
|         { |  | ||||||
|             anim = 0; |  | ||||||
|             dir = state.getValue( Properties.FACING ); |  | ||||||
|             switch( state.getValue( Properties.VARIANT ) ) |  | ||||||
|             { |  | ||||||
|                 case WirelessModemDownOff: |  | ||||||
|                 case WirelessModemDownOn: |  | ||||||
|                 { |  | ||||||
|                     dir = EnumFacing.DOWN; |  | ||||||
|                     break; |  | ||||||
|                 } |  | ||||||
|                 case WirelessModemUpOff: |  | ||||||
|                 case WirelessModemUpOn: |  | ||||||
|                 { |  | ||||||
|                     dir = EnumFacing.UP; |  | ||||||
|                     break; |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         PeripheralType type = getPeripheralType( state ); |         PeripheralType type = getPeripheralType( state ); | ||||||
|         switch( type ) |         switch( type ) | ||||||
|         { |         { | ||||||
|             case DiskDrive: |             case DiskDrive: | ||||||
|             { |             { | ||||||
|                 state = state.withProperty( Properties.FACING, dir ); |                 if( !(tile instanceof TileDiskDrive) ) return state; | ||||||
|                 switch( anim ) |  | ||||||
|  |                 TileDiskDrive drive = (TileDiskDrive) tile; | ||||||
|  |                 state = state.withProperty( Properties.FACING, drive.getDirection() ); | ||||||
|  |                 switch( drive.getAnim() ) | ||||||
|                 { |                 { | ||||||
|                     case 0: |  | ||||||
|                     default: |                     default: | ||||||
|                     { |                     case 0: | ||||||
|                         state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveEmpty ); |                         return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveEmpty ); | ||||||
|                         break; |  | ||||||
|                     } |  | ||||||
|                     case 1: |                     case 1: | ||||||
|                     { |                         return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveInvalid ); | ||||||
|                         state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveInvalid ); |  | ||||||
|                         break; |  | ||||||
|                     } |  | ||||||
|                     case 2: |                     case 2: | ||||||
|                     { |                         return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveFull ); | ||||||
|                         state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveFull ); |  | ||||||
|                         break; |  | ||||||
|                     } |  | ||||||
|                 } |                 } | ||||||
|                 break; |  | ||||||
|             } |             } | ||||||
|             case Printer: |             case Printer: | ||||||
|             { |             { | ||||||
|                 state = state.withProperty( Properties.FACING, dir ); |                 if( !(tile instanceof TilePrinter) ) return state; | ||||||
|                 switch( anim ) |  | ||||||
|  |                 TilePrinter printer = (TilePrinter) tile; | ||||||
|  |                 state = state.withProperty( Properties.FACING, printer.getDirection() ); | ||||||
|  |                 switch( printer.getAnim() ) | ||||||
|                 { |                 { | ||||||
|                     case 0: |  | ||||||
|                     default: |                     default: | ||||||
|                     { |                     case 0: | ||||||
|                         state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterEmpty ); |                         return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterEmpty ); | ||||||
|                         break; |  | ||||||
|                     } |  | ||||||
|                     case 1: |                     case 1: | ||||||
|                     { |                         return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterTopFull ); | ||||||
|                         state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterTopFull ); |  | ||||||
|                         break; |  | ||||||
|                     } |  | ||||||
|                     case 2: |                     case 2: | ||||||
|                     { |                         return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBottomFull ); | ||||||
|                         state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBottomFull ); |  | ||||||
|                         break; |  | ||||||
|                     } |  | ||||||
|                     case 3: |                     case 3: | ||||||
|                     { |                         return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBothFull ); | ||||||
|                         state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBothFull ); |  | ||||||
|                         break; |  | ||||||
|                     } |  | ||||||
|                 } |                 } | ||||||
|                 break; |  | ||||||
|             } |             } | ||||||
|             case WirelessModem: |             case WirelessModem: | ||||||
|             { |             { | ||||||
|                 switch( dir ) |                 if( !(tile instanceof TileWirelessModem) ) return state; | ||||||
|  |  | ||||||
|  |                 TileWirelessModem modem = (TileWirelessModem) tile; | ||||||
|  |                 EnumFacing direction = modem.getDirection(); | ||||||
|  |                 switch( direction ) | ||||||
|                 { |                 { | ||||||
|                     case UP: |                     case UP: | ||||||
|                     { |                         return state | ||||||
|                         state = state.withProperty( Properties.FACING, EnumFacing.NORTH ); |                             .withProperty( Properties.FACING, EnumFacing.NORTH ) | ||||||
|                         switch( anim ) |                             .withProperty( Properties.VARIANT, | ||||||
|                         { |                                 modem.isOn() ? BlockPeripheralVariant.WirelessModemUpOn : BlockPeripheralVariant.WirelessModemUpOff ); | ||||||
|                             case 0: |  | ||||||
|                             default: |  | ||||||
|                             { |  | ||||||
|                                 state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemUpOff ); |  | ||||||
|                                 break; |  | ||||||
|                             } |  | ||||||
|                             case 1: |  | ||||||
|                             { |  | ||||||
|                                 state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemUpOn ); |  | ||||||
|                                 break; |  | ||||||
|                             } |  | ||||||
|                         } |  | ||||||
|                         break; |  | ||||||
|                     } |  | ||||||
|                     case DOWN: |                     case DOWN: | ||||||
|                     { |                         return state | ||||||
|                         state = state.withProperty( Properties.FACING, EnumFacing.NORTH ); |                             .withProperty( Properties.FACING, EnumFacing.NORTH ) | ||||||
|                         switch( anim ) |                             .withProperty( Properties.VARIANT, | ||||||
|                         { |                                 modem.isOn() ? BlockPeripheralVariant.WirelessModemDownOn : BlockPeripheralVariant.WirelessModemDownOff ); | ||||||
|                             case 0: |  | ||||||
|                             default: |  | ||||||
|                             { |  | ||||||
|                                 state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemDownOff ); |  | ||||||
|                                 break; |  | ||||||
|                             } |  | ||||||
|                             case 1: |  | ||||||
|                             { |  | ||||||
|                                 state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemDownOn ); |  | ||||||
|                                 break; |  | ||||||
|                             } |  | ||||||
|                         } |  | ||||||
|                         break; |  | ||||||
|                     } |  | ||||||
|                     default: |                     default: | ||||||
|                     { |                     { | ||||||
|                         state = state.withProperty( Properties.FACING, dir ); |                         return state | ||||||
|                         switch( anim ) |                             .withProperty( Properties.FACING, direction ) | ||||||
|                         { |                             .withProperty( Properties.VARIANT, | ||||||
|                             case 0: |                                 modem.isOn() ? BlockPeripheralVariant.WirelessModemOn : BlockPeripheralVariant.WirelessModemOff ); | ||||||
|                             default: |  | ||||||
|                             { |  | ||||||
|                                 state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemOff ); |  | ||||||
|                                 break; |  | ||||||
|                             } |  | ||||||
|                             case 1: |  | ||||||
|                             { |  | ||||||
|                                 state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemOn ); |  | ||||||
|                                 break; |  | ||||||
|                             } |  | ||||||
|                         } |  | ||||||
|                         break; |  | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|                 break; |  | ||||||
|             } |             } | ||||||
|             case Speaker: |             case Speaker: | ||||||
|             { |             { | ||||||
|                 state = state.withProperty( Properties.FACING, dir ); |                 if( !(tile instanceof TileSpeaker) ) return state; | ||||||
|                 break; |                 return state.withProperty( Properties.FACING, ((TileSpeaker) tile).getDirection() ); | ||||||
|             } |             } | ||||||
|             case Monitor: |             case Monitor: | ||||||
|             case AdvancedMonitor: |             case AdvancedMonitor: | ||||||
|             { |             { | ||||||
|                 EnumFacing front; |                 if( !(tile instanceof TileMonitor) ) return state; | ||||||
|                 int xIndex, yIndex, width, height; |  | ||||||
|                 if( tile instanceof TileMonitor ) |                 TileMonitor monitor = (TileMonitor) tile; | ||||||
|                 { |                 EnumFacing dir = monitor.getDirection(); | ||||||
|                     TileMonitor monitor = (TileMonitor) tile; |                 EnumFacing front = monitor.getFront(); | ||||||
|                     dir = monitor.getDirection(); |                 int xIndex = monitor.getXIndex(); | ||||||
|                     front = monitor.getFront(); |                 int yIndex = monitor.getYIndex(); | ||||||
|                     xIndex = monitor.getXIndex(); |                 int width = monitor.getWidth(); | ||||||
|                     yIndex = monitor.getYIndex(); |                 int height = monitor.getHeight(); | ||||||
|                     width = monitor.getWidth(); |  | ||||||
|                     height = monitor.getHeight(); |  | ||||||
|                 } |  | ||||||
|                 else |  | ||||||
|                 { |  | ||||||
|                     dir = EnumFacing.NORTH; |  | ||||||
|                     front = EnumFacing.NORTH; |  | ||||||
|                     xIndex = 0; |  | ||||||
|                     yIndex = 0; |  | ||||||
|                     width = 1; |  | ||||||
|                     height = 1; |  | ||||||
|                 } |  | ||||||
|  |  | ||||||
|                 BlockPeripheralVariant baseVariant; |                 BlockPeripheralVariant baseVariant; | ||||||
|                 if( front == EnumFacing.UP ) |                 if( front == EnumFacing.UP ) | ||||||
|                 { |                 { | ||||||
|                     baseVariant = (type == PeripheralType.AdvancedMonitor) ? |                     baseVariant = type == PeripheralType.AdvancedMonitor ? | ||||||
|                         BlockPeripheralVariant.AdvancedMonitorUp : |                         BlockPeripheralVariant.AdvancedMonitorUp : | ||||||
|                         BlockPeripheralVariant.MonitorUp; |                         BlockPeripheralVariant.MonitorUp; | ||||||
|                 } |                 } | ||||||
|                 else if( front == EnumFacing.DOWN ) |                 else if( front == EnumFacing.DOWN ) | ||||||
|                 { |                 { | ||||||
|                     baseVariant = (type == PeripheralType.AdvancedMonitor) ? |                     baseVariant = type == PeripheralType.AdvancedMonitor ? | ||||||
|                         BlockPeripheralVariant.AdvancedMonitorDown : |                         BlockPeripheralVariant.AdvancedMonitorDown : | ||||||
|                         BlockPeripheralVariant.MonitorDown; |                         BlockPeripheralVariant.MonitorDown; | ||||||
|                 } |                 } | ||||||
|                 else |                 else | ||||||
|                 { |                 { | ||||||
|                     baseVariant = (type == PeripheralType.AdvancedMonitor) ? |                     baseVariant = type == PeripheralType.AdvancedMonitor ? | ||||||
|                         BlockPeripheralVariant.AdvancedMonitor : |                         BlockPeripheralVariant.AdvancedMonitor : | ||||||
|                         BlockPeripheralVariant.Monitor; |                         BlockPeripheralVariant.Monitor; | ||||||
|                 } |                 } | ||||||
| @@ -446,20 +357,21 @@ public class BlockPeripheral extends BlockPeripheralBase | |||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 state = state.withProperty( Properties.FACING, dir ); |                 return state | ||||||
|                 state = state.withProperty( Properties.VARIANT, |                     .withProperty( Properties.FACING, dir ) | ||||||
|                     BlockPeripheralVariant.values()[baseVariant.ordinal() + subType] |                     .withProperty( Properties.VARIANT, BlockPeripheralVariant.values()[baseVariant.ordinal() + subType] ); | ||||||
|                 ); |  | ||||||
|                 break; |  | ||||||
|             } |             } | ||||||
|  |             default: | ||||||
|  |                 return state; | ||||||
|         } |         } | ||||||
|         return state; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Nonnull | ||||||
|     @Override |     @Override | ||||||
|     public IBlockState getDefaultBlockState( PeripheralType type, EnumFacing placedSide ) |     @Deprecated | ||||||
|  |     public final IBlockState getStateForPlacement( World world, BlockPos pos, EnumFacing placedSide, float hitX, float hitY, float hitZ, int damage, EntityLivingBase placer ) | ||||||
|     { |     { | ||||||
|         switch( type ) |         switch( getPeripheralType( damage ) ) | ||||||
|         { |         { | ||||||
|             case DiskDrive: |             case DiskDrive: | ||||||
|             default: |             default: | ||||||
| @@ -515,45 +427,32 @@ public class BlockPeripheral extends BlockPeripheralBase | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public PeripheralType getPeripheralType( int damage ) |     public PeripheralType getPeripheralType( int damage ) | ||||||
|     { |     { | ||||||
|         return ComputerCraft.Items.peripheral.getPeripheralType( damage ); |         return ComputerCraft.Items.peripheral.getPeripheralType( damage ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public PeripheralType getPeripheralType( IBlockState state ) |     public PeripheralType getPeripheralType( IBlockState state ) | ||||||
|     { |     { | ||||||
|         return state.getValue( Properties.VARIANT ).getPeripheralType(); |         return state.getValue( Properties.VARIANT ).getPeripheralType(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     private TileGeneric createTile( PeripheralType type ) | ||||||
|     public TilePeripheralBase createTile( PeripheralType type ) |  | ||||||
|     { |     { | ||||||
|         switch( type ) |         switch( type ) | ||||||
|         { |         { | ||||||
|             case DiskDrive: |             case DiskDrive: | ||||||
|             default: |             default: | ||||||
|             { |  | ||||||
|                 return new TileDiskDrive(); |                 return new TileDiskDrive(); | ||||||
|             } |  | ||||||
|             case WirelessModem: |             case WirelessModem: | ||||||
|             { |  | ||||||
|                 return new TileWirelessModem(); |                 return new TileWirelessModem(); | ||||||
|             } |  | ||||||
|             case Monitor: |             case Monitor: | ||||||
|             case AdvancedMonitor: |             case AdvancedMonitor: | ||||||
|             { |  | ||||||
|                 return new TileMonitor(); |                 return new TileMonitor(); | ||||||
|             } |  | ||||||
|             case Printer: |             case Printer: | ||||||
|             { |  | ||||||
|                 return new TilePrinter(); |                 return new TilePrinter(); | ||||||
|             } |  | ||||||
|             case Speaker: |             case Speaker: | ||||||
|             { |  | ||||||
|                 return new TileSpeaker(); |                 return new TileSpeaker(); | ||||||
|             } |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -567,12 +466,11 @@ public class BlockPeripheral extends BlockPeripheralBase | |||||||
|             case DiskDrive: |             case DiskDrive: | ||||||
|             case Printer: |             case Printer: | ||||||
|             { |             { | ||||||
|                 EnumFacing dir = DirectionUtil.fromEntityRot( player ); |                 if( stack.hasDisplayName() && tile instanceof ITilePeripheral ) | ||||||
|                 setDirection( world, pos, dir ); |  | ||||||
|                 if( stack.hasDisplayName() && tile instanceof TilePeripheralBase ) |  | ||||||
|                 { |                 { | ||||||
|                     TilePeripheralBase peripheral = (TilePeripheralBase) tile; |                     ITilePeripheral peripheral = (ITilePeripheral) tile; | ||||||
|                     peripheral.setLabel( stack.getDisplayName() ); |                     peripheral.setLabel( stack.getDisplayName() ); | ||||||
|  |                     peripheral.setDirection( DirectionUtil.fromEntityRot( player ) ); | ||||||
|                 } |                 } | ||||||
|                 break; |                 break; | ||||||
|             } |             } | ||||||
| @@ -659,6 +557,7 @@ public class BlockPeripheral extends BlockPeripheralBase | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     @Deprecated |     @Deprecated | ||||||
|  |     @Nonnull | ||||||
|     public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos ) |     public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos ) | ||||||
|     { |     { | ||||||
|         if( getPeripheralType( state ) == PeripheralType.WirelessModem ) |         if( getPeripheralType( state ) == PeripheralType.WirelessModem ) | ||||||
| @@ -668,4 +567,32 @@ public class BlockPeripheral extends BlockPeripheralBase | |||||||
|  |  | ||||||
|         return super.getBoundingBox( state, source, pos ); |         return super.getBoundingBox( state, source, pos ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public final boolean canPlaceBlockOnSide( @Nonnull World world, @Nonnull BlockPos pos, EnumFacing side ) | ||||||
|  |     { | ||||||
|  |         return true; // ItemPeripheralBase handles this | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public final TileGeneric createTile( IBlockState state ) | ||||||
|  |     { | ||||||
|  |         return createTile( getPeripheralType( state ) ); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public final TileGeneric createTile( int damage ) | ||||||
|  |     { | ||||||
|  |         return createTile( getPeripheralType( damage ) ); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Nonnull | ||||||
|  |     @Override | ||||||
|  |     public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player ) | ||||||
|  |     { | ||||||
|  |         TileEntity tile = world.getTileEntity( pos ); | ||||||
|  |         return tile instanceof ITilePeripheral | ||||||
|  |             ? PeripheralItemFactory.create( (ITilePeripheral) tile ) | ||||||
|  |             : super.getPickBlock( state, target, world, pos, player ); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,81 +0,0 @@ | |||||||
| /* |  | ||||||
|  * This file is part of ComputerCraft - http://www.computercraft.info |  | ||||||
|  * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission. |  | ||||||
|  * Send enquiries to dratcliffe@gmail.com |  | ||||||
|  */ |  | ||||||
|  |  | ||||||
| package dan200.computercraft.shared.peripheral.common; |  | ||||||
|  |  | ||||||
| import dan200.computercraft.shared.common.BlockDirectional; |  | ||||||
| import dan200.computercraft.shared.common.TileGeneric; |  | ||||||
| import dan200.computercraft.shared.peripheral.PeripheralType; |  | ||||||
| import net.minecraft.block.material.Material; |  | ||||||
| import net.minecraft.block.state.IBlockState; |  | ||||||
| import net.minecraft.entity.EntityLivingBase; |  | ||||||
| import net.minecraft.entity.player.EntityPlayer; |  | ||||||
| import net.minecraft.item.ItemStack; |  | ||||||
| import net.minecraft.tileentity.TileEntity; |  | ||||||
| import net.minecraft.util.EnumFacing; |  | ||||||
| import net.minecraft.util.math.BlockPos; |  | ||||||
| import net.minecraft.util.math.RayTraceResult; |  | ||||||
| import net.minecraft.world.IBlockAccess; |  | ||||||
| import net.minecraft.world.World; |  | ||||||
|  |  | ||||||
| import javax.annotation.Nonnull; |  | ||||||
|  |  | ||||||
| public abstract class BlockPeripheralBase extends BlockDirectional |  | ||||||
| { |  | ||||||
|     public BlockPeripheralBase() |  | ||||||
|     { |  | ||||||
|         super( Material.ROCK ); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     protected abstract IBlockState getDefaultBlockState( PeripheralType type, EnumFacing placedSide ); |  | ||||||
|  |  | ||||||
|     protected abstract PeripheralType getPeripheralType( int damage ); |  | ||||||
|  |  | ||||||
|     protected abstract PeripheralType getPeripheralType( IBlockState state ); |  | ||||||
|  |  | ||||||
|     protected abstract TilePeripheralBase createTile( PeripheralType type ); |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public final boolean canPlaceBlockOnSide( @Nonnull World world, @Nonnull BlockPos pos, EnumFacing side ) |  | ||||||
|     { |  | ||||||
|         return true; // ItemPeripheralBase handles this |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Nonnull |  | ||||||
|     @Override |  | ||||||
|     @Deprecated |  | ||||||
|     public final IBlockState getStateForPlacement( World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, int damage, EntityLivingBase placer ) |  | ||||||
|     { |  | ||||||
|         return getDefaultBlockState( getPeripheralType( damage ), side ); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public final TileGeneric createTile( IBlockState state ) |  | ||||||
|     { |  | ||||||
|         return createTile( getPeripheralType( state ) ); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public final TileGeneric createTile( int damage ) |  | ||||||
|     { |  | ||||||
|         return createTile( getPeripheralType( damage ) ); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     public final PeripheralType getPeripheralType( IBlockAccess world, BlockPos pos ) |  | ||||||
|     { |  | ||||||
|         return getPeripheralType( world.getBlockState( pos ) ); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Nonnull |  | ||||||
|     @Override |  | ||||||
|     public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player ) |  | ||||||
|     { |  | ||||||
|         TileEntity tile = world.getTileEntity( pos ); |  | ||||||
|         return tile instanceof TilePeripheralBase |  | ||||||
|             ? PeripheralItemFactory.create( (TilePeripheralBase) tile ) |  | ||||||
|             : super.getPickBlock( state, target, world, pos, player ); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -0,0 +1,27 @@ | |||||||
|  | /* | ||||||
|  |  * This file is part of ComputerCraft - http://www.computercraft.info | ||||||
|  |  * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission. | ||||||
|  |  * Send enquiries to dratcliffe@gmail.com | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | package dan200.computercraft.shared.peripheral.common; | ||||||
|  |  | ||||||
|  | import dan200.computercraft.shared.common.IDirectionalTile; | ||||||
|  | import dan200.computercraft.shared.peripheral.PeripheralType; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * The tile for {@link BlockPeripheral}. | ||||||
|  |  */ | ||||||
|  | public interface ITilePeripheral extends IDirectionalTile | ||||||
|  | { | ||||||
|  |     PeripheralType getPeripheralType(); | ||||||
|  |  | ||||||
|  |     default String getLabel() | ||||||
|  |     { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     default void setLabel( String label ) | ||||||
|  |     { | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -15,7 +15,7 @@ import javax.annotation.Nonnull; | |||||||
| public class PeripheralItemFactory | public class PeripheralItemFactory | ||||||
| { | { | ||||||
|     @Nonnull |     @Nonnull | ||||||
|     public static ItemStack create( TilePeripheralBase tile ) |     public static ItemStack create( ITilePeripheral tile ) | ||||||
|     { |     { | ||||||
|         return create( tile.getPeripheralType(), tile.getLabel(), 1 ); |         return create( tile.getPeripheralType(), tile.getLabel(), 1 ); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -18,11 +18,9 @@ import net.minecraft.util.NonNullList; | |||||||
|  |  | ||||||
| import javax.annotation.Nonnull; | import javax.annotation.Nonnull; | ||||||
|  |  | ||||||
| public abstract class TilePeripheralBase extends TileGeneric implements IPeripheralTile, ITickable, IDirectionalTile | public abstract class TilePeripheralBase extends TileGeneric implements IPeripheralTile, ITickable, IDirectionalTile, ITilePeripheral | ||||||
| { | { | ||||||
|     // Statics |     private EnumFacing m_dir; | ||||||
|  |  | ||||||
|     protected EnumFacing m_dir; |  | ||||||
|     private int m_anim; |     private int m_anim; | ||||||
|     private boolean m_changed; |     private boolean m_changed; | ||||||
|  |  | ||||||
| @@ -39,9 +37,9 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe | |||||||
|  |  | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public BlockPeripheralBase getBlock() |     public BlockPeripheral getBlock() | ||||||
|     { |     { | ||||||
|         return (BlockPeripheralBase) super.getBlock(); |         return (BlockPeripheral) super.getBlock(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -53,6 +51,7 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|     public final PeripheralType getPeripheralType() |     public final PeripheralType getPeripheralType() | ||||||
|     { |     { | ||||||
|         return getBlock().getPeripheralType( getBlockState() ); |         return getBlock().getPeripheralType( getBlockState() ); | ||||||
| @@ -64,6 +63,7 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe | |||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|     public String getLabel() |     public String getLabel() | ||||||
|     { |     { | ||||||
|         if( m_label != null && m_label.length() > 0 ) |         if( m_label != null && m_label.length() > 0 ) | ||||||
| @@ -86,11 +86,6 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe | |||||||
|         return m_dir; |         return m_dir; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public EnumFacing getCachedDirection() |  | ||||||
|     { |  | ||||||
|         return m_dir; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void setDirection( EnumFacing dir ) |     public void setDirection( EnumFacing dir ) | ||||||
|     { |     { | ||||||
| @@ -106,7 +101,7 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe | |||||||
|         return m_anim; |         return m_anim; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void setAnim( int anim ) |     protected void setAnim( int anim ) | ||||||
|     { |     { | ||||||
|         if( anim != m_anim ) |         if( anim != m_anim ) | ||||||
|         { |         { | ||||||
|   | |||||||
| @@ -1,80 +0,0 @@ | |||||||
| /* |  | ||||||
|  * This file is part of ComputerCraft - http://www.computercraft.info |  | ||||||
|  * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission. |  | ||||||
|  * Send enquiries to dratcliffe@gmail.com |  | ||||||
|  */ |  | ||||||
|  |  | ||||||
| package dan200.computercraft.shared.peripheral.modem; |  | ||||||
|  |  | ||||||
| import dan200.computercraft.api.peripheral.IPeripheral; |  | ||||||
| import dan200.computercraft.shared.common.BlockGeneric; |  | ||||||
| import dan200.computercraft.shared.peripheral.common.TilePeripheralBase; |  | ||||||
| import net.minecraft.nbt.NBTTagCompound; |  | ||||||
| import net.minecraft.util.EnumFacing; |  | ||||||
|  |  | ||||||
| import javax.annotation.Nonnull; |  | ||||||
|  |  | ||||||
| public abstract class TileModemBase extends TilePeripheralBase |  | ||||||
| { |  | ||||||
|  |  | ||||||
|     protected ModemPeripheral m_modem; |  | ||||||
|  |  | ||||||
|     protected TileModemBase() |  | ||||||
|     { |  | ||||||
|         m_modem = createPeripheral(); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     protected abstract ModemPeripheral createPeripheral(); |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public void destroy() |  | ||||||
|     { |  | ||||||
|         if( m_modem != null ) |  | ||||||
|         { |  | ||||||
|             m_modem.destroy(); |  | ||||||
|             m_modem = null; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public void onNeighbourChange() |  | ||||||
|     { |  | ||||||
|         EnumFacing dir = getDirection(); |  | ||||||
|         if( !getWorld().isSideSolid( getPos().offset( dir ), dir.getOpposite() ) ) |  | ||||||
|         { |  | ||||||
|             // Drop everything and remove block |  | ||||||
|             ((BlockGeneric) getBlockType()).dropAllItems( getWorld(), getPos(), false ); |  | ||||||
|             getWorld().setBlockToAir( getPos() ); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public void update() |  | ||||||
|     { |  | ||||||
|         super.update(); |  | ||||||
|         if( !getWorld().isRemote && m_modem.getModemState().pollChanged() ) |  | ||||||
|         { |  | ||||||
|             updateAnim(); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     protected void updateAnim() |  | ||||||
|     { |  | ||||||
|         setAnim( m_modem.getModemState().isOpen() ? 1 : 0 ); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public final void readDescription( @Nonnull NBTTagCompound nbt ) |  | ||||||
|     { |  | ||||||
|         super.readDescription( nbt ); |  | ||||||
|         updateBlock(); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     // IPeripheralTile implementation |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public IPeripheral getPeripheral( EnumFacing side ) |  | ||||||
|     { |  | ||||||
|         return side == getDirection() ? m_modem : null; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -13,7 +13,6 @@ import dan200.computercraft.api.network.wired.IWiredElement; | |||||||
| import dan200.computercraft.api.network.wired.IWiredNode; | import dan200.computercraft.api.network.wired.IWiredNode; | ||||||
| import dan200.computercraft.api.peripheral.IPeripheral; | import dan200.computercraft.api.peripheral.IPeripheral; | ||||||
| import dan200.computercraft.shared.command.CommandCopy; | import dan200.computercraft.shared.command.CommandCopy; | ||||||
| import dan200.computercraft.shared.common.BlockGeneric; |  | ||||||
| import dan200.computercraft.shared.common.TileGeneric; | import dan200.computercraft.shared.common.TileGeneric; | ||||||
| import dan200.computercraft.shared.peripheral.PeripheralType; | import dan200.computercraft.shared.peripheral.PeripheralType; | ||||||
| import dan200.computercraft.shared.peripheral.common.IPeripheralTile; | import dan200.computercraft.shared.peripheral.common.IPeripheralTile; | ||||||
| @@ -215,7 +214,7 @@ public class TileCable extends TileGeneric implements IPeripheralTile | |||||||
|                 case WiredModem: |                 case WiredModem: | ||||||
|                 { |                 { | ||||||
|                     // Drop everything and remove block |                     // Drop everything and remove block | ||||||
|                     ((BlockGeneric) getBlockType()).dropAllItems( getWorld(), getPos(), false ); |                     getBlock().dropAllItems( getWorld(), getPos(), false ); | ||||||
|                     getWorld().setBlockToAir( getPos() ); |                     getWorld().setBlockToAir( getPos() ); | ||||||
|  |  | ||||||
|                     // This'll call #destroy(), so we don't need to reset the network here. |                     // This'll call #destroy(), so we don't need to reset the network here. | ||||||
| @@ -224,7 +223,7 @@ public class TileCable extends TileGeneric implements IPeripheralTile | |||||||
|                 case WiredModemWithCable: |                 case WiredModemWithCable: | ||||||
|                 { |                 { | ||||||
|                     // Drop the modem and convert to cable |                     // Drop the modem and convert to cable | ||||||
|                     ((BlockGeneric) getBlockType()).dropItem( getWorld(), getPos(), PeripheralItemFactory.create( PeripheralType.WiredModem, null, 1 ) ); |                     getBlock().dropItem( getWorld(), getPos(), PeripheralItemFactory.create( PeripheralType.WiredModem, null, 1 ) ); | ||||||
|                     setBlockState( getBlockState().withProperty( BlockCable.Properties.MODEM, BlockCableModemVariant.None ) ); |                     setBlockState( getBlockState().withProperty( BlockCable.Properties.MODEM, BlockCableModemVariant.None ) ); | ||||||
|                     modemChanged(); |                     modemChanged(); | ||||||
|                     connectionsChanged(); |                     connectionsChanged(); | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ | |||||||
| package dan200.computercraft.shared.peripheral.modem.wired; | package dan200.computercraft.shared.peripheral.modem.wired; | ||||||
|  |  | ||||||
| import com.google.common.base.Objects; | import com.google.common.base.Objects; | ||||||
|  | import dan200.computercraft.ComputerCraft; | ||||||
| import dan200.computercraft.api.ComputerCraftAPI; | import dan200.computercraft.api.ComputerCraftAPI; | ||||||
| import dan200.computercraft.api.network.wired.IWiredElement; | import dan200.computercraft.api.network.wired.IWiredElement; | ||||||
| import dan200.computercraft.api.network.wired.IWiredNode; | import dan200.computercraft.api.network.wired.IWiredNode; | ||||||
| @@ -18,9 +19,11 @@ import dan200.computercraft.shared.peripheral.modem.ModemState; | |||||||
| import dan200.computercraft.shared.util.TickScheduler; | import dan200.computercraft.shared.util.TickScheduler; | ||||||
| import dan200.computercraft.shared.wired.CapabilityWiredElement; | import dan200.computercraft.shared.wired.CapabilityWiredElement; | ||||||
| import net.minecraft.entity.player.EntityPlayer; | import net.minecraft.entity.player.EntityPlayer; | ||||||
|  | import net.minecraft.item.ItemStack; | ||||||
| import net.minecraft.nbt.NBTTagCompound; | import net.minecraft.nbt.NBTTagCompound; | ||||||
| import net.minecraft.util.EnumFacing; | import net.minecraft.util.EnumFacing; | ||||||
| import net.minecraft.util.EnumHand; | import net.minecraft.util.EnumHand; | ||||||
|  | import net.minecraft.util.NonNullList; | ||||||
| import net.minecraft.util.math.BlockPos; | import net.minecraft.util.math.BlockPos; | ||||||
| import net.minecraft.util.math.Vec3d; | import net.minecraft.util.math.Vec3d; | ||||||
| import net.minecraft.util.text.TextComponentString; | import net.minecraft.util.text.TextComponentString; | ||||||
| @@ -132,6 +135,12 @@ public class TileWiredModemFull extends TileGeneric implements IPeripheralTile | |||||||
|         remove(); |         remove(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void getDroppedItems( @Nonnull NonNullList<ItemStack> drops, boolean creative ) | ||||||
|  |     { | ||||||
|  |         drops.add( new ItemStack( ComputerCraft.Items.wiredModemFull ) ); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void onNeighbourChange() |     public void onNeighbourChange() | ||||||
|     { |     { | ||||||
|   | |||||||
| @@ -7,140 +7,23 @@ | |||||||
| package dan200.computercraft.shared.peripheral.modem.wireless; | package dan200.computercraft.shared.peripheral.modem.wireless; | ||||||
|  |  | ||||||
| import dan200.computercraft.ComputerCraft; | import dan200.computercraft.ComputerCraft; | ||||||
| import dan200.computercraft.api.peripheral.IPeripheral; | import net.minecraft.item.ItemStack; | ||||||
| import dan200.computercraft.shared.common.TileGeneric; |  | ||||||
| import dan200.computercraft.shared.peripheral.common.IPeripheralTile; |  | ||||||
| import dan200.computercraft.shared.peripheral.modem.ModemPeripheral; |  | ||||||
| import dan200.computercraft.shared.peripheral.modem.ModemState; |  | ||||||
| import dan200.computercraft.shared.util.TickScheduler; |  | ||||||
| import net.minecraft.nbt.NBTTagCompound; |  | ||||||
| import net.minecraft.util.EnumFacing; | import net.minecraft.util.EnumFacing; | ||||||
| import net.minecraft.util.math.BlockPos; | import net.minecraft.util.NonNullList; | ||||||
| import net.minecraft.util.math.Vec3d; |  | ||||||
| import net.minecraft.world.World; |  | ||||||
|  |  | ||||||
| import javax.annotation.Nonnull; | import javax.annotation.Nonnull; | ||||||
|  |  | ||||||
| public class TileAdvancedModem extends TileGeneric implements IPeripheralTile | public class TileAdvancedModem extends TileWirelessModemBase | ||||||
| { | { | ||||||
|     private static class Peripheral extends WirelessModemPeripheral |  | ||||||
|     { |  | ||||||
|         private TileAdvancedModem tile; |  | ||||||
|  |  | ||||||
|         Peripheral( TileAdvancedModem entity ) |  | ||||||
|         { |  | ||||||
|             super( new ModemState( () -> TickScheduler.schedule( entity ) ), true ); |  | ||||||
|             tile = entity; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         @Nonnull |  | ||||||
|         @Override |  | ||||||
|         public World getWorld() |  | ||||||
|         { |  | ||||||
|             return tile.getWorld(); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         @Nonnull |  | ||||||
|         @Override |  | ||||||
|         public Vec3d getPosition() |  | ||||||
|         { |  | ||||||
|             BlockPos pos = tile.getPos().offset( tile.modemDirection ); |  | ||||||
|             return new Vec3d( pos.getX(), pos.getY(), pos.getZ() ); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         @Override |  | ||||||
|         public boolean equals( IPeripheral other ) |  | ||||||
|         { |  | ||||||
|             return this == other; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private boolean hasModemDirection = false; |  | ||||||
|     private EnumFacing modemDirection = EnumFacing.DOWN; |  | ||||||
|     private final ModemPeripheral modem = new Peripheral( this ); |  | ||||||
|     private boolean destroyed = false; |  | ||||||
|  |  | ||||||
|     private boolean on = false; |  | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void onLoad() |     protected EnumFacing getDirection() | ||||||
|     { |  | ||||||
|         super.onLoad(); |  | ||||||
|         updateDirection(); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public void destroy() |  | ||||||
|     { |  | ||||||
|         if( !destroyed ) |  | ||||||
|         { |  | ||||||
|             modem.destroy(); |  | ||||||
|             destroyed = true; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public void updateContainingBlockInfo() |  | ||||||
|     { |  | ||||||
|         hasModemDirection = false; |  | ||||||
|         super.updateContainingBlockInfo(); |  | ||||||
|         world.scheduleUpdate( getPos(), ComputerCraft.Blocks.advancedModem, 0 ); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public void updateTick() |  | ||||||
|     { |  | ||||||
|         updateDirection(); |  | ||||||
|  |  | ||||||
|         if( modem.getModemState().pollChanged() ) |  | ||||||
|         { |  | ||||||
|             boolean newOn = modem.getModemState().isOpen(); |  | ||||||
|             if( newOn != on ) |  | ||||||
|             { |  | ||||||
|                 on = newOn; |  | ||||||
|                 updateBlock(); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private void updateDirection() |  | ||||||
|     { |  | ||||||
|         if( !hasModemDirection ) |  | ||||||
|         { |  | ||||||
|             hasModemDirection = true; |  | ||||||
|             modemDirection = getDirection(); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|  |  | ||||||
|     private EnumFacing getDirection() |  | ||||||
|     { |     { | ||||||
|         return getBlockState().getValue( BlockAdvancedModem.Properties.FACING ); |         return getBlockState().getValue( BlockAdvancedModem.Properties.FACING ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     protected void writeDescription( @Nonnull NBTTagCompound nbt ) |     public void getDroppedItems( @Nonnull NonNullList<ItemStack> drops, boolean creative ) | ||||||
|     { |     { | ||||||
|         super.writeDescription( nbt ); |         if( !creative ) drops.add( new ItemStack( ComputerCraft.Items.advancedModem ) ); | ||||||
|         nbt.setBoolean( "on", on ); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public final void readDescription( @Nonnull NBTTagCompound nbt ) |  | ||||||
|     { |  | ||||||
|         super.readDescription( nbt ); |  | ||||||
|         on = nbt.getBoolean( "on" ); |  | ||||||
|         updateBlock(); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     public boolean isOn() |  | ||||||
|     { |  | ||||||
|         return on; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public IPeripheral getPeripheral( EnumFacing side ) |  | ||||||
|     { |  | ||||||
|         return !destroyed && side == getDirection() ? modem : null; |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,100 +7,23 @@ | |||||||
| package dan200.computercraft.shared.peripheral.modem.wireless; | package dan200.computercraft.shared.peripheral.modem.wireless; | ||||||
|  |  | ||||||
| import dan200.computercraft.ComputerCraft; | import dan200.computercraft.ComputerCraft; | ||||||
| import dan200.computercraft.api.peripheral.IPeripheral; | import dan200.computercraft.shared.common.IDirectionalTile; | ||||||
| import dan200.computercraft.shared.peripheral.PeripheralType; | import dan200.computercraft.shared.peripheral.PeripheralType; | ||||||
| import dan200.computercraft.shared.peripheral.common.BlockPeripheral; | import dan200.computercraft.shared.peripheral.common.BlockPeripheral; | ||||||
| import dan200.computercraft.shared.peripheral.common.BlockPeripheralVariant; | import dan200.computercraft.shared.peripheral.common.BlockPeripheralVariant; | ||||||
| import dan200.computercraft.shared.peripheral.modem.ModemPeripheral; | import dan200.computercraft.shared.peripheral.common.ITilePeripheral; | ||||||
| import dan200.computercraft.shared.peripheral.modem.ModemState; | import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory; | ||||||
| import dan200.computercraft.shared.peripheral.modem.TileModemBase; |  | ||||||
| import net.minecraft.block.state.IBlockState; | import net.minecraft.block.state.IBlockState; | ||||||
|  | import net.minecraft.item.ItemStack; | ||||||
| import net.minecraft.util.EnumFacing; | import net.minecraft.util.EnumFacing; | ||||||
|  | import net.minecraft.util.NonNullList; | ||||||
| import net.minecraft.util.math.BlockPos; | import net.minecraft.util.math.BlockPos; | ||||||
| import net.minecraft.util.math.Vec3d; |  | ||||||
| import net.minecraft.world.World; | import net.minecraft.world.World; | ||||||
|  |  | ||||||
| import javax.annotation.Nonnull; | import javax.annotation.Nonnull; | ||||||
|  |  | ||||||
| public class TileWirelessModem extends TileModemBase | public class TileWirelessModem extends TileWirelessModemBase implements IDirectionalTile, ITilePeripheral | ||||||
| { | { | ||||||
|     // Statics |  | ||||||
|  |  | ||||||
|     private static class Peripheral extends WirelessModemPeripheral |  | ||||||
|     { |  | ||||||
|         private TileModemBase m_entity; |  | ||||||
|  |  | ||||||
|         public Peripheral( TileModemBase entity ) |  | ||||||
|         { |  | ||||||
|             super( new ModemState(), false ); |  | ||||||
|             m_entity = entity; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         @Nonnull |  | ||||||
|         @Override |  | ||||||
|         public World getWorld() |  | ||||||
|         { |  | ||||||
|             return m_entity.getWorld(); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         @Nonnull |  | ||||||
|         @Override |  | ||||||
|         public Vec3d getPosition() |  | ||||||
|         { |  | ||||||
|             BlockPos pos = m_entity.getPos().offset( m_entity.getCachedDirection() ); |  | ||||||
|             return new Vec3d( pos.getX(), pos.getY(), pos.getZ() ); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         @Override |  | ||||||
|         public boolean equals( IPeripheral other ) |  | ||||||
|         { |  | ||||||
|             if( other instanceof Peripheral ) |  | ||||||
|             { |  | ||||||
|                 Peripheral otherModem = (Peripheral) other; |  | ||||||
|                 return otherModem.m_entity == m_entity; |  | ||||||
|             } |  | ||||||
|             return false; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     // Members |  | ||||||
|  |  | ||||||
|     private boolean m_hasDirection = false; |  | ||||||
|  |  | ||||||
|     public TileWirelessModem() |  | ||||||
|     { |  | ||||||
|         m_dir = EnumFacing.DOWN; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public void onLoad() |  | ||||||
|     { |  | ||||||
|         super.onLoad(); |  | ||||||
|         updateDirection(); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public void updateContainingBlockInfo() |  | ||||||
|     { |  | ||||||
|         m_hasDirection = false; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public void update() |  | ||||||
|     { |  | ||||||
|         super.update(); |  | ||||||
|         updateDirection(); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private void updateDirection() |  | ||||||
|     { |  | ||||||
|         if( !m_hasDirection ) |  | ||||||
|         { |  | ||||||
|             m_hasDirection = true; |  | ||||||
|             m_dir = getDirection(); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public EnumFacing getDirection() |     public EnumFacing getDirection() | ||||||
|     { |     { | ||||||
| @@ -110,18 +33,12 @@ public class TileWirelessModem extends TileModemBase | |||||||
|         { |         { | ||||||
|             case WirelessModemDownOff: |             case WirelessModemDownOff: | ||||||
|             case WirelessModemDownOn: |             case WirelessModemDownOn: | ||||||
|             { |  | ||||||
|                 return EnumFacing.DOWN; |                 return EnumFacing.DOWN; | ||||||
|             } |  | ||||||
|             case WirelessModemUpOff: |             case WirelessModemUpOff: | ||||||
|             case WirelessModemUpOn: |             case WirelessModemUpOn: | ||||||
|             { |  | ||||||
|                 return EnumFacing.UP; |                 return EnumFacing.UP; | ||||||
|             } |  | ||||||
|             default: |             default: | ||||||
|             { |  | ||||||
|                 return state.getValue( BlockPeripheral.Properties.FACING ); |                 return state.getValue( BlockPeripheral.Properties.FACING ); | ||||||
|             } |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -152,15 +69,21 @@ public class TileWirelessModem extends TileModemBase | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     protected ModemPeripheral createPeripheral() |  | ||||||
|     { |  | ||||||
|         return new Peripheral( this ); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public boolean shouldRefresh( World world, BlockPos pos, @Nonnull IBlockState oldState, @Nonnull IBlockState newState ) |     public boolean shouldRefresh( World world, BlockPos pos, @Nonnull IBlockState oldState, @Nonnull IBlockState newState ) | ||||||
|     { |     { | ||||||
|         return super.shouldRefresh( world, pos, oldState, newState ) || ComputerCraft.Blocks.peripheral.getPeripheralType( newState ) != PeripheralType.WirelessModem; |         return super.shouldRefresh( world, pos, oldState, newState ) || ComputerCraft.Blocks.peripheral.getPeripheralType( newState ) != PeripheralType.WirelessModem; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void getDroppedItems( @Nonnull NonNullList<ItemStack> drops, boolean creative ) | ||||||
|  |     { | ||||||
|  |         if( !creative ) drops.add( PeripheralItemFactory.create( PeripheralType.WirelessModem, null, 1 ) ); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public PeripheralType getPeripheralType() | ||||||
|  |     { | ||||||
|  |         return PeripheralType.WirelessModem; | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -0,0 +1,154 @@ | |||||||
|  | /* | ||||||
|  |  * This file is part of ComputerCraft - http://www.computercraft.info | ||||||
|  |  * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission. | ||||||
|  |  * Send enquiries to dratcliffe@gmail.com | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | package dan200.computercraft.shared.peripheral.modem.wireless; | ||||||
|  |  | ||||||
|  | import dan200.computercraft.api.peripheral.IPeripheral; | ||||||
|  | import dan200.computercraft.shared.common.TileGeneric; | ||||||
|  | import dan200.computercraft.shared.peripheral.common.IPeripheralTile; | ||||||
|  | import dan200.computercraft.shared.peripheral.modem.ModemPeripheral; | ||||||
|  | import dan200.computercraft.shared.peripheral.modem.ModemState; | ||||||
|  | import dan200.computercraft.shared.util.TickScheduler; | ||||||
|  | import net.minecraft.nbt.NBTTagCompound; | ||||||
|  | import net.minecraft.util.EnumFacing; | ||||||
|  | import net.minecraft.util.math.BlockPos; | ||||||
|  | import net.minecraft.util.math.Vec3d; | ||||||
|  | import net.minecraft.world.World; | ||||||
|  |  | ||||||
|  | import javax.annotation.Nonnull; | ||||||
|  |  | ||||||
|  | public abstract class TileWirelessModemBase extends TileGeneric implements IPeripheralTile | ||||||
|  | { | ||||||
|  |     private static class Peripheral extends WirelessModemPeripheral | ||||||
|  |     { | ||||||
|  |         private final TileWirelessModemBase entity; | ||||||
|  |  | ||||||
|  |         Peripheral( TileWirelessModemBase entity ) | ||||||
|  |         { | ||||||
|  |             super( new ModemState( () -> TickScheduler.schedule( entity ) ), true ); | ||||||
|  |             this.entity = entity; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         @Nonnull | ||||||
|  |         @Override | ||||||
|  |         public World getWorld() | ||||||
|  |         { | ||||||
|  |             return entity.getWorld(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         @Nonnull | ||||||
|  |         @Override | ||||||
|  |         public Vec3d getPosition() | ||||||
|  |         { | ||||||
|  |             BlockPos pos = entity.getPos().offset( entity.modemDirection ); | ||||||
|  |             return new Vec3d( pos.getX(), pos.getY(), pos.getZ() ); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public boolean equals( IPeripheral other ) | ||||||
|  |         { | ||||||
|  |             return this == other; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private boolean hasModemDirection = false; | ||||||
|  |     private EnumFacing modemDirection = EnumFacing.DOWN; | ||||||
|  |     private final ModemPeripheral modem = new Peripheral( this ); | ||||||
|  |     private boolean destroyed = false; | ||||||
|  |  | ||||||
|  |     private boolean on = false; | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void onLoad() | ||||||
|  |     { | ||||||
|  |         super.onLoad(); | ||||||
|  |         updateDirection(); | ||||||
|  |         world.scheduleUpdate( getPos(), getBlockType(), 0 ); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void destroy() | ||||||
|  |     { | ||||||
|  |         if( !destroyed ) | ||||||
|  |         { | ||||||
|  |             modem.destroy(); | ||||||
|  |             destroyed = true; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void updateContainingBlockInfo() | ||||||
|  |     { | ||||||
|  |         hasModemDirection = false; | ||||||
|  |         super.updateContainingBlockInfo(); | ||||||
|  |         world.scheduleUpdate( getPos(), getBlockType(), 0 ); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void updateTick() | ||||||
|  |     { | ||||||
|  |         updateDirection(); | ||||||
|  |  | ||||||
|  |         if( modem.getModemState().pollChanged() ) | ||||||
|  |         { | ||||||
|  |             boolean newOn = modem.getModemState().isOpen(); | ||||||
|  |             if( newOn != on ) | ||||||
|  |             { | ||||||
|  |                 on = newOn; | ||||||
|  |                 updateBlock(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private void updateDirection() | ||||||
|  |     { | ||||||
|  |         if( !hasModemDirection ) | ||||||
|  |         { | ||||||
|  |             hasModemDirection = true; | ||||||
|  |             modemDirection = getDirection(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     protected abstract EnumFacing getDirection(); | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void onNeighbourChange() | ||||||
|  |     { | ||||||
|  |         EnumFacing dir = getDirection(); | ||||||
|  |         if( !getWorld().isSideSolid( getPos().offset( dir ), dir.getOpposite() ) ) | ||||||
|  |         { | ||||||
|  |             // Drop everything and remove block | ||||||
|  |             getBlock().dropAllItems( getWorld(), getPos(), false ); | ||||||
|  |             getWorld().setBlockToAir( getPos() ); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     protected void writeDescription( @Nonnull NBTTagCompound nbt ) | ||||||
|  |     { | ||||||
|  |         super.writeDescription( nbt ); | ||||||
|  |         nbt.setBoolean( "on", on ); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public final void readDescription( @Nonnull NBTTagCompound nbt ) | ||||||
|  |     { | ||||||
|  |         super.readDescription( nbt ); | ||||||
|  |         on = nbt.getBoolean( "on" ); | ||||||
|  |         updateBlock(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public boolean isOn() | ||||||
|  |     { | ||||||
|  |         return on; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public IPeripheral getPeripheral( EnumFacing side ) | ||||||
|  |     { | ||||||
|  |         return !destroyed && side == getDirection() ? modem : null; | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 SquidDev
					SquidDev