Fix certain sounds not correctly playing

Fixes #3463
This commit is contained in:
Camotoy 2022-12-21 00:35:03 -05:00
parent 3b5984117d
commit 98069cff83
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F

View file

@ -25,6 +25,7 @@
package org.geysermc.geyser.util; package org.geysermc.geyser.util;
import com.github.steveice10.mc.protocol.data.game.level.sound.BuiltinSound;
import com.github.steveice10.mc.protocol.data.game.level.sound.Sound; import com.github.steveice10.mc.protocol.data.game.level.sound.Sound;
import com.nukkitx.math.vector.Vector3f; import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.LevelEventType; import com.nukkitx.protocol.bedrock.data.LevelEventType;
@ -65,10 +66,7 @@ public final class SoundUtils {
* @return a Bedrock sound * @return a Bedrock sound
*/ */
public static String translatePlaySound(String javaIdentifier) { public static String translatePlaySound(String javaIdentifier) {
// Drop the Minecraft namespace if applicable javaIdentifier = trim(javaIdentifier);
if (javaIdentifier.startsWith("minecraft:")) {
javaIdentifier = javaIdentifier.substring("minecraft:".length());
}
SoundMapping soundMapping = Registries.SOUNDS.get(javaIdentifier); SoundMapping soundMapping = Registries.SOUNDS.get(javaIdentifier);
if (soundMapping == null || soundMapping.getPlaysound() == null) { if (soundMapping == null || soundMapping.getPlaysound() == null) {
@ -79,6 +77,23 @@ public final class SoundUtils {
return soundMapping.getPlaysound(); return soundMapping.getPlaysound();
} }
private static String trim(String identifier) {
// Drop the Minecraft namespace if applicable
if (identifier.startsWith("minecraft:")) {
return identifier.substring("minecraft:".length());
}
return identifier;
}
private static void playSound(GeyserSession session, String bedrockName, Vector3f position, float volume, float pitch) {
PlaySoundPacket playSoundPacket = new PlaySoundPacket();
playSoundPacket.setSound(bedrockName);
playSoundPacket.setPosition(position);
playSoundPacket.setVolume(volume);
playSoundPacket.setPitch(pitch);
session.sendUpstreamPacket(playSoundPacket);
}
/** /**
* Translates and plays a Java Builtin Sound for a Bedrock client * Translates and plays a Java Builtin Sound for a Bedrock client
* *
@ -88,22 +103,24 @@ public final class SoundUtils {
* @param pitch the pitch * @param pitch the pitch
*/ */
public static void playSound(GeyserSession session, Sound javaSound, Vector3f position, float volume, float pitch) { public static void playSound(GeyserSession session, Sound javaSound, Vector3f position, float volume, float pitch) {
String packetSound = javaSound.getName(); String packetSound;
if (!(javaSound instanceof BuiltinSound)) {
// Identifier needs trimmed probably.
packetSound = translatePlaySound(javaSound.getName());
} else {
packetSound = javaSound.getName();
}
SoundMapping soundMapping = Registries.SOUNDS.get(packetSound); SoundMapping soundMapping = Registries.SOUNDS.get(packetSound);
if (soundMapping == null) { if (soundMapping == null) {
session.getGeyser().getLogger().debug("[Builtin] Sound mapping for " + packetSound + " not found"); session.getGeyser().getLogger().debug("[Builtin] Sound mapping for " + packetSound + " not found; assuming custom.");
playSound(session, packetSound, position, volume, pitch);
return; return;
} }
if (soundMapping.getPlaysound() != null) { if (soundMapping.getPlaysound() != null) {
// We always prefer the PlaySound mapping because we can control volume and pitch // We always prefer the PlaySound mapping because we can control volume and pitch
PlaySoundPacket playSoundPacket = new PlaySoundPacket(); playSound(session, soundMapping.getPlaysound(), position, volume, pitch);
playSoundPacket.setSound(soundMapping.getPlaysound());
playSoundPacket.setPosition(position);
playSoundPacket.setVolume(volume);
playSoundPacket.setPitch(pitch);
session.sendUpstreamPacket(playSoundPacket);
return; return;
} }