Jump to content

Checking if player used an item


Recommended Posts



I'm trying to figure out how to check if a player has used my custom item, then do something accordingly.

I currently have following custom item class:

package com.mta.utzonmod.items;

import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;

public class SchematicSign extends ItemBase {
	public SchematicSign(String name) {
	public void onItemUse() {
		EntityPlayerSP player = Minecraft.getMinecraft().player;
		player.isDead = true; //Just checking if chatMessage was the issue	


As can be seen I'm trying to send a chat message when the player clicks with the item in hand, but it doesn't work. I should put a disclaimer I'm not only new to Minecraft modding, but also rather inexperienced with Java. I assume the issue relies in the "onItemUse()" function not getting called, but hopefully you guys can enlighten me.


Best regards

Edited by Stenbergcsgo
Link to comment
Share on other sites

18 minutes ago, diesieben07 said:

Your method will not be called by anybody (unless you call it yourself, in which case, show that code). What made you think that it would be called?


Also, you cannot use client-only classes (Minecraft for example) in common code (your item class). You can read the documentation about sides for more information.

As mentioned I'm rather clueless about modding. I guessed the function might be in-built, and would get called when a player used the item the function was attached to. I've read the documentation and understand it somewhat, but utilizing it is a different story. If I wanted to make this work, where should I begin?

Link to comment
Share on other sites

8 minutes ago, diesieben07 said:

The Item class does indeed have a method onItemUse, which is called when a block is right-clicked with that item. However your item does not override this method, it is completely distinct from it.

I highly suggest you use your IDE auto-completion to generate overriding methods for you and also always use @Override if you intend to override.

If you are unfamiliar with the concept of overriding methods, you need to go back to learning basic Java before starting to mod.

To go on a tangent, where can I find the onItemUse documentation? Everytime I try to access the Forge API I get a 404 error. 

Link to comment
Share on other sites

Alright I think I'm getting the hang of it :-) I found the documentation on the function in the files shown in the IDE, which revealed that the variables I needed to parse in to do an overload, didn't match the ones I found online. Now, lets say I want to create an overload of the onItemUse(), which uses the EntityPlayer as one of its variables. In which class would I implement this  in order to use client-only classes? Do I create a separate manager, do it in main, or am I completely missing the mark in my speculations?

Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

Parsing and overloading do not have anything to do with this. This is about overriding a method. Like I said, you should always let your IDE do it for you.


That's not how this works. You cannot create new overloads of methods and expect them to be called with the right parameters "by magic". You must make due with the parameters that are provided. In case of onItemUse the player using the item is already provided though, so you do not need to add a new overload (make sure you understand the difference between overload and override, they are not at all the same thing).


In no class at all. "This item was used" is a server-side operation and you cannot use client-only classes there.

Written mistake, meant override. So, if I want to do a check whether or not a player used a specific item, what's the next step? All I'm getting from this is that I'm unable to use the class which checks for that thing exactly. 

Link to comment
Share on other sites

5 minutes ago, diesieben07 said:

Why is that? Why does overriding onItemUse not work for you?

So overriding onItemUse is as following:

	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer player, EnumHand handIn) {
		return null;


One of the parameters is EntityPlayer from the package Minecraft. You mentioned I couldn't use this in my common code (item class), which is why I asked what class I should be doing this override then. Sorry if I'm being unclear, I'm just trying to connect the dots.

Link to comment
Share on other sites

9 minutes ago, diesieben07 said:

I mentioned you cannot use the Minecraft class in common code, you can see this by it being annotated with @SideOnly(CLIENT). Did you read the documentation I linked to?

Alright then I guess we can get to the core of the issue. How do I get the player whom is holding the item using EntityPlayer, without doing 



Link to comment
Share on other sites

9 minutes ago, diesieben07 said:

You don't call onItemUse. It is called by the game when your item is used on a block.

Alright I think I wrapped my head around it now and got it working. Sorry for the tedious questioning, but I wanted to make sure I understood every aspect instead of only knowing half the story. Thanks again =)

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.

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.

  • Create New...

Important Information

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