From 987edbb2172c0f86ba40c18b03b33f873d3f8a65 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Wed, 22 Apr 2020 18:17:34 -0500 Subject: [PATCH] Nitpicks and add serverbound sound packet translator --- .../BedrockLevelSoundEventTranslator.java | 41 +++++++++ .../player/JavaPlayerPlaySoundTranslator.java | 35 +++++-- .../player/JavaPlayerStopSoundTranslator.java | 29 +++++- .../world/JavaPlayBuiltinSoundTranslator.java | 35 ++++++- .../SoundMap.java => utils/SoundUtils.java} | 91 +++++++++---------- .../org/geysermc/connector/utils/Toolbox.java | 4 +- connector/src/main/resources/mappings | 2 +- 7 files changed, 173 insertions(+), 64 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockLevelSoundEventTranslator.java rename connector/src/main/java/org/geysermc/connector/{sound/SoundMap.java => utils/SoundUtils.java} (53%) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockLevelSoundEventTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockLevelSoundEventTranslator.java new file mode 100644 index 00000000..c1c29e0b --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockLevelSoundEventTranslator.java @@ -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 { + + @Override + public void translate(LevelSoundEventPacket packet, GeyserSession session) { + // lol what even :thinking: + session.getUpstream().sendPacket(packet); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/player/JavaPlayerPlaySoundTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/player/JavaPlayerPlaySoundTranslator.java index 78d826dc..2bc3a794 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/player/JavaPlayerPlaySoundTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/player/JavaPlayerPlaySoundTranslator.java @@ -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 { - 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 " + soundMapping + (soundMapping == null ? "[not found]" : "") @@ -46,7 +67,7 @@ public class JavaPlayerPlaySoundTranslator extends PacketTranslator { @@ -23,7 +48,7 @@ public class JavaPlayerStopSoundTranslator extends PacketTranslator " + soundMapping + (soundMapping == null ? "[not found]" : "") diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaPlayBuiltinSoundTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaPlayBuiltinSoundTranslator.java index b824c89d..a21dc388 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaPlayBuiltinSoundTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaPlayBuiltinSoundTranslator.java @@ -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 { @@ -16,7 +41,7 @@ public class JavaPlayBuiltinSoundTranslator extends PacketTranslator " + soundMapping + (soundMapping == null ? "[not found]" : "") @@ -26,11 +51,11 @@ public class JavaPlayBuiltinSoundTranslator extends PacketTranslator SOUNDS; - public static SoundMap get() { - if(instance == null) { - instance = new SoundMap(make()); - } - return instance; + public static void init() { + // no-op } - private static ArrayList 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 soundMappings = new ArrayList<>(); + Map soundMappings = new HashMap<>(); Iterator> soundsIterator = soundsTree.fields(); while(soundsIterator.hasNext()) { Map.Entry 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 sounds; - - public SoundMap(ArrayList 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; } } - } diff --git a/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java b/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java index 28e18521..a4fa5cfb 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java +++ b/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java @@ -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(); } diff --git a/connector/src/main/resources/mappings b/connector/src/main/resources/mappings index efc9db6b..9ecd90c7 160000 --- a/connector/src/main/resources/mappings +++ b/connector/src/main/resources/mappings @@ -1 +1 @@ -Subproject commit efc9db6b7d51bdf145230933ac23b321ac1c132d +Subproject commit 9ecd90c71a26423a5f824554cce9b4236e544723