Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.8]Get player's held item


Hamster_Furtif
 Share

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

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

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Whenever I try to start my server I get this crash: [14:05:21] [main/FATAL]: Failed to start the minecraft server com.google.gson.JsonParseException: Error loading registry data: Not a valid resource location: #minecraft:infiniburn_end Non [a-z0-9_.-] character in namespace of location: #minecraft:infiniburn_end         at net.minecraft.core.RegistryAccess.m_175498_(RegistryAccess.java:156) ~[server-1.18.1-20211210.034407-srg.jar%2390!/:?]         at java.util.Optional.ifPresent(Optional.java:178) ~[?:?]         at net.minecraft.core.RegistryAccess.m_175503_(RegistryAccess.java:155) ~[server-1.18.1-20211210.034407-srg.jar%2390!/:?]         at net.minecraft.core.RegistryAccess.m_175500_(RegistryAccess.java:146) ~[server-1.18.1-20211210.034407-srg.jar%2390!/:?]         at net.minecraft.resources.RegistryReadOps.m_195841_(RegistryReadOps.java:37) ~[server-1.18.1-20211210.034407-srg.jar%2390!/:?]         at net.minecraft.resources.RegistryReadOps.m_179866_(RegistryReadOps.java:31) ~[server-1.18.1-20211210.034407-srg.jar%2390!/:?]         at net.minecraft.server.Main.main(Main.java:161) ~[server-1.18.1-20211210.034407-srg.jar%2390!/:?]         at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]         at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]         at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]         at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]         at net.minecraftforge.fml.loading.targets.CommonServerLaunchHandler.lambda$launchService$0(CommonServerLaunchHandler.java:46) ~[fmlloader-1.18.1-39.0.75.jar%2361!/:?]         at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) ~[modlauncher-9.1.0.jar%2343!/:?]         at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) ~[modlauncher-9.1.0.jar%2343!/:?]         at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) ~[modlauncher-9.1.0.jar%2343!/:?]         at cpw.mods.modlauncher.Launcher.run(Launcher.java:106) ~[modlauncher-9.1.0.jar%2343!/:?]         at cpw.mods.modlauncher.Launcher.main(Launcher.java:77) ~[modlauncher-9.1.0.jar%2343!/:?]         at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26) ~[modlauncher-9.1.0.jar%2343!/:?]         at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23) ~[modlauncher-9.1.0.jar%2343!/:?]         at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:149) ~[bootstraplauncher-1.0.0.jar:?]         at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]         at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]         at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]         at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]         at org.jmt.mcmt.modlauncher.FastUtilTransformerService.argumentValues(FastUtilTransformerService.java:200) ~[jmt_mcmt-1.18.1-0.24.2-118-packaged.jar%2375!/:?]         at cpw.mods.modlauncher.TransformationServicesHandler.lambda$offerArgumentResultsToServices$5(TransformationServicesHandler.java:80) ~[modlauncher-9.1.0.jar%235!/:?]         at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) ~[?:?]         at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[?:?]         at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1779) ~[?:?]         at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[?:?]         at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[?:?]         at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) ~[?:?]         at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) ~[?:?]         at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:?]         at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596) ~[?:?]         at cpw.mods.modlauncher.TransformationServicesHandler.offerArgumentResultsToServices(TransformationServicesHandler.java:80) ~[modlauncher-9.1.0.jar%235!/:?]         at cpw.mods.modlauncher.ArgumentHandler.processArguments(ArgumentHandler.java:71) [modlauncher-9.1.0.jar%235!/:?]         at cpw.mods.modlauncher.TransformationServicesHandler.processArguments(TransformationServicesHandler.java:68) [modlauncher-9.1.0.jar%235!/:?]         at cpw.mods.modlauncher.TransformationServicesHandler.initializeTransformationServices(TransformationServicesHandler.java:50) [modlauncher-9.1.0.jar%235!/:?]         at cpw.mods.modlauncher.Launcher.run(Launcher.java:87) [modlauncher-9.1.0.jar%235!/:?]         at cpw.mods.modlauncher.Launcher.main(Launcher.java:77) [modlauncher-9.1.0.jar%235!/:?]         at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26) [modlauncher-9.1.0.jar%235!/:?]         at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23) [modlauncher-9.1.0.jar%235!/:?]         at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:149) [bootstraplauncher-1.0.0.jar:?] container@pterodactyl~ Server marked as offline... [Game2 Daemon]: ---------- Detected server process in a crashed state! ---------- [Game2 Daemon]: Exit code: 1 [Game2 Daemon]: Out of memory: false [Game2 Daemon]: Aborting automatic restart, last crash occurred less than 120 seconds ago.
    • why, you have Event instance so call the getters we all have the code of the Event the Event exist to perform an action at a specific 'time'/'action' in the game what are you talking about
    • I am sorry, but I have zero idea what you are trying to say.
    • Thanks for the tip, I managed to get the data I wanted in the return parameters of the listened event. Here is part of the code @SubscribeEvent public static void Demo02(LivingHurtEvent event) { System.out.println("sadawedawdawdawdadwa" + '\n' + event.getAmount()); }  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.