Jump to content

Harvest block from code


Adil Yilan

Recommended Posts

Hi,

I have made a block that once it is activated, it removes all other nearby blocks. To accomplish that, I have used level.destroyBlock(..) method.

However, what I would like to accomplish is that once the block is destroyed, it should leave item that is equal what would drop after using silk touch.

I.e. after block with iron ore is destroyed, a block of iron ore should remain on the ground that can be looted.

I am using Forge for Minecraft 1.18.1.

I have read that there used to be a method harvestBlock, but I can't find it anymore.

What would be a proper way to make effect of block being harvested with silk touch effect?

Thanks!

EDIT:

This is the code of use method on my TerraformerBlock:
 

    public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {

        int x = pos.getX();
        int y = pos.getY();
        int z = pos.getZ();
        
        for (int i = -8; i <= 8; i++) {
            for (int j = -8; j <= 8; j++) {

                if (!(i == 0 && j == 0)) {

                    BlockPos blockPos = new BlockPos(x + i, y, z + j);

                    level.destroyBlock(blockPos, false);
                    
                }
                
            }
        }
        
        return InteractionResult.SUCCESS;
    }

 

Edited by Adil Yilan
Link to comment
Share on other sites

Final version of working code, with comments:

 

	public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {

		// Create diamond pickaxe
        ItemStack pickaxe = new ItemStack(Items.DIAMOND_PICKAXE);
        
        // Enchant it with silk touch
        pickaxe.enchant(Enchantments.SILK_TOUCH, 1);

		// Read coordinates of terraformer
		int x = pos.getX();
		int y = pos.getY();
		int z = pos.getZ();
		
		for (int i = -8; i <= 8; i++) {

			for (int j = -8; j <= 8; j++) {

				// IF: it is not location of terraformer
				if (!(i == 0 && j == 0)) {

					// Get position of nearby block
					BlockPos blockPos = new BlockPos(x + i, y, z + j);

					// Get state of nearby block
					BlockState blockstate = level.getBlockState(blockPos);

					// IF: Block is not air
					if (!blockstate.isAir()) {
	
						// Try resolve blockentity for block
			            BlockEntity blockentity = blockstate.hasBlockEntity() ? level.getBlockEntity(blockPos) : null;
	
			            // Drop resources from loot table as if they were mined with silk touch
			            Block.dropResources(blockstate, level, blockPos, blockentity, null, pickaxe);
	
			            // Destroy the block without loot drops
			            level.destroyBlock(blockPos, false);
				    }
				}
			}
		}
		
		return InteractionResult.SUCCESS;
	}

 

Thanks @diesieben07!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

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