Catch async usage of playsound (#10021)

This commit is contained in:
Owen 2023-12-26 15:00:21 -05:00 committed by GitHub
parent f483b38596
commit dc621507f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 88 additions and 45 deletions

View file

@ -3309,7 +3309,7 @@ index 23bdb77690ba15bcbbfb0c70af23336d08ac7752..8f144a357174bbe096ac9b38a5e67a61
}
collection = icons;
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 0a39f95c77f0a2015200bb95b17eee9cbe95c416..67e90e18793852baf6dbd1aa41351a6943ef3d07 100644
index 0a39f95c77f0a2015200bb95b17eee9cbe95c416..4e155510337d73cd65f722c1360d87d14312b874 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -608,8 +608,10 @@ public final class CraftServer implements Server {
@ -3439,7 +3439,7 @@ index 0a39f95c77f0a2015200bb95b17eee9cbe95c416..67e90e18793852baf6dbd1aa41351a69
@Override
public String getMotd() {
return this.console.getMotd();
@@ -2534,4 +2591,53 @@ public final class CraftServer implements Server {
@@ -2534,4 +2591,57 @@ public final class CraftServer implements Server {
return this.spigot;
}
// Spigot end
@ -3447,6 +3447,7 @@ index 0a39f95c77f0a2015200bb95b17eee9cbe95c416..67e90e18793852baf6dbd1aa41351a69
+ // 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 0a39f95c77f0a2015200bb95b17eee9cbe95c416..67e90e18793852baf6dbd1aa41351a69
+
+ @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 {
@ -3494,7 +3498,7 @@ index 0a39f95c77f0a2015200bb95b17eee9cbe95c416..67e90e18793852baf6dbd1aa41351a69
+ // Paper end
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index 8bc43dde03f461d9f7470c521f47e959d07cde67..1ae3d9fc4ed6bba1881079b86b965e2f51bcb5e2 100644
index 8bc43dde03f461d9f7470c521f47e959d07cde67..d98020ea7f56418fdab03c7e7772ce062672b728 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -155,6 +155,7 @@ public class CraftWorld extends CraftRegionAccessor implements World {
@ -3505,13 +3509,14 @@ index 8bc43dde03f461d9f7470c521f47e959d07cde67..1ae3d9fc4ed6bba1881079b86b965e2f
private static final Random rand = new Random();
@@ -1646,6 +1647,39 @@ public class CraftWorld extends CraftRegionAccessor implements World {
@@ -1646,6 +1647,42 @@ public class CraftWorld extends CraftRegionAccessor implements World {
entityTracker.broadcastAndSend(packet);
}
}
+ // 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 8bc43dde03f461d9f7470c521f47e959d07cde67..1ae3d9fc4ed6bba1881079b86b965e2f
+
+ @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()) {
@ -3545,7 +3552,7 @@ index 8bc43dde03f461d9f7470c521f47e959d07cde67..1ae3d9fc4ed6bba1881079b86b965e2f
private static Map<String, GameRules.Key<?>> gamerules;
public static synchronized Map<String, GameRules.Key<?>> getGameRulesNMS() {
@@ -2054,5 +2088,18 @@ public class CraftWorld extends CraftRegionAccessor implements World {
@@ -2054,5 +2091,18 @@ public class CraftWorld extends CraftRegionAccessor implements World {
return ret;
}