From fab261ae0bcbd0b5204170c750bbe39dcd6b9442 Mon Sep 17 00:00:00 2001 From: Owen1212055 <23108066+Owen1212055@users.noreply.github.com> Date: Tue, 26 Dec 2023 15:00:21 -0500 Subject: [PATCH] Catch async usage of playsound (#10021) --- patches/server/Adventure.patch | 7 ++++ .../Improve-and-expand-AsyncCatcher.patch | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/patches/server/Adventure.patch b/patches/server/Adventure.patch index 5a0e291445..f7ef5adca7 100644 --- a/patches/server/Adventure.patch +++ b/patches/server/Adventure.patch @@ -3447,6 +3447,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + // Paper start - adventure sounds + @Override + public void playSound(final net.kyori.adventure.sound.Sound sound) { ++ if (sound.seed().isEmpty()) org.spigotmc.AsyncCatcher.catchOp("play sound; cannot generate seed with world random"); // Paper + final long seed = sound.seed().orElseGet(this.console.overworld().getRandom()::nextLong); + for (ServerPlayer player : this.playerList.getPlayers()) { + player.connection.send(io.papermc.paper.adventure.PaperAdventure.asSoundPacket(sound, player.getX(), player.getY(), player.getZ(), seed, null)); @@ -3455,17 +3456,20 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + @Override + public void playSound(final net.kyori.adventure.sound.Sound sound, final double x, final double y, final double z) { ++ org.spigotmc.AsyncCatcher.catchOp("play sound"); // Paper + io.papermc.paper.adventure.PaperAdventure.asSoundPacket(sound, x, y, z, sound.seed().orElseGet(this.console.overworld().getRandom()::nextLong), this.playSound0(x, y, z, this.console.getAllLevels())); + } + + @Override + public void playSound(final net.kyori.adventure.sound.Sound sound, final net.kyori.adventure.sound.Sound.Emitter emitter) { ++ if (sound.seed().isEmpty()) org.spigotmc.AsyncCatcher.catchOp("play sound; cannot generate seed with world random"); // Paper + final long seed = sound.seed().orElseGet(this.console.overworld().getRandom()::nextLong); + if (emitter == net.kyori.adventure.sound.Sound.Emitter.self()) { + for (ServerPlayer player : this.playerList.getPlayers()) { + player.connection.send(io.papermc.paper.adventure.PaperAdventure.asSoundPacket(sound, player, seed, null)); + } + } else if (emitter instanceof org.bukkit.craftbukkit.entity.CraftEntity craftEntity) { ++ org.spigotmc.AsyncCatcher.catchOp("play sound; cannot use entity emitter"); // Paper + final net.minecraft.world.entity.Entity entity = craftEntity.getHandle(); + io.papermc.paper.adventure.PaperAdventure.asSoundPacket(sound, entity, seed, this.playSound0(entity.getX(), entity.getY(), entity.getZ(), List.of((ServerLevel) entity.level()))); + } else { @@ -3512,6 +3516,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + // Paper start - Adventure + @Override + public void playSound(final net.kyori.adventure.sound.Sound sound) { ++ org.spigotmc.AsyncCatcher.catchOp("play sound"); // Paper + final long seed = sound.seed().orElseGet(this.world.getRandom()::nextLong); + for (ServerPlayer player : this.getHandle().players()) { + player.connection.send(io.papermc.paper.adventure.PaperAdventure.asSoundPacket(sound, player.getX(), player.getY(), player.getZ(), seed, null)); @@ -3520,11 +3525,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + @Override + public void playSound(final net.kyori.adventure.sound.Sound sound, final double x, final double y, final double z) { ++ org.spigotmc.AsyncCatcher.catchOp("play sound"); // Paper + io.papermc.paper.adventure.PaperAdventure.asSoundPacket(sound, x, y, z, sound.seed().orElseGet(this.world.getRandom()::nextLong), this.playSound0(x, y, z)); + } + + @Override + public void playSound(final net.kyori.adventure.sound.Sound sound, final net.kyori.adventure.sound.Sound.Emitter emitter) { ++ org.spigotmc.AsyncCatcher.catchOp("play sound"); // Paper + final long seed = sound.seed().orElseGet(this.getHandle().getRandom()::nextLong); + if (emitter == net.kyori.adventure.sound.Sound.Emitter.self()) { + for (ServerPlayer player : this.getHandle().players()) { diff --git a/patches/server/Improve-and-expand-AsyncCatcher.patch b/patches/server/Improve-and-expand-AsyncCatcher.patch index c3f10784bb..e8c3112bdb 100644 --- a/patches/server/Improve-and-expand-AsyncCatcher.patch +++ b/patches/server/Improve-and-expand-AsyncCatcher.patch @@ -165,6 +165,42 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 if (!this.currentSection.remove(this.entity)) { PersistentEntitySectionManager.LOGGER.warn("Entity {} wasn't found in section {} (destroying due to {})", new Object[]{this.entity, SectionPos.of(this.currentSectionKey), reason}); } +diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java ++++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +@@ -0,0 +0,0 @@ public class CraftWorld extends CraftRegionAccessor implements World { + + @Override + public void playSound(Location loc, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) { ++ org.spigotmc.AsyncCatcher.catchOp("play sound"); // Paper + if (loc == null || sound == null || category == null) return; + + double x = loc.getX(); +@@ -0,0 +0,0 @@ public class CraftWorld extends CraftRegionAccessor implements World { + + @Override + public void playSound(Location loc, String sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) { ++ org.spigotmc.AsyncCatcher.catchOp("play sound"); // Paper + if (loc == null || sound == null || category == null) return; + + double x = loc.getX(); +@@ -0,0 +0,0 @@ public class CraftWorld extends CraftRegionAccessor implements World { + + @Override + public void playSound(Entity entity, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) { ++ org.spigotmc.AsyncCatcher.catchOp("play sound"); // Paper + if (!(entity instanceof CraftEntity craftEntity) || entity.getWorld() != this || sound == null || category == null) return; + + ClientboundSoundEntityPacket packet = new ClientboundSoundEntityPacket(CraftSound.bukkitToMinecraftHolder(sound), net.minecraft.sounds.SoundSource.valueOf(category.name()), craftEntity.getHandle(), volume, pitch, seed); +@@ -0,0 +0,0 @@ public class CraftWorld extends CraftRegionAccessor implements World { + + @Override + public void playSound(Entity entity, String sound, org.bukkit.SoundCategory category, float volume, float pitch, long seed) { ++ org.spigotmc.AsyncCatcher.catchOp("play sound"); // Paper + if (!(entity instanceof CraftEntity craftEntity) || entity.getWorld() != this || sound == null || category == null) return; + + ClientboundSoundEntityPacket packet = new ClientboundSoundEntityPacket(Holder.direct(SoundEvent.createVariableRangeEvent(new ResourceLocation(sound))), net.minecraft.sounds.SoundSource.valueOf(category.name()), craftEntity.getHandle(), volume, pitch, seed); diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java