From 61daab910e3babcd1d7e01d38bad6d073829775a Mon Sep 17 00:00:00 2001 From: SquidDev Date: Mon, 14 Jan 2019 10:27:19 +0000 Subject: [PATCH] Simplify placement logic of various blocks - For those where placement is stored in the metadata (computers), don't also set it in onBlockPlacedBy. - Remove .getDefaultState(int, EnumFacing) override, as this means we have more control over what is passed to us (namely, placer's direction too). --- .../shared/common/BlockGeneric.java | 11 ------ .../computer/blocks/BlockCommandComputer.java | 25 +----------- .../shared/computer/blocks/BlockComputer.java | 39 ++----------------- .../computer/blocks/BlockComputerBase.java | 13 ++++--- .../peripheral/common/BlockPeripheral.java | 7 ---- .../common/BlockPeripheralBase.java | 9 +++-- .../shared/turtle/blocks/BlockTurtle.java | 10 +---- 7 files changed, 19 insertions(+), 95 deletions(-) diff --git a/src/main/java/dan200/computercraft/shared/common/BlockGeneric.java b/src/main/java/dan200/computercraft/shared/common/BlockGeneric.java index 2803db856..ed92c8074 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.Block; import net.minecraft.block.ITileEntityProvider; 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; @@ -31,8 +30,6 @@ public abstract class BlockGeneric extends Block implements ITileEntityProvider this.hasTileEntity = true; } - protected abstract IBlockState getDefaultBlockState( int damage, EnumFacing placedSide ); - protected abstract TileGeneric createTile( IBlockState state ); protected abstract TileGeneric createTile( int damage ); @@ -53,14 +50,6 @@ public abstract class BlockGeneric extends Block implements ITileEntityProvider } } - @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( damage, side ); - } - @Override public boolean removedByPlayer( @Nonnull IBlockState state, World world, @Nonnull BlockPos pos, @Nonnull EntityPlayer player, boolean willHarvest ) { diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/BlockCommandComputer.java b/src/main/java/dan200/computercraft/shared/computer/blocks/BlockCommandComputer.java index a6ed79a89..164647e55 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/BlockCommandComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/BlockCommandComputer.java @@ -87,14 +87,7 @@ public class BlockCommandComputer extends BlockComputerBase @Override protected IBlockState getDefaultBlockState( ComputerFamily family, EnumFacing placedSide ) { - if( placedSide.getAxis() != EnumFacing.Axis.Y ) - { - return getDefaultState().withProperty( Properties.FACING, placedSide ); - } - else - { - return getDefaultState(); - } + return getDefaultState().withProperty( Properties.FACING, placedSide ); } @Override @@ -114,20 +107,4 @@ public class BlockCommandComputer extends BlockComputerBase { return new TileCommandComputer(); } - - @Override - public void onBlockPlacedBy( World world, BlockPos pos, IBlockState state, EntityLivingBase player, @Nonnull ItemStack itemstack ) - { - // Not sure why this is necessary - TileEntity tile = world.getTileEntity( pos ); - if( tile instanceof TileCommandComputer ) - { - tile.setWorld( world ); // Not sure why this is necessary - tile.setPos( pos ); // Not sure why this is necessary - } - - // Set direction - EnumFacing dir = DirectionUtil.fromEntityRot( player ); - setDirection( world, pos, dir ); - } } 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 a031b1ba0..c2ba4db74 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputer.java @@ -9,14 +9,12 @@ package dan200.computercraft.shared.computer.blocks; import dan200.computercraft.ComputerCraft; import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.items.ComputerItemFactory; -import dan200.computercraft.shared.util.DirectionUtil; import net.minecraft.block.material.Material; import net.minecraft.block.properties.PropertyBool; import net.minecraft.block.properties.PropertyDirection; import net.minecraft.block.properties.PropertyEnum; 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; @@ -97,24 +95,9 @@ public class BlockComputer extends BlockComputerBase @Override protected IBlockState getDefaultBlockState( ComputerFamily family, EnumFacing placedSide ) { - IBlockState state = getDefaultState(); - if( placedSide.getAxis() != EnumFacing.Axis.Y ) - { - state = state.withProperty( Properties.FACING, placedSide ); - } - - switch( family ) - { - case Normal: - default: - { - return state.withProperty( Properties.ADVANCED, false ); - } - case Advanced: - { - return state.withProperty( Properties.ADVANCED, true ); - } - } + return getDefaultState() + .withProperty( Properties.FACING, placedSide ) + .withProperty( Properties.ADVANCED, family == ComputerFamily.Advanced ); } @Nonnull @@ -151,22 +134,6 @@ public class BlockComputer extends BlockComputerBase return new TileComputer(); } - @Override - public void onBlockPlacedBy( World world, BlockPos pos, IBlockState state, EntityLivingBase player, @Nonnull ItemStack stack ) - { - // Not sure why this is necessary - TileEntity tile = world.getTileEntity( pos ); - if( tile instanceof TileComputer ) - { - tile.setWorld( world ); // Not sure why this is necessary - tile.setPos( pos ); // Not sure why this is necessary - } - - // Set direction - 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 ) diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputerBase.java b/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputerBase.java index 2185f5089..0340e19ad 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputerBase.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputerBase.java @@ -8,16 +8,18 @@ package dan200.computercraft.shared.computer.blocks; import dan200.computercraft.shared.common.BlockDirectional; import dan200.computercraft.shared.computer.core.ComputerFamily; -import dan200.computercraft.shared.computer.items.ItemComputerBase; +import dan200.computercraft.shared.util.DirectionUtil; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; -import net.minecraft.item.Item; +import net.minecraft.entity.EntityLivingBase; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; +import javax.annotation.Nonnull; + public abstract class BlockComputerBase extends BlockDirectional { public BlockComputerBase( Material material ) @@ -47,11 +49,12 @@ public abstract class BlockComputerBase extends BlockDirectional protected abstract TileComputerBase createTile( ComputerFamily family ); + @Nonnull @Override - protected final IBlockState getDefaultBlockState( int damage, EnumFacing placedSide ) + @Deprecated + public final IBlockState getStateForPlacement( World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, int damage, EntityLivingBase placer ) { - ItemComputerBase item = (ItemComputerBase) Item.getItemFromBlock( this ); - return getDefaultBlockState( item.getFamily( damage ), placedSide ); + return getDefaultBlockState( getFamily( damage ), DirectionUtil.fromEntityRot( placer ) ); } @Override 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 11a87d608..40b3b6016 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java @@ -560,14 +560,7 @@ public class BlockPeripheral extends BlockPeripheralBase @Override public void onBlockPlacedBy( World world, BlockPos pos, IBlockState state, EntityLivingBase player, @Nonnull ItemStack stack ) { - // Not sure why this is necessary TileEntity tile = world.getTileEntity( pos ); - if( tile instanceof TilePeripheralBase ) - { - tile.setWorld( world ); // Not sure why this is necessary - tile.setPos( pos ); // Not sure why this is necessary - } - switch( getPeripheralType( state ) ) { case Speaker: 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 658817656..a46bbecab 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java @@ -11,8 +11,8 @@ 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.Item; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; @@ -44,11 +44,12 @@ public abstract class BlockPeripheralBase extends BlockDirectional return true; // ItemPeripheralBase handles this } + @Nonnull @Override - protected final IBlockState getDefaultBlockState( int damage, EnumFacing placedSide ) + @Deprecated + public final IBlockState getStateForPlacement( World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, int damage, EntityLivingBase placer ) { - ItemPeripheralBase item = (ItemPeripheralBase) Item.getItemFromBlock( this ); - return getDefaultBlockState( item.getPeripheralType( damage ), placedSide ); + return getDefaultBlockState( getPeripheralType( damage ), side ); } @Override 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 e9bb6f189..586a64137 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/blocks/BlockTurtle.java +++ b/src/main/java/dan200/computercraft/shared/turtle/blocks/BlockTurtle.java @@ -177,16 +177,10 @@ public class BlockTurtle extends BlockComputerBase @Override public void onBlockPlacedBy( World world, BlockPos pos, IBlockState state, EntityLivingBase player, @Nonnull ItemStack itemstack ) { - // Not sure why this is necessary TileEntity tile = world.getTileEntity( pos ); - if( tile instanceof TileTurtle ) + if( tile instanceof TileTurtle && player instanceof EntityPlayer ) { - tile.setWorld( world ); // Not sure why this is necessary - tile.setPos( pos ); // Not sure why this is necessary - if( player instanceof EntityPlayer ) - { - ((TileTurtle) tile).setOwningPlayer( ((EntityPlayer) player).getGameProfile() ); - } + ((TileTurtle) tile).setOwningPlayer( ((EntityPlayer) player).getGameProfile() ); } // Set direction