diff --git a/src/main/java/net/minestom/server/adventure/AdventurePacketConvertor.java b/src/main/java/net/minestom/server/adventure/AdventurePacketConvertor.java index b4bdadd2c..592b440f9 100644 --- a/src/main/java/net/minestom/server/adventure/AdventurePacketConvertor.java +++ b/src/main/java/net/minestom/server/adventure/AdventurePacketConvertor.java @@ -6,8 +6,12 @@ import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.bossbar.BossBar; import net.kyori.adventure.sound.Sound; import net.kyori.adventure.sound.SoundStop; +import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import net.minestom.server.entity.Entity; import net.minestom.server.network.packet.server.ServerPacket; +import net.minestom.server.network.packet.server.play.EntitySoundEffectPacket; import net.minestom.server.network.packet.server.play.NamedSoundEffectPacket; import net.minestom.server.network.packet.server.play.SoundEffectPacket; import net.minestom.server.network.packet.server.play.StopSoundPacket; @@ -124,6 +128,28 @@ public class AdventurePacketConvertor { } } + /** + * Creates an entity sound packet from an Adventure sound. + * @param sound the sound + * @param entity the entity the sound is coming from + * @return the packet + */ + public static ServerPacket createEntitySoundPacket(@NotNull Sound sound, @NotNull Entity entity) { + SoundEvent soundEvent = Registries.getSoundEvent(sound.name()); + + if (soundEvent == null) { + throw new IllegalArgumentException("Sound must be a valid sound event."); + } else { + EntitySoundEffectPacket packet = new EntitySoundEffectPacket(); + packet.soundId = soundEvent.getId(); + packet.soundSource = sound.source(); + packet.entityId = entity.getEntityId(); + packet.volume = sound.volume(); + packet.pitch = sound.pitch(); + return packet; + } + } + /** * Creates a sound stop packet from a sound stop. * @param stop the sound stop diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 78dff11f1..c11459009 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -6,6 +6,7 @@ import net.kyori.adventure.bossbar.BossBar; import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.inventory.Book; +import net.kyori.adventure.sound.Sound; import net.kyori.adventure.sound.SoundStop; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.HoverEvent; @@ -920,12 +921,12 @@ public class Player extends LivingEntity implements CommandSender, Localizable, } @Override - public void playSound(net.kyori.adventure.sound.@NotNull Sound sound) { - this.playSound(sound, this.position.getX(), this.position.getY(), this.position.getZ()); + public void playSound(@NotNull Sound sound) { + playerConnection.sendPacket(AdventurePacketConvertor.createEntitySoundPacket(sound, this)); } @Override - public void playSound(net.kyori.adventure.sound.@NotNull Sound sound, double x, double y, double z) { + public void playSound(@NotNull Sound sound, double x, double y, double z) { playerConnection.sendPacket(AdventurePacketConvertor.createSoundPacket(sound, x, y, z)); }