1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +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:
SquidDev 2019-01-20 14:06:41 +00:00
parent 80a5759bae
commit 8a7e651c99
11 changed files with 323 additions and 567 deletions

View File

@ -7,6 +7,8 @@
package dan200.computercraft.shared.peripheral.common;
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.diskdrive.TileDiskDrive;
import dan200.computercraft.shared.peripheral.modem.ModemBounds;
@ -15,18 +17,21 @@
import dan200.computercraft.shared.peripheral.printer.TilePrinter;
import dan200.computercraft.shared.peripheral.speaker.TileSpeaker;
import dan200.computercraft.shared.util.DirectionUtil;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.BlockStateContainer;
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.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
@ -34,7 +39,7 @@
import javax.annotation.Nonnull;
public class BlockPeripheral extends BlockPeripheralBase
public class BlockPeripheral extends BlockDirectional
{
public static class Properties
{
@ -44,6 +49,7 @@ public static class Properties
public BlockPeripheral()
{
super( Material.ROCK );
setHardness( 2.0f );
setTranslationKey( "computercraft:peripheral" );
setCreativeTab( ComputerCraft.mainCreativeTab );
@ -187,202 +193,107 @@ public int getMetaFromState( IBlockState state )
@Deprecated
public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess world, BlockPos pos )
{
int anim;
EnumFacing dir;
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 );
switch( type )
{
case DiskDrive:
{
state = state.withProperty( Properties.FACING, dir );
switch( anim )
if( !(tile instanceof TileDiskDrive) ) return state;
TileDiskDrive drive = (TileDiskDrive) tile;
state = state.withProperty( Properties.FACING, drive.getDirection() );
switch( drive.getAnim() )
{
case 0:
default:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveEmpty );
break;
}
case 0:
return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveEmpty );
case 1:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveInvalid );
break;
}
return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveInvalid );
case 2:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveFull );
break;
}
return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveFull );
}
break;
}
case Printer:
{
state = state.withProperty( Properties.FACING, dir );
switch( anim )
if( !(tile instanceof TilePrinter) ) return state;
TilePrinter printer = (TilePrinter) tile;
state = state.withProperty( Properties.FACING, printer.getDirection() );
switch( printer.getAnim() )
{
case 0:
default:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterEmpty );
break;
}
case 0:
return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterEmpty );
case 1:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterTopFull );
break;
}
return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterTopFull );
case 2:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBottomFull );
break;
}
return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBottomFull );
case 3:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBothFull );
break;
}
return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBothFull );
}
break;
}
case WirelessModem:
{
switch( dir )
if( !(tile instanceof TileWirelessModem) ) return state;
TileWirelessModem modem = (TileWirelessModem) tile;
EnumFacing direction = modem.getDirection();
switch( direction )
{
case UP:
{
state = state.withProperty( Properties.FACING, EnumFacing.NORTH );
switch( anim )
{
case 0:
default:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemUpOff );
break;
}
case 1:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemUpOn );
break;
}
}
break;
}
return state
.withProperty( Properties.FACING, EnumFacing.NORTH )
.withProperty( Properties.VARIANT,
modem.isOn() ? BlockPeripheralVariant.WirelessModemUpOn : BlockPeripheralVariant.WirelessModemUpOff );
case DOWN:
{
state = state.withProperty( Properties.FACING, EnumFacing.NORTH );
switch( anim )
{
case 0:
default:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemDownOff );
break;
}
case 1:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemDownOn );
break;
}
}
break;
}
return state
.withProperty( Properties.FACING, EnumFacing.NORTH )
.withProperty( Properties.VARIANT,
modem.isOn() ? BlockPeripheralVariant.WirelessModemDownOn : BlockPeripheralVariant.WirelessModemDownOff );
default:
{
state = state.withProperty( Properties.FACING, dir );
switch( anim )
{
case 0:
default:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemOff );
break;
}
case 1:
{
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemOn );
break;
}
}
break;
return state
.withProperty( Properties.FACING, direction )
.withProperty( Properties.VARIANT,
modem.isOn() ? BlockPeripheralVariant.WirelessModemOn : BlockPeripheralVariant.WirelessModemOff );
}
}
break;
}
case Speaker:
{
state = state.withProperty( Properties.FACING, dir );
break;
if( !(tile instanceof TileSpeaker) ) return state;
return state.withProperty( Properties.FACING, ((TileSpeaker) tile).getDirection() );
}
case Monitor:
case AdvancedMonitor:
{
EnumFacing front;
int xIndex, yIndex, width, height;
if( tile instanceof TileMonitor )
{
TileMonitor monitor = (TileMonitor) tile;
dir = monitor.getDirection();
front = monitor.getFront();
xIndex = monitor.getXIndex();
yIndex = monitor.getYIndex();
width = monitor.getWidth();
height = monitor.getHeight();
}
else
{
dir = EnumFacing.NORTH;
front = EnumFacing.NORTH;
xIndex = 0;
yIndex = 0;
width = 1;
height = 1;
}
if( !(tile instanceof TileMonitor) ) return state;
TileMonitor monitor = (TileMonitor) tile;
EnumFacing dir = monitor.getDirection();
EnumFacing front = monitor.getFront();
int xIndex = monitor.getXIndex();
int yIndex = monitor.getYIndex();
int width = monitor.getWidth();
int height = monitor.getHeight();
BlockPeripheralVariant baseVariant;
if( front == EnumFacing.UP )
{
baseVariant = (type == PeripheralType.AdvancedMonitor) ?
baseVariant = type == PeripheralType.AdvancedMonitor ?
BlockPeripheralVariant.AdvancedMonitorUp :
BlockPeripheralVariant.MonitorUp;
}
else if( front == EnumFacing.DOWN )
{
baseVariant = (type == PeripheralType.AdvancedMonitor) ?
baseVariant = type == PeripheralType.AdvancedMonitor ?
BlockPeripheralVariant.AdvancedMonitorDown :
BlockPeripheralVariant.MonitorDown;
}
else
{
baseVariant = (type == PeripheralType.AdvancedMonitor) ?
baseVariant = type == PeripheralType.AdvancedMonitor ?
BlockPeripheralVariant.AdvancedMonitor :
BlockPeripheralVariant.Monitor;
}
@ -446,20 +357,21 @@ else if( yIndex < height - 1 )
}
}
state = state.withProperty( Properties.FACING, dir );
state = state.withProperty( Properties.VARIANT,
BlockPeripheralVariant.values()[baseVariant.ordinal() + subType]
);
break;
return state
.withProperty( Properties.FACING, dir )
.withProperty( Properties.VARIANT, BlockPeripheralVariant.values()[baseVariant.ordinal() + subType] );
}
default:
return state;
}
return state;
}
@Nonnull
@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:
default:
@ -515,45 +427,32 @@ else if( dir == EnumFacing.UP )
}
}
@Override
public PeripheralType getPeripheralType( int damage )
{
return ComputerCraft.Items.peripheral.getPeripheralType( damage );
}
@Override
public PeripheralType getPeripheralType( IBlockState state )
{
return state.getValue( Properties.VARIANT ).getPeripheralType();
}
@Override
public TilePeripheralBase createTile( PeripheralType type )
private TileGeneric createTile( PeripheralType type )
{
switch( type )
{
case DiskDrive:
default:
{
return new TileDiskDrive();
}
case WirelessModem:
{
return new TileWirelessModem();
}
case Monitor:
case AdvancedMonitor:
{
return new TileMonitor();
}
case Printer:
{
return new TilePrinter();
}
case Speaker:
{
return new TileSpeaker();
}
}
}
@ -567,12 +466,11 @@ public void onBlockPlacedBy( World world, BlockPos pos, IBlockState state, Entit
case DiskDrive:
case Printer:
{
EnumFacing dir = DirectionUtil.fromEntityRot( player );
setDirection( world, pos, dir );
if( stack.hasDisplayName() && tile instanceof TilePeripheralBase )
if( stack.hasDisplayName() && tile instanceof ITilePeripheral )
{
TilePeripheralBase peripheral = (TilePeripheralBase) tile;
ITilePeripheral peripheral = (ITilePeripheral) tile;
peripheral.setLabel( stack.getDisplayName() );
peripheral.setDirection( DirectionUtil.fromEntityRot( player ) );
}
break;
}
@ -659,6 +557,7 @@ public int getLightOpacity( IBlockState state )
@Override
@Deprecated
@Nonnull
public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos )
{
if( getPeripheralType( state ) == PeripheralType.WirelessModem )
@ -668,4 +567,32 @@ public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, Blo
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 );
}
}

