Jump to content

[Solved][1.10.2] EntityInteract event seems to fire twice on each side


jabelar

Recommended Posts

Hi, I've been away from modding for over a year and I'm stuck in China for a few weeks so had some down time to try porting my 1.8 mods to 1.10. It is mostly going well and now I'm working through the new bugs that have cropped up.

 

The issue I'm looking at now is that I have an interrupt handler that converts an ordinary cow into a custom cow if you right-click on it with a golden carrot. This worked fine in 1.8.9 and I didn't do much at all to convert to 1.10.2 but it is behaving differently -- if I interact it creates two of my custom cows for just one click.

 

So to debug I added some println statements to help trace the code. And it seems that the EntityInteract event is being called twice on each side. I see it print out that the interaction happened on the client twice and then on the server twice.

 

I checked on a few theories on why this is happening.

 

Theory 0) I thought maybe I wasn't handling fact that the event fires on both client and server, but my console output shows that I'm properly handling the server case by short-circuiting the execution.

 

Theory 1) I thought maybe the EntityInteract had been extended to have some sub-events, like a Start and End event, such that it is now called twice but I couldn't find any such thing.

 

Theory 2) I thought maybe I had some duplicate event handling somewhere due to cut and paste or something. But I only have one EventHandler class and searching and checking for duplicate methods doesn't come up with anything.

 

Theory 3) I thought maybe EventInteract was actually constructed twice now. So I traced the call hierarchy and found the following:

  1) The EntityInteract event is only constructed in the Forge Hook for onInteract()

  2) The onInteract() method is only called in one place: the EntityPlayer#interact()

  3) The interact() method is called in two places:

      a) PlayerControllerMP#interactWithEntity()

      b) NetHandlerPlayServer.processUseEntity()

 

Now that all makes sense because the interactWithEntity() is called on the client and in same code a CPacketUseEntity is sent, and the NetHandlerPlayServer handles the packet when received at the server. So that is the client and server side.

 

I can't figure out why all this is happening twice then! Any ideas?

 

Here is my EventHandling code:

 

 

    @SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)

    public void onEvent(EntityInteract event)

    {

World world = event.getWorld();

if (world.isRemote)

{

    // DEBUG

        System.out.println("EntityInteract event on client side");

 

return;

}

 

    // DEBUG

System.out.println("EntityInteract entity event on server side");

   

    Entity theEntity = event.getTarget();

 

        if (theEntity instanceof EntityCow && !(theEntity instanceof EntityFamilyCow))

        {

        // DEBUG

        System.out.println("Interacting with cow");

        ItemStack theItemStack = event.getEntityPlayer().getHeldItemMainhand();

        if (theItemStack != null)

        {

        if (theItemStack.getItem() == Items.GOLDEN_CARROT)

        {

        // DEBUG

        // System.out.println("While holding a golden carrot");

        if (!ModWorldData.get(world).getHasCastleSpawned())

        {

        // DEBUG

        // System.out.println("Haven't spawned castle yet so okay to make a family cow");

                   

            EntityPlayer thePlayer = event.getEntityPlayer();

       

            if (!((EntityCow) theEntity).isChild())

            {

        thePlayer.addChatMessage(new TextComponentString(Utilities.stringToRainbow("This cow is now your Family Cow!")));

       

        EntityFamilyCow entityToSpawn = new EntityFamilyCow(world);

        entityToSpawn.setLocationAndAngles(theEntity.posX, theEntity.posY, theEntity.posZ,

                    MathHelper.wrapDegrees(world.rand.nextFloat()

                    * 360.0F), 0.0F);

        world.spawnEntityInWorld(entityToSpawn);

       

        theEntity.setDead();

 

        thePlayer.addStat(MagicBeans.achievementStartMagicBeans, 1);

            }           

        }

        }     

        }

        }     

    }

 

 

 

And here is the console output:

 

 

[11:48:37] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.EventHandler:onEvent:420]: EntityInteract event on client side

[11:48:37] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.EventHandler:onEvent:420]: EntityInteract event on client side

[11:48:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.EventHandler:onEvent:426]: EntityInteract entity event on server side

[11:48:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.EventHandler:onEvent:433]: Interacting with cow

[11:48:37] [Client thread/INFO]: [CHAT] §cT§6h§ei§as§b §9c§do§5w§c §6i§es§a §bn§9o§dw§5 §cy§6o§eu§ar§b §9F§da§5m§ci§6l§ey§a §bC§9o§dw§5!§f

[11:48:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.entities.EntityFamilyCow:<init>:67]: EntityCowMagicBeans constructor

[11:48:37] [server thread/INFO]: Player208 the Ugly has just earned the achievement [started Jack and Beanstalk Adventure]

[11:48:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.EventHandler:onEvent:426]: EntityInteract entity event on server side

[11:48:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.EventHandler:onEvent:433]: Interacting with cow

[11:48:37] [server thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.entities.EntityFamilyCow:<init>:67]: EntityCowMagicBeans constructor

[11:48:37] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.entities.EntityFamilyCow:<init>:67]: EntityCowMagicBeans constructor

[11:48:37] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.entities.EntityFamilyCow:readSpawnData:342]: EntityCowMagicBeans spawn data received, scaleFactor = 1.0, hasSpawnedMysteriousStranger = false

[11:48:37] [Client thread/INFO]: [CHAT] Player208 the Ugly has just earned the achievement [started Jack and Beanstalk Adventure]

[11:48:37] [Client thread/INFO]: [CHAT] §cT§6h§ei§as§b §9c§do§5w§c §6i§es§a §bn§9o§dw§5 §cy§6o§eu§ar§b §9F§da§5m§ci§6l§ey§a §bC§9o§dw§5!§f

[11:48:37] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.entities.EntityFamilyCow:<init>:67]: EntityCowMagicBeans constructor

[11:48:37] [Client thread/INFO] [sTDOUT]: [com.blogspot.jabelarminecraft.magicbeans.entities.EntityFamilyCow:readSpawnData:342]: EntityCowMagicBeans spawn data received, scaleFactor = 1.0, hasSpawnedMysteriousStranger = false

 

 

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Try using EntityInteractSpecific.

 

Okay, I'll try. But do you have a reason, or just guessing. It seems to have exactly the same call hierarchy as EntityInteract does.

I am just guessing as I do not know the exact call hierarchy of those events as I haven't messed around with them yet. Haven't really done much since 1.7.10 except the basics... I will try my best to help though.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I tried EntityInteractSpecific and it does exactly the same thing.

 

Weird.

 

I guess I might do the work to create a new mod that only handles these events to make sure there is nothing else in the mod causing this. But I have a lot of experience with modding and event handling so generally know how to debug these issues...  but I am a bit rusty so maybe I made a mistake somewhere.

 

Anyone else have any ideas? Especially anything that might have changed since 1.8?

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.



×
×
  • Create New...

Important Information

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