Spigot API playSound

Tags: ,



So I’m trying to create a minecraft plugin using the spigot api and i’m having lots of fun! I’m trying to make a /play command that plays a music disc to the player. I’ve come across a problem with trying to play the music to the player. I’m using this: player.playSound(player.getLocation(), Sound.MUSIC_DISC_PIGSTEP, 500.0f, 1.0f);
this works great, but for some reason, the music stops playing when I enter a new dimension such as the nether. Is there any way to stop this? Or is there a work around to this? Thank you guys for all your help!

(I’m 13 and really new to Java, sorry if its really obvious)

Answer

The playSound method will only play the sounds in an area for that player at the time it was used, but because you use a volume of 500 it covers a very very large area so the player will keep hearing the sound unless they move far away or change dimension. Note that the volume value is normally between min 0.0 and max 1.0, and any value over 1.0 just increases the distance the sound can be heard from.

The only real workaround I can think of is to use playSound again after the player changes dimension or moves away from the sound location. For example, you could use a PlayerChangedWorldEvent to detect when the player moves and then play the sound again.

Reference to the event listener: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerChangedWorldEvent.html

If you were not sure on how to use event listeners then here is a great tutorial: https://www.spigotmc.org/wiki/using-the-event-api/



Source: stackoverflow