Initial Commit

This commit is contained in:
Ethan 2024-07-02 17:49:54 +08:00
parent 55e90b6f57
commit 7851210264
4 changed files with 115 additions and 19 deletions

View file

@ -26,22 +26,40 @@
package org.geysermc.geyser.translator.sound.block; package org.geysermc.geyser.translator.sound.block;
import org.cloudburstmc.math.vector.Vector3f; import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.LevelEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelEventPacket;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.sound.BlockSoundInteractionTranslator; import org.geysermc.geyser.translator.sound.BlockSoundInteractionTranslator;
import org.geysermc.geyser.translator.sound.SoundTranslator; import org.geysermc.geyser.translator.sound.SoundTranslator;
import org.geysermc.geyser.util.SoundUtils;
@SoundTranslator(blocks = {"door", "fence_gate"}) @SoundTranslator(blocks = {"door"})
public class DoorSoundInteractionTranslator implements BlockSoundInteractionTranslator { public class DoorSoundInteractionTranslator implements BlockSoundInteractionTranslator {
@Override @Override
public void translate(GeyserSession session, Vector3f position, String identifier) { public void translate(GeyserSession session, Vector3f position, String identifier) {
if (identifier.contains("iron")) return; if (identifier.contains("iron")) return;
LevelEventPacket levelEventPacket = new LevelEventPacket(); boolean open = identifier.contains("open=true");
levelEventPacket.setType(LevelEvent.SOUND_DOOR_OPEN); boolean trapdoor = identifier.contains("_trapdoor");
levelEventPacket.setPosition(position); String materialIdentifier = getMaterialIdentifier(identifier);
levelEventPacket.setData(0); float volume = 1.0f;
session.sendUpstreamPacket(levelEventPacket); // Sounds are quieter for wooden trapdoors and bamboo wood doors
if ((trapdoor && materialIdentifier.equals("block.wooden")) || (!trapdoor && materialIdentifier.equals("block.bamboo_wood"))) {
volume = 0.9f;
}
String doorType = trapdoor ? "_trapdoor" : "_door";
String status = open ? ".open" : ".close";
SoundUtils.playSound(session, "minecraft:" + materialIdentifier + doorType + status, position, volume, 1.0f);
}
private static String getMaterialIdentifier(String identifier) {
String type = "block.wooden";
if (identifier.contains("copper_")) {
type = "block.copper";
} else if (identifier.contains("bamboo_")) {
type = "block.bamboo_wood";
} else if (identifier.contains("cherry_")) {
type = "block.cherry_wood";
} else if (identifier.contains("crimson_") || identifier.contains("warped_")) {
type = "block.nether_wood";
}
return type;
} }
} }

View file

@ -0,0 +1,67 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.translator.sound.block;
import org.cloudburstmc.math.vector.Vector3f;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.sound.BlockSoundInteractionTranslator;
import org.geysermc.geyser.translator.sound.SoundTranslator;
import org.geysermc.geyser.util.SoundUtils;
@SoundTranslator(blocks = {"fence_gate"})
public class FenceGateSoundInteractionTranslator implements BlockSoundInteractionTranslator {
@Override
public void translate(GeyserSession session, Vector3f position, String identifier) {
if (identifier.contains("iron")) return;
GeyserImpl.getInstance().getLogger().info(identifier);
boolean open = identifier.contains("open=true");
String materialIdentifier = getMaterialIdentifier(identifier);
float volume = 1.0f;
float pitch = 1.0f;
// Sounds are quieter for wooden trapdoors and bamboo wood doors
if (materialIdentifier.equals("block.") || materialIdentifier.equals("block.bamboo_wood_")) {
volume = 0.9f;
}
if (materialIdentifier.equals("block.bamboo_wood_")) {
pitch = 1.1f;
}
String status = open ? "fence_gate.open" : "fence_gate.close";
SoundUtils.playSound(session, "minecraft:" + materialIdentifier + status, position, volume, pitch);
}
private static String getMaterialIdentifier(String identifier) {
String type = "block.";
if (identifier.contains("bamboo_")) {
type = "block.bamboo_wood_";
} else if (identifier.contains("cherry_")) {
type = "block.cherry_wood_";
} else if (identifier.contains("crimson_") || identifier.contains("warped_")) {
type = "block.nether_wood_";
}
return type;
}
}

View file

@ -84,9 +84,9 @@ public final class SoundUtils {
return identifier; return identifier;
} }
private static void playSound(GeyserSession session, String bedrockName, Vector3f position, float volume, float pitch) { private static void sendPlaySoundPacket(GeyserSession session, String bedrockIdentifier, Vector3f position, float volume, float pitch) {
PlaySoundPacket playSoundPacket = new PlaySoundPacket(); PlaySoundPacket playSoundPacket = new PlaySoundPacket();
playSoundPacket.setSound(bedrockName); playSoundPacket.setSound(bedrockIdentifier);
playSoundPacket.setPosition(position); playSoundPacket.setPosition(position);
playSoundPacket.setVolume(volume); playSoundPacket.setVolume(volume);
playSoundPacket.setPitch(pitch); playSoundPacket.setPitch(pitch);
@ -102,18 +102,29 @@ 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 soundIdentifier = removeMinecraftNamespace(javaSound.getName()); playSound(session, javaSound.getName(), position, volume, pitch);
}
/**
* Translates and plays a Java Builtin Sound by identifier for a Bedrock client
*
* @param session the Bedrock client session.
* @param soundIdentifier the sound identifier to play
* @param position the position
* @param pitch the pitch
*/
public static void playSound(GeyserSession session, String soundIdentifier, Vector3f position, float volume, float pitch) {
soundIdentifier = removeMinecraftNamespace(soundIdentifier);
SoundMapping soundMapping = Registries.SOUNDS.get(soundIdentifier); SoundMapping soundMapping = Registries.SOUNDS.get(soundIdentifier);
if (soundMapping == null) { if (soundMapping == null) {
session.getGeyser().getLogger().debug("[Builtin] Sound mapping for " + soundIdentifier + " not found; assuming custom."); session.getGeyser().getLogger().debug("[Builtin] Sound mapping for " + soundIdentifier + " not found; assuming custom.");
playSound(session, soundIdentifier, position, volume, pitch); sendPlaySoundPacket(session, soundIdentifier, 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
playSound(session, soundMapping.getPlaysound(), position, volume, pitch); sendPlaySoundPacket(session, soundMapping.getPlaysound(), position, volume, pitch);
return; return;
} }

@ -1 +1 @@
Subproject commit 23cb22f9ceeb7f24b896a69a711944d7f3e756ed Subproject commit 014914f30b9838e69330abe25f108db5744d6161