Posted June 25, 201411 yr Hey guys. I wanted to ask some help, because I am new to modding. How can i check if a player has an item in his inventory?
June 25, 201411 yr there are a couple of ways but one is to set up an event handler and use something like this. @SubscribeEvent public void onLivingUpdateEvent(LivingUpdateEvent event) { if (event.entity instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer)event.entity; ItemStack heldItem = player.getHeldItem(); if (heldItem != null && heldItem.isItemEqual(new ItemStack(Items.arrow))) { System.out.println("True"); } } } I am the author of Draconic Evolution
June 25, 201411 yr Author I am afraid it didn't work. The method in my main mod class is: public void Load(FMLLoadEvent e) { FMLCommonHandler.instance().bus().register(new PlayerEventHandler()); } and the PlayerEventHandler class is: public class PlayerEventHandler { @SubscribeEvent public void onLivingUpdateEvent(LivingUpdateEvent event) { if (event.entity instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer)event.entity; ItemStack heldItem = player.getHeldItem(); if (heldItem != null && heldItem.isItemEqual(new ItemStack(Items.arrow))) { player.addExperience(1999); } } } }
June 25, 201411 yr Wrong event bus. Yes,I know. It sucks. Use MinecraftForge.EVENT_BUS.register() We all stuff up sometimes... But I seem to be at the bottom of that pot.
June 25, 201411 yr Author Are you sure that's the problem? Because it doesn't work even if I changed to MinecraftForge.EVENT_BUS.register()
June 25, 201411 yr Author Don't get so mad. I replied to the other guy. It seems it just ignores what I coded. About your advice, I get an error when I replace LivingUpdateEvent with PlayerTickEvent
June 25, 201411 yr Author "PlayerTickEvent cannot be resolved to a type". Maybe I'm doing something very nooby.
June 25, 201411 yr Author I fixed that problem with PlayerTickEvent, but it still does nothing. Here is the code, if you can help me, you're the best: public void Load(FMLLoadEvent e) { MinecraftForge.EVENT_BUS.register(new PlayerEventHandler()); } public class PlayerEventHandler { @SubscribeEvent public void onPlayerTickEvent(PlayerTickEvent event) { if (event.player instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer)event.player; ItemStack heldItem = player.getHeldItem(); if (heldItem != null && heldItem.isItemEqual(new ItemStack(Items.arrow))) { System.out.println("True"); } } } }
June 25, 201411 yr Author You're right, that was a silly mistake, but even after fixing that, it just simply does nothing. When i get arrows, it doesn't do anything.
June 26, 201411 yr public void Load(FMLLoadEvent e) { MinecraftForge.EVENT_BUS.register(new PlayerEventHandler()); } Why not FMLPreInitializationEvent ? Also you could check InventoryPlayer for other methods to check item in slots and such.
June 26, 201411 yr Author Guys, I listened to your advices, it works now. Thanks a lot, that's a push for me in modding.
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.