Jump to content

Recommended Posts

Posted

 

Hi. I am creating a hammer that will dig across the area. I was able to set up digging vertically, but I can't implement digging up or down. I know everyone uses RayTraceResult() but I don't understand how to use it. Here is what I have ready at the moment:

public class Destroyer extends Item {

	public Destroyer(Properties properties) {
		super(properties);
	}

	@Override
	public boolean mineBlock(ItemStack stack, World world, BlockState state, BlockPos pos, LivingEntity entity) {
		Direction direction = entity.getDirection(); 
		int xPos = pos.getX(), yPos = pos.getY(), zPos = pos.getZ();
		
		
		if((direction == Direction.NORTH || direction == Direction.SOUTH) && state.getHarvestTool() == ToolType.PICKAXE && state.getHarvestLevel() <= 2) {
			for(int i = xPos - 1; i < xPos + 2; i++) {
				for(int j = yPos - 1; j < yPos + 2; j++) {
					BlockPos pos2 = new BlockPos(i, j, zPos);
					BlockState newState = world.getBlockState(pos2);
					if(newState.getHarvestTool() == ToolType.PICKAXE && newState.getHarvestLevel() <= 2) {
						world.destroyBlock(pos2, true);
					}
				}
			}
			
			if (!world.isClientSide && state.getDestroySpeed(world, pos) != 0.0F) {
		         stack.hurtAndBreak(1, entity, (p_220038_0_) -> {
		            p_220038_0_.broadcastBreakEvent(EquipmentSlotType.MAINHAND);
		         });
		      }
		}
		
		if((direction == Direction.EAST || direction == Direction.WEST) && state.getHarvestTool() == ToolType.PICKAXE && state.getHarvestLevel() <= 2) {
			for(int i = zPos - 1; i < zPos + 2; i++) {
				for(int j = yPos - 1; j < yPos + 2; j++) {
					BlockPos pos2 = new BlockPos(xPos, j, i);
					BlockState newState = world.getBlockState(pos2);
					if(newState.getHarvestTool() == ToolType.PICKAXE && newState.getHarvestLevel() <= 2) {
						world.destroyBlock(pos2, true);
					}
				}
			}
			
			if (!world.isClientSide && state.getDestroySpeed(world, pos) != 0.0F) {
		         stack.hurtAndBreak(1, entity, (p_220038_0_) -> {
		            p_220038_0_.broadcastBreakEvent(EquipmentSlotType.MAINHAND);
		         });
		      }
		}
		
		if(direction == Direction.UP || direction == Direction.DOWN && state.getHarvestTool() == ToolType.PICKAXE && state.getHarvestLevel() <= 2) {
			for(int i = xPos - 1; i < xPos + 2; i++) {
				for(int j = zPos -1; j < zPos + 2; j++) {
					BlockPos pos2 = new BlockPos(xPos, j, i);
					BlockState newState = world.getBlockState(pos2);
					if(newState.getHarvestTool() == ToolType.PICKAXE && newState.getHarvestLevel() <= 2) {
						world.destroyBlock(pos2, true);
					}
				}
			}
			
			if (!world.isClientSide && state.getDestroySpeed(world, pos) != 0.0F) {
		         stack.hurtAndBreak(1, entity, (p_220038_0_) -> {
		            p_220038_0_.broadcastBreakEvent(EquipmentSlotType.MAINHAND);
		         });
		      }
		}

		return true;
	}
	
	@Override
	public float getDestroySpeed(ItemStack stack, BlockState state) {
		return state.getHarvestTool() == ToolType.PICKAXE && state.getHarvestLevel() <= 2 ? 12f : 1f;
	}
	
	@Override
	public boolean isDamageable(ItemStack stack) {
		return true;
	}
	
	@Override
	public boolean isCorrectToolForDrops(BlockState state) {
		return state.getHarvestTool() == ToolType.PICKAXE && state.getHarvestLevel() <= 2;
	}
}

 

At least I understood the problem. As it turns out, the player does not have an UP or DOWN direction, but what should I do then?

Posted (edited)

Maybe you can try getting the blocks by getting the relative position of blocks from other blocks, based on the direction the player is looking.

public boolean mineBlock(...) {
    // get the blocks you want to break
    BlockPos middle = blockPos;
    BlockPos topMiddle = middle.above();
    BlockPos topLeft = topMiddle.relative(playerEntity.getDirection().getCounterClockWise());
    BlockPos topRight = topMiddle.relative(playerEntity.getDirection().getClockWise());
    BlockPos bottomMiddle = middle.below();
    // add the rest of the blocks here
}

// maybe call your own function to break those blocks
if (this.breakAndDrop(world, playerEntity, stack, topMiddle)) {
	// you could do something or count something here idk
}

// which could look something like this
private void breakAndDrop(World world, PlayerEntity player, ItemStack itemStack, BlockPos pos) {
    Block.dropResources(world.getBlockState(pos), world, pos, null, player, itemStack);
    world.destroyBlock(pos, false, player);
}

 

Edited by Toasterkid
gramma
Posted

The version you are using is no longer supported on this forum.

Please update to a modern version of Minecraft to receive support.

Currently supported versions are 1.19.2 (Latest) and 1.18.2 (LTS).

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.