Add packet translators for Play, Stop, and Builtin sounds

This commit is contained in:
Jason Ho 2020-03-28 16:50:50 -07:00
parent 03e11df58b
commit 3721cc084e
5 changed files with 270 additions and 0 deletions

View file

@ -0,0 +1,54 @@
package org.geysermc.connector.network.translators.java.entity.player;
import com.github.steveice10.mc.protocol.data.game.world.sound.BuiltinSound;
import com.github.steveice10.mc.protocol.data.game.world.sound.CustomSound;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerPlaySoundPacket;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.packet.*;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.sound.SoundMap;
public class JavaPlayerPlaySoundTranslator extends PacketTranslator<ServerPlaySoundPacket> {
public static double processCoordinate(double f) {
return (f / 3D) * 8D;
}
@Override
public void translate(ServerPlaySoundPacket packet, GeyserSession session) {
String packetSound;
if(packet.getSound() instanceof BuiltinSound) {
packetSound = ((BuiltinSound) packet.getSound()).getName();
} else if(packet.getSound() instanceof CustomSound) {
packetSound = ((CustomSound) packet.getSound()).getName();
} else {
session.getConnector().getLogger().debug("Unknown sound packet, we were unable to map this. " + packet.toString());
return;
}
SoundMap.SoundMapping soundMapping = SoundMap.get().fromJava(packetSound);
session.getConnector().getLogger()
.debug("[PlaySound] Sound mapping " + packetSound + " -> "
+ soundMapping + (soundMapping == null ? "[not found]" : "")
+ " - " + packet.toString());
String playsound;
if(soundMapping == null || soundMapping.getPlaysound() == null) {
// no mapping
session.getConnector().getLogger()
.debug("[PlaySound] Defaulting to sound server gave us.");
playsound = packetSound;
} else {
playsound = soundMapping.getPlaysound();
}
PlaySoundPacket playSoundPacket = new PlaySoundPacket();
playSoundPacket.setSound(playsound);
playSoundPacket.setPosition(Vector3f.from(processCoordinate(packet.getX()), processCoordinate(packet.getY()), processCoordinate(packet.getZ())));
playSoundPacket.setVolume(packet.getVolume());
playSoundPacket.setPitch(packet.getPitch());
session.getUpstream().sendPacket(playSoundPacket);
session.getConnector().getLogger().debug("[PlaySound] Packet sent - " + packet.toString() + " --> " + playSoundPacket);
}
}

View file

@ -0,0 +1,47 @@
package org.geysermc.connector.network.translators.java.entity.player;
import com.github.steveice10.mc.protocol.data.game.world.sound.BuiltinSound;
import com.github.steveice10.mc.protocol.data.game.world.sound.CustomSound;
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerStopSoundPacket;
import com.nukkitx.protocol.bedrock.packet.StopSoundPacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.sound.SoundMap;
public class JavaPlayerStopSoundTranslator extends PacketTranslator<ServerStopSoundPacket> {
@Override
public void translate(ServerStopSoundPacket packet, GeyserSession session) {
String packetSound;
if(packet.getSound() instanceof BuiltinSound) {
packetSound = ((BuiltinSound) packet.getSound()).getName();
} else if(packet.getSound() instanceof CustomSound) {
packetSound = ((CustomSound) packet.getSound()).getName();
} else {
session.getConnector().getLogger().debug("Unknown sound packet, we were unable to map this. " + packet.toString());
return;
}
SoundMap.SoundMapping soundMapping = SoundMap.get().fromJava(packetSound);
session.getConnector().getLogger()
.debug("[StopSound] Sound mapping " + packetSound + " -> "
+ soundMapping + (soundMapping == null ? "[not found]" : "")
+ " - " + packet.toString());
String playsound;
if(soundMapping == null || soundMapping.getPlaysound() == null) {
// no mapping
session.getConnector().getLogger()
.debug("[StopSound] Defaulting to sound server gave us.");
playsound = packetSound;
} else {
playsound = soundMapping.getPlaysound();
}
StopSoundPacket stopSoundPacket = new StopSoundPacket();
stopSoundPacket.setSoundName(playsound);
// packet not mapped in the library
stopSoundPacket.setStoppingAllSound(false);
session.getUpstream().sendPacket(stopSoundPacket);
session.getConnector().getLogger().debug("[StopSound] Packet sent - " + packet.toString() + " --> " + stopSoundPacket);
}
}

View file

