Jump to content

Checking if player used an item


Stenbergcsgo

Recommended Posts

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

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

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 

Minecraft.getMinecraft().player

 

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.

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 everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
    • Update your drivers: https://www.amd.com/en/support/graphics/amd-radeon-r9-series/amd-radeon-r9-200-series/amd-radeon-r9-280x
  • Topics

×
×
  • Create New...

Important Information

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