266 lines
16 KiB
Diff
266 lines
16 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 f466bfdf5557c94ebee3ad609d9b6f18f86aefef..687907f2ad51dceb523994adabcf8025960aa336 100644
|
|
--- a/src/main/java/io/papermc/paper/adventure/PaperAdventure.java
|
|
+++ b/src/main/java/io/papermc/paper/adventure/PaperAdventure.java
|
|
@@ -390,6 +390,36 @@ 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 Optional<SoundEvent> asVanillaSound(final Key key) {
|
|
+ return BuiltInRegistries.SOUND_EVENT.getOptional(asVanilla(key));
|
|
+ }
|
|
+
|
|
+ public static Optional<Holder.Reference<SoundEvent>> asSoundHolder(final net.minecraft.resources.ResourceKey<SoundEvent> key) {
|
|
+ return BuiltInRegistries.SOUND_EVENT.get(key);
|
|
+ }
|
|
+
|
|
+ public static Optional<Holder.Reference<net.minecraft.sounds.SoundEvent>> asVanillaSoundHolder(final Key key) {
|
|
+ return asSoundHolder(net.minecraft.resources.ResourceKey.create(net.minecraft.core.registries.Registries.SOUND_EVENT, asVanilla(key)));
|
|
+ }
|
|
+
|
|
+ // Parchment end
|
|
+
|
|
public static Packet<?> asSoundPacket(final Sound sound, final double x, final double y, final double z, final long seed, @Nullable BiConsumer<Packet<?>, Float> packetConsumer) {
|
|
final ResourceLocation name = asVanilla(sound.name());
|
|
final Optional<SoundEvent> soundEvent = BuiltInRegistries.SOUND_EVENT.getOptional(name);
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
index 957cae6ddeba9efe3b55588567ae51e8b86b6a42..f6753ed14350b1c2dd7c69e2aed8657295863547 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
@@ -1693,12 +1693,46 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
|
|
|
|
@Override
|
|
public void playSeededSound(@Nullable Player source, double x, double y, double z, Holder<SoundEvent> sound, SoundSource category, float volume, float pitch, long seed) {
|
|
- this.server.getPlayerList().broadcast(source, x, y, z, (double) ((SoundEvent) sound.value()).getRange(volume), this.dimension(), new ClientboundSoundPacket(sound, category, x, y, z, volume, pitch, seed));
|
|
+ // Parchment start - sound event
|
|
+ CraftEventFactory.playSoundEvent(new gg.projecteden.parchment.event.sound.SoundEvent(
|
|
+ source == null ? null : source.getBukkitEntity(),
|
|
+ net.kyori.adventure.sound.Sound.sound()
|
|
+ .type(sound.unwrap().<net.kyori.adventure.key.Key>map(
|
|
+ key -> io.papermc.paper.adventure.PaperAdventure.asAdventure(key.location()),
|
|
+ soundEvent -> io.papermc.paper.adventure.PaperAdventure.asAdventure(soundEvent.location())
|
|
+ ))
|
|
+ .source(io.papermc.paper.adventure.PaperAdventure.asAdventure(category))
|
|
+ .volume(volume)
|
|
+ .pitch(pitch)
|
|
+ .seed(seed)
|
|
+ .build(),
|
|
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.createEmitter(this, x, y, z),
|
|
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.DISTANCE_FUNCTION,
|
|
+ null
|
|
+ ));
|
|
+ // Parchment end
|
|
}
|
|
|
|
@Override
|
|
public void playSeededSound(@Nullable Player source, Entity entity, Holder<SoundEvent> sound, SoundSource category, float volume, float pitch, long seed) {
|
|
- this.server.getPlayerList().broadcast(source, entity.getX(), entity.getY(), entity.getZ(), (double) ((SoundEvent) sound.value()).getRange(volume), this.dimension(), new ClientboundSoundEntityPacket(sound, category, entity, volume, pitch, seed));
|
|
+ // Parchment start - sound event
|
|
+ CraftEventFactory.playSoundEvent(new gg.projecteden.parchment.event.sound.SoundEvent(
|
|
+ source == null ? null : source.getBukkitEntity(),
|
|
+ net.kyori.adventure.sound.Sound.sound()
|
|
+ .type(sound.unwrap().<net.kyori.adventure.key.Key>map(
|
|
+ key -> io.papermc.paper.adventure.PaperAdventure.asAdventure(key.location()),
|
|
+ soundEvent -> io.papermc.paper.adventure.PaperAdventure.asAdventure(soundEvent.location())
|
|
+ ))
|
|
+ .source(io.papermc.paper.adventure.PaperAdventure.asAdventure(category))
|
|
+ .volume(volume)
|
|
+ .pitch(pitch)
|
|
+ .seed(seed)
|
|
+ .build(),
|
|
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.createEmitter(entity),
|
|
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.DISTANCE_FUNCTION,
|
|
+ null
|
|
+ ));
|
|
+ // Parchment end
|
|
}
|
|
|
|
@Override
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
index 2e8ecf3bbb9f9ceba6f896738fa1ab8e2bd0fed6..94de83a35998c82b807c6e5836d957f317dd2ceb 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
@@ -2805,7 +2805,20 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player imple
|
|
|
|
@Override
|
|
public void playNotifySound(SoundEvent sound, SoundSource category, float volume, float pitch) {
|
|
- this.connection.send(new ClientboundSoundPacket(BuiltInRegistries.SOUND_EVENT.wrapAsHolder(sound), 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()
|
|
+ .type(io.papermc.paper.adventure.PaperAdventure.asAdventure(sound.location()))
|
|
+ .source(io.papermc.paper.adventure.PaperAdventure.asAdventure(category))
|
|
+ .volume(volume)
|
|
+ .pitch(pitch)
|
|
+ .seed(this.random.nextLong())
|
|
+ .build(),
|
|
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.createEmitter(level(), getX(), getY(), getZ()),
|
|
+ _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 30de3d1a7792c38ae946f19cb0e14637919b5001..ef10ed017ad5d06c3f34f1287a8721a33fc61ce1 100644
|
|
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
|
|
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
|
|
@@ -876,6 +876,9 @@ public abstract class PlayerList {
|
|
isBedSpawn = true;
|
|
}
|
|
// Paper end - Add PlayerPostRespawnEvent
|
|
+ entityplayer1.playNotifySound(SoundEvents.RESPAWN_ANCHOR_DEPLETE.value(), SoundSource.BLOCKS, 1.0F, 1.0F); // Parchment - use existing play sound method
|
|
+ // Paper end - Fix SPIGOT-5989
|
|
+
|
|
}
|
|
// Added from changeDimension
|
|
this.sendAllPlayerInfo(entityplayer); // Update health, etc...
|
|
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 61d412c4f1ebd55661cc3f0260468e3ac0efe0bb..6b73ca4ca59e3080926155613ce10af82cd43615 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/player/Player.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/player/Player.java
|
|
@@ -1840,9 +1840,23 @@ public abstract class Player extends LivingEntity {
|
|
}
|
|
// Paper start - send while respecting visibility
|
|
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 itself
|
|
- if (fromEntity instanceof ServerPlayer serverPlayer) {
|
|
- serverPlayer.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()));
|
|
+ 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) {
|
|
+ // Parchment start - sound event
|
|
+ CraftEventFactory.playSoundEvent(new gg.projecteden.parchment.event.sound.SoundEvent(
|
|
+ null,
|
|
+ net.kyori.adventure.sound.Sound.sound()
|
|
+ .type(io.papermc.paper.adventure.PaperAdventure.asAdventure(soundEffect.location()))
|
|
+ .source(io.papermc.paper.adventure.PaperAdventure.asAdventure(soundCategory))
|
|
+ .volume(volume)
|
|
+ .pitch(pitch)
|
|
+ .seed(fromEntity.random.nextLong())
|
|
+ .build(),
|
|
+ gg.projecteden.parchment.event.sound.ParchmentSoundEvent.createEmitter(fromEntity.level(), x, y, z),
|
|
+ sound -> 0d,
|
|
+ soundEvent -> java.util.Collections.singletonList(((ServerPlayer) fromEntity).getBukkitEntity())
|
|
+ ));
|
|
+ // Parchment end
|
|
}
|
|
}
|
|
// Paper end - send while respecting visibility
|
|
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 11cf2d9def087b0898c828eaa21eb5f7b8811d5f..7c1332f6c2fbe615e2831911b6548310532b022e 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/raid/Raid.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/raid/Raid.java
|
|
@@ -552,7 +552,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 e37aaf77f94b97b736cc20ef070cefdff0400188..c01ea04bc72ea7342722a463a384b1cc664c7afb 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
|
|
@@ -2271,4 +2271,42 @@ public class CraftEventFactory {
|
|
return event;
|
|
}
|
|
// Paper end - add EntityFertilizeEggEvent
|
|
+
|
|
+ // 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) {
|
|
+ net.kyori.adventure.sound.Sound sound = event.calculateSound(recipient);
|
|
+ ResourceLocation name = io.papermc.paper.adventure.PaperAdventure.asVanilla(sound.name());
|
|
+ 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 = sound.seed().orElse(0L); // TODO: random source?
|
|
+ net.minecraft.resources.ResourceKey<net.minecraft.sounds.SoundEvent> soundKey = net.minecraft.resources.ResourceKey.create(net.minecraft.core.registries.Registries.SOUND_EVENT, name);
|
|
+ net.minecraft.core.Holder.Reference<net.minecraft.sounds.SoundEvent> soundEvent = io.papermc.paper.adventure.PaperAdventure.asVanillaSoundHolder(sound.name()) // TODO: calculate event distance?
|
|
+ .orElseGet(() -> net.minecraft.core.Holder.Reference.create(net.minecraft.core.registries.BuiltInRegistries.SOUND_EVENT, soundKey, net.minecraft.sounds.SoundEvent.createFixedRangeEvent(name, (float) event.calculateDistance())));
|
|
+ if (emitter instanceof gg.projecteden.parchment.event.sound.SoundEvent.EntityEmitter entityEmitter)
|
|
+ return new net.minecraft.network.protocol.game.ClientboundSoundEntityPacket(soundEvent, source, ((CraftEntity) entityEmitter.entity()).getHandle(), volume, pitch, seed);
|
|
+ else if (emitter instanceof gg.projecteden.parchment.event.sound.SoundEvent.LocationEmitter locationEmitter) {
|
|
+ org.bukkit.Location loc = locationEmitter.getLocation();
|
|
+ return new net.minecraft.network.protocol.game.ClientboundSoundPacket(soundEvent, source, loc.getX(), loc.getY(), loc.getZ(), volume, pitch, seed);
|
|
+ }
|
|
+ throw new IllegalArgumentException("Unknown emitter type: " + emitter.getClass().getName());
|
|
+ }
|
|
+
|
|
+ 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
|
|
}
|