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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I tried do download the essential mod to my mod pack but i didnt work. I paly on 1.21 and it should work. I use neoforge for my modding. The weird things is my friend somehow added the mod to his modpack and many others that I somehow can´t. Is there anything i can do? 
    • Thanks, I've now installed a slightly newer version and the server is at least starting up now.
    • i have the same issue. Found 1 Create mod class dependency(ies) in createdeco-1.3.3-1.19.2.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Found 11 Create mod class dependency(ies) in createaddition-fabric+1.19.2-20230723a.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Detailed walkthrough of mods which rely on missing Create mod classes: Mod: createaddition-fabric+1.19.2-20230723a.jar Missing classes of create: com/simibubi/create/compat/jei/category/sequencedAssembly/JeiSequencedAssemblySubCategory com/simibubi/create/compat/recipeViewerCommon/SequencedAssemblySubCategoryType com/simibubi/create/compat/rei/CreateREI com/simibubi/create/compat/rei/EmptyBackground com/simibubi/create/compat/rei/ItemIcon com/simibubi/create/compat/rei/category/CreateRecipeCategory com/simibubi/create/compat/rei/category/WidgetUtil com/simibubi/create/compat/rei/category/animations/AnimatedBlazeBurner com/simibubi/create/compat/rei/category/animations/AnimatedKinetics com/simibubi/create/compat/rei/category/sequencedAssembly/ReiSequencedAssemblySubCategory com/simibubi/create/compat/rei/display/CreateDisplay Mod: createdeco-1.3.3-1.19.2.jar Missing classes of create: com/simibubi/create/content/kinetics/fan/SplashingRecipe
    • The crash points to moonlight lib - try other builds or make a test without this mod and the mods requiring it
    • Do you have shaders enabled? There is an issue with the mod simpleclouds - remove this mod or disable shaders, if enabled  
  • Topics

×
×
  • Create New...

Important Information

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