Files
ParchmentMC/patches/server/0003-Add-SoundEvent.patch
2023-03-16 22:26:09 -04:00

262 lines
15 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: lexikiq <noellekiq@gmail.com>
Date: Sat, 19 Jun 2021 16:30:39 -0400
Subject: [PATCH] Add SoundEvent
diff --git a/src/main/java/gg/projecteden/parchment/event/sound/ParchmentSoundEvent.java b/src/main/java/gg/projecteden/parchment/event/sound/ParchmentSoundEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..44245b61f64e4e2eb21ac4f5e540aa9a583bfcfe
--- /dev/null
+++ b/src/main/java/gg/projecteden/parchment/event/sound/ParchmentSoundEvent.java
@@ -0,0 +1,28 @@
+package gg.projecteden.parchment.event.sound;
+
+import io.papermc.paper.adventure.PaperAdventure;
+import net.kyori.adventure.sound.Sound;
+import net.minecraft.sounds.SoundEvent;
+import net.minecraft.world.entity.Entity;
+import net.minecraft.world.level.Level;
+import org.bukkit.Location;
+
+import java.util.Optional;
+import java.util.function.Function;
+
+public class ParchmentSoundEvent {
+ public static final Function<Sound, Double> DISTANCE_FUNCTION = sound -> {
+ Optional<SoundEvent> soundEvent = PaperAdventure.asVanillaSound(sound.name());
+ if (soundEvent.isPresent())
+ return Double.valueOf(soundEvent.get().getRange(sound.volume()));
+ return gg.projecteden.parchment.event.sound.SoundEvent.DEFAULT_DISTANCE_FUNCTION.apply(sound);
+ };
+
+ public static gg.projecteden.parchment.event.sound.SoundEvent.Emitter createEmitter(Level level, double x, double y, double z) {
+ return new gg.projecteden.parchment.event.sound.SoundEvent.LocationEmitter(new Location(level.getWorld(), x, y, z));
+ }
+
+ public static gg.projecteden.parchment.event.sound.SoundEvent.Emitter createEmitter(Entity entity) {
+ return new gg.projecteden.parchment.event.sound.SoundEvent.EntityEmitter(entity.getBukkitEntity());
+ }
+}
diff --git a/src/main/java/io/papermc/paper/adventure/PaperAdventure.java b/src/main/java/io/papermc/paper/adventure/PaperAdventure.java
index 01e424792f68bac73ec41726031ebbb53df13da7..3a1968baf415d0fe6586c45749810878d3359c9e 100644
--- a/src/main/java/io/papermc/paper/adventure/PaperAdventure.java
+++ b/src/main/java/io/papermc/paper/adventure/PaperAdventure.java
@@ -325,6 +325,32 @@ public final class PaperAdventure {
return asVanilla(source);
}
+ // Parchment start
+ public static Sound.Source asAdventure(final SoundSource source) {
+ return switch (source) {
+ case MASTER -> Sound.Source.MASTER;
+ case MUSIC -> Sound.Source.MUSIC;
+ case RECORDS -> Sound.Source.RECORD;
+ case WEATHER -> Sound.Source.WEATHER;
+ case BLOCKS -> Sound.Source.BLOCK;
+ case HOSTILE -> Sound.Source.HOSTILE;
+ case NEUTRAL -> Sound.Source.NEUTRAL;
+ case PLAYERS -> Sound.Source.PLAYER;
+ case AMBIENT -> Sound.Source.AMBIENT;
+ case VOICE -> Sound.Source.VOICE;
+ };
+ }
+
+ public static java.util.Optional<net.minecraft.sounds.SoundEvent> asVanillaSound(final Key key) {
+ return net.minecraft.core.registries.BuiltInRegistries.SOUND_EVENT.getOptional(asVanilla(key));
+ }
+
+ @SuppressWarnings("PatternValidation")
+ public static Key asAdventure(final ResourceLocation key) {
+ return Key.key(key.getNamespace(), key.getPath());
+ }
+ // Parchment end
+
// NBT
public static @Nullable BinaryTagHolder asBinaryTagHolder(final @Nullable CompoundTag tag) {
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index 5a5ff40df37db9cbd53c584ed26a3ce4888b29c0..30c05db4ad0fcd5aa83cb3168ed32c7b0d205972 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -1718,6 +1718,43 @@ public class ServerLevel extends Level implements WorldGenLevel {
this.neighborUpdater.neighborChanged(state, pos, sourceBlock, sourcePos, notify);
}
+ public void playSeededSound(@org.jetbrains.annotations.Nullable Player except, double x, double y, double z, SoundEvent sound, SoundSource category, float volume, float pitch, long seed) {
+ // Parchment start - sound event
+ CraftEventFactory.playSoundEvent(new gg.projecteden.parchment.event.sound.SoundEvent(
+ except == null ? null : except.getBukkitEntity(),
+ net.kyori.adventure.sound.Sound.sound(
+ io.papermc.paper.adventure.PaperAdventure.asAdventure(sound.getLocation()),
+ io.papermc.paper.adventure.PaperAdventure.asAdventure(category),
+ volume,
+ pitch
+ ),
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.createEmitter(this, x, y, z),
+ seed,
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.DISTANCE_FUNCTION,
+ null
+ ));
+ // Parchment end
+ }
+
+ @Override
+ public void playSeededSound(@org.jetbrains.annotations.Nullable Player except, Entity entity, SoundEvent sound, SoundSource category, float volume, float pitch, long seed) {
+ // Parchment start - sound event
+ CraftEventFactory.playSoundEvent(new gg.projecteden.parchment.event.sound.SoundEvent(
+ except == null ? null : except.getBukkitEntity(),
+ net.kyori.adventure.sound.Sound.sound(
+ io.papermc.paper.adventure.PaperAdventure.asAdventure(sound.getLocation()),
+ io.papermc.paper.adventure.PaperAdventure.asAdventure(category),
+ volume,
+ pitch
+ ),
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.createEmitter(entity),
+ seed,
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.DISTANCE_FUNCTION,
+ null
+ ));
+ // Parchment end
+ }
+
@Override
public void broadcastEntityEvent(Entity entity, byte status) {
this.getChunkSource().broadcastAndSend(entity, new ClientboundEntityEventPacket(entity, status));
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
index ca5291a9573a62cb5c19539cf5c7aceff11f9829..a793532ae1786d9c8fc04d21f49c69b336fd8cfb 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -2259,7 +2259,19 @@ public class ServerPlayer extends Player {
@Override
public void playNotifySound(SoundEvent event, SoundSource category, float volume, float pitch) {
- this.connection.send(new ClientboundSoundPacket(BuiltInRegistries.SOUND_EVENT.wrapAsHolder(event), category, this.getX(), this.getY(), this.getZ(), volume, pitch, this.random.nextLong()));
+ // Parchment start - sound event
+ CraftEventFactory.playSoundEvent(new gg.projecteden.parchment.event.sound.SoundEvent(
+ null,
+ net.kyori.adventure.sound.Sound.sound(
+ io.papermc.paper.adventure.PaperAdventure.asAdventure(event.getLocation()),
+ io.papermc.paper.adventure.PaperAdventure.asAdventure(category),
+ volume,
+ pitch
+ ),
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.createEmitter(level, getX(), getY(), getZ()),
+ this.random.nextLong(), sound -> 0d, soundEvent -> java.util.Collections.singletonList(getBukkitEntity())
+ ));
+ // Parchment end
}
@Override
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
index c0c14766adaac855112f85a203a6163b8adfdded..d59d36c8a77524d5ac0c25a4ef1cf57f339eeda2 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -932,7 +932,7 @@ public abstract class PlayerList {
BlockState data = worldserver1.getBlockState(blockposition);
worldserver1.setBlock(blockposition, data.setValue(net.minecraft.world.level.block.RespawnAnchorBlock.CHARGE, data.getValue(net.minecraft.world.level.block.RespawnAnchorBlock.CHARGE) - 1), 3);
}
- entityplayer1.connection.send(new ClientboundSoundPacket(SoundEvents.RESPAWN_ANCHOR_DEPLETE, SoundSource.BLOCKS, (double) location.getX(), (double) location.getY(), (double) location.getZ(), 1.0F, 1.0F, worldserver1.getRandom().nextLong()));
+ entityplayer1.playNotifySound(SoundEvents.RESPAWN_ANCHOR_DEPLETE.value(), SoundSource.BLOCKS, 1.0F, 1.0F); // Parchment - use existing play sound method
// Paper end
}
// Added from changeDimension
diff --git a/src/main/java/net/minecraft/world/entity/player/Player.java b/src/main/java/net/minecraft/world/entity/player/Player.java
index 2b02800666b358159c8ecb63208a14855f90657b..8a59514b2c343c935403ef8f4805bfa3e7f89bcc 100644
--- a/src/main/java/net/minecraft/world/entity/player/Player.java
+++ b/src/main/java/net/minecraft/world/entity/player/Player.java
@@ -1885,7 +1885,21 @@ public abstract class Player extends LivingEntity {
private static void sendSoundEffect(Player fromEntity, double x, double y, double z, SoundEvent soundEffect, SoundSource soundCategory, float volume, float pitch) {
fromEntity.level.playSound(fromEntity, x, y, z, soundEffect, soundCategory, volume, pitch); // This will not send the effect to the entity himself
if (fromEntity instanceof ServerPlayer) {
- ((ServerPlayer) fromEntity).connection.send(new net.minecraft.network.protocol.game.ClientboundSoundPacket(net.minecraft.core.registries.BuiltInRegistries.SOUND_EVENT.wrapAsHolder(soundEffect), soundCategory, x, y, z, volume, pitch, fromEntity.random.nextLong()));
+ // Parchment start - sound event
+ CraftEventFactory.playSoundEvent(new gg.projecteden.parchment.event.sound.SoundEvent(
+ null,
+ net.kyori.adventure.sound.Sound.sound(
+ io.papermc.paper.adventure.PaperAdventure.asAdventure(soundEffect.getLocation()),
+ io.papermc.paper.adventure.PaperAdventure.asAdventure(soundCategory),
+ volume,
+ pitch
+ ),
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.createEmitter(fromEntity.level, x, y, z),
+ fromEntity.random.nextLong(),
+ sound -> 0d,
+ soundEvent -> java.util.Collections.singletonList(((ServerPlayer) fromEntity).getBukkitEntity())
+ ));
+ // Parchment end
}
}
// Paper end
diff --git a/src/main/java/net/minecraft/world/entity/raid/Raid.java b/src/main/java/net/minecraft/world/entity/raid/Raid.java
index 879c3bb661e24b9682b654def57c2800f4f8ca92..78c2067b9683611ea31cfe1c95755aa9e23e5982 100644
--- a/src/main/java/net/minecraft/world/entity/raid/Raid.java
+++ b/src/main/java/net/minecraft/world/entity/raid/Raid.java
@@ -538,7 +538,7 @@ public class Raid {
double d2 = vec3d.z + 13.0D / d0 * (vec3d1.z - vec3d.z);
if (d0 <= 64.0D || collection.contains(entityplayer)) {
- entityplayer.connection.send(new ClientboundSoundPacket(SoundEvents.RAID_HORN, SoundSource.NEUTRAL, d1, entityplayer.getY(), d2, 64.0F, 1.0F, i));
+ entityplayer.playNotifySound(SoundEvents.RAID_HORN.value(), SoundSource.NEUTRAL, 64.0F, 1.0F); // Parchment - use existing play sound method
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 89c07723a89a394a583f46179671b283dfcc8900..451088940d1db0f32c29d0fd192c97b50cf3c4c8 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -1943,4 +1943,53 @@ public class CraftEventFactory {
return event.callEvent();
}
// Paper end
+
+ // Parchment start
+ private static net.minecraft.network.protocol.@org.jetbrains.annotations.NotNull Packet<net.minecraft.network.protocol.game.ClientGamePacketListener> handleSoundEvent(gg.projecteden.parchment.event.sound.SoundEvent event, CraftPlayer recipient) {
+ // init variables
+ net.kyori.adventure.sound.Sound sound = event.calculateSound(recipient);
+ gg.projecteden.parchment.event.sound.SoundEvent.Emitter emitter = event.calculateEmitter(recipient);
+ net.minecraft.sounds.SoundSource source = io.papermc.paper.adventure.PaperAdventure.asVanilla(sound.source());
+ float volume = sound.volume();
+ float pitch = sound.pitch();
+ long seed = event.getSeed();
+ java.util.Optional<net.minecraft.sounds.SoundEvent> soundEvent = io.papermc.paper.adventure.PaperAdventure.asVanillaSound(sound.name());
+ // handle vanilla sounds
+ if (soundEvent.isPresent()) {
+ if (emitter instanceof gg.projecteden.parchment.event.sound.SoundEvent.EntityEmitter entityEmitter)
+ return new net.minecraft.network.protocol.game.ClientboundSoundEntityPacket(net.minecraft.core.Holder.direct(soundEvent.get()), source, ((CraftEntity) entityEmitter.entity()).getHandle(), volume, pitch, seed);
+ else if (emitter instanceof gg.projecteden.parchment.event.sound.SoundEvent.LocationEmitter locationEmitter) {
+ Location loc = locationEmitter.location();
+ return new net.minecraft.network.protocol.game.ClientboundSoundPacket(net.minecraft.core.Holder.direct(soundEvent.get()), source, loc.getX(), loc.getY(), loc.getZ(), volume, pitch, seed);
+ }
+ else
+ throw new IllegalArgumentException("Unknown emitter type: " + emitter.getClass().getName());
+ }
+ // handle custom sounds
+ else {
+ return null; // ClientboundCustomSoundPacket was removed and the standard packet now supports custom sounds
+// // warn if trying to use EntityEmitter with custom sound
+// if (emitter instanceof gg.projecteden.parchment.event.sound.SoundEvent.EntityEmitter)
+// org.slf4j.LoggerFactory.getLogger("SoundEvent").warn("Sound event is using a custom sound ({}) which cannot be used with EntityEmitter. Falling back to playing at the entity's location.", sound.name().asString());
+// Vec3 pos = org.bukkit.craftbukkit.util.CraftVector.toNMS(emitter.location().toVector());
+// return new net.minecraft.network.protocol.game.ClientboundCustomSoundPacket(io.papermc.paper.adventure.PaperAdventure.asVanilla(sound.name()), source, pos, volume, pitch, seed);
+ }
+ }
+
+ public static void playSoundEvent(gg.projecteden.parchment.event.sound.SoundEvent event) {
+ org.apache.commons.lang3.Validate.notNull(event, "event");
+ io.papermc.paper.util.MCUtil.asyncExecutor.execute(() -> {
+ if (!event.callEvent())
+ return;
+ try {
+ for (Player _player : event.calculateRecipients()) {
+ CraftPlayer player = (CraftPlayer) _player;
+ player.getHandle().connection.send(handleSoundEvent(event, player));
+ }
+ } catch (Throwable e) {
+ org.slf4j.LoggerFactory.getLogger("SoundEvent").error("Error playing sound event", e);
+ }
+ });
+ }
+ // Parchment end
}