Jump to content

[1.12.2] PlaySoundAtEntityEvent#getEntity() is always null


Fusseel

Recommended Posts

Hi, I'm trying to work with the PlaySoundAtEntityEvent to catch a certain sound being made. Then I want to get the entity that made this sound. This is what the piece of code looks like:

    @SubscribeEvent
    public void playSound(PlaySoundAtEntityEvent evt) {
        if (evt.getEntity() instanceof EntityPig && evt.getSound() == SoundEvents.ENTITY_PIG_AMBIENT) {
            evt.getEntity().getEntityId();
        }
    }

The problem is that it never finds anything. I've even gone as far to check for

if (evt.getEntity() != null)

but even this never yields any result. Only

if (evt.getEntity() == null)

actually triggers, but I don't understand why this is the case. 

I'd very much appreciate a solution to getting the entity that makes a certain sound.

Link to comment
Share on other sites

I've actually been looking into the same thing for a server-side system, but unfortunately the existing systems don't track the entity that made the sound (except for one circumstance which only ever allows for instances of EntityPlayer to be tracked).

 

In Entity.java:

    public void playSound(SoundEvent soundIn, float volume, float pitch)
    {
        if (!this.isSilent())
        {
            this.world.playSound((EntityPlayer)null, this.posX, this.posY, this.posZ, soundIn, this.getSoundCategory(), volume, pitch);
        }
    }

 

Edit: I've just finished implementing a workaround in my library; custom event which actually includes the entity the sound came from.  When I finish the current version of the library I'll post a link in case you decide to use it.

Edited by Laike_Endaril
Link to comment
Share on other sites

@Fusseel Not sure if you already tried it or not, but I just got around to doing more testing and it seems the trigger I based my DSoundEvent on doesn't catch all sound events.  I need to do more R&D.

To be more specific, I noticed it not catching block breaking sounds from myself while in single player mode.  I haven't gotten to do much testing yet.  In any case, the current version of DSoundEvent is not a catch-all, unfortunately

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.