Jump to content

Recommended Posts

Posted (edited)

I made a method for filling an empty bowl with milk (a modded milk bowl item) when right-clicking a cow by handling the PlayerInteractEvent.EntityInteract event. However, seems like the method is called twice since each time that I right-click a cow two bowls are extracted and two milk bowls are added to the player's inventory. Any idea on why this happens? Here I attach the code of the method for handling the event: 

@SubscribeEvent
public static void onRightClickBowl(PlayerInteractEvent.EntityInteract event) {
  if(!event.getLevel().isClientSide()) {
    // if the target entity is a cow
    if (event.getTarget() instanceof Cow cow) {
      // check if the target entity is an adult cow and if the player holds an empty bowl
      if ((cow.getAge() != -1) && event.getEntity().getItemInHand(InteractionHand.MAIN_HAND).getItem().equals(Items.BOWL)) {
        event.getEntity().setItemInHand(InteractionHand.MAIN_HAND,
                              new ItemStack(Items.BOWL, event.getEntity().getItemInHand(InteractionHand.MAIN_HAND).getCount() - 1));
        event.getEntity().addItem(new ItemStack(ModItems.MILK_BOWL.get()));
      }
    }
  }
}

 

Thanks in advance!!

Edited by iEviiL
  • iEviiL changed the title to Method for handling event is triggering twice [1.20.1]
Posted

Posting code snippets out of context in the forums will usually just mean your question gets ignored unless the problem is obvious.

You should post a simple build on github that reproduces your problem.

 

I know little about that specific event, but I do know you are not really handling the event.

You are running some code and then letting the normal vanilla processing run as well.

You need to cancel the event to stop the normal processing. e.g. related example from immersive engineering
https://github.com/BluSunrize/ImmersiveEngineering/blob/fc79b842ac1bceba655a15f1f30b4140d6b2423c/src/main/java/blusunrize/immersiveengineering/common/EventHandler.java#L157

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

  • 2 weeks later...
Posted

As I understand it, the event occurs 1 time for the right hand, 2 times for the left hand

you need to write a code that excludes the action for the left hand

if ((cow.getAge() != -1) && event.getEntity().getItemInHand(InteractionHand.MAIN_HAND).getItem().equals(Items.BOWL))

checks for the right hand, but does not preclude execution of the code for the left hand when the right hand condition is true

add && hand == InteractionHand.MAIN_HAND in    if(!event.getLevel().isClientSide()) should work

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



×
×
  • Create New...

Important Information

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