Use EntitySoundEffectPacket in no location playSound method, fixes #194

This commit is contained in:
Kieran Wallbanks 2021-03-27 13:59:08 +00:00
parent 404d6aa45e
commit d086d16fa1
2 changed files with 30 additions and 3 deletions

View File

@ -6,8 +6,12 @@ import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.bossbar.BossBar; import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.sound.Sound; import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.sound.SoundStop; import net.kyori.adventure.sound.SoundStop;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; 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.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.NamedSoundEffectPacket;
import net.minestom.server.network.packet.server.play.SoundEffectPacket; import net.minestom.server.network.packet.server.play.SoundEffectPacket;
import net.minestom.server.network.packet.server.play.StopSoundPacket; 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. * Creates a sound stop packet from a sound stop.
* @param stop the sound stop * @param stop the sound stop

View File

@ -6,6 +6,7 @@ import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identified;
import net.kyori.adventure.identity.Identity; import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.inventory.Book; import net.kyori.adventure.inventory.Book;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.sound.SoundStop; import net.kyori.adventure.sound.SoundStop;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEvent;
@ -920,12 +921,12 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
} }
@Override @Override
public void playSound(net.kyori.adventure.sound.@NotNull Sound sound) { public void playSound(@NotNull Sound sound) {
this.playSound(sound, this.position.getX(), this.position.getY(), this.position.getZ()); playerConnection.sendPacket(AdventurePacketConvertor.createEntitySoundPacket(sound, this));
} }
@Override @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)); playerConnection.sendPacket(AdventurePacketConvertor.createSoundPacket(sound, x, y, z));
} }