Jump to content

1.16.4 Spawn Diamonds When Breaking Blocks


Strahlend

Recommended Posts

I am trying to make a sword that will spawn diamonds upon breaking any block. This is my first ever mod and I don't think I am understanding how to spawn objects. I overriding the onBlockDestroyed method and adding the spawnAdditionalDrops method to drop diamonds upon the block breaking. Would someone be able to help me figure out how to set diamonds to be dropped?
 

@Override
	public boolean onBlockDestroyed(ItemStack stack, World worldIn, BlockState state, BlockPos pos,
			LivingEntity entityLiving) {
		
		IItemProvider provider = new IItemProvider() {

			@Override
			public Item asItem() {
				// I'm not sure whats going on here.
				return null;
			}
				
		};
		
		ItemStack itemDrop = new ItemStack(provider);
		
		if (state.getBlockHardness(worldIn, pos) != 0.0F) {
			stack.damageItem(100, entityLiving, (entity) -> {
	            entity.sendBreakAnimation(EquipmentSlotType.MAINHAND);
	            state.spawnAdditionalDrops((ServerWorld) worldIn, pos, itemDrop);
	         });
		}
				
		return super.onBlockDestroyed(stack, worldIn, state, pos, entityLiving);
	}

 

Link to comment
Share on other sites

I got it working! This was what I ended up with. Thank you for your help.

@Override
	public boolean onBlockDestroyed(ItemStack stack, World worldIn, BlockState state, BlockPos pos,
			LivingEntity entityLiving) {
		
		ItemStack item = new ItemStack(Items.DIAMOND, 1);
		ItemEntity itemDrop = new ItemEntity(worldIn, pos.getX(), pos.getY(), pos.getZ(), item);
		
		if (state.getBlockHardness(worldIn, pos) != 0.0F) {
			stack.damageItem(0, entityLiving, (entity) -> {
	            entity.sendBreakAnimation(EquipmentSlotType.MAINHAND);
	         });
		}
		
		worldIn.addEntity(itemDrop);
				
		return true;
	}

 

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.

Announcements



×
×
  • Create New...

Important Information

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