Fusseel Posted March 12, 2019 Posted March 12, 2019 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. Quote
Laike_Endaril Posted March 12, 2019 Posted March 12, 2019 (edited) 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 March 13, 2019 by Laike_Endaril Quote
Laike_Endaril Posted March 13, 2019 Posted March 13, 2019 https://minecraft.curseforge.com/projects/fantastic-lib/files/2686512 The world events mentioned in the changelog there include a few for sounds. The event you'd want from the library is DSoundEvent Feel free to ping me if you decide to use it and find an issue / need more information Quote
Fusseel Posted March 14, 2019 Author Posted March 14, 2019 Thanks a lot for your response. I'll definitely be taking a look at your library sometime soon. Quote
Laike_Endaril Posted March 16, 2019 Posted March 16, 2019 @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 Quote
Recommended Posts
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.