Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

 

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.

  • Author

Thank you for the reply poopoodice! I guess I don't understand how to create an ItemStack. Could you explain it for me or point in the direction to figure it out?

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

 

  • Author

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.