diff --git a/src/main/java/dan200/computercraft/ComputerCraft.java b/src/main/java/dan200/computercraft/ComputerCraft.java index 136751fed..14e78c882 100644 --- a/src/main/java/dan200/computercraft/ComputerCraft.java +++ b/src/main/java/dan200/computercraft/ComputerCraft.java @@ -43,10 +43,10 @@ import dan200.computercraft.shared.media.items.ItemTreasureDisk; import dan200.computercraft.shared.network.ComputerCraftPacket; import dan200.computercraft.shared.network.PacketHandler; -import dan200.computercraft.shared.peripheral.modem.wired.BlockCable; import dan200.computercraft.shared.peripheral.common.BlockPeripheral; -import dan200.computercraft.shared.peripheral.modem.wired.BlockWiredModemFull; import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive; +import dan200.computercraft.shared.peripheral.modem.wired.BlockCable; +import dan200.computercraft.shared.peripheral.modem.wired.BlockWiredModemFull; import dan200.computercraft.shared.peripheral.modem.wireless.BlockAdvancedModem; import dan200.computercraft.shared.peripheral.modem.wireless.WirelessNetwork; import dan200.computercraft.shared.peripheral.printer.TilePrinter; diff --git a/src/main/java/dan200/computercraft/client/render/RenderOverlayCable.java b/src/main/java/dan200/computercraft/client/render/RenderOverlayCable.java index 87c208fe2..481a6dd25 100644 --- a/src/main/java/dan200/computercraft/client/render/RenderOverlayCable.java +++ b/src/main/java/dan200/computercraft/client/render/RenderOverlayCable.java @@ -3,7 +3,7 @@ import dan200.computercraft.ComputerCraft; import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.modem.wired.BlockCable; -import dan200.computercraft.shared.peripheral.modem.wired.TileCable; +import dan200.computercraft.shared.peripheral.modem.wired.CableBounds; import dan200.computercraft.shared.util.WorldUtil; import net.minecraft.block.state.IBlockState; import net.minecraft.client.renderer.BufferBuilder; @@ -12,7 +12,6 @@ import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.RayTraceResult; @@ -24,8 +23,8 @@ public class RenderOverlayCable { private static final float EXPAND = 0.002f; - private static final double MIN = TileCable.MIN - EXPAND; - private static final double MAX = TileCable.MAX + EXPAND; + private static final double MIN = CableBounds.MIN - EXPAND; + private static final double MAX = CableBounds.MAX + EXPAND; @SubscribeEvent public void drawHighlight( DrawBlockHighlightEvent event ) @@ -38,13 +37,10 @@ public void drawHighlight( DrawBlockHighlightEvent event ) IBlockState state = world.getBlockState( pos ); if( state.getBlock() != ComputerCraft.Blocks.cable ) return; - TileEntity tile = world.getTileEntity( pos ); - if( tile == null || !(tile instanceof TileCable) ) return; + state = state.getActualState( world, pos ); event.setCanceled( true ); - TileCable cable = (TileCable) tile; - - PeripheralType type = cable.getPeripheralType(); + PeripheralType type = ComputerCraft.Blocks.cable.getPeripheralType( state ); GlStateManager.enableBlend(); GlStateManager.tryBlendFuncSeparate( GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0 ); @@ -63,9 +59,9 @@ public void drawHighlight( DrawBlockHighlightEvent event ) GlStateManager.translate( -x + pos.getX(), -y + pos.getY(), -z + pos.getZ() ); } - if( type != PeripheralType.Cable && WorldUtil.isVecInsideInclusive( cable.getModemBounds(), event.getTarget().hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) ) + if( type != PeripheralType.Cable && WorldUtil.isVecInsideInclusive( CableBounds.getModemBounds( state ), event.getTarget().hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) ) { - RenderGlobal.drawSelectionBoundingBox( cable.getModemBounds(), 0, 0, 0, 0.4f ); + RenderGlobal.drawSelectionBoundingBox( CableBounds.getModemBounds( state ), 0, 0, 0, 0.4f ); } else { diff --git a/src/main/java/dan200/computercraft/client/render/TileEntityCableRenderer.java b/src/main/java/dan200/computercraft/client/render/TileEntityCableRenderer.java index 2d20924c1..a7ed38b63 100644 --- a/src/main/java/dan200/computercraft/client/render/TileEntityCableRenderer.java +++ b/src/main/java/dan200/computercraft/client/render/TileEntityCableRenderer.java @@ -4,6 +4,7 @@ import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.modem.wired.BlockCable; import dan200.computercraft.shared.peripheral.modem.wired.BlockCableModemVariant; +import dan200.computercraft.shared.peripheral.modem.wired.CableBounds; import dan200.computercraft.shared.peripheral.modem.wired.TileCable; import dan200.computercraft.shared.util.WorldUtil; import net.minecraft.block.Block; @@ -52,7 +53,7 @@ public void render( @Nonnull TileCable te, double x, double y, double z, float p if( block != ComputerCraft.Blocks.cable ) return; state = state.getActualState( world, pos ); - if( te.getPeripheralType() != PeripheralType.Cable && WorldUtil.isVecInsideInclusive( te.getModemBounds(), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) ) + if( te.getPeripheralType() != PeripheralType.Cable && WorldUtil.isVecInsideInclusive( CableBounds.getModemBounds( state ), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) ) { state = block.getDefaultState().withProperty( BlockCable.Properties.MODEM, state.getValue( BlockCable.Properties.MODEM ) ); } diff --git a/src/main/java/dan200/computercraft/shared/common/BlockDirectional.java b/src/main/java/dan200/computercraft/shared/common/BlockDirectional.java index 0b048df93..2a9d3aee7 100644 --- a/src/main/java/dan200/computercraft/shared/common/BlockDirectional.java +++ b/src/main/java/dan200/computercraft/shared/common/BlockDirectional.java @@ -23,7 +23,7 @@ protected BlockDirectional( Material material ) public EnumFacing getDirection( IBlockAccess world, BlockPos pos ) { TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof IDirectionalTile ) + if( tile instanceof IDirectionalTile ) { IDirectionalTile directional = (IDirectionalTile) tile; return directional.getDirection(); @@ -34,7 +34,7 @@ public EnumFacing getDirection( IBlockAccess world, BlockPos pos ) public void setDirection( World world, BlockPos pos, EnumFacing dir ) { TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof IDirectionalTile ) + if( tile instanceof IDirectionalTile ) { IDirectionalTile directional = (IDirectionalTile) tile; directional.setDirection( dir ); diff --git a/src/main/java/dan200/computercraft/shared/common/BlockGeneric.java b/src/main/java/dan200/computercraft/shared/common/BlockGeneric.java index 8140f42cf..9748d1db4 100644 --- a/src/main/java/dan200/computercraft/shared/common/BlockGeneric.java +++ b/src/main/java/dan200/computercraft/shared/common/BlockGeneric.java @@ -10,7 +10,6 @@ import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; @@ -18,20 +17,13 @@ import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.NonNullList; -import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.RayTraceResult; -import net.minecraft.world.Explosion; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.List; -public abstract class BlockGeneric extends Block implements - ITileEntityProvider +public abstract class BlockGeneric extends Block implements ITileEntityProvider { protected BlockGeneric( Material material ) { @@ -54,7 +46,7 @@ public final void dropBlockAsItemWithChance( World world, @Nonnull BlockPos pos, public final void getDrops( @Nonnull NonNullList drops, IBlockAccess world, BlockPos pos, @Nonnull IBlockState state, int fortune ) { TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric ) + if( tile instanceof TileGeneric ) { TileGeneric generic = (TileGeneric) tile; generic.getDroppedItems( drops, false ); @@ -88,7 +80,7 @@ public final void dropAllItems( World world, BlockPos pos, boolean creative ) // Get items to drop NonNullList drops = NonNullList.create(); TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric ) + if( tile instanceof TileGeneric ) { TileGeneric generic = (TileGeneric) tile; generic.getDroppedItems( drops, creative ); @@ -115,34 +107,21 @@ public final void breakBlock( @Nonnull World world, @Nonnull BlockPos pos, @Nonn TileEntity tile = world.getTileEntity( pos ); super.breakBlock( world, pos, newState ); world.removeTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric ) + if( tile instanceof TileGeneric ) { TileGeneric generic = (TileGeneric) tile; generic.destroy(); } } - @Nonnull - @Override - public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player ) - { - TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric ) - { - TileGeneric generic = (TileGeneric) tile; - return generic.getPickedItem(); - } - return ItemStack.EMPTY; - } - @Override public final boolean onBlockActivated( World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ ) { TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric ) + if( tile instanceof TileGeneric ) { TileGeneric generic = (TileGeneric) tile; - return generic.onActivate( player, side, hitX, hitY, hitZ ); + return generic.onActivate( player, hand, side, hitX, hitY, hitZ ); } return false; } @@ -152,7 +131,7 @@ public final boolean onBlockActivated( World world, BlockPos pos, IBlockState st public final void neighborChanged( IBlockState state, World world, BlockPos pos, Block block, BlockPos neighorPos ) { TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric ) + if( tile instanceof TileGeneric ) { TileGeneric generic = (TileGeneric) tile; generic.onNeighbourChange(); @@ -170,113 +149,6 @@ public final void onNeighborChange( IBlockAccess world, BlockPos pos, BlockPos n } } - @Override - @Deprecated - public final boolean isSideSolid( IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, EnumFacing side ) - { - TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric ) - { - TileGeneric generic = (TileGeneric) tile; - return generic.isSolidOnSide( side.ordinal() ); - } - return false; - } - - @Override - public final boolean canBeReplacedByLeaves( @Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos ) - { - return false; // Generify me if anyone ever feels the need to change this - } - - @Override - public float getExplosionResistance( World world, BlockPos pos, @Nullable Entity exploder, Explosion explosion ) - { - TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric && tile.hasWorld() ) - { - TileGeneric generic = (TileGeneric) tile; - if( generic.isImmuneToExplosion( exploder ) ) - { - return 2000.0f; - } - } - return super.getExplosionResistance( world, pos, exploder, explosion ); - } - - @Nonnull - @Override - @Deprecated - public final AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess world, BlockPos pos ) - { - TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric && tile.hasWorld() ) - { - TileGeneric generic = (TileGeneric) tile; - return generic.getBounds(); - } - return FULL_BLOCK_AABB; - } - - @Nonnull - @Override - @Deprecated - public final AxisAlignedBB getSelectedBoundingBox( IBlockState state, @Nonnull World world, @Nonnull BlockPos pos ) - { - return getBoundingBox( state, world, pos ).offset( pos ); - } - - @Override - @Deprecated - public final AxisAlignedBB getCollisionBoundingBox( IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos ) - { - TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric && tile.hasWorld() ) - { - TileGeneric generic = (TileGeneric) tile; - - // Get collision bounds - List collision = new ArrayList<>( 1 ); - generic.getCollisionBounds( collision ); - - // Return the union of the collision bounds - if( collision.size() > 0 ) - { - AxisAlignedBB aabb = collision.get( 0 ); - for( int i = 1; i < collision.size(); i++ ) - { - aabb = aabb.union( collision.get( i ) ); - } - return aabb; - } - } - return FULL_BLOCK_AABB; - } - - @Override - @Deprecated - public final void addCollisionBoxToList( IBlockState state, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull AxisAlignedBB bigBox, @Nonnull List list, Entity entity, boolean p_185477_7_ ) - { - TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric && tile.hasWorld() ) - { - TileGeneric generic = (TileGeneric) tile; - - // Get collision bounds - List collision = new ArrayList<>( 1 ); - generic.getCollisionBounds( collision ); - - // Add collision bounds to list - if( collision.size() > 0 ) - { - for( AxisAlignedBB localBounds : collision ) - { - addCollisionBoxToList( pos, bigBox, list, localBounds ); - } - } - } - } - @Override @Deprecated public final boolean canProvidePower( IBlockState state ) @@ -288,7 +160,7 @@ public final boolean canProvidePower( IBlockState state ) public final boolean canConnectRedstone( IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing side ) { TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric ) + if( tile instanceof TileGeneric ) { TileGeneric generic = (TileGeneric) tile; return generic.getRedstoneConnectivity( side ); @@ -301,7 +173,7 @@ public final boolean canConnectRedstone( IBlockState state, IBlockAccess world, public final int getStrongPower( IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing oppositeSide ) { TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric && tile.hasWorld() ) + if( tile instanceof TileGeneric && tile.hasWorld() ) { TileGeneric generic = (TileGeneric) tile; return generic.getRedstoneOutput( oppositeSide.getOpposite() ); @@ -319,7 +191,7 @@ public final int getWeakPower( IBlockState state, IBlockAccess world, BlockPos p public boolean getBundledRedstoneConnectivity( World world, BlockPos pos, EnumFacing side ) { TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric ) + if( tile instanceof TileGeneric ) { TileGeneric generic = (TileGeneric) tile; return generic.getBundledRedstoneConnectivity( side ); @@ -330,7 +202,7 @@ public boolean getBundledRedstoneConnectivity( World world, BlockPos pos, EnumFa public int getBundledRedstoneOutput( World world, BlockPos pos, EnumFacing side ) { TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileGeneric && tile.hasWorld() ) + if( tile instanceof TileGeneric && tile.hasWorld() ) { TileGeneric generic = (TileGeneric) tile; return generic.getBundledRedstoneOutput( side ); @@ -351,4 +223,12 @@ public final TileEntity createNewTileEntity( @Nonnull World world, int damage ) { return createTile( damage ); } + + @Override + public boolean isSideSolid( IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, EnumFacing side ) + { + // We need to override this as the default implementation uses isNormalCube, which returns false if + // it can provide power. + return isFullCube( state ); + } } diff --git a/src/main/java/dan200/computercraft/shared/common/TileGeneric.java b/src/main/java/dan200/computercraft/shared/common/TileGeneric.java index 49514e76d..520d26273 100644 --- a/src/main/java/dan200/computercraft/shared/common/TileGeneric.java +++ b/src/main/java/dan200/computercraft/shared/common/TileGeneric.java @@ -10,7 +10,6 @@ import dan200.computercraft.shared.network.ComputerCraftPacket; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -18,21 +17,16 @@ import net.minecraft.network.play.server.SPacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; import net.minecraft.util.NonNullList; -import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.List; public abstract class TileGeneric extends TileEntity { - public TileGeneric() - { - } - public void requestTileEntityUpdate() { if( getWorld().isRemote ) @@ -54,11 +48,7 @@ public void destroy() public BlockGeneric getBlock() { Block block = getWorld().getBlockState( getPos() ).getBlock(); - if( block != null && block instanceof BlockGeneric ) - { - return (BlockGeneric) block; - } - return null; + return block instanceof BlockGeneric ? (BlockGeneric) block : null; } protected final IBlockState getBlockState() @@ -84,12 +74,7 @@ public void getDroppedItems( @Nonnull NonNullList drops, boolean crea { } - public ItemStack getPickedItem() - { - return null; - } - - public boolean onActivate( EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ ) + public boolean onActivate( EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ ) { return false; } @@ -102,27 +87,6 @@ public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour ) { } - public boolean isSolidOnSide( int side ) - { - return true; - } - - public boolean isImmuneToExplosion( Entity exploder ) - { - return false; - } - - @Nonnull - public AxisAlignedBB getBounds() - { - return new AxisAlignedBB( 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 ); - } - - public void getCollisionBounds( @Nonnull List bounds ) - { - bounds.add( getBounds() ); - } - public boolean getRedstoneConnectivity( EnumFacing side ) { return false; @@ -150,21 +114,13 @@ protected double getInteractRange( EntityPlayer player ) public boolean isUsable( EntityPlayer player, boolean ignoreRange ) { - if( player != null && player.isEntityAlive() ) - { - if( getWorld().getTileEntity( getPos() ) == this ) - { - if( !ignoreRange ) - { - double range = getInteractRange( player ); - BlockPos pos = getPos(); - return player.getEntityWorld() == getWorld() && - player.getDistanceSq( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 ) <= (range * range); - } - return true; - } - } - return false; + if( player == null || !player.isEntityAlive() || getWorld().getTileEntity( getPos() ) != this ) return false; + if( ignoreRange ) return true; + + double range = getInteractRange( player ); + BlockPos pos = getPos(); + return player.getEntityWorld() == getWorld() && + player.getDistanceSq( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 ) <= (range * range); } protected void writeDescription( @Nonnull NBTTagCompound nbttagcompound ) diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputer.java b/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputer.java index aeb7d39b5..815d778ae 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputer.java @@ -9,6 +9,7 @@ import dan200.computercraft.ComputerCraft; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.IComputer; +import dan200.computercraft.shared.computer.items.ComputerItemFactory; import dan200.computercraft.shared.computer.items.ItemComputer; import dan200.computercraft.shared.util.DirectionUtil; import net.minecraft.block.material.Material; @@ -18,11 +19,13 @@ 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.Item; 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; @@ -181,4 +184,12 @@ public void onBlockPlacedBy( World world, BlockPos pos, IBlockState state, Entit EnumFacing dir = DirectionUtil.fromEntityRot( player ); setDirection( world, pos, dir ); } + + @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 TileComputer ? ComputerItemFactory.create( (TileComputer) tile ) : super.getPickBlock( state, target, world, pos, player ); + } } diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java index 06c299e04..fc5e0b7ba 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputer.java @@ -75,12 +75,6 @@ public void getDroppedItems( @Nonnull NonNullList drops, boolean crea } } - @Override - public ItemStack getPickedItem() - { - return ComputerItemFactory.create( this ); - } - @Override public void openGUI( EntityPlayer player ) { diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java index 06f4a5347..07fde38e7 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/TileComputerBase.java @@ -116,22 +116,15 @@ protected boolean onDefaultComputerInteract( EntityPlayer player ) } @Override - public boolean onActivate( EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ ) + public boolean onActivate( EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ ) { - ItemStack currentItem = player.getHeldItem( EnumHand.MAIN_HAND ); + ItemStack currentItem = player.getHeldItem( hand ); if( !currentItem.isEmpty() && currentItem.getItem() == Items.NAME_TAG && canNameWithTag( player ) ) { // Label to rename computer if( !getWorld().isRemote ) { - if( currentItem.hasDisplayName() ) - { - setLabel( currentItem.getDisplayName() ); - } - else - { - setLabel( null ); - } + setLabel( currentItem.hasDisplayName() ? currentItem.getDisplayName() : null ); currentItem.shrink( 1 ); } return true; diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java index a2ff07371..af0b79c71 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java @@ -9,6 +9,7 @@ import dan200.computercraft.ComputerCraft; import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive; +import dan200.computercraft.shared.peripheral.modem.ModemBounds; import dan200.computercraft.shared.peripheral.modem.wireless.TileWirelessModem; import dan200.computercraft.shared.peripheral.monitor.TileMonitor; import dan200.computercraft.shared.peripheral.printer.TilePrinter; @@ -25,6 +26,7 @@ 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.world.IBlockAccess; import net.minecraft.world.World; @@ -662,4 +664,16 @@ public int getLightOpacity( IBlockState state ) // This normally uses the default state return isOpaqueCube( state ) ? 255 : 0; } + + @Override + @Deprecated + public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos ) + { + if( getPeripheralType( state ) == PeripheralType.WirelessModem ) + { + return ModemBounds.getBounds( getDirection( source, pos ) ); + } + + return super.getBoundingBox( state, source, pos ); + } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java index e184780c3..3e45e2dd3 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java @@ -11,9 +11,13 @@ import dan200.computercraft.shared.peripheral.PeripheralType; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; +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; @@ -63,4 +67,12 @@ 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 IPeripheralTile ? PeripheralItemFactory.create( (IPeripheralTile) tile ) : super.getPickBlock( state, target, world, pos, player ); + } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java b/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java index 168012675..4524aacff 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java @@ -53,12 +53,6 @@ public void getDroppedItems( @Nonnull NonNullList drops, boolean crea } } - @Override - public ItemStack getPickedItem() - { - return PeripheralItemFactory.create( this ); - } - // IPeripheralTile implementation @Override diff --git a/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/TileDiskDrive.java b/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/TileDiskDrive.java index 32a4457df..3a104c18a 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/TileDiskDrive.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/diskdrive/TileDiskDrive.java @@ -87,20 +87,20 @@ public void destroy() } @Override - public boolean onActivate( EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ ) + public boolean onActivate( EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ ) { if( player.isSneaking() ) { // Try to put a disk into the drive if( !getWorld().isRemote ) { - ItemStack disk = player.getHeldItem( EnumHand.MAIN_HAND ); + ItemStack disk = player.getHeldItem( hand ); if( !disk.isEmpty() && getStackInSlot( 0 ).isEmpty() ) { if( ComputerCraft.getMedia( disk ) != null ) { setInventorySlotContents( 0, disk ); - player.setHeldItem( EnumHand.MAIN_HAND, ItemStack.EMPTY ); + player.setHeldItem( hand, ItemStack.EMPTY ); return true; } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemBounds.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemBounds.java new file mode 100644 index 000000000..049d4eaf0 --- /dev/null +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemBounds.java @@ -0,0 +1,32 @@ +/* + * This file is part of ComputerCraft - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission. + * Send enquiries to dratcliffe@gmail.com + */ + +package dan200.computercraft.shared.peripheral.modem; + +import net.minecraft.block.Block; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.AxisAlignedBB; + +import javax.annotation.Nonnull; + +public class ModemBounds +{ + private static final AxisAlignedBB[] BOXES = new AxisAlignedBB[] { + new AxisAlignedBB( 0.125, 0.0, 0.125, 0.875, 0.1875, 0.875 ), // Down + new AxisAlignedBB( 0.125, 0.8125, 0.125, 0.875, 1.0, 0.875 ), // Up + new AxisAlignedBB( 0.125, 0.125, 0.0, 0.875, 0.875, 0.1875 ), // North + new AxisAlignedBB( 0.125, 0.125, 0.8125, 0.875, 0.875, 1.0 ), // South + new AxisAlignedBB( 0.0, 0.125, 0.125, 0.1875, 0.875, 0.875 ), // West + new AxisAlignedBB( 0.8125, 0.125, 0.125, 1.0, 0.875, 0.875 ), // East + }; + + @Nonnull + public static AxisAlignedBB getBounds( EnumFacing facing ) + { + int direction = facing.ordinal(); + return direction < BOXES.length ? BOXES[direction] : Block.FULL_BLOCK_AABB; + } +} diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileModemBase.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileModemBase.java index 1bed57029..1c0a32418 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileModemBase.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileModemBase.java @@ -9,23 +9,13 @@ import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.shared.common.BlockGeneric; import dan200.computercraft.shared.peripheral.common.TilePeripheralBase; -import net.minecraft.block.Block; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; -import net.minecraft.util.math.AxisAlignedBB; import javax.annotation.Nonnull; public abstract class TileModemBase extends TilePeripheralBase { - private static final AxisAlignedBB[] BOXES = new AxisAlignedBB[] { - new AxisAlignedBB( 0.125, 0.0, 0.125, 0.875, 0.1875, 0.875 ), // Down - new AxisAlignedBB( 0.125, 0.8125, 0.125, 0.875, 1.0, 0.875 ), // Up - new AxisAlignedBB( 0.125, 0.125, 0.0, 0.875, 0.875, 0.1875 ), // North - new AxisAlignedBB( 0.125, 0.125, 0.8125, 0.875, 0.875, 1.0 ), // South - new AxisAlignedBB( 0.0, 0.125, 0.125, 0.1875, 0.875, 0.875 ), // West - new AxisAlignedBB( 0.8125, 0.125, 0.125, 1.0, 0.875, 0.875 ), // East - }; protected ModemPeripheral m_modem; @@ -46,12 +36,6 @@ public void destroy() } } - @Override - public boolean isSolidOnSide( int side ) - { - return false; - } - @Override public void onNeighbourChange() { @@ -64,14 +48,6 @@ public void onNeighbourChange() } } - @Nonnull - @Override - public AxisAlignedBB getBounds() - { - int direction = getDirection().ordinal(); - return direction >= 0 && direction < BOXES.length ? BOXES[direction] : Block.FULL_BLOCK_AABB; - } - @Override public void update() { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java index ab687dcbc..f947946fb 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java @@ -6,8 +6,8 @@ package dan200.computercraft.shared.peripheral.modem.wired; +import com.google.common.collect.ImmutableMap; import dan200.computercraft.ComputerCraft; -import dan200.computercraft.shared.common.TileGeneric; import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.common.BlockPeripheralBase; import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory; @@ -18,6 +18,7 @@ import net.minecraft.block.state.BlockFaceShape; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; @@ -34,6 +35,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.ArrayList; +import java.util.EnumMap; import java.util.List; public class BlockCable extends BlockPeripheralBase @@ -50,6 +52,13 @@ public static class Properties public static final PropertyBool WEST = PropertyBool.create( "west" ); public static final PropertyBool UP = PropertyBool.create( "up" ); public static final PropertyBool DOWN = PropertyBool.create( "down" ); + + static final EnumMap CONNECTIONS = + new EnumMap<>( new ImmutableMap.Builder() + .put( EnumFacing.DOWN, DOWN ).put( EnumFacing.UP, UP ) + .put( EnumFacing.NORTH, NORTH ).put( EnumFacing.SOUTH, SOUTH ) + .put( EnumFacing.WEST, WEST ).put( EnumFacing.EAST, EAST ) + .build() ); } // Members @@ -230,51 +239,60 @@ public TilePeripheralBase createTile( PeripheralType type ) return new TileCable(); } + @Override + @Deprecated + public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos ) + { + return CableBounds.getBounds( state.getActualState( source, pos ) ); + } + + @Override + @Deprecated + public void addCollisionBoxToList( IBlockState state, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull AxisAlignedBB bigBox, @Nonnull List list, Entity entity, boolean isActualState ) + { + if( !isActualState ) state = state.getActualState( world, pos ); + + // Get collision bounds + List collision = new ArrayList<>( 1 ); + CableBounds.getBounds( state, collision ); + for( AxisAlignedBB localBounds : collision ) addCollisionBoxToList( pos, bigBox, list, localBounds ); + } + @Nullable @Override @Deprecated - public RayTraceResult collisionRayTrace( IBlockState blockState, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull Vec3d start, @Nonnull Vec3d end ) + public RayTraceResult collisionRayTrace( IBlockState state, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull Vec3d start, @Nonnull Vec3d end ) { - TileEntity tile = world.getTileEntity( pos ); - if( tile instanceof TileGeneric && tile.hasWorld() ) + double distance = Double.POSITIVE_INFINITY; + RayTraceResult result = null; + + List bounds = new ArrayList( 7 ); + CableBounds.getBounds( state.getActualState( world, pos ), bounds ); + + Vec3d startOff = start.subtract( pos.getX(), pos.getY(), pos.getZ() ); + Vec3d endOff = end.subtract( pos.getX(), pos.getY(), pos.getZ() ); + + for( AxisAlignedBB bb : bounds ) { - TileGeneric generic = (TileGeneric) tile; - - double distance = Double.POSITIVE_INFINITY; - RayTraceResult result = null; - - List bounds = new ArrayList( 7 ); - generic.getCollisionBounds( bounds ); - - Vec3d startOff = start.subtract( pos.getX(), pos.getY(), pos.getZ() ); - Vec3d endOff = end.subtract( pos.getX(), pos.getY(), pos.getZ() ); - - for( AxisAlignedBB bb : bounds ) + RayTraceResult hit = bb.calculateIntercept( startOff, endOff ); + if( hit != null ) { - RayTraceResult hit = bb.calculateIntercept( startOff, endOff ); - if( hit != null ) + double newDistance = hit.hitVec.squareDistanceTo( startOff ); + if( newDistance <= distance ) { - double newDistance = hit.hitVec.squareDistanceTo( startOff ); - if( newDistance <= distance ) - { - distance = newDistance; - result = hit; - } + distance = newDistance; + result = hit; } } + } - return result == null ? null : new RayTraceResult( result.hitVec.add( pos.getX(), pos.getY(), pos.getZ() ), result.sideHit, pos ); - } - else - { - return super.collisionRayTrace( blockState, world, pos, start, end ); - } + return result == null ? null : new RayTraceResult( result.hitVec.add( pos.getX(), pos.getY(), pos.getZ() ), result.sideHit, pos ); } @Override public boolean removedByPlayer( @Nonnull IBlockState state, World world, @Nonnull BlockPos pos, @Nonnull EntityPlayer player, boolean willHarvest ) { - PeripheralType type = getPeripheralType( world, pos ); + PeripheralType type = getPeripheralType( state ); if( type == PeripheralType.WiredModemWithCable ) { RayTraceResult hit = state.collisionRayTrace( world, pos, WorldUtil.getRayStart( player ), WorldUtil.getRayEnd( player ) ); @@ -287,7 +305,7 @@ public boolean removedByPlayer( @Nonnull IBlockState state, World world, @Nonnul ItemStack item; - AxisAlignedBB bb = cable.getModemBounds(); + AxisAlignedBB bb = CableBounds.getModemBounds( state ); if( WorldUtil.isVecInsideInclusive( bb, hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) ) { world.setBlockState( pos, state.withProperty( Properties.MODEM, BlockCableModemVariant.None ), 3 ); @@ -315,30 +333,14 @@ public boolean removedByPlayer( @Nonnull IBlockState state, World world, @Nonnul @Override public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult hit, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player ) { - TileEntity tile = world.getTileEntity( pos ); - if( tile instanceof TileCable && tile.hasWorld() ) + PeripheralType type = getPeripheralType( state ); + if( type == PeripheralType.WiredModemWithCable ) { - TileCable cable = (TileCable) tile; - PeripheralType type = getPeripheralType( state ); - - if( type == PeripheralType.WiredModemWithCable ) - { - if( hit == null || WorldUtil.isVecInsideInclusive( cable.getModemBounds(), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) ) - { - return PeripheralItemFactory.create( PeripheralType.WiredModem, null, 1 ); - } - else - { - return PeripheralItemFactory.create( PeripheralType.Cable, null, 1 ); - } - } - else - { - return PeripheralItemFactory.create( type, null, 1 ); - } + type = hit == null || WorldUtil.isVecInsideInclusive( CableBounds.getModemBounds( state ), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) + ? PeripheralType.WiredModem : PeripheralType.Cable; } - return PeripheralItemFactory.create( PeripheralType.Cable, null, 1 ); + return PeripheralItemFactory.create( type, null, 1 ); } @Override diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/CableBounds.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/CableBounds.java new file mode 100644 index 000000000..8f5442aec --- /dev/null +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/CableBounds.java @@ -0,0 +1,118 @@ +/* + * This file is part of ComputerCraft - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission. + * Send enquiries to dratcliffe@gmail.com + */ + +package dan200.computercraft.shared.peripheral.modem.wired; + +import com.google.common.collect.ImmutableMap; +import dan200.computercraft.shared.peripheral.modem.ModemBounds; +import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.AxisAlignedBB; + +import java.util.EnumMap; +import java.util.List; + +import static dan200.computercraft.shared.peripheral.modem.wired.BlockCable.Properties.*; + +public final class CableBounds +{ + public static final double MIN = 0.375; + public static final double MAX = 1 - MIN; + + private static final AxisAlignedBB SHAPE_CABLE_CORE = new AxisAlignedBB( MIN, MIN, MIN, MAX, MAX, MAX ); + private static final EnumMap SHAPE_CABLE_ARM = + new EnumMap<>( new ImmutableMap.Builder() + .put( EnumFacing.DOWN, new AxisAlignedBB( MIN, 0, MIN, MAX, MIN, MAX ) ) + .put( EnumFacing.UP, new AxisAlignedBB( MIN, MAX, MIN, MAX, 1, MAX ) ) + .put( EnumFacing.NORTH, new AxisAlignedBB( MIN, MIN, 0, MAX, MAX, MIN ) ) + .put( EnumFacing.SOUTH, new AxisAlignedBB( MIN, MIN, MAX, MAX, MAX, 1 ) ) + .put( EnumFacing.WEST, new AxisAlignedBB( 0, MIN, MIN, MIN, MAX, MAX ) ) + .put( EnumFacing.EAST, new AxisAlignedBB( MAX, MIN, MIN, 1, MAX, MAX ) ) + .build() + ); + + private static final AxisAlignedBB[] SHAPES = new AxisAlignedBB[(1 << 6) * 7]; + private static final AxisAlignedBB[] CABLE_SHAPES = new AxisAlignedBB[1 << 6]; + + private CableBounds() + { + } + + private static int getCableIndex( IBlockState state ) + { + int index = 0; + for( EnumFacing facing : EnumFacing.VALUES ) + { + if( state.getValue( CONNECTIONS.get( facing ) ) ) index |= 1 << facing.ordinal(); + } + + return index; + } + + private static AxisAlignedBB getCableBounds( int index ) + { + AxisAlignedBB bounds = CABLE_SHAPES[index]; + if( bounds != null ) return bounds; + + bounds = SHAPE_CABLE_CORE; + for( EnumFacing facing : EnumFacing.VALUES ) + { + if( (index & (1 << facing.ordinal())) != 0 ) + { + bounds = bounds.union( SHAPE_CABLE_ARM.get( facing ) ); + } + } + + return CABLE_SHAPES[index] = bounds; + } + + public static AxisAlignedBB getCableBounds( IBlockState state ) + { + if( !state.getValue( CABLE ) ) return BlockCable.NULL_AABB; + return getCableBounds( getCableIndex( state ) ); + } + + public static void getCableBounds( IBlockState state, List bounds ) + { + if( !state.getValue( CABLE ) ) return; + + bounds.add( SHAPE_CABLE_CORE ); + for( EnumFacing facing : EnumFacing.VALUES ) + { + if( state.getValue( CONNECTIONS.get( facing ) ) ) bounds.add( SHAPE_CABLE_ARM.get( facing ) ); + } + } + + public static AxisAlignedBB getModemBounds( IBlockState state ) + { + EnumFacing facing = state.getValue( MODEM ).getFacing(); + return facing == null ? Block.NULL_AABB : ModemBounds.getBounds( facing ); + } + + public static AxisAlignedBB getBounds( IBlockState state ) + { + if( !state.getValue( CABLE ) ) return getModemBounds( state ); + + EnumFacing facing = state.getValue( MODEM ).getFacing(); + int cableIndex = getCableIndex( state ); + int index = cableIndex + ((facing == null ? 0 : facing.ordinal() + 1) << 6); + + AxisAlignedBB shape = SHAPES[index]; + if( shape != null ) return shape; + + shape = getCableBounds( cableIndex ); + if( facing != null ) shape = shape.union( ModemBounds.getBounds( facing ) ); + return SHAPES[index] = shape; + } + + public static void getBounds( IBlockState state, List bounds ) + { + EnumFacing facing = state.getValue( MODEM ).getFacing(); + if( facing != null ) bounds.add( getModemBounds( state ) ); + getCableBounds( state, bounds ); + } +} diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java index e5ac47150..540dfd619 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java @@ -24,8 +24,8 @@ 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.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.text.TextComponentTranslation; @@ -35,24 +35,10 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.Collections; -import java.util.List; import java.util.Map; public class TileCable extends TileModemBase { - public static final double MIN = 0.375; - public static final double MAX = 1 - MIN; - - private static final AxisAlignedBB BOX_CENTRE = new AxisAlignedBB( MIN, MIN, MIN, MAX, MAX, MAX ); - private static final AxisAlignedBB[] BOXES = new AxisAlignedBB[] { - new AxisAlignedBB( MIN, 0, MIN, MAX, MIN, MAX ), // Down - new AxisAlignedBB( MIN, MAX, MIN, MAX, 1, MAX ), // Up - new AxisAlignedBB( MIN, MIN, 0, MAX, MAX, MIN ), // North - new AxisAlignedBB( MIN, MIN, MAX, MAX, MAX, 1 ), // South - new AxisAlignedBB( 0, MIN, MIN, MIN, MAX, MAX ), // West - new AxisAlignedBB( MAX, MIN, MIN, 1, MAX, MAX ), // East - }; - private static class CableElement extends WiredModemElement { private final TileCable m_entity; @@ -187,15 +173,8 @@ private void updateDirection() public EnumFacing getDirection() { IBlockState state = getBlockState(); - BlockCableModemVariant modem = state.getValue( BlockCable.Properties.MODEM ); - if( modem != BlockCableModemVariant.None ) - { - return modem.getFacing(); - } - else - { - return EnumFacing.NORTH; - } + EnumFacing facing = state.getValue( BlockCable.Properties.MODEM ).getFacing(); + return facing != null ? facing : EnumFacing.NORTH; } @Override @@ -233,19 +212,6 @@ public void getDroppedItems( @Nonnull NonNullList drops, boolean crea } } - @Override - public ItemStack getPickedItem() - { - if( getPeripheralType() == PeripheralType.WiredModemWithCable ) - { - return PeripheralItemFactory.create( PeripheralType.WiredModem, getLabel(), 1 ); - } - else - { - return super.getPickedItem(); - } - } - @Override public void onNeighbourChange() { @@ -300,100 +266,8 @@ public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour ) } } - public AxisAlignedBB getModemBounds() - { - return super.getBounds(); - } - - private AxisAlignedBB getCableBounds() - { - double xMin = 0.375; - double yMin = 0.375; - double zMin = 0.375; - double xMax = 0.625; - double yMax = 0.625; - double zMax = 0.625; - BlockPos pos = getPos(); - World world = getWorld(); - - IBlockState state = getBlockState(); - if( BlockCable.doesConnectVisually( state, world, pos, EnumFacing.WEST ) ) - { - xMin = 0.0; - } - if( BlockCable.doesConnectVisually( state, world, pos, EnumFacing.EAST ) ) - { - xMax = 1.0; - } - if( BlockCable.doesConnectVisually( state, world, pos, EnumFacing.DOWN ) ) - { - yMin = 0.0; - } - if( BlockCable.doesConnectVisually( state, world, pos, EnumFacing.UP ) ) - { - yMax = 1.0; - } - if( BlockCable.doesConnectVisually( state, world, pos, EnumFacing.NORTH ) ) - { - zMin = 0.0; - } - if( BlockCable.doesConnectVisually( state, world, pos, EnumFacing.SOUTH ) ) - { - zMax = 1.0; - } - return new AxisAlignedBB( xMin, yMin, zMin, xMax, yMax, zMax ); - } - - @Nonnull @Override - public AxisAlignedBB getBounds() - { - PeripheralType type = getPeripheralType(); - switch( type ) - { - case WiredModem: - default: - { - return getModemBounds(); - } - case Cable: - { - return getCableBounds(); - } - case WiredModemWithCable: - { - AxisAlignedBB modem = getModemBounds(); - AxisAlignedBB cable = getCableBounds(); - return modem.union( cable ); - } - } - } - - @Override - public void getCollisionBounds( @Nonnull List bounds ) - { - PeripheralType type = getPeripheralType(); - if( type == PeripheralType.WiredModem || type == PeripheralType.WiredModemWithCable ) - { - bounds.add( getModemBounds() ); - } - if( type == PeripheralType.Cable || type == PeripheralType.WiredModemWithCable ) - { - bounds.add( BOX_CENTRE ); - - IBlockState state = getBlockState(); - for( EnumFacing facing : EnumFacing.VALUES ) - { - if( BlockCable.doesConnectVisually( state, world, pos, facing ) ) - { - bounds.add( BOXES[facing.ordinal()] ); - } - } - } - } - - @Override - public boolean onActivate( EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ ) + public boolean onActivate( EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ ) { if( getPeripheralType() == PeripheralType.WiredModemWithCable && !player.isSneaking() ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java index f8a0db3ab..f5c46e058 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java @@ -18,7 +18,7 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; -import net.minecraft.util.math.AxisAlignedBB; +import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.text.TextComponentString; @@ -170,15 +170,8 @@ public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour ) } } - @Nonnull @Override - public AxisAlignedBB getBounds() - { - return BlockCable.FULL_BLOCK_AABB; - } - - @Override - public boolean onActivate( EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ ) + public boolean onActivate( EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ ) { if( !getWorld().isRemote ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/BlockAdvancedModem.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/BlockAdvancedModem.java index d7f16a258..342985701 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/BlockAdvancedModem.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/BlockAdvancedModem.java @@ -10,6 +10,7 @@ import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.common.BlockPeripheralBase; import dan200.computercraft.shared.peripheral.common.TilePeripheralBase; +import dan200.computercraft.shared.peripheral.modem.ModemBounds; import net.minecraft.block.properties.PropertyBool; import net.minecraft.block.properties.PropertyDirection; import net.minecraft.block.state.BlockFaceShape; @@ -17,6 +18,7 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; @@ -74,7 +76,7 @@ public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess worl int anim; EnumFacing dir; TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TilePeripheralBase ) + if( tile instanceof TilePeripheralBase ) { TilePeripheralBase peripheral = (TilePeripheralBase) tile; anim = peripheral.getAnim(); @@ -137,4 +139,10 @@ public BlockFaceShape getBlockFaceShape( IBlockAccess world, IBlockState state, { return BlockFaceShape.UNDEFINED; } + + @Override + public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos ) + { + return ModemBounds.getBounds( state.getValue( Properties.FACING ) ); + } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java index c20e77c8e..5eaae344e 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java @@ -19,6 +19,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -107,7 +108,7 @@ public void onChunkUnload() } @Override - public boolean onActivate( EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ ) + public boolean onActivate( EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ ) { if( !player.isSneaking() && getFront() == side ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java b/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java index dfbf15f8f..327338c6c 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/printer/TilePrinter.java @@ -23,6 +23,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; 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.text.ITextComponent; @@ -74,7 +75,7 @@ public void destroy() } @Override - public boolean onActivate( EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ ) + public boolean onActivate( EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ ) { if( !player.isSneaking() ) { diff --git a/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java b/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java index 7c90d8d23..713b1c196 100644 --- a/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java +++ b/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java @@ -35,7 +35,9 @@ import dan200.computercraft.shared.media.recipes.PrintoutRecipe; import dan200.computercraft.shared.network.ComputerCraftPacket; import dan200.computercraft.shared.peripheral.commandblock.CommandBlockPeripheralProvider; -import dan200.computercraft.shared.peripheral.common.*; +import dan200.computercraft.shared.peripheral.common.BlockPeripheral; +import dan200.computercraft.shared.peripheral.common.DefaultPeripheralProvider; +import dan200.computercraft.shared.peripheral.common.ItemPeripheral; import dan200.computercraft.shared.peripheral.diskdrive.ContainerDiskDrive; import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive; import dan200.computercraft.shared.peripheral.modem.wired.*; diff --git a/src/main/java/dan200/computercraft/shared/turtle/blocks/BlockTurtle.java b/src/main/java/dan200/computercraft/shared/turtle/blocks/BlockTurtle.java index 5b16f586f..084a65b06 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/blocks/BlockTurtle.java +++ b/src/main/java/dan200/computercraft/shared/turtle/blocks/BlockTurtle.java @@ -10,19 +10,25 @@ import dan200.computercraft.shared.computer.blocks.BlockComputerBase; import dan200.computercraft.shared.computer.blocks.TileComputerBase; import dan200.computercraft.shared.computer.core.ComputerFamily; +import dan200.computercraft.shared.turtle.items.TurtleItemFactory; import dan200.computercraft.shared.util.DirectionUtil; import net.minecraft.block.material.Material; import net.minecraft.block.properties.PropertyDirection; import net.minecraft.block.state.BlockFaceShape; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.projectile.EntityFireball; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumBlockRenderType; 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.util.math.Vec3d; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; @@ -120,6 +126,18 @@ protected IBlockState getDefaultBlockState( ComputerFamily family, EnumFacing pl return getDefaultState(); } + @Override + @Deprecated + public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess world, BlockPos pos ) + { + TileEntity tile = world.getTileEntity( pos ); + Vec3d offset = tile instanceof TileTurtle ? ((TileTurtle) tile).getRenderOffset( 1.0f ) : Vec3d.ZERO; + return new AxisAlignedBB( + offset.x + 0.125, offset.y + 0.125, offset.z + 0.125, + offset.x + 0.875, offset.y + 0.875, offset.z + 0.875 + ); + } + private ComputerFamily getFamily() { if( this == ComputerCraft.Blocks.turtleAdvanced ) @@ -166,7 +184,7 @@ public void onBlockPlacedBy( World world, BlockPos pos, IBlockState state, Entit { // Not sure why this is necessary TileEntity tile = world.getTileEntity( pos ); - if( tile != null && tile instanceof TileTurtle ) + if( tile instanceof TileTurtle ) { tile.setWorld( world ); // Not sure why this is necessary tile.setPos( pos ); // Not sure why this is necessary @@ -180,4 +198,24 @@ public void onBlockPlacedBy( World world, BlockPos pos, IBlockState state, Entit EnumFacing dir = DirectionUtil.fromEntityRot( player ); setDirection( world, pos, dir.getOpposite() ); } + + @Override + @Deprecated + public float getExplosionResistance( Entity exploder ) + { + if( getFamily() == ComputerFamily.Advanced && (exploder instanceof EntityLivingBase || exploder instanceof EntityFireball) ) + { + return 2000; + } + + return super.getExplosionResistance( exploder ); + } + + @Override + @Nonnull + public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player ) + { + TileEntity tile = world.getTileEntity( pos ); + return tile instanceof TileTurtle ? TurtleItemFactory.create( (TileTurtle) tile ) : super.getPickBlock( state, target, world, pos, player ); + } } diff --git a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java index 09e72191b..65a58cf07 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java +++ b/src/main/java/dan200/computercraft/shared/turtle/blocks/TileTurtle.java @@ -22,10 +22,7 @@ import dan200.computercraft.shared.util.InventoryUtil; import dan200.computercraft.shared.util.RedstoneUtil; import dan200.computercraft.shared.util.WorldUtil; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.projectile.EntityFireball; import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; @@ -35,7 +32,6 @@ import net.minecraft.util.EnumHand; import net.minecraft.util.NonNullList; import net.minecraft.util.ResourceLocation; -import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.text.ITextComponent; @@ -183,19 +179,13 @@ public void getDroppedItems( @Nonnull NonNullList drops, boolean crea } @Override - public ItemStack getPickedItem() - { - return TurtleItemFactory.create( this ); - } - - @Override - public boolean onActivate( EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ ) + public boolean onActivate( EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ ) { // Request description from server requestTileEntityUpdate(); // Apply dye - ItemStack currentItem = player.getHeldItem( EnumHand.MAIN_HAND ); + ItemStack currentItem = player.getHeldItem( hand ); if( !currentItem.isEmpty() ) { if( currentItem.getItem() == Items.DYE ) @@ -225,7 +215,7 @@ else if( currentItem.getItem() == Items.WATER_BUCKET && m_brain.getColour() != - m_brain.setColour( -1 ); if( !player.capabilities.isCreativeMode ) { - player.setHeldItem( EnumHand.MAIN_HAND, new ItemStack( Items.BUCKET ) ); + player.setHeldItem( hand, new ItemStack( Items.BUCKET ) ); player.inventory.markDirty(); } } @@ -235,7 +225,7 @@ else if( currentItem.getItem() == Items.WATER_BUCKET && m_brain.getColour() != - } // Open GUI or whatever - return super.onActivate( player, side, hitX, hitY, hitZ ); + return super.onActivate( player, hand, side, hitX, hitY, hitZ ); } @Override @@ -250,36 +240,6 @@ public void openGUI( EntityPlayer player ) ComputerCraft.openTurtleGUI( player, this ); } - @Override - public boolean isSolidOnSide( int side ) - { - return false; - } - - @Override - public boolean isImmuneToExplosion( Entity exploder ) - { - if( getFamily() == ComputerFamily.Advanced ) - { - return true; - } - else - { - return exploder != null && (exploder instanceof EntityLivingBase || exploder instanceof EntityFireball); - } - } - - @Nonnull - @Override - public AxisAlignedBB getBounds() - { - Vec3d offset = getRenderOffset( 1.0f ); - return new AxisAlignedBB( - offset.x + 0.125, offset.y + 0.125, offset.z + 0.125, - offset.x + 0.875, offset.y + 0.875, offset.z + 0.875 - ); - } - @Override protected double getInteractRange( EntityPlayer player ) { diff --git a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java index d3819f1cf..72c642fc8 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java +++ b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleBrain.java @@ -19,7 +19,10 @@ import dan200.computercraft.shared.computer.core.IComputer; import dan200.computercraft.shared.computer.core.ServerComputer; import dan200.computercraft.shared.turtle.blocks.TileTurtle; -import dan200.computercraft.shared.util.*; +import dan200.computercraft.shared.util.Colour; +import dan200.computercraft.shared.util.ColourUtils; +import dan200.computercraft.shared.util.Holiday; +import dan200.computercraft.shared.util.HolidayUtil; import net.minecraft.block.Block; import net.minecraft.entity.Entity; import net.minecraft.entity.MoverType; @@ -999,7 +1002,7 @@ public Vec3d getRenderOffset( float f ) } default: { - return new Vec3d( 0.0, 0.0, 0.0 ); + return Vec3d.ZERO; } } } diff --git a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleMoveCommand.java b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleMoveCommand.java index 2af3a4617..79d962752 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/core/TurtleMoveCommand.java +++ b/src/main/java/dan200/computercraft/shared/turtle/core/TurtleMoveCommand.java @@ -14,6 +14,7 @@ import dan200.computercraft.api.turtle.event.TurtleBlockEvent; import dan200.computercraft.shared.util.WorldUtil; import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; import net.minecraft.entity.Entity; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.AxisAlignedBB; @@ -53,7 +54,8 @@ public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle ) } // Check existing block is air or replaceable - Block block = oldWorld.getBlockState( newPosition ).getBlock(); + IBlockState state = oldWorld.getBlockState( newPosition ); + Block block = state.getBlock(); if( block != null && !oldWorld.isAirBlock( newPosition ) && !WorldUtil.isLiquidBlock( oldWorld, newPosition ) && @@ -63,7 +65,7 @@ public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle ) } // Check there isn't anything in the way - AxisAlignedBB aabb = ((TurtleBrain) turtle).getOwner().getBounds(); + AxisAlignedBB aabb = state.getBoundingBox( oldWorld, oldPosition ); aabb = aabb.offset( newPosition.getX(), newPosition.getY(),