View File

@ -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 );
}
}

View File

@ -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 )
{
}
}

View File

@ -15,7 +15,7 @@
public class PeripheralItemFactory
{
@Nonnull
public static ItemStack create( TilePeripheralBase tile )
public static ItemStack create( ITilePeripheral tile )
{
return create( tile.getPeripheralType(), tile.getLabel(), 1 );
}

View File

@ -18,11 +18,9 @@
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
protected EnumFacing m_dir;
private EnumFacing m_dir;
private int m_anim;
private boolean m_changed;
@ -39,9 +37,9 @@ public TilePeripheralBase()
@Override
public BlockPeripheralBase getBlock()
public BlockPeripheral getBlock()
{
return (BlockPeripheralBase) super.getBlock();
return (BlockPeripheral) super.getBlock();
}
@Override
@ -53,6 +51,7 @@ public void getDroppedItems( @Nonnull NonNullList<ItemStack> drops, boolean crea
}
}
@Override
public final PeripheralType getPeripheralType()
{
return getBlock().getPeripheralType( getBlockState() );
@ -64,6 +63,7 @@ public IPeripheral getPeripheral( EnumFacing side )
return null;
}
@Override
public String getLabel()
{
if( m_label != null && m_label.length() > 0 )
@ -86,11 +86,6 @@ public EnumFacing getDirection()
return m_dir;
}
public EnumFacing getCachedDirection()
{
return m_dir;
}
@Override
public void setDirection( EnumFacing dir )
{
@ -106,7 +101,7 @@ public int getAnim()
return m_anim;
}
public void setAnim( int anim )
protected void setAnim( int anim )
{
if( anim != m_anim )
{

View File

@ -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;
}
}

