Jump to content

Recommended Posts

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 !

Posted

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.

Posted

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

Posted

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/

Posted

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

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