#1301: Various Sound API improvements

By: Jishuna <joshl5324@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2023-11-17 19:54:48 +13:00
parent efb9f1bd5c
commit 9a3c24e787
2 changed files with 78 additions and 49 deletions

View File

@@ -436,29 +436,11 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
if (getHandle().connection == null) return;
Sound instrumentSound = switch (instrument.ordinal()) {
case 0 -> Sound.BLOCK_NOTE_BLOCK_HARP;
case 1 -> Sound.BLOCK_NOTE_BLOCK_BASEDRUM;
case 2 -> Sound.BLOCK_NOTE_BLOCK_SNARE;
case 3 -> Sound.BLOCK_NOTE_BLOCK_HAT;
case 4 -> Sound.BLOCK_NOTE_BLOCK_BASS;
case 5 -> Sound.BLOCK_NOTE_BLOCK_FLUTE;
case 6 -> Sound.BLOCK_NOTE_BLOCK_BELL;
case 7 -> Sound.BLOCK_NOTE_BLOCK_GUITAR;
case 8 -> Sound.BLOCK_NOTE_BLOCK_CHIME;
case 9 -> Sound.BLOCK_NOTE_BLOCK_XYLOPHONE;
case 10 -> Sound.BLOCK_NOTE_BLOCK_IRON_XYLOPHONE;
case 11 -> Sound.BLOCK_NOTE_BLOCK_COW_BELL;
case 12 -> Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO;
case 13 -> Sound.BLOCK_NOTE_BLOCK_BIT;
case 14 -> Sound.BLOCK_NOTE_BLOCK_BANJO;
case 15 -> Sound.BLOCK_NOTE_BLOCK_PLING;
case 16 -> Sound.BLOCK_NOTE_BLOCK_XYLOPHONE;
default -> null;
};
Sound instrumentSound = instrument.getSound();
if (instrumentSound == null) return;
float f = (float) Math.pow(2.0D, (note.getId() - 12.0D) / 12.0D);
getHandle().connection.send(new PacketPlayOutNamedSoundEffect(CraftSound.bukkitToMinecraftHolder(instrumentSound), net.minecraft.sounds.SoundCategory.RECORDS, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), 3.0f, f, getHandle().getRandom().nextLong()));
float pitch = note.getPitch();
getHandle().connection.send(new PacketPlayOutNamedSoundEffect(CraftSound.bukkitToMinecraftHolder(instrumentSound), net.minecraft.sounds.SoundCategory.RECORDS, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), 3.0f, pitch, getHandle().getRandom().nextLong()));
}
@Override
@@ -473,24 +455,34 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
@Override
public void playSound(Location loc, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch) {
if (loc == null || sound == null || category == null || getHandle().connection == null) return;
playSound0(loc, CraftSound.bukkitToMinecraftHolder(sound), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch);
playSound(loc, sound, category, volume, pitch, getHandle().random.nextLong());
}
@Override
public void playSound(Location loc, String sound, org.bukkit.SoundCategory category, float volume, float pitch) {
if (loc == null || sound == null || category == null || getHandle().connection == null) return;
playSound0(loc, Holder.direct(SoundEffect.createVariableRangeEvent(new MinecraftKey(sound))), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch);
playSound(loc, sound, category, volume, pitch, getHandle().random.nextLong());
}
private void playSound0(Location loc, Holder<SoundEffect> soundEffectHolder, net.minecraft.sounds.SoundCategory categoryNMS, float volume, float pitch) {
@Override
public void playSound(Location loc, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) {
if (loc == null || sound == null || category == null || getHandle().connection == null) return;
playSound0(loc, CraftSound.bukkitToMinecraftHolder(sound), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch, seed);
}
@Override
public void playSound(Location loc, String sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) {
if (loc == null || sound == null || category == null || getHandle().connection == null) return;
playSound0(loc, Holder.direct(SoundEffect.createVariableRangeEvent(new MinecraftKey(sound))), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch, seed);
}
private void playSound0(Location loc, Holder<SoundEffect> soundEffectHolder, net.minecraft.sounds.SoundCategory categoryNMS, float volume, float pitch, long seed) {
Preconditions.checkArgument(loc != null, "Location cannot be null");
if (getHandle().connection == null) return;
PacketPlayOutNamedSoundEffect packet = new PacketPlayOutNamedSoundEffect(soundEffectHolder, categoryNMS, loc.getX(), loc.getY(), loc.getZ(), volume, pitch, getHandle().getRandom().nextLong());
PacketPlayOutNamedSoundEffect packet = new PacketPlayOutNamedSoundEffect(soundEffectHolder, categoryNMS, loc.getX(), loc.getY(), loc.getZ(), volume, pitch, seed);
getHandle().connection.send(packet);
}
@@ -506,19 +498,29 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
@Override
public void playSound(org.bukkit.entity.Entity entity, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch) {
if (!(entity instanceof CraftEntity craftEntity) || sound == null || category == null || getHandle().connection == null) return;
playSound0(entity, CraftSound.bukkitToMinecraftHolder(sound), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch);
playSound(entity, sound, category, volume, pitch, getHandle().random.nextLong());
}
@Override
public void playSound(org.bukkit.entity.Entity entity, String sound, org.bukkit.SoundCategory category, float volume, float pitch) {
if (!(entity instanceof CraftEntity craftEntity) || sound == null || category == null || getHandle().connection == null) return;
playSound0(entity, Holder.direct(SoundEffect.createVariableRangeEvent(new MinecraftKey(sound))), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch);
playSound(entity, sound, category, volume, pitch, getHandle().random.nextLong());
}
private void playSound0(org.bukkit.entity.Entity entity, Holder<SoundEffect> soundEffectHolder, net.minecraft.sounds.SoundCategory categoryNMS, float volume, float pitch) {
@Override
public void playSound(org.bukkit.entity.Entity entity, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) {
if (!(entity instanceof CraftEntity craftEntity) || sound == null || category == null || getHandle().connection == null) return;
playSound0(entity, CraftSound.bukkitToMinecraftHolder(sound), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch, seed);
}
@Override
public void playSound(org.bukkit.entity.Entity entity, String sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) {
if (!(entity instanceof CraftEntity craftEntity) || sound == null || category == null || getHandle().connection == null) return;
playSound0(entity, Holder.direct(SoundEffect.createVariableRangeEvent(new MinecraftKey(sound))), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch, seed);
}
private void playSound0(org.bukkit.entity.Entity entity, Holder<SoundEffect> soundEffectHolder, net.minecraft.sounds.SoundCategory categoryNMS, float volume, float pitch, long seed) {
Preconditions.checkArgument(entity != null, "Entity cannot be null");
Preconditions.checkArgument(soundEffectHolder != null, "Holder of SoundEffect cannot be null");
Preconditions.checkArgument(categoryNMS != null, "SoundCategory cannot be null");
@@ -526,7 +528,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
if (getHandle().connection == null) return;
if (!(entity instanceof CraftEntity craftEntity)) return;
PacketPlayOutEntitySound packet = new PacketPlayOutEntitySound(soundEffectHolder, categoryNMS, craftEntity.getHandle(), volume, pitch, getHandle().getRandom().nextLong());
PacketPlayOutEntitySound packet = new PacketPlayOutEntitySound(soundEffectHolder, categoryNMS, craftEntity.getHandle(), volume, pitch, seed);
getHandle().connection.send(packet);
}