Posted March 26, 20187 yr Hello, 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) { super(name); } public void onItemUse() { EntityPlayerSP player = Minecraft.getMinecraft().player; player.sendChatMessage("Help"); 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 March 26, 20187 yr by Stenbergcsgo
March 26, 20187 yr Author 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?
March 26, 20187 yr Author 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.
March 26, 20187 yr Author 1 minute ago, Stenbergcsgo said: 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. nvm found it through YATS
March 26, 20187 yr Author 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?
March 26, 20187 yr Author 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.
March 26, 20187 yr Author 5 minutes ago, diesieben07 said: Why is that? Why does overriding onItemUse not work for you? So overriding onItemUse is as following: @Override 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.
March 26, 20187 yr Author 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 Minecraft.getMinecraft().player
March 26, 20187 yr Author 8 minutes ago, diesieben07 said: I might be thick now, but my onItemUse has an EntityPlayer paremeter, so for me to call the function I would need to initialize a variable into that parameter right? Therefore I need to initialize the player?
March 26, 20187 yr Author 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 =)
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.