Jump to content

Recommended Posts

Posted (edited)

I wrote this code, but test_item don't drops when i break a gravel with a stick in mainhand. What's wrong?

		if ((event.getState().getBlock() instanceof BlockGravel)) {
			if (event.getHarvester() != null) {
				ItemStack tool = event.getHarvester().getHeldItemMainhand();
				ItemStack item = new ItemStack(Items.STICK);
				if (tool == item) {
					event.getDrops().add(new ItemStack(ItemList.test_item, 1));
					event.setDropChance((float) 0.8);
				}
			}
		}

 

Edited by Tameet
Posted

You've created two different instance of ItemStack. They don't double equal "==".

Some tips:

  Reveal hidden contents

 

Posted

Try this:

if ((event.getState().getBlock() instanceof BlockGravel)) {
	if (event.getHarvester() != null) {
		Item tool = event.getHarvester().getHeldItemMainhand().getItem();
		if (tool == Items.STICK) {
			event.getDrops().add(new ItemStack(ItemList.test_item, 1));
			event.setDropChance((float) 0.8);
		}
	}
}

 

Posted
  On 6/1/2019 at 9:04 AM, TheOnlyTrueEnte said:

Try this:

if ((event.getState().getBlock() instanceof BlockGravel)) {
	if (event.getHarvester() != null) {
		Item tool = event.getHarvester().getHeldItemMainhand().getItem();
		if (tool == Items.STICK) {
			event.getDrops().add(new ItemStack(ItemList.test_item, 1));
			event.setDropChance((float) 0.8);
		}
	}
}

 

Expand  

Many thanks

Posted

In java "==" does mean something different than in other languages, "==" compares if both references reference the same instance, if you wanna compare if 2 objects are equal, you can use .equals() method or as provided by Forge a reference to the registered Item/Block in Items.<name of block>, Blocks.<name of block> ( note that as pointed out by @TheOnlyTrueEnte you can use == here because the getItem() method you're using returns a reference to some item in Items or your custom item list not a new instance of that item)

Posted
  On 6/1/2019 at 9:04 AM, TheOnlyTrueEnte said:

Try this:

if ((event.getState().getBlock() instanceof BlockGravel)) {
	if (event.getHarvester() != null) {
		Item tool = event.getHarvester().getHeldItemMainhand().getItem();
		if (tool == Items.STICK) {
			event.getDrops().add(new ItemStack(ItemList.test_item, 1));
			event.setDropChance((float) 0.8);
		}
	}
}

 

Expand  

Please do not post copy-pastable code like this. Others might copy it without learning anything.

Some tips:

  Reveal hidden contents

 

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.