Jump to content

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


Recommended Posts

Posted

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/

Posted

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.

Posted

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/

Posted

This had me confused for a while too, until I realised that in 1.9+, the player now has two usable hands...

 

event.getHand()

will be your friend here.

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

    • that happens every time I enter a new dimension.
    • This is the last line before the crash: [ebwizardry]: Synchronising spell emitters for PixelTraveler But I have no idea what this means
    • What in particular? I barely used that mod this time around, and it's never been a problem in the past.
    • Im trying to build my mod using shade since i use the luaj library however i keep getting this error Reason: Task ':reobfJar' uses this output of task ':shadowJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. So i try adding reobfJar.dependsOn shadowJar  Could not get unknown property 'reobfJar' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. my gradle file plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' id 'com.github.johnrengelman.shadow' version '7.1.2' id 'org.spongepowered.mixin' version '0.7.+' } apply plugin: 'net.minecraftforge.gradle' apply plugin: 'org.spongepowered.mixin' apply plugin: 'com.github.johnrengelman.shadow' version = mod_version group = mod_group_id base { archivesName = mod_id } // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. java.toolchain.languageVersion = JavaLanguageVersion.of(17) //jarJar.enable() println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { mappings channel: mapping_channel, version: mapping_version copyIdeResources = true runs { configureEach { workingDirectory project.file('run') property 'forge.logging.markers', 'REGISTRIES' property 'forge.logging.console.level', 'debug' arg "-mixin.config=derp.mixin.json" mods { "${mod_id}" { source sourceSets.main } } } client { // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id } server { property 'forge.enabledGameTestNamespaces', mod_id args '--nogui' } gameTestServer { property 'forge.enabledGameTestNamespaces', mod_id } data { workingDirectory project.file('run-data') args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') } } } sourceSets.main.resources { srcDir 'src/generated/resources' } repositories { flatDir { dirs './libs' } maven { url = "https://jitpack.io" } } configurations { shade implementation.extendsFrom shade } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation 'org.luaj:luaj-jse-3.0.2' implementation fg.deobf("com.github.Virtuoel:Pehkui:${pehkui_version}") annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' minecraftLibrary 'luaj:luaj-jse:3.0.2' shade 'luaj:luaj-jse:3.0.2' } // Example for how to get properties into the manifest for reading at runtime. tasks.named('jar', Jar).configure { manifest { attributes([ 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors, 'Specification-Version' : '1', // We are version 1 of ourselves 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), "TweakClass" : "org.spongepowered.asm.launch.MixinTweaker", "TweakOrder" : 0, "MixinConfigs" : "derp.mixin.json" ]) } rename 'mixin.refmap.json', 'derp.mixin-refmap.json' } shadowJar { archiveClassifier = '' configurations = [project.configurations.shade] finalizedBy 'reobfShadowJar' } assemble.dependsOn shadowJar reobf { re shadowJar {} } publishing { publications { mavenJava(MavenPublication) { artifact jar } } repositories { maven { url "file://${project.projectDir}/mcmodsrepo" } } } my entire project:https://github.com/kevin051606/DERP-Mod/tree/Derp-1.0-1.20
  • Topics

×
×
  • Create New...

Important Information

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