Jump to content

Recommended Posts

Posted

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);
	}

 

Posted

You don't need to implement a new IItemProvider as Item already does that... so just use the items in Items.java and create a new ItemStack with it, then add them to the world using World#addEntity.

Posted
20 minutes ago, Strahlend said:

 


ItemStack itemDrop = new ItemStack(provider);

 

Since Item already implements IItemProvider, you can reference them directly, e.g.:

//this creates a stack of 5 iron nuggets
ItemStack ironNuggets = new ItemStack(Items.IRON_NUGGETS, 5);

 

Posted

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;
	}

 

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.