Rewrite the code, now functioning correctly with the runtime id

This commit is contained in:
Ethan 2024-07-05 18:09:20 +08:00
parent 33b2fd615a
commit 29fa9fb94d
12 changed files with 61 additions and 126 deletions

View file

@ -100,8 +100,8 @@ public class JavaBlockUpdateTranslator extends PacketTranslator<ClientboundBlock
|| lastInteractPos.getZ() != packet.getEntry().getPosition().getZ())) {
return;
}
String identifier = BlockState.of(packet.getEntry().getBlock()).toString(); // This will be yeeted soon. Thanks Chris.
BlockState state = BlockState.of(packet.getEntry().getBlock()); // This will be yeeted soon. Thanks Chris.
session.setInteracting(false);
BlockSoundInteractionTranslator.handleBlockInteraction(session, lastInteractPos.toFloat(), identifier);
BlockSoundInteractionTranslator.handleBlockInteraction(session, lastInteractPos.toFloat(), state);
}
}

View file

@ -27,6 +27,7 @@ package org.geysermc.geyser.translator.sound;
import org.cloudburstmc.math.vector.Vector3f;
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.registry.Registries;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
@ -37,17 +38,16 @@ import java.util.Map;
/**
* Sound interaction handler for when a block is right-clicked.
*/
public interface BlockSoundInteractionTranslator extends SoundInteractionTranslator<String> {
public interface BlockSoundInteractionTranslator extends SoundInteractionTranslator<BlockState> {
/**
* Handles the block interaction when a player
* right-clicks a block.
*
* @param session the session interacting with the block
* @param position the position of the block
* @param identifier the identifier of the block
* @param state the state of the block
*/
static void handleBlockInteraction(GeyserSession session, Vector3f position, String identifier) {
static void handleBlockInteraction(GeyserSession session, Vector3f position, BlockState state) {
// If we need to get the hand identifier, only get it once and save it to a variable
String handIdentifier = null;
@ -58,7 +58,7 @@ public interface BlockSoundInteractionTranslator extends SoundInteractionTransla
if (interactionEntry.getKey().blocks().length != 0) {
boolean contains = false;
for (String blockIdentifier : interactionEntry.getKey().blocks()) {
if (identifier.contains(blockIdentifier)) {
if (state.toString().contains(blockIdentifier)) {
contains = true;
break;
}
@ -87,7 +87,7 @@ public interface BlockSoundInteractionTranslator extends SoundInteractionTransla
continue;
}
}
((BlockSoundInteractionTranslator) interactionEntry.getValue()).translate(session, position, identifier);
((BlockSoundInteractionTranslator) interactionEntry.getValue()).translate(session, position, state);
}
}

View file

@ -29,6 +29,7 @@ import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.SoundEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.sound.BlockSoundInteractionTranslator;
import org.geysermc.geyser.translator.sound.SoundTranslator;
@ -37,7 +38,8 @@ import org.geysermc.geyser.translator.sound.SoundTranslator;
public class BucketSoundInteractionTranslator implements BlockSoundInteractionTranslator {
@Override
public void translate(GeyserSession session, Vector3f position, String identifier) {
public void translate(GeyserSession session, Vector3f position, BlockState state) {
String identifier = state.toString();
if (!session.isPlacedBucket()) {
return; // No bucket was really interacted with
}

View file

@ -28,6 +28,7 @@ package org.geysermc.geyser.translator.sound.block;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.LevelEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelEventPacket;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.sound.BlockSoundInteractionTranslator;
import org.geysermc.geyser.translator.sound.SoundTranslator;
@ -36,7 +37,8 @@ import org.geysermc.geyser.translator.sound.SoundTranslator;
public class ComparatorSoundInteractionTranslator implements BlockSoundInteractionTranslator {
@Override
public void translate(GeyserSession session, Vector3f position, String identifier) {
public void translate(GeyserSession session, Vector3f position, BlockState state) {
String identifier = state.toString();
boolean powered = identifier.contains("mode=compare");
LevelEventPacket levelEventPacket = new LevelEventPacket();
levelEventPacket.setPosition(position);

View file

@ -1,65 +0,0 @@
/*
* 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) {
boolean open = identifier.contains("open=true");
String materialIdentifier = getMaterialIdentifier(identifier);
float volume = 1.0f;
float pitch = 1.0f;
// Sounds are quieter for wooden and bamboo fence gates.
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

@ -28,6 +28,7 @@ package org.geysermc.geyser.translator.sound.block;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.SoundEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.sound.BlockSoundInteractionTranslator;
import org.geysermc.geyser.translator.sound.SoundTranslator;
@ -36,7 +37,7 @@ import org.geysermc.geyser.translator.sound.SoundTranslator;
public class FlintAndSteelInteractionTranslator implements BlockSoundInteractionTranslator {
@Override
public void translate(GeyserSession session, Vector3f position, String identifier) {
public void translate(GeyserSession session, Vector3f position, BlockState state) {
LevelSoundEventPacket levelSoundEventPacket = new LevelSoundEventPacket();
levelSoundEventPacket.setPosition(position);
levelSoundEventPacket.setBabySound(false);

View file

@ -28,6 +28,7 @@ package org.geysermc.geyser.translator.sound.block;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.SoundEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.sound.BlockSoundInteractionTranslator;
@ -37,7 +38,8 @@ import org.geysermc.geyser.translator.sound.SoundTranslator;
public class GrassPathInteractionTranslator implements BlockSoundInteractionTranslator {
@Override
public void translate(GeyserSession session, Vector3f position, String identifier) {
public void translate(GeyserSession session, Vector3f position, BlockState state) {
String identifier = state.toString();
LevelSoundEventPacket levelSoundEventPacket = new LevelSoundEventPacket();
levelSoundEventPacket.setPosition(position);
levelSoundEventPacket.setBabySound(false);

View file

@ -28,6 +28,7 @@ package org.geysermc.geyser.translator.sound.block;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.SoundEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.sound.BlockSoundInteractionTranslator;
@ -37,7 +38,8 @@ import org.geysermc.geyser.translator.sound.SoundTranslator;
public class HoeInteractionTranslator implements BlockSoundInteractionTranslator {
@Override
public void translate(GeyserSession session, Vector3f position, String identifier) {
public void translate(GeyserSession session, Vector3f position, BlockState state) {
String identifier = state.toString();
LevelSoundEventPacket levelSoundEventPacket = new LevelSoundEventPacket();
levelSoundEventPacket.setPosition(position);
levelSoundEventPacket.setBabySound(false);

View file

@ -28,6 +28,7 @@ package org.geysermc.geyser.translator.sound.block;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.LevelEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelEventPacket;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.sound.BlockSoundInteractionTranslator;
import org.geysermc.geyser.translator.sound.SoundTranslator;
@ -36,7 +37,8 @@ import org.geysermc.geyser.translator.sound.SoundTranslator;
public class LeverSoundInteractionTranslator implements BlockSoundInteractionTranslator {
@Override
public void translate(GeyserSession session, Vector3f position, String identifier) {
public void translate(GeyserSession session, Vector3f position, BlockState state) {
String identifier = state.toString();
boolean powered = identifier.contains("powered=true");
LevelEventPacket levelEventPacket = new LevelEventPacket();
levelEventPacket.setPosition(position);

View file

@ -26,40 +26,40 @@
package org.geysermc.geyser.translator.sound.block;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.SoundEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.geyser.level.block.type.BlockState;
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 = "door")
public class DoorSoundInteractionTranslator implements BlockSoundInteractionTranslator {
@SoundTranslator(blocks = {"door", "fence_gate"})
public class OpenableSoundInteractionTranslator implements BlockSoundInteractionTranslator {
@Override
public void translate(GeyserSession session, Vector3f position, String identifier) {
public void translate(GeyserSession session, Vector3f position, BlockState state) {
String identifier = state.toString();
if (identifier.contains("iron")) return;
boolean open = identifier.contains("open=true");
boolean trapdoor = identifier.contains("_trapdoor");
String materialIdentifier = getMaterialIdentifier(identifier);
float volume = 1.0f;
// 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);
SoundEvent event = getSound(identifier.contains("open=true"), identifier);
LevelSoundEventPacket levelSoundEventPacket = new LevelSoundEventPacket();
levelSoundEventPacket.setPosition(position.add(0.5, 0.5, 0.5));
levelSoundEventPacket.setBabySound(false);
levelSoundEventPacket.setRelativeVolumeDisabled(false);
levelSoundEventPacket.setIdentifier(":");
levelSoundEventPacket.setSound(event);
levelSoundEventPacket.setExtraData(session.getBlockMappings().getBedrockBlock(state).getRuntimeId());
session.sendUpstreamPacket(levelSoundEventPacket);
}
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";
private SoundEvent getSound(boolean open, String identifier) {
if (identifier.contains("_door")) {
return open ? SoundEvent.DOOR_OPEN : SoundEvent.DOOR_CLOSE;
}
else if (identifier.contains("_trapdoor")){
return open ? SoundEvent.TRAPDOOR_OPEN : SoundEvent.TRAPDOOR_CLOSE;
}
else { // Fence Gate
return open ? SoundEvent.FENCE_GATE_OPEN : SoundEvent.FENCE_GATE_CLOSE;
}
return type;
}
}

View file

@ -84,9 +84,9 @@ public final class SoundUtils {
return identifier;
}
private static void sendPlaySoundPacket(GeyserSession session, String bedrockIdentifier, Vector3f position, float volume, float pitch) {
private static void playSound(GeyserSession session, String bedrockName, Vector3f position, float volume, float pitch) {
PlaySoundPacket playSoundPacket = new PlaySoundPacket();
playSoundPacket.setSound(bedrockIdentifier);
playSoundPacket.setSound(bedrockName);
playSoundPacket.setPosition(position);
playSoundPacket.setVolume(volume);
playSoundPacket.setPitch(pitch);
@ -96,35 +96,24 @@ public final class SoundUtils {
/**
* Translates and plays a Java Builtin Sound for a Bedrock client
*
* @param session the Bedrock client session.
* @param session the Bedrock client session.
* @param javaSound the builtin sound to play
* @param position the position
* @param pitch the pitch
* @param position the position
* @param pitch the pitch
*/
public static void playSound(GeyserSession session, Sound javaSound, Vector3f position, float volume, float pitch) {
playSound(session, javaSound.getName(), position, volume, pitch);
}
String soundIdentifier = removeMinecraftNamespace(javaSound.getName());
/**
* 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);
if (soundMapping == null) {
session.getGeyser().getLogger().debug("[Builtin] Sound mapping for " + soundIdentifier + " not found; assuming custom.");
sendPlaySoundPacket(session, soundIdentifier, position, volume, pitch);
playSound(session, soundIdentifier, position, volume, pitch);
return;
}
if (soundMapping.getPlaysound() != null) {
// We always prefer the PlaySound mapping because we can control volume and pitch
sendPlaySoundPacket(session, soundMapping.getPlaysound(), position, volume, pitch);
playSound(session, soundMapping.getPlaysound(), position, volume, pitch);
return;
}
@ -144,7 +133,7 @@ public final class SoundUtils {
}
if (sound == null) {
session.getGeyser().getLogger().debug("[Builtin] Sound for original '" + soundIdentifier + "' to mappings '" + soundMapping.getBedrock()
+ "' was not a playable level sound, or has yet to be mapped to an enum in SoundEvent.");
+ "' was not a playable level sound, or has yet to be mapped to an enum in SoundEvent.");
return;
}

@ -1 +1 @@
Subproject commit 014914f30b9838e69330abe25f108db5744d6161
Subproject commit 0812e95816bc2959e46d26a755928c5ddb7585c3