diff --git a/src/main/java/carpet/helpers/BlockRotator.java b/src/main/java/carpet/helpers/BlockRotator.java new file mode 100644 index 00000000..ce33f5b5 --- /dev/null +++ b/src/main/java/carpet/helpers/BlockRotator.java @@ -0,0 +1,255 @@ +package carpet.helpers; + +import carpet.CarpetSettings; +import net.minecraft.block.*; +import net.minecraft.block.enums.BlockHalf; +import net.minecraft.block.enums.ComparatorMode; +import net.minecraft.block.enums.SlabType; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.BlockItem; +import net.minecraft.item.ItemPlacementContext; +import net.minecraft.item.ItemStack; +import net.minecraft.util.BlockRotation; +import net.minecraft.util.Hand; +import net.minecraft.util.math.*; +import net.minecraft.world.World; + +public class BlockRotator +{ + public static boolean flipBlockWithCactus(World worldIn, BlockPos pos, BlockState state, PlayerEntity playerIn, Hand hand, Direction facing, float hitX, float hitY, float hitZ) + { + if (!playerIn.abilities.allowModifyWorld || !CarpetSettings.getBool("flippinCactus") || !player_holds_cactus_mainhand(playerIn)) + { + return false; + } + return flip_block(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ); + } + + public static BlockState alternativeBlockPlacement(Block block, ItemPlacementContext context)//World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) + { + //actual alternative block placement code + // + Direction facing; + Vec3d vec3d = context.getPos(); + float hitX = (float) vec3d.x; + PlayerEntity placer = context.getPlayer(); + BlockPos pos = context.getBlockPos(); + World world = context.getWorld(); + + if (block instanceof GlazedTerracottaBlock) + { + facing = Direction.byId((int)hitX - 2); + if(facing == Direction.UP || facing == Direction.DOWN) + { + facing = placer.getHorizontalFacing().getOpposite(); + } + return block.getDefaultState().with(HorizontalFacingBlock.FACING, facing); + } + else if (block instanceof ObserverBlock) + { + return block.getDefaultState() + .with(FacingBlock.FACING, Direction.byId((int)hitX - 2)) + .with(ObserverBlock.POWERED, true); + } + else if (block instanceof RepeaterBlock) + { + facing = Direction.byId((((int)hitX) % 10) - 2); + if(facing == Direction.UP || facing == Direction.DOWN) + { + facing = placer.getHorizontalFacing().getOpposite(); + } + return block.getDefaultState() + .with(HorizontalFacingBlock.FACING, facing) + .with(RepeaterBlock.DELAY, MathHelper.clamp((((int) hitX) / 10) + 1, 1, 4)) + .with(RepeaterBlock.LOCKED, Boolean.FALSE); + } + else if (block instanceof TrapdoorBlock) + { + return block.getDefaultState() + .with(TrapdoorBlock.FACING, Direction.byId((((int)hitX) % 10) - 2)) + .with(TrapdoorBlock.OPEN, Boolean.FALSE) + .with(TrapdoorBlock.HALF, (hitX > 10) ? BlockHalf.TOP : BlockHalf.BOTTOM) + .with(TrapdoorBlock.OPEN, world.isReceivingRedstonePower(pos)); + } + else if (block instanceof ComparatorBlock) + { + facing = Direction.byId((((int)hitX) % 10) - 2); + if((facing == Direction.UP) || (facing == Direction.DOWN)) + { + facing = placer.getHorizontalFacing().getOpposite(); + } + ComparatorMode m = (hitX > 10)?ComparatorMode.SUBTRACT: ComparatorMode.COMPARE; + return block.getDefaultState() + .with(HorizontalFacingBlock.FACING, facing) + .with(ComparatorBlock.POWERED, Boolean.FALSE) + .with(ComparatorBlock.MODE, m); + } + else if (block instanceof DispenserBlock) + { + return block.getDefaultState() + .with(DispenserBlock.FACING, Direction.byId((int)hitX - 2)) + .with(DispenserBlock.TRIGGERED, Boolean.FALSE); + } + else if (block instanceof PistonBlock) + { + return block.getDefaultState() + .with(FacingBlock.FACING, Direction.byId((int)hitX - 2) ) + .with(PistonBlock.EXTENDED, Boolean.FALSE); + } + else if (block instanceof StairsBlock) + { + return block.getPlacementState(context)//worldIn, pos, facing, hitX, hitY, hitZ, meta, placer) + .with(StairsBlock.FACING, Direction.byId((((int)hitX) % 10) - 2)) + .with(StairsBlock.HALF, ( hitX > 10)?BlockHalf.TOP : BlockHalf.BOTTOM); + } + return null; + } + + public static ItemStack dispenserRotate(BlockPointer source, ItemStack stack) + { + Direction sourceFace = source.getBlockState().get(DispenserBlock.FACING); + World world = source.getWorld(); + BlockPos blockpos = source.getBlockPos().offset(sourceFace); + BlockState iblockstate = world.getBlockState(blockpos); + Block block = iblockstate.getBlock(); + + // Block rotation for blocks that can be placed in all 6 rotations. + if(block instanceof FacingBlock || block instanceof DispenserBlock) + { + Direction face = iblockstate.get(FacingBlock.FACING); + face = face.rotateClockwise(sourceFace.getAxis()); + if(sourceFace.getId() % 2 == 0) + { // Rotate twice more to make blocks always rotate clockwise relative to the dispenser + // when index is equal to zero. when index is equal to zero the dispenser is in the opposite direction. + face = face.rotateClockwise(sourceFace.getAxis()); + face = face.rotateClockwise(sourceFace.getAxis()); + } + world.setBlockState(blockpos, iblockstate.with(FacingBlock.FACING, face), 3); + + } + else if(block instanceof HorizontalFacingBlock) // Block rotation for blocks that can be placed in only 4 horizontal rotations. + { + Direction face = iblockstate.get(HorizontalFacingBlock.FACING); + face = face.rotateClockwise(sourceFace.getAxis()); + if(sourceFace.getId() % 2 == 0) + { // same as above. + face = face.rotateClockwise(sourceFace.getAxis()); + face = face.rotateClockwise(sourceFace.getAxis()); + } + if(sourceFace.getId() <= 1) + { // Make sure to suppress rotation when index is lower then 2 as that will result in a faulty rotation for + // blocks that only can be placed horizontaly. + world.setBlockState(blockpos, iblockstate.with(FacingBlock.FACING, face), 3); + } + } + // Send block update to the block that just have been rotated. + world.updateNeighbor(blockpos, block, source.getBlockPos()); + + return stack; + } + + + + + public static boolean flip_block(World worldIn, BlockPos pos, BlockState state, PlayerEntity playerIn, Hand hand, Direction facing, float hitX, float hitY, float hitZ) + { + Block block = state.getBlock(); + if ( (block instanceof GlazedTerracottaBlock) || (block instanceof AbstractRedstoneGateBlock) || (block instanceof RailBlock) || + (block instanceof TrapdoorBlock) || (block instanceof LeverBlock) || (block instanceof FenceGateBlock)) + { + worldIn.setBlockState(pos, block.rotate(state, BlockRotation.CLOCKWISE_90), 2 | 1024); + } + else if ((block instanceof ObserverBlock) || (block instanceof EndRodBlock)) + { + worldIn.setBlockState(pos, state.with(FacingBlock.FACING, (Direction) state.get(FacingBlock.FACING).getOpposite()), 2 | 1024); + } + else if (block instanceof DispenserBlock) + { + worldIn.setBlockState(pos, state.with(DispenserBlock.FACING, state.get(DispenserBlock.FACING).getOpposite()), 2 | 1024); + } + else if (block instanceof PistonBlock) + { + if (!(state.get(PistonBlock.EXTENDED))) + worldIn.setBlockState(pos, state.with(FacingBlock.FACING, state.get(FacingBlock.FACING).getOpposite()), 2 | 1024); + } + else if (block instanceof SlabBlock) + { + if (((SlabBlock) block).hasSidedTransparency(state)) + { + worldIn.setBlockState(pos, state.with(SlabBlock.TYPE, state.get(SlabBlock.TYPE) == SlabType.TOP ? SlabType.BOTTOM : SlabType.TOP), 2 | 1024); + } + } + else if (block instanceof HopperBlock) + { + if ((Direction)state.get(HopperBlock.FACING) != Direction.DOWN) + { + worldIn.setBlockState(pos, state.with(HopperBlock.FACING, state.get(HopperBlock.FACING).rotateYClockwise()), 2 | 1024); + } + } + else if (block instanceof StairsBlock) + { + //LOG.error(String.format("hit with facing: %s, at side %.1fX, X %.1fY, Y %.1fZ",facing, hitX, hitY, hitZ)); + if ((facing == Direction.UP && hitY == 1.0f) || (facing == Direction.DOWN && hitY == 0.0f)) + { + worldIn.setBlockState(pos, state.with(StairsBlock.HALF, state.get(StairsBlock.HALF) == BlockHalf.TOP ? BlockHalf.BOTTOM : BlockHalf.TOP ), 2 | 1024); + } + else + { + boolean turn_right; + if (facing == Direction.NORTH) + { + turn_right = (hitX <= 0.5); + } + else if (facing == Direction.SOUTH) + { + turn_right = !(hitX <= 0.5); + } + else if (facing == Direction.EAST) + { + turn_right = (hitZ <= 0.5); + } + else if (facing == Direction.WEST) + { + turn_right = !(hitZ <= 0.5); + } + else + { + return false; + } + if (turn_right) + { + worldIn.setBlockState(pos, block.rotate(state, BlockRotation.COUNTERCLOCKWISE_90), 2 | 1024); + } + else + { + worldIn.setBlockState(pos, block.rotate(state, BlockRotation.CLOCKWISE_90), 2 | 1024); + } + } + } + else + { + return false; + } + worldIn.scheduleBlockRender(pos); + return true; + } + private static boolean player_holds_cactus_mainhand(PlayerEntity playerIn) + { + return (!playerIn.getMainHandStack().isEmpty() + && playerIn.getMainHandStack().getItem() instanceof BlockItem && + ((BlockItem) (playerIn.getMainHandStack().getItem())).getBlock() == Blocks.CACTUS); + } + public static boolean flippinEligibility(Entity entity) + { + if (CarpetSettings.getBool("flippinCactus") + && (entity instanceof PlayerEntity)) + { + PlayerEntity player = (PlayerEntity)entity; + return (!player.getMainHandStack().isEmpty() + && player.getMainHandStack().getItem() instanceof BlockItem && + ((BlockItem) (player.getMainHandStack().getItem())).getBlock() == Blocks.CACTUS); + } + return false; + } +} diff --git a/src/main/java/carpet/helpers/BlockRotator.javax b/src/main/java/carpet/helpers/BlockRotator.javax deleted file mode 100644 index edf7bb14..00000000 --- a/src/main/java/carpet/helpers/BlockRotator.javax +++ /dev/null @@ -1,262 +0,0 @@ -package carpet.helpers; - -import carpet.CarpetSettings; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; -import net.minecraft.block.TrapdoorBlock; -import net.minecraft.dispenser.IBlockSource; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.init.Blocks; -import net.minecraft.item.BlockItemUseContext; -import net.minecraft.item.ItemBlock; -import net.minecraft.item.ItemStack; -import net.minecraft.state.properties.ComparatorMode; -import net.minecraft.state.properties.Half; -import net.minecraft.state.properties.SlabType; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.EnumHand; -import net.minecraft.util.Rotation; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.MathHelper; -import net.minecraft.world.World; - -public class BlockRotator -{ - public static boolean flipBlockWithCactus(World worldIn, BlockPos pos, BlockState state, PlayerEntity playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) - { - if (!playerIn.abilities.allowModifyWorld || !CarpetSettings.getBool("flippinCactus") || !player_holds_cactus_mainhand(playerIn)) - { - return false; - } - return flip_block(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ); - } - - public static BlockState alternativeBlockPlacement(Block block, BlockItemUseContext context)//World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) - { - //actual alternative block placement code - // - EnumFacing facing; - float hitX = context.getHitX(); - PlayerEntity placer = context.getPlayer(); - BlockPos pos = context.getPos(); - World world = context.getWorld(); - - if (block instanceof BlockGlazedTerracotta) - { - facing = EnumFacing.byIndex((int)hitX - 2); - if(facing == EnumFacing.UP || facing == EnumFacing.DOWN) - { - facing = placer.getHorizontalFacing().getOpposite(); - } - return block.getDefaultState().with(BlockHorizontal.HORIZONTAL_FACING, facing); - } - else if (block instanceof BlockObserver) - { - return block.getDefaultState() - .with(BlockDirectional.FACING, EnumFacing.byIndex((int)hitX - 2)) - .with(BlockObserver.POWERED, true); - } - else if (block instanceof BlockRedstoneRepeater) - { - facing = EnumFacing.byIndex((((int)hitX) % 10) - 2); - if(facing == EnumFacing.UP || facing == EnumFacing.DOWN) - { - facing = placer.getHorizontalFacing().getOpposite(); - } - return block.getDefaultState() - .with(BlockHorizontal.HORIZONTAL_FACING, facing) - .with(BlockRedstoneRepeater.DELAY, MathHelper.clamp((((int) hitX) / 10) + 1, 1, 4)) - .with(BlockRedstoneRepeater.LOCKED, Boolean.FALSE); - } - else if (block instanceof BlockTrapDoor) - { - return block.getDefaultState() - .with(TrapdoorBlock.HORIZONTAL_FACING, EnumFacing.byIndex((((int)hitX) % 10) - 2)) - .with(TrapdoorBlock.OPEN, Boolean.FALSE) - .with(TrapdoorBlock.HALF, (hitX > 10) ? Half.TOP : Half.BOTTOM) - .with(TrapdoorBlock.OPEN, world.isBlockPowered(pos)); - } - else if (block instanceof BlockRedstoneComparator) - { - facing = EnumFacing.byIndex((((int)hitX) % 10) - 2); - if((facing == EnumFacing.UP) || (facing == EnumFacing.DOWN)) - { - facing = placer.getHorizontalFacing().getOpposite(); - } - ComparatorMode m = (hitX > 10)?ComparatorMode.SUBTRACT: ComparatorMode.COMPARE; - return block.getDefaultState() - .with(BlockHorizontal.HORIZONTAL_FACING, facing) - .with(BlockRedstoneComparator.POWERED, Boolean.FALSE) - .with(BlockRedstoneComparator.MODE, m); - } - else if (block instanceof BlockDispenser) - { - return block.getDefaultState() - .with(BlockDispenser.FACING, EnumFacing.byIndex((int)hitX - 2)) - .with(BlockDispenser.TRIGGERED, Boolean.FALSE); - } - else if (block instanceof BlockPistonBase) - { - return block.getDefaultState() - .with(BlockDirectional.FACING,EnumFacing.byIndex((int)hitX - 2) ) - .with(BlockPistonBase.EXTENDED, Boolean.FALSE); - } - else if (block instanceof BlockStairs) - { - return block.getStateForPlacement(context)//worldIn, pos, facing, hitX, hitY, hitZ, meta, placer) - .with(BlockStairs.FACING, EnumFacing.byIndex((((int)hitX) % 10) - 2)) - .with(BlockStairs.HALF, ( hitX > 10)?Half.TOP : Half.BOTTOM); - } - return null; - } - - public static ItemStack dispenserRotate(IBlockSource source, ItemStack stack) - { - EnumFacing sourceFace = source.getBlockState().get(BlockDispenser.FACING); - World world = source.getWorld(); - BlockPos blockpos = source.getBlockPos().offset(sourceFace); - BlockState iblockstate = world.getBlockState(blockpos); - Block block = iblockstate.getBlock(); - - // Block rotation for blocks that can be placed in all 6 rotations. - if(block instanceof BlockDirectional || block instanceof BlockDispenser) - { - EnumFacing face = iblockstate.get(BlockDirectional.FACING); - face = face.rotateAround(sourceFace.getAxis()); - if(sourceFace.getIndex() % 2 == 0) - { // Rotate twice more to make blocks always rotate clockwise relative to the dispenser - // when index is equal to zero. when index is equal to zero the dispenser is in the opposite direction. - face = face.rotateAround(sourceFace.getAxis()); - face = face.rotateAround(sourceFace.getAxis()); - } - world.setBlockState(blockpos, iblockstate.with(BlockDirectional.FACING, face), 3); - - } - else if(block instanceof BlockHorizontal) // Block rotation for blocks that can be placed in only 4 horizontal rotations. - { - EnumFacing face = iblockstate.get(BlockHorizontal.HORIZONTAL_FACING); - face = face.rotateAround(sourceFace.getAxis()); - if(sourceFace.getIndex() % 2 == 0) - { // same as above. - face = face.rotateAround(sourceFace.getAxis()); - face = face.rotateAround(sourceFace.getAxis()); - } - if(sourceFace.getIndex() <= 1) - { // Make sure to suppress rotation when index is lower then 2 as that will result in a faulty rotation for - // blocks that only can be placed horizontaly. - world.setBlockState(blockpos, iblockstate.with(BlockHorizontal.HORIZONTAL_FACING, face), 3); - } - } - // Send block update to the block that just have been rotated. - world.neighborChanged(blockpos, block, source.getBlockPos()); - - return stack; - } - - - - - public static boolean flip_block(World worldIn, BlockPos pos, BlockState state, PlayerEntity playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) - { - Block block = state.getBlock(); - if ( (block instanceof BlockGlazedTerracotta) || (block instanceof BlockRedstoneDiode) || (block instanceof BlockRailBase) || - (block instanceof BlockTrapDoor) || (block instanceof BlockLever) || (block instanceof BlockFenceGate)) - { - worldIn.setBlockState(pos, block.rotate(state, Rotation.CLOCKWISE_90), 2 | 1024); - } - else if ((block instanceof BlockObserver) || (block instanceof BlockEndRod)) - { - worldIn.setBlockState(pos, state.with(BlockDirectional.FACING, (EnumFacing)state.get(BlockDirectional.FACING).getOpposite()), 2 | 1024); - } - else if (block instanceof BlockDispenser) - { - worldIn.setBlockState(pos, state.with(BlockDispenser.FACING, state.get(BlockDispenser.FACING).getOpposite()), 2 | 1024); - } - else if (block instanceof BlockPistonBase) - { - if (!(state.get(BlockPistonBase.EXTENDED))) - worldIn.setBlockState(pos, state.with(BlockDirectional.FACING, state.get(BlockDirectional.FACING).getOpposite()), 2 | 1024); - } - else if (block instanceof BlockSlab) - { - if (!((BlockSlab) block).isFullCube(state)) - { - worldIn.setBlockState(pos, state.with(BlockSlab.TYPE, state.get(BlockSlab.TYPE) == SlabType.TOP ? SlabType.BOTTOM : SlabType.TOP), 2 | 1024); - } - } - else if (block instanceof BlockHopper) - { - if ((EnumFacing)state.get(BlockHopper.FACING) != EnumFacing.DOWN) - { - worldIn.setBlockState(pos, state.with(BlockHopper.FACING, state.get(BlockHopper.FACING).rotateY()), 2 | 1024); - } - } - else if (block instanceof BlockStairs) - { - //LOG.error(String.format("hit with facing: %s, at side %.1fX, X %.1fY, Y %.1fZ",facing, hitX, hitY, hitZ)); - if ((facing == EnumFacing.UP && hitY == 1.0f) || (facing == EnumFacing.DOWN && hitY == 0.0f)) - { - worldIn.setBlockState(pos, state.with(BlockStairs.HALF, state.get(BlockStairs.HALF) == Half.TOP ? Half.BOTTOM : Half.TOP ), 2 | 1024); - } - else - { - boolean turn_right; - if (facing == EnumFacing.NORTH) - { - turn_right = (hitX <= 0.5); - } - else if (facing == EnumFacing.SOUTH) - { - turn_right = !(hitX <= 0.5); - } - else if (facing == EnumFacing.EAST) - { - turn_right = (hitZ <= 0.5); - } - else if (facing == EnumFacing.WEST) - { - turn_right = !(hitZ <= 0.5); - } - else - { - return false; - } - if (turn_right) - { - worldIn.setBlockState(pos, block.rotate(state, Rotation.COUNTERCLOCKWISE_90), 2 | 1024); - } - else - { - worldIn.setBlockState(pos, block.rotate(state, Rotation.CLOCKWISE_90), 2 | 1024); - } - } - } - else - { - return false; - } - worldIn.markBlockRangeForRenderUpdate(pos, pos); - return true; - } - private static boolean player_holds_cactus_mainhand(PlayerEntity playerIn) - { - return (!playerIn.getHeldItemMainhand().isEmpty() - && playerIn.getHeldItemMainhand().getItem() instanceof ItemBlock && - ((ItemBlock) (playerIn.getHeldItemMainhand().getItem())).getBlock() == Blocks.CACTUS); - } - public static boolean flippinEligibility(Entity entity) - { - if (CarpetSettings.getBool("flippinCactus") - && (entity instanceof PlayerEntity)) - { - PlayerEntity player = (PlayerEntity)entity; - return (!player.getHeldItemOffhand().isEmpty() - && player.getHeldItemOffhand().getItem() instanceof ItemBlock && - ((ItemBlock) (player.getHeldItemOffhand().getItem())).getBlock() == Blocks.CACTUS); - } - return false; - } -}