Nitpicks and add serverbound sound packet translator

This commit is contained in:
RednedEpic 2020-04-22 18:17:34 -05:00
parent 12b99c5b1b
commit 987edbb217
7 changed files with 173 additions and 64 deletions

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2019-2020 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.connector.network.translators.bedrock;
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.network.translators.Translator;
@Translator(packet = LevelSoundEventPacket.class)
public class BedrockLevelSoundEventTranslator extends PacketTranslator<LevelSoundEventPacket> {
@Override
public void translate(LevelSoundEventPacket packet, GeyserSession session) {
// lol what even :thinking:
session.getUpstream().sendPacket(packet);
}
}

View File

@ -1,3 +1,28 @@
/*
* Copyright (c) 2019-2020 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.connector.network.translators.java.entity.player;
import com.github.steveice10.mc.protocol.data.game.world.sound.BuiltinSound;
@ -8,15 +33,11 @@ import com.nukkitx.protocol.bedrock.packet.*;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator;
import org.geysermc.connector.sound.SoundMap;
import org.geysermc.connector.utils.SoundUtils;
@Translator(packet = ServerPlaySoundPacket.class)
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;
@ -29,7 +50,7 @@ public class JavaPlayerPlaySoundTranslator extends PacketTranslator<ServerPlaySo
return;
}
SoundMap.SoundMapping soundMapping = SoundMap.get().fromJava(packetSound);
SoundUtils.SoundMapping soundMapping = SoundUtils.fromJava(packetSound);
session.getConnector().getLogger()
.debug("[PlaySound] Sound mapping " + packetSound + " -> "
+ soundMapping + (soundMapping == null ? "[not found]" : "")
@ -46,7 +67,7 @@ public class JavaPlayerPlaySoundTranslator extends PacketTranslator<ServerPlaySo
PlaySoundPacket playSoundPacket = new PlaySoundPacket();
playSoundPacket.setSound(playsound);
playSoundPacket.setPosition(Vector3f.from(processCoordinate(packet.getX()), processCoordinate(packet.getY()), processCoordinate(packet.getZ())));
playSoundPacket.setPosition(Vector3f.from(SoundUtils.processCoordinate(packet.getX()), SoundUtils.processCoordinate(packet.getY()), SoundUtils.processCoordinate(packet.getZ())));
playSoundPacket.setVolume(packet.getVolume());
playSoundPacket.setPitch(packet.getPitch());

View File

@ -1,3 +1,28 @@
/*
* Copyright (c) 2019-2020 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.connector.network.translators.java.entity.player;
import com.github.steveice10.mc.protocol.data.game.world.sound.BuiltinSound;
@ -7,7 +32,7 @@ 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.network.translators.Translator;
import org.geysermc.connector.sound.SoundMap;
import org.geysermc.connector.utils.SoundUtils;
@Translator(packet = ServerStopSoundPacket.class)
public class JavaPlayerStopSoundTranslator extends PacketTranslator<ServerStopSoundPacket> {
@ -23,7 +48,7 @@ public class JavaPlayerStopSoundTranslator extends PacketTranslator<ServerStopSo
session.getConnector().getLogger().debug("Unknown sound packet, we were unable to map this. " + packet.toString());
return;
}
SoundMap.SoundMapping soundMapping = SoundMap.get().fromJava(packetSound);
SoundUtils.SoundMapping soundMapping = SoundUtils.fromJava(packetSound);
session.getConnector().getLogger()
.debug("[StopSound] Sound mapping " + packetSound + " -> "
+ soundMapping + (soundMapping == null ? "[not found]" : "")

View File

@ -1,3 +1,28 @@
/*
* Copyright (c) 2019-2020 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.connector.network.translators.java.world;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerPlayBuiltinSoundPacket;
@ -7,7 +32,7 @@ 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.network.translators.Translator;
import org.geysermc.connector.sound.SoundMap;
import org.geysermc.connector.utils.SoundUtils;
@Translator(packet = ServerPlayBuiltinSoundPacket.class)
public class JavaPlayBuiltinSoundTranslator extends PacketTranslator<ServerPlayBuiltinSoundPacket> {
@ -16,7 +41,7 @@ public class JavaPlayBuiltinSoundTranslator extends PacketTranslator<ServerPlayB
public void translate(ServerPlayBuiltinSoundPacket packet, GeyserSession session) {
String packetSound = packet.getSound().getName();
SoundMap.SoundMapping soundMapping = SoundMap.get().fromJava(packetSound);
SoundUtils.SoundMapping soundMapping = SoundUtils.fromJava(packetSound);
session.getConnector().getLogger()
.debug("[Builtin] Sound mapping " + packetSound + " -> "
+ soundMapping + (soundMapping == null ? "[not found]" : "")
@ -26,11 +51,11 @@ public class JavaPlayBuiltinSoundTranslator extends PacketTranslator<ServerPlayB
}
LevelSoundEventPacket soundPacket = new LevelSoundEventPacket();
SoundEvent sound = SoundMap.toSoundEvent(soundMapping.getBedrock());
SoundEvent sound = SoundUtils.toSoundEvent(soundMapping.getBedrock());
if(sound == null) {
sound = SoundMap.toSoundEvent(soundMapping.getBedrock());
sound = SoundUtils.toSoundEvent(soundMapping.getBedrock());
if(sound == null) {
sound = SoundMap.toSoundEvent(packetSound);
sound = SoundUtils.toSoundEvent(packetSound);
if(sound == null) {
session.getConnector().getLogger()
.debug("[Builtin] Sound for original " + packetSound + " to mappings " + soundPacket

View File

@ -1,29 +1,51 @@
package org.geysermc.connector.sound;
/*
* Copyright (c) 2019-2020 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.connector.utils;
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.HashMap;
import java.util.Iterator;
import java.util.Map;
public class SoundMap {
public class SoundUtils {
private static SoundMap instance;
private static final Map<String, SoundMapping> SOUNDS;
public static SoundMap get() {
if(instance == null) {
instance = new SoundMap(make());
}
return instance;
public static void init() {
// no-op
}
private static ArrayList<SoundMapping> make() {
static {
/* Load sound mappings */
InputStream stream = Toolbox.getResource("mappings/sounds.json");
JsonNode soundsTree;
@ -33,14 +55,13 @@ public class SoundMap {
throw new AssertionError("Unable to load sound mappings", e);
}
ArrayList<SoundMapping> soundMappings = new ArrayList<>();
Map<String, SoundMapping> soundMappings = new HashMap<>();
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(
soundMappings.put(next.getKey(), 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,
@ -48,15 +69,7 @@ public class SoundMap {
)
);
}
return soundMappings;
}
private ArrayList<SoundMapping> sounds;
public SoundMap(ArrayList<SoundMapping> sounds) {
this.sounds = sounds;
SOUNDS = soundMappings;
}
/**
@ -64,35 +77,22 @@ public class SoundMap {
* @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 static SoundMapping fromJava(String java) {
return SOUNDS.get(java);
}
public void refresh() {
this.sounds = make();
}
public static SoundEvent toSoundEvent(String s) {
SoundEvent sound;
public static SoundEvent toSoundEvent(String sound) {
try {
sound = SoundEvent.valueOf(
s
.toUpperCase()
.replaceAll("\\.", "_")
);
return sound;
} catch(Exception e) {
return SoundEvent.valueOf(sound.toUpperCase().replaceAll("\\.", "_"));
} catch (IllegalArgumentException ex) {
return null;
}
}
public static double processCoordinate(double f) {
return (f / 3D) * 8D;
}
@Data
@ToString
public static class SoundMapping {
@ -108,5 +108,4 @@ public class SoundMap {
this.extraData = extraData;
}
}
}

View File

@ -39,8 +39,6 @@ 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.*;
import java.util.*;
@ -109,7 +107,7 @@ public class Toolbox {
}
// Load sound mappings
SoundMap.get();
SoundUtils.init();
// Load the locale data
LocaleUtils.init();
}

@ -1 +1 @@
Subproject commit efc9db6b7d51bdf145230933ac23b321ac1c132d
Subproject commit 9ecd90c71a26423a5f824554cce9b4236e544723