Jump to content

[1.8]Get player's held item


Hamster_Furtif

Recommended Posts

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 !

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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

		}
	}
}
}

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

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