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

Hey guys !

I a making an event, that turn a block into another when right-clicked with the flint.

The only problem is... I can't detect what Item the player is holding. I looked for the solution on many topics, but the answer always was <<You are using a custom item, use the onItemRightClick method>>.

 

So, here's my current event:

public class EventCraftPyrite 
{

@SubscribeEvent
public void onCraft(PlayerInteractEvent event)
{	
	EntityPlayer player = event.entityPlayer;

	Item item = player.getCurrentEquippedItem().getItem();

	Block block = event.world.getBlockState(event.pos).getBlock();


	if (item == Items.flint && block == KlondikCraft.pyrite_gold)
	{
		event.world.setBlockState(event.pos, KlondikCraft.pyrite_block.getDefaultState());
	}

}


}

 

And when the game crash (beacause is does chrash), it tells my the problem is at this line:

 

Item item = player.getCurrentEquippedItem().getItem();

 

I browsed a few forums, and learned that the getCurrentEquippedItem() method only works on client side.

 

So, I would like to know, how can I check if the item my player is holding is flint ?

 

 

Thank you !

Equipped item is NOT always an item - it can be hand, in which case the held ItemStack is == null. You need to check if getCurrentEquippedItem() is not null before getting Item.

 

Also - not that the minecraft is supposed to be logical, but you know that pyrite and gold are two different things, unless that is a color, then "meh"? :D (ofc. that doesn't matter).

1.7.10 is no longer supported by forge, you are on your own.

  • Author

First:

 

-I love your avatar. Doge is my favorite meme.

-It's called pyrite_gold because it's a fake gold block made of pyrite.

 

Now...

 

This doesn't work:

public class EventCraftPyrite 
{

@SubscribeEvent
public void onCraft(PlayerInteractEvent event)
{		
	Block block = event.world.getBlockState(event.pos).getBlock();

	if (block == KlondikCraft.pyrite_block)
	{
		EntityPlayer player = event.entityPlayer;	
		Item item = player.getCurrentEquippedItem().getItem();

		if (item != null)
		{
			if (item == Items.flint)
			{
				event.world.setBlockState(event.pos, KlondikCraft.pyrite_block.getDefaultState());
			}
		}
	}
}
}

 

Actually, even if you do have an Item in you hand (like flint) it crashs. But checkin if the item==null doesn't help, because is crashes at this line:

Item item = player.getCurrentEquippedItem().getItem();

 

Because of this part:

player.getCurrentEquippedItem().getItem()

 

So this may be helpfull, but it doesn't help for THIS problem (even though it solve anotherone).

The Item from ItemStack can never be null. The ItemStack returned from

getCurrentEquippedItem()

can be null, so you need to check if thats not null, and then you can safely get the Item.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

  • Author

Thanks guys, it's working now !

Here's the code if some of you are interested:

 

public class EventCraftPyrite 
{

@SubscribeEvent
public void onCraft(PlayerInteractEvent event)
{		
	Block block = event.world.getBlockState(event.pos).getBlock();

	if (block == KlondikCraft.pyrite_gold)
	{
		System.out.println("Block: OK");
		EntityPlayer player = event.entityPlayer;	

		if (player.getCurrentEquippedItem() != null)
		{
			Item item = player.getCurrentEquippedItem().getItem();
			System.out.println(item.getUnlocalizedName());
				if (item == Items.flint)
				{
					event.world.setBlockState(event.pos, KlondikCraft.pyrite_block.getDefaultState());
				}

		}
	}
}
}

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.