View File

@ -13,7 +13,6 @@
import dan200.computercraft.api.network.wired.IWiredNode;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.command.CommandCopy;
import dan200.computercraft.shared.common.BlockGeneric;
import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.common.IPeripheralTile;
@ -215,7 +214,7 @@ public void onNeighbourChange()
case WiredModem:
{
// Drop everything and remove block
((BlockGeneric) getBlockType()).dropAllItems( getWorld(), getPos(), false );
getBlock().dropAllItems( getWorld(), getPos(), false );
getWorld().setBlockToAir( getPos() );
// This'll call #destroy(), so we don't need to reset the network here.
@ -224,7 +223,7 @@ public void onNeighbourChange()
case WiredModemWithCable:
{
// 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 ) );
modemChanged();
connectionsChanged();

View File

@ -7,6 +7,7 @@
package dan200.computercraft.shared.peripheral.modem.wired;
import com.google.common.base.Objects;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.network.wired.IWiredElement;
import dan200.computercraft.api.network.wired.IWiredNode;
@ -18,9 +19,11 @@
import dan200.computercraft.shared.util.TickScheduler;
import dan200.computercraft.shared.wired.CapabilityWiredElement;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentString;
@ -132,6 +135,12 @@ public void invalidate()
remove();
}
@Override
public void getDroppedItems( @Nonnull NonNullList<ItemStack> drops, boolean creative )
{
drops.add( new ItemStack( ComputerCraft.Items.wiredModemFull ) );
}
@Override
public void onNeighbourChange()
{

View File

@ -7,140 +7,23 @@
package dan200.computercraft.shared.peripheral.modem.wireless;
import dan200.computercraft.ComputerCraft;
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.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.util.NonNullList;
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
public void onLoad()
{
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()
protected EnumFacing getDirection()
{
return getBlockState().getValue( BlockAdvancedModem.Properties.FACING );
}
@Override
protected void writeDescription( @Nonnull NBTTagCompound nbt )
public void getDroppedItems( @Nonnull NonNullList<ItemStack> drops, boolean creative )
{
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;
if( !creative ) drops.add( new ItemStack( ComputerCraft.Items.advancedModem ) );
}
}

View File

@ -7,100 +7,23 @@
package dan200.computercraft.shared.peripheral.modem.wireless;
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.common.BlockPeripheral;
import dan200.computercraft.shared.peripheral.common.BlockPeripheralVariant;
import dan200.computercraft.shared.peripheral.modem.ModemPeripheral;
import dan200.computercraft.shared.peripheral.modem.ModemState;
import dan200.computercraft.shared.peripheral.modem.TileModemBase;
import dan200.computercraft.shared.peripheral.common.ITilePeripheral;
import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
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
public EnumFacing getDirection()
{
@ -110,18 +33,12 @@ public EnumFacing getDirection()
{
case WirelessModemDownOff:
case WirelessModemDownOn:
{
return EnumFacing.DOWN;
}
case WirelessModemUpOff:
case WirelessModemUpOn:
{
return EnumFacing.UP;
}
default:
{
return state.getValue( BlockPeripheral.Properties.FACING );
}
}
}
@ -152,15 +69,21 @@ else if( dir == EnumFacing.DOWN )
}
}
@Override
protected ModemPeripheral createPeripheral()
{
return new Peripheral( this );
}
@Override
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;
}
@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;
}
}

View File

@ -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;
}
}