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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello , when I try to launch the forge installer it just crash with a message for 0,5 secondes. I'm using java 17 to launch it. Here's the link of the error :https://cdn.corenexis.com/view/?img=d/ma24/qs7u4U.jpg  
    • You will find the crash-report or log in your minecraft directory (crash-report or logs folder)
    • Use a modpack which is using these 2 mods as working base:   https://www.curseforge.com/minecraft/modpacks/life-in-the-village-3
    • inicie un mundo donde instale Croptopia y Farmer's Delight, entonces instale el addon Croptopia Delight pero no funciona. es la version 1.18.2
    • Hello all. I'm currently grappling with the updateShape method in a custom class extending Block.  My code currently looks like this: The conditionals in CheckState are there to switch blockstate properties, which is working fine, as it functions correctly every time in getStateForPlacement.  The problem I'm running into is that when I update a state, the blocks seem to call CheckState with the position of the block which was changed updated last.  If I build a wall I can see the same change propagate across. My question thus is this: is updateShape sending its return to the neighbouring block?  Is each block not independently executing the updateShape method, thus inserting its own current position?  The first statement appears to be true, and the second false (each block is not independently executing the method). I have tried to fix this by saving the block's own position to a variable myPos at inception, and then feeding this in as CheckState(myPos) but this causes a worse outcome, where all blocks take the update of the first modified block, rather than just their neighbour.  This raises more questions than it answers, obviously: how is a different instance's variable propagating here?  I also tried changing it so that CheckState did not take a BlockPos, but had myPos built into the body - same problem. I have previously looked at neighbourUpdate and onNeighbourUpdate, but could not find a way to get this to work at all.  One post on here about updatePostPlacement and other methods has proven itself long superceded.  All other sources on the net seem to be out of date. Many thanks in advance for any help you might offer me, it's been several days now of trying to get this work and several weeks of generally trying to get round this roadblock.  - Sandermall
  • Topics

×
×
  • Create New...

Important Information

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