So I’ve been looking at some youtube tutorials and javadocs because I’m interested in making the switch to Sponge. There is, however, one thing that I miss, which is playing a sound to one specific player.
All methods I have seen for this, will make the sound audible to all players within range.
Is this possible in Sponge?
Just make the position the players position
Does that ensure the sound is played to only the player at this position? According to the javadocs, the sound is audible to all players within range, that’s why I’m asking
I actually don’t think theres a method for just to the player, i might make a PR for one then if there already isnt one.
Unless you have an issue with player standing inside of each other, using a distance of 1 should play only to the player at that position
You can use the method Player.playSound
(from the Viewer
interface). It will only send the sound to that player.
*/ void spawnParticles(ParticleEffect particleEffect, Vector3d position, int radius); /** * Plays a sound. * * @param sound the sound * @param pos the position to play the sound at */ default void playSound(final @NonNull Sound sound, final Vector3d pos) { this.playSound(sound, pos.getX(), pos.getY(), pos.getZ()); } /** * Plays the given {@link MusicDisc} at the given position. The benefit of playing * {@link MusicDisc} instead of a {@link SoundType} allows you to stop them through * the {@link #stopMusicDisc(Vector3i)}. Playing a new {@link MusicDisc} at the same * position will cancel the currently playing one. * * @param position The position * @param musicDiscType The music disc
In fact others will hear it.
If this means that provided the others are at least 1 block away from the target player, that’s good enough for me. Thanks!
But the sound will be heard by all players in range, that method doesn’t solve the issue
This is also not good, if a player is running quickly or flying on elytra the sound may fade out quickly or the player’s ping may delay the sound and make it inaudible to the target player.
Is there any new solution to solve this issue?
It shouldn’t do if you call playSound
on a specific Player
instance.
Note that both World
and Player
extend Viewer
, so it is possible to play a sound in the world (all nearby players) or to an individual player.
3 Likes
Thank you. The “All players within range will hear the sound with the given volume” phrase causes confusion
I also had swapped “playTo” and “playAt” in my implementation and it has contributed to my confusion. It should work now
override fun playSoundAt(session: SpongeSession, sound: PlatformSound): Boolean {
SpongeSound[sound].playTo(session.player) // <-- This was swapped
return true
}
override fun playSoundTo(session: SpongeSession, sound: PlatformSound): Boolean {
if(!session.player.isOnline)
return false
SpongeSound[sound].playAt(session.player) // <-- This was swapped
return true
}