From 8087b42c4059e5699d272ae741ada726ac7be8a9 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Sun, 15 Mar 2020 12:09:56 +0100 Subject: [PATCH] Map named sound packets, some cleanup --- .../api/data/VBSoundMappings.java | 41 ++++++ .../api/rewriters/LegacySoundRewriter.java | 97 ++++++++++++++ .../api/rewriters/SoundRewriter.java | 125 ++++++------------ .../packets/SoundPackets1_11.java | 4 +- .../packets/SoundPackets1_12.java | 4 +- .../data/BackwardsMappings.java | 5 +- .../packets/SoundPackets1_13.java | 64 ++++----- .../Protocol1_13_2To1_14.java | 1 - .../data/BackwardsMappings.java | 5 +- .../packets/SoundPackets1_14.java | 82 +++++------- .../Protocol1_14_4To1_15.java | 37 +----- .../data/BackwardsMappings.java | 5 +- .../Protocol1_15_2To1_16.java | 34 +---- .../data/BackwardsMappings.java | 5 +- .../packets/SoundPackets1_10.java | 4 +- 15 files changed, 266 insertions(+), 247 deletions(-) create mode 100644 core/src/main/java/nl/matsv/viabackwards/api/data/VBSoundMappings.java create mode 100644 core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacySoundRewriter.java diff --git a/core/src/main/java/nl/matsv/viabackwards/api/data/VBSoundMappings.java b/core/src/main/java/nl/matsv/viabackwards/api/data/VBSoundMappings.java new file mode 100644 index 00000000..6a482f7d --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/api/data/VBSoundMappings.java @@ -0,0 +1,41 @@ +package nl.matsv.viabackwards.api.data; + +import us.myles.ViaVersion.api.data.Mappings; +import us.myles.viaversion.libs.gson.JsonArray; +import us.myles.viaversion.libs.gson.JsonElement; +import us.myles.viaversion.libs.gson.JsonObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * Backwards mappings for sound ids and names. + */ +public class VBSoundMappings { + + private final Map namedSoundMappings = new HashMap<>(); + private final Mappings idMappings; + + public VBSoundMappings(JsonArray oldSounds, JsonArray newSounds, JsonObject diffMapping) { + idMappings = new VBMappings(oldSounds, newSounds, diffMapping); + for (Map.Entry entry : diffMapping.entrySet()) { + String key = entry.getKey(); + if (key.indexOf(':') == -1) { + key = "minecraft:" + key; + } + String value = entry.getValue().getAsString(); + if (value.indexOf(':') == -1) { + value = "minecraft:" + value; + } + namedSoundMappings.put(key, value); + } + } + + public String getNewId(String oldId) { + return namedSoundMappings.get(oldId); + } + + public int getNewId(int oldId) { + return idMappings.getNewId(oldId); + } +} diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacySoundRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacySoundRewriter.java new file mode 100644 index 00000000..b08cfa44 --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacySoundRewriter.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2016 Matsv + * + * 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. + */ + +package nl.matsv.viabackwards.api.rewriters; + +import nl.matsv.viabackwards.api.BackwardsProtocol; + +import java.util.HashMap; +import java.util.Map; + +public abstract class LegacySoundRewriter extends Rewriter { + private final Map soundRewrites = new HashMap<>(); + + protected LegacySoundRewriter(T protocol) { + super(protocol); + } + + public SoundData added(int id, int replacement) { + return added(id, replacement, -1); + } + + public SoundData added(int id, int replacement, float newPitch) { + SoundData data = new SoundData(replacement, true, newPitch, true); + soundRewrites.put(id, data); + return data; + } + + public SoundData removed(int id) { + SoundData data = new SoundData(-1, false, -1, false); + soundRewrites.put(id, data); + return data; + } + + public int handleSounds(int soundId) { + int newSoundId = soundId; + SoundData data = soundRewrites.get(soundId); + if (data != null) return data.getReplacementSound(); + + for (Map.Entry entry : soundRewrites.entrySet()) { + if (soundId > entry.getKey()) { + if (entry.getValue().isAdded()) { + newSoundId--; + } else { + newSoundId++; + } + } + } + return newSoundId; + } + + public boolean hasPitch(int soundId) { + SoundData data = soundRewrites.get(soundId); + return data != null && data.isChangePitch(); + } + + public float handlePitch(int soundId) { + SoundData data = soundRewrites.get(soundId); + return data != null ? data.getNewPitch() : 1F; + } + + public static final class SoundData { + private final int replacementSound; + private final boolean changePitch; + private final float newPitch; + private final boolean added; + + private SoundData(int replacementSound, boolean changePitch, float newPitch, boolean added) { + this.replacementSound = replacementSound; + this.changePitch = changePitch; + this.newPitch = newPitch; + this.added = added; + } + + public int getReplacementSound() { + return replacementSound; + } + + public boolean isChangePitch() { + return changePitch; + } + + public float getNewPitch() { + return newPitch; + } + + public boolean isAdded() { + return added; + } + } +} diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java index 3d9ca58c..7b0c7ae6 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java @@ -1,97 +1,54 @@ -/* - * Copyright (c) 2016 Matsv - * - * 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. - */ - package nl.matsv.viabackwards.api.rewriters; import nl.matsv.viabackwards.api.BackwardsProtocol; +import nl.matsv.viabackwards.api.data.VBSoundMappings; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.packets.State; -import java.util.HashMap; -import java.util.Map; +public class SoundRewriter { -public abstract class SoundRewriter extends Rewriter { - private final Map soundRewrites = new HashMap<>(); + private final BackwardsProtocol protocol; + private final VBSoundMappings soundMappings; - protected SoundRewriter(T protocol) { - super(protocol); + public SoundRewriter(BackwardsProtocol protocol, VBSoundMappings soundMappings) { + this.protocol = protocol; + this.soundMappings = soundMappings; } - public SoundData added(int id, int replacement) { - return added(id, replacement, -1); - } - - public SoundData added(int id, int replacement, float newPitch) { - SoundData data = new SoundData(replacement, true, newPitch, true); - soundRewrites.put(id, data); - return data; - } - - public SoundData removed(int id) { - SoundData data = new SoundData(-1, false, -1, false); - soundRewrites.put(id, data); - return data; - } - - public int handleSounds(int soundId) { - int newSoundId = soundId; - SoundData data = soundRewrites.get(soundId); - if (data != null) return data.getReplacementSound(); - - for (Map.Entry entry : soundRewrites.entrySet()) { - if (soundId > entry.getKey()) { - if (entry.getValue().isAdded()) { - newSoundId--; - } else { - newSoundId++; - } + // The same for entity sound effect + public void registerSound(int oldId, int newId) { + protocol.registerOutgoing(State.PLAY, oldId, newId, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // Sound Id + handler(wrapper -> { + int soundId = wrapper.get(Type.VAR_INT, 0); + int mappedId = soundMappings.getNewId(soundId); + if (mappedId != -1 && soundId != mappedId) { + wrapper.set(Type.VAR_INT, 0, mappedId); + } + }); } - } - return newSoundId; + }); } - public boolean hasPitch(int soundId) { - SoundData data = soundRewrites.get(soundId); - return data != null && data.isChangePitch(); - } - - public float handlePitch(int soundId) { - SoundData data = soundRewrites.get(soundId); - return data != null ? data.getNewPitch() : 1F; - } - - public static final class SoundData { - private final int replacementSound; - private final boolean changePitch; - private final float newPitch; - private final boolean added; - - private SoundData(int replacementSound, boolean changePitch, float newPitch, boolean added) { - this.replacementSound = replacementSound; - this.changePitch = changePitch; - this.newPitch = newPitch; - this.added = added; - } - - public int getReplacementSound() { - return replacementSound; - } - - public boolean isChangePitch() { - return changePitch; - } - - public float getNewPitch() { - return newPitch; - } - - public boolean isAdded() { - return added; - } + public void registerNamedSound(int oldId, int newId) { + protocol.registerOutgoing(State.PLAY, oldId, newId, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.STRING); // Sound identifier + handler(wrapper -> { + String soundId = wrapper.get(Type.STRING, 0); + String mappedId = soundMappings.getNewId(soundId); + if (mappedId == null) return; + if (!mappedId.isEmpty()) { + wrapper.set(Type.STRING, 0, mappedId); + } else { + wrapper.cancel(); + } + }); + } + }); } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/SoundPackets1_11.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/SoundPackets1_11.java index 7989b5f2..6a4cb84a 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/SoundPackets1_11.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/SoundPackets1_11.java @@ -10,7 +10,7 @@ package nl.matsv.viabackwards.protocol.protocol1_10to1_11.packets; -import nl.matsv.viabackwards.api.rewriters.SoundRewriter; +import nl.matsv.viabackwards.api.rewriters.LegacySoundRewriter; import nl.matsv.viabackwards.protocol.protocol1_10to1_11.Protocol1_10To1_11; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.remapper.PacketHandler; @@ -18,7 +18,7 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; -public class SoundPackets1_11 extends SoundRewriter { +public class SoundPackets1_11 extends LegacySoundRewriter { public SoundPackets1_11(Protocol1_10To1_11 protocol) { super(protocol); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/SoundPackets1_12.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/SoundPackets1_12.java index 383b9d46..07128abf 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/SoundPackets1_12.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/SoundPackets1_12.java @@ -10,7 +10,7 @@ package nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.packets; -import nl.matsv.viabackwards.api.rewriters.SoundRewriter; +import nl.matsv.viabackwards.api.rewriters.LegacySoundRewriter; import nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.remapper.PacketHandler; @@ -18,7 +18,7 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; -public class SoundPackets1_12 extends SoundRewriter { +public class SoundPackets1_12 extends LegacySoundRewriter { public SoundPackets1_12(Protocol1_11_1To1_12 protocol) { super(protocol); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java index b516beaf..ca8fe3b5 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java @@ -14,6 +14,7 @@ import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.data.VBItemMappings; import nl.matsv.viabackwards.api.data.VBMappingDataLoader; import nl.matsv.viabackwards.api.data.VBMappings; +import nl.matsv.viabackwards.api.data.VBSoundMappings; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.Mappings; @@ -31,7 +32,7 @@ public class BackwardsMappings { public static final Map statisticMappings = new HashMap<>(); public static final Map translateMappings = new HashMap<>(); public static BlockMappingsShortArray blockMappings; - public static Mappings soundMappings; + public static VBSoundMappings soundMappings; public static VBItemMappings itemMappings; public static void init() { @@ -42,7 +43,7 @@ public class BackwardsMappings { ViaBackwards.getPlatform().getLogger().info("Loading 1.13 -> 1.12.2 mappings..."); blockMappings = new BlockMappingsShortArray(mapping1_13.getAsJsonObject("blocks"), mapping1_12.getAsJsonObject("blocks"), mapping1_12_2to1_13.getAsJsonObject("blockstates")); itemMappings = new VBItemMappings(mapping1_13.getAsJsonObject("items"), mapping1_12.getAsJsonObject("items"), mapping1_12_2to1_13.getAsJsonObject("items")); - soundMappings = new VBMappings(mapping1_13.getAsJsonArray("sounds"), mapping1_12.getAsJsonArray("sounds"), mapping1_12_2to1_13.getAsJsonObject("sounds")); + soundMappings = new VBSoundMappings(mapping1_13.getAsJsonArray("sounds"), mapping1_12.getAsJsonArray("sounds"), mapping1_12_2to1_13.getAsJsonObject("sounds")); for (Map.Entry entry : StatisticMappings.statistics.entrySet()) { statisticMappings.put(entry.getValue(), entry.getKey()); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java index b9e92e90..22916973 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java @@ -5,9 +5,7 @@ import nl.matsv.viabackwards.api.rewriters.Rewriter; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.NamedSoundMapping; -import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; @@ -21,22 +19,18 @@ public class SoundPackets1_13 extends Rewriter { @Override protected void registerPackets() { - // Named Sound Event protocol.out(State.PLAY, 0x1A, 0x19, new PacketRemapper() { @Override public void registerMap() { map(Type.STRING); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - String newSound = wrapper.get(Type.STRING, 0); - String oldSound = NamedSoundMapping.getOldId(newSound); - if (oldSound != null) { - wrapper.set(Type.STRING, 0, oldSound); - } else if (!Via.getConfig().isSuppressConversionWarnings()) { - ViaBackwards.getPlatform().getLogger().warning("Unknown named sound in 1.13->1.12 protocol: " + newSound); - } + handler(wrapper -> { + String newSound = wrapper.get(Type.STRING, 0); + String oldSound = NamedSoundMapping.getOldId(newSound); + if (oldSound != null || (oldSound = BackwardsMappings.soundMappings.getNewId(newSound)) != null) { + wrapper.set(Type.STRING, 0, oldSound); + } else if (!Via.getConfig().isSuppressConversionWarnings()) { + ViaBackwards.getPlatform().getLogger().warning("Unknown named sound in 1.13->1.12 protocol: " + newSound); } }); } @@ -46,22 +40,19 @@ public class SoundPackets1_13 extends Rewriter { protocol.out(State.PLAY, 0x4C, 0x18, new PacketRemapper() { @Override public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - wrapper.write(Type.STRING, "MC|StopSound"); - byte flags = wrapper.read(Type.BYTE); - String source; - if ((flags & 0x01) != 0) { - source = SOUND_SOURCES[wrapper.read(Type.VAR_INT)]; - } else { - source = ""; - } - String sound = (flags & 0x02) != 0 ? wrapper.read(Type.STRING) : ""; - - wrapper.write(Type.STRING, source); - wrapper.write(Type.STRING, sound); + handler(wrapper -> { + wrapper.write(Type.STRING, "MC|StopSound"); + byte flags = wrapper.read(Type.BYTE); + String source; + if ((flags & 0x01) != 0) { + source = SOUND_SOURCES[wrapper.read(Type.VAR_INT)]; + } else { + source = ""; } + String sound = (flags & 0x02) != 0 ? wrapper.read(Type.STRING) : ""; + + wrapper.write(Type.STRING, source); + wrapper.write(Type.STRING, sound); }); } }); @@ -71,16 +62,13 @@ public class SoundPackets1_13 extends Rewriter { @Override public void registerMap() { map(Type.VAR_INT); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int newSound = wrapper.get(Type.VAR_INT, 0); - int oldSound = BackwardsMappings.soundMappings.getNewId(newSound); - if (oldSound == -1) { - wrapper.cancel(); - } else { - wrapper.set(Type.VAR_INT, 0, oldSound); - } + handler(wrapper -> { + int newSound = wrapper.get(Type.VAR_INT, 0); + int oldSound = BackwardsMappings.soundMappings.getNewId(newSound); + if (oldSound == -1) { + wrapper.cancel(); + } else { + wrapper.set(Type.VAR_INT, 0, oldSound); } }); } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java index 8913007a..331ed0b8 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java @@ -49,7 +49,6 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol { registerOutgoing(State.PLAY, 0x15, 0x16); registerOutgoing(State.PLAY, 0x18, 0x19); - registerOutgoing(State.PLAY, 0x19, 0x1A); registerOutgoing(State.PLAY, 0x1B, 0x1C); registerOutgoing(State.PLAY, 0x54, 0x1D); registerOutgoing(State.PLAY, 0x1E, 0x20); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/data/BackwardsMappings.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/data/BackwardsMappings.java index f784eab2..e5e54f6c 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/data/BackwardsMappings.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/data/BackwardsMappings.java @@ -4,6 +4,7 @@ import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.data.VBItemMappings; import nl.matsv.viabackwards.api.data.VBMappingDataLoader; import nl.matsv.viabackwards.api.data.VBMappings; +import nl.matsv.viabackwards.api.data.VBSoundMappings; import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.Mappings; import us.myles.viaversion.libs.gson.JsonObject; @@ -11,7 +12,7 @@ import us.myles.viaversion.libs.gson.JsonObject; public class BackwardsMappings { public static Mappings blockStateMappings; public static Mappings blockMappings; - public static Mappings soundMappings; + public static VBSoundMappings soundMappings; public static VBItemMappings itemMappings; public static void init() { @@ -23,6 +24,6 @@ public class BackwardsMappings { blockStateMappings = new VBMappings(mapping1_14.getAsJsonObject("blockstates"), mapping1_13_2.getAsJsonObject("blockstates"), mapping1_13_2to1_14.getAsJsonObject("blockstates")); blockMappings = new VBMappings(mapping1_14.getAsJsonObject("blocks"), mapping1_13_2.getAsJsonObject("blocks"), mapping1_13_2to1_14.getAsJsonObject("blocks"), false); itemMappings = new VBItemMappings(mapping1_14.getAsJsonObject("items"), mapping1_13_2.getAsJsonObject("items"), mapping1_13_2to1_14.getAsJsonObject("items")); - soundMappings = new VBMappings(mapping1_14.getAsJsonArray("sounds"), mapping1_13_2.getAsJsonArray("sounds"), mapping1_13_2to1_14.getAsJsonObject("sounds")); + soundMappings = new VBSoundMappings(mapping1_14.getAsJsonArray("sounds"), mapping1_13_2.getAsJsonArray("sounds"), mapping1_13_2to1_14.getAsJsonObject("sounds")); } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java index 7a833739..4109c2cf 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java @@ -3,11 +3,11 @@ package nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.packets; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.rewriters.Rewriter; +import nl.matsv.viabackwards.api.rewriters.SoundRewriter; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; @@ -20,64 +20,46 @@ public class SoundPackets1_14 extends Rewriter { @Override protected void registerPackets() { - // Sound Effect - protocol.registerOutgoing(State.PLAY, 0x51, 0x4D, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // Sound Id - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int newId = BackwardsMappings.soundMappings.getNewId(wrapper.get(Type.VAR_INT, 0)); - if (newId == -1) { - wrapper.cancel(); - } else { - wrapper.set(Type.VAR_INT, 0, newId); - } - } - }); - } - }); + SoundRewriter soundRewriter = new SoundRewriter(protocol, BackwardsMappings.soundMappings); + soundRewriter.registerSound(0x51, 0x4D); + soundRewriter.registerNamedSound(0x19, 0x1A); // Entity Sound Effect protocol.registerOutgoing(State.PLAY, 0x50, -1, new PacketRemapper() { @Override public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - wrapper.cancel(); + handler(wrapper -> { + wrapper.cancel(); - int soundId = wrapper.read(Type.VAR_INT); - int newId = BackwardsMappings.soundMappings.getNewId(soundId); - if (newId == -1) return; + int soundId = wrapper.read(Type.VAR_INT); + int newId = BackwardsMappings.soundMappings.getNewId(soundId); + if (newId == -1) return; - int category = wrapper.read(Type.VAR_INT); - int entityId = wrapper.read(Type.VAR_INT); + int category = wrapper.read(Type.VAR_INT); + int entityId = wrapper.read(Type.VAR_INT); - EntityTracker.StoredEntity storedEntity = wrapper.user().get(EntityTracker.class).get(protocol).getEntity(entityId); - EntityPositionStorage1_14 entityStorage; - if (storedEntity == null || (entityStorage = storedEntity.get(EntityPositionStorage1_14.class)) == null) { - ViaBackwards.getPlatform().getLogger().warning("Untracked entity with id " + entityId); - return; - } - - float volume = wrapper.read(Type.FLOAT); - float pitch = wrapper.read(Type.FLOAT); - int x = (int) (entityStorage.getX() * 8D); - int y = (int) (entityStorage.getY() * 8D); - int z = (int) (entityStorage.getZ() * 8D); - - PacketWrapper soundPacket = wrapper.create(0x4D); - soundPacket.write(Type.VAR_INT, newId); - soundPacket.write(Type.VAR_INT, category); - soundPacket.write(Type.INT, x); - soundPacket.write(Type.INT, y); - soundPacket.write(Type.INT, z); - soundPacket.write(Type.FLOAT, volume); - soundPacket.write(Type.FLOAT, pitch); - soundPacket.send(Protocol1_13_2To1_14.class); + EntityTracker.StoredEntity storedEntity = wrapper.user().get(EntityTracker.class).get(protocol).getEntity(entityId); + EntityPositionStorage1_14 entityStorage; + if (storedEntity == null || (entityStorage = storedEntity.get(EntityPositionStorage1_14.class)) == null) { + ViaBackwards.getPlatform().getLogger().warning("Untracked entity with id " + entityId); + return; } + + float volume = wrapper.read(Type.FLOAT); + float pitch = wrapper.read(Type.FLOAT); + int x = (int) (entityStorage.getX() * 8D); + int y = (int) (entityStorage.getY() * 8D); + int z = (int) (entityStorage.getZ() * 8D); + + PacketWrapper soundPacket = wrapper.create(0x4D); + soundPacket.write(Type.VAR_INT, newId); + soundPacket.write(Type.VAR_INT, category); + soundPacket.write(Type.INT, x); + soundPacket.write(Type.INT, y); + soundPacket.write(Type.INT, z); + soundPacket.write(Type.FLOAT, volume); + soundPacket.write(Type.FLOAT, pitch); + soundPacket.send(Protocol1_13_2To1_14.class); }); } }); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java index 897e7950..a886a7e7 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java @@ -3,6 +3,7 @@ package nl.matsv.viabackwards.protocol.protocol1_14_4to1_15; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.BackwardsProtocol; import nl.matsv.viabackwards.api.entities.storage.EntityTracker; +import nl.matsv.viabackwards.api.rewriters.SoundRewriter; import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.EntityTypeMapping; @@ -38,6 +39,11 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol { translatableRewriter.registerTitle(0x50, 0x4F); translatableRewriter.registerPing(); + SoundRewriter soundRewriter = new SoundRewriter(this, BackwardsMappings.soundMappings); + soundRewriter.registerSound(0x52, 0x51); + soundRewriter.registerSound(0x51, 0x50); + soundRewriter.registerNamedSound(0x1A, 0x19); + // Explosion - manually send an explosion sound registerOutgoing(State.PLAY, 0x1D, 0x1C, new PacketRemapper() { @Override @@ -63,36 +69,6 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol { } }); - // Entity Sound Effect - registerOutgoing(State.PLAY, 0x51, 0x50, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // Sound Id - handler(wrapper -> { - int id = wrapper.get(Type.VAR_INT, 0); - int newId = BackwardsMappings.soundMappings.getNewId(id); - if (newId != -1 && id != newId) { - wrapper.set(Type.VAR_INT, 0, newId); - } - }); - } - }); - - // Sound Effect - registerOutgoing(State.PLAY, 0x52, 0x51, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // Sound Id - handler(wrapper -> { - int id = wrapper.get(Type.VAR_INT, 0); - int newId = BackwardsMappings.soundMappings.getNewId(id); - if (newId != -1 && id != newId) { - wrapper.set(Type.VAR_INT, 0, newId); - } - }); - } - }); - // Advancements registerOutgoing(State.PLAY, 0x58, 0x57, new PacketRemapper() { @Override @@ -188,7 +164,6 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol { registerOutgoing(State.PLAY, 0x14, 0x13); registerOutgoing(State.PLAY, 0x16, 0x15); registerOutgoing(State.PLAY, 0x19, 0x18); - registerOutgoing(State.PLAY, 0x1A, 0x19); registerOutgoing(State.PLAY, 0x1C, 0x1B); registerOutgoing(State.PLAY, 0x1E, 0x1D); registerOutgoing(State.PLAY, 0x20, 0x1F); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/data/BackwardsMappings.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/data/BackwardsMappings.java index 00ef2d3a..a4059999 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/data/BackwardsMappings.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/data/BackwardsMappings.java @@ -4,6 +4,7 @@ import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.data.VBItemMappings; import nl.matsv.viabackwards.api.data.VBMappingDataLoader; import nl.matsv.viabackwards.api.data.VBMappings; +import nl.matsv.viabackwards.api.data.VBSoundMappings; import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.Mappings; import us.myles.viaversion.libs.gson.JsonObject; @@ -11,7 +12,7 @@ import us.myles.viaversion.libs.gson.JsonObject; public class BackwardsMappings { public static Mappings blockStateMappings; public static Mappings blockMappings; - public static Mappings soundMappings; + public static VBSoundMappings soundMappings; public static VBItemMappings itemMappings; public static void init() { @@ -23,6 +24,6 @@ public class BackwardsMappings { blockStateMappings = new VBMappings(mapping1_15.getAsJsonObject("blockstates"), mapping1_14.getAsJsonObject("blockstates"), mapping1_14to1_15.getAsJsonObject("blockstates")); blockMappings = new VBMappings(mapping1_15.getAsJsonObject("blocks"), mapping1_14.getAsJsonObject("blocks"), mapping1_14to1_15.getAsJsonObject("blocks"), false); itemMappings = new VBItemMappings(mapping1_15.getAsJsonObject("items"), mapping1_14.getAsJsonObject("items"), mapping1_14to1_15.getAsJsonObject("items")); - soundMappings = new VBMappings(mapping1_15.getAsJsonArray("sounds"), mapping1_14.getAsJsonArray("sounds"), mapping1_14to1_15.getAsJsonObject("sounds")); + soundMappings = new VBSoundMappings(mapping1_15.getAsJsonArray("sounds"), mapping1_14.getAsJsonArray("sounds"), mapping1_14to1_15.getAsJsonObject("sounds")); } } \ No newline at end of file diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java index 5b8aacff..389e6334 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java @@ -3,6 +3,7 @@ package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.BackwardsProtocol; import nl.matsv.viabackwards.api.entities.storage.EntityTracker; +import nl.matsv.viabackwards.api.rewriters.SoundRewriter; import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.packets.BlockItemPackets1_16; @@ -34,35 +35,10 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol { translatableRewriter.registerTitle(0x50, 0x50); translatableRewriter.registerPing(); - // Entity Sound Effect - registerOutgoing(State.PLAY, 0x51, 0x51, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // Sound Id - handler(wrapper -> { - int id = wrapper.get(Type.VAR_INT, 0); - int newId = BackwardsMappings.soundMappings.getNewId(id); - if (newId != -1 && id != newId) { - wrapper.set(Type.VAR_INT, 0, newId); - } - }); - } - }); - - // Sound Effect - registerOutgoing(State.PLAY, 0x52, 0x52, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // Sound Id - handler(wrapper -> { - int id = wrapper.get(Type.VAR_INT, 0); - int newId = BackwardsMappings.soundMappings.getNewId(id); - if (newId != -1 && id != newId) { - wrapper.set(Type.VAR_INT, 0, newId); - } - }); - } - }); + SoundRewriter soundRewriter = new SoundRewriter(this, BackwardsMappings.soundMappings); + soundRewriter.registerSound(0x51, 0x51); + soundRewriter.registerSound(0x52, 0x52); + soundRewriter.registerNamedSound(0x1A, 0x1A); // Advancements registerOutgoing(State.PLAY, 0x58, 0x58, new PacketRemapper() { diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java index d95db290..8c521ccb 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java @@ -4,6 +4,7 @@ import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.data.VBItemMappings; import nl.matsv.viabackwards.api.data.VBMappingDataLoader; import nl.matsv.viabackwards.api.data.VBMappings; +import nl.matsv.viabackwards.api.data.VBSoundMappings; import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.Mappings; import us.myles.viaversion.libs.gson.JsonObject; @@ -11,7 +12,7 @@ import us.myles.viaversion.libs.gson.JsonObject; public class BackwardsMappings { public static Mappings blockStateMappings; public static Mappings blockMappings; - public static Mappings soundMappings; + public static VBSoundMappings soundMappings; public static VBItemMappings itemMappings; public static void init() { @@ -23,6 +24,6 @@ public class BackwardsMappings { blockStateMappings = new VBMappings(mapping1_16.getAsJsonObject("blockstates"), mapping1_15.getAsJsonObject("blockstates"), mapping1_15to1_16.getAsJsonObject("blockstates")); blockMappings = new VBMappings(mapping1_16.getAsJsonObject("blocks"), mapping1_15.getAsJsonObject("blocks"), mapping1_15to1_16.getAsJsonObject("blocks"), false); itemMappings = new VBItemMappings(mapping1_16.getAsJsonObject("items"), mapping1_15.getAsJsonObject("items"), mapping1_15to1_16.getAsJsonObject("items")); - soundMappings = new VBMappings(mapping1_16.getAsJsonArray("sounds"), mapping1_15.getAsJsonArray("sounds"), mapping1_15to1_16.getAsJsonObject("sounds")); + soundMappings = new VBSoundMappings(mapping1_16.getAsJsonArray("sounds"), mapping1_15.getAsJsonArray("sounds"), mapping1_15to1_16.getAsJsonObject("sounds")); } } \ No newline at end of file diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/SoundPackets1_10.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/SoundPackets1_10.java index 1e3a3cc5..875bd82f 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/SoundPackets1_10.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/SoundPackets1_10.java @@ -10,7 +10,7 @@ package nl.matsv.viabackwards.protocol.protocol1_9_4to1_10.packets; -import nl.matsv.viabackwards.api.rewriters.SoundRewriter; +import nl.matsv.viabackwards.api.rewriters.LegacySoundRewriter; import nl.matsv.viabackwards.protocol.protocol1_9_4to1_10.Protocol1_9_4To1_10; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.remapper.PacketHandler; @@ -19,7 +19,7 @@ import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; -public class SoundPackets1_10 extends SoundRewriter { +public class SoundPackets1_10 extends LegacySoundRewriter { protected static ValueTransformer toOldPitch = new ValueTransformer(Type.UNSIGNED_BYTE) { public Short transform(PacketWrapper packetWrapper, Float inputValue) throws Exception { return (short) Math.round(inputValue * 63.5F);