@ -0,0 +1,55 @@
package org.geysermc.connector.network.translators.java.world;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerPlayBuiltinSoundPacket;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.SoundEvent;
import com.nukkitx.protocol.bedrock.packet.LevelSoundEventPacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.sound.SoundMap;
public class JavaPlayBuiltinSoundTranslator extends PacketTranslator<ServerPlayBuiltinSoundPacket> {
@Override
public void translate(ServerPlayBuiltinSoundPacket packet, GeyserSession session) {
String packetSound = packet.getSound().getName();
SoundMap.SoundMapping soundMapping = SoundMap.get().fromJava(packetSound);
session.getConnector().getLogger()
.debug("[Builtin] Sound mapping " + packetSound + " -> "
+ soundMapping + (soundMapping == null ? "[not found]" : "")
+ " - " + packet.toString());
if(soundMapping == null) {
return;
}
LevelSoundEventPacket soundPacket = new LevelSoundEventPacket();
SoundEvent sound = SoundMap.toSoundEvent(soundMapping.getBedrock());
if(sound == null) {
sound = SoundMap.toSoundEvent(soundMapping.getBedrock());
if(sound == null) {
sound = SoundMap.toSoundEvent(packetSound);
if(sound == null) {
session.getConnector().getLogger()
.debug("[Builtin] Sound for original " + packetSound + " to mappings " + soundPacket
+ " was not a playable level sound, or has yet to be mapped to an enum in " +
"NukkitX SoundEvent ");
} else {
session.getConnector().getLogger()
.debug("[Builtin] Sound for original " + packetSound + " to mappings " + soundPacket
+ " was not found in NukkitX SoundEvent, but original packet sound name was.");
}
return;
}
}
soundPacket.setSound(sound);
soundPacket.setPosition(Vector3f.from(packet.getX(), packet.getY(), packet.getZ()));
soundPacket.setExtraData(-1);
soundPacket.setIdentifier(":"); // ???
soundPacket.setBabySound(false); // might need to adjust this in the future
soundPacket.setRelativeVolumeDisabled(false);
session.getUpstream().sendPacket(soundPacket);
session.getConnector().getLogger().debug("Packet sent - " + packet.toString() + " --> " + soundPacket.toString());
}
}

View file

@ -0,0 +1,109 @@
package org.geysermc.connector.sound;
import com.fasterxml.jackson.databind.JsonNode;
import com.nukkitx.protocol.bedrock.data.SoundEvent;
import lombok.Data;
import lombok.ToString;
import org.geysermc.connector.utils.Toolbox;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
public class SoundMap {
private static SoundMap instance;
public static SoundMap get() {
if(instance == null) {
instance = new SoundMap(make());
}
return instance;
}
private static ArrayList<SoundMapping> make() {
/* Load sound mappings */
InputStream stream = Toolbox.getResource("mappings/sounds.json");
JsonNode soundsTree;
try {
soundsTree = Toolbox.JSON_MAPPER.readTree(stream);
} catch (IOException e) {
throw new AssertionError("Unable to load sound mappings", e);
}
ArrayList<SoundMapping> soundMappings = new ArrayList<>();
Iterator<Map.Entry<String, JsonNode>> soundsIterator = soundsTree.fields();
while(soundsIterator.hasNext()) {
Map.Entry<String, JsonNode> next = soundsIterator.next();
JsonNode brMap = next.getValue();
soundMappings.add(
new SoundMapping(
next.getKey(),
brMap.has("bedrock_mapping") && brMap.get("bedrock_mapping").isTextual() ? brMap.get("bedrock_mapping").asText() : null,
brMap.has("playsound_mapping") && brMap.get("playsound_mapping").isTextual() ? brMap.get("playsound_mapping").asText() : null
)
);
}
return soundMappings;
}
private ArrayList<SoundMapping> sounds;
public SoundMap(ArrayList<SoundMapping> sounds) {
this.sounds = sounds;
}
/**
* Get's the sound mapping for a Java edition sound identifier
* @param java Java edition sound identifier
* @return SoundMapping object with information for bedrock, nukkit, java, etc. null if not found
*/
public SoundMapping fromJava(String java) {
for (SoundMapping sound : this.sounds) {
if(sound.getJava().equals(java)) {
return sound;
}
}
return null;
}
public void refresh() {
this.sounds = make();
}
public static SoundEvent toSoundEvent(String s) {
SoundEvent sound;
try {
sound = SoundEvent.valueOf(
s
.toUpperCase()
.replaceAll("\\.", "_")
);
return sound;
} catch(Exception e) {
return null;
}
}
@Data
@ToString
public static class SoundMapping {
private final String java;
private final String bedrock;
private final String playsound;
public SoundMapping(String java, String bedrock, String playsound) {
this.java = java;
this.bedrock = bedrock == null || bedrock.equalsIgnoreCase("") ? null : bedrock;
this.playsound = playsound == null || playsound.equalsIgnoreCase("") ? null : playsound;
}
}
}

View file

@ -39,6 +39,8 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.sound.SoundMap;
import java.io.InputStream;
import java.util.*;
@ -103,6 +105,9 @@ public class Toolbox {
entry.getValue().get("bedrock_id").intValue(), entry.getValue().get("bedrock_data").intValue()));
itemIndex++;
}
/* Load sound mappings */
SoundMap.get();
}
public static InputStream getResource(String resource) {