From 6b9e67f96be0eac910840a4937d8f24e0dff3684 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Sat, 23 May 2020 10:56:27 +0200 Subject: [PATCH 1/2] Sound rewriter cleanup - don't send -1 sound ids Addresses #1783 --- .../api/rewriters/SoundRewriter.java | 36 +++++++++++++++++++ .../Protocol1_11To1_10.java | 27 ++------------ .../Protocol1_12To1_11_1.java | 28 ++------------- .../Protocol1_13To1_12_2.java | 20 ++--------- .../Protocol1_14To1_13_2.java | 25 ++----------- .../Protocol1_15To1_14_4.java | 31 +++------------- .../Protocol1_16To1_15_2.java | 21 +++-------- 7 files changed, 53 insertions(+), 135 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java new file mode 100644 index 000000000..4c795f10d --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java @@ -0,0 +1,36 @@ +package us.myles.ViaVersion.api.rewriters; + +import us.myles.ViaVersion.api.protocol.Protocol; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.packets.State; + +public class SoundRewriter { + private final Protocol protocol; + // Can't hold the mappings instance here since it's loaded later + private final IdRewriteFunction idRewriter; + + public SoundRewriter(Protocol protocol, IdRewriteFunction idRewriter) { + this.protocol = protocol; + this.idRewriter = idRewriter; + } + + // 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 = idRewriter.rewrite(soundId); + if (mappedId == -1) { + wrapper.cancel(); + } else if (soundId != mappedId) { + wrapper.set(Type.VAR_INT, 0, mappedId); + } + }); + } + }); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java index 04c78021d..c482ad5ec 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java @@ -12,6 +12,7 @@ import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.ValueCreator; import us.myles.ViaVersion.api.remapper.ValueTransformer; +import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.packets.State; @@ -86,31 +87,7 @@ public class Protocol1_11To1_10 extends Protocol { } }); - // Sound effect - registerOutgoing(State.PLAY, 0x46, 0x46, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Sound name - map(Type.VAR_INT); // 1 - Sound Category - map(Type.INT); // 2 - x - map(Type.INT); // 3 - y - map(Type.INT); // 4 - z - map(Type.FLOAT); // 5 - Volume - map(Type.FLOAT); // 6 - Pitch - - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.VAR_INT, 0); - id = getNewSoundId(id); - - if (id == -1) // Removed - wrapper.cancel(); - wrapper.set(Type.VAR_INT, 0, id); - } - }); - } - }); + new SoundRewriter(this, this::getNewSoundId).registerSound(0x46, 0x46); // Collect item packet registerOutgoing(State.PLAY, 0x48, 0x48, new PacketRemapper() { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java index 09d7b16e2..b657f6eb4 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java @@ -4,7 +4,6 @@ import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.IntTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.google.gson.JsonElement; -import com.google.gson.JsonParser; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; @@ -14,6 +13,7 @@ import us.myles.ViaVersion.api.platform.providers.ViaProviders; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_12; import us.myles.ViaVersion.packets.State; @@ -183,31 +183,7 @@ public class Protocol1_12To1_11_1 extends Protocol { registerOutgoing(State.PLAY, 0x44, 0x46); registerOutgoing(State.PLAY, 0x45, 0x47); - // Sound effect - registerOutgoing(State.PLAY, 0x46, 0x48, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Sound name - map(Type.VAR_INT); // 1 - Sound Category - map(Type.INT); // 2 - x - map(Type.INT); // 3 - y - map(Type.INT); // 4 - z - map(Type.FLOAT); // 5 - Volume - map(Type.FLOAT); // 6 - Pitch - - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.VAR_INT, 0); - id = getNewSoundId(id); - - if (id == -1) // Removed - wrapper.cancel(); - wrapper.set(Type.VAR_INT, 0, id); - } - }); - } - }); + new SoundRewriter(this, this::getNewSoundId).registerSound(0x46, 0x48); registerOutgoing(State.PLAY, 0x47, 0x49); registerOutgoing(State.PLAY, 0x48, 0x4a); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index 954f50ff3..d319436cc 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -15,6 +15,7 @@ import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.ValueCreator; import us.myles.ViaVersion.api.remapper.ValueTransformer; +import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionData; @@ -759,21 +760,8 @@ public class Protocol1_13To1_12_2 extends Protocol { }); // New 0x4C - Stop Sound - // Sound Effect packet - registerOutgoing(State.PLAY, 0x49, 0x4D, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Sound ID + new SoundRewriter(this, id -> MappingData.soundMappings.getNewId(id)).registerSound(0x49, 0x4D); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int soundId = wrapper.get(Type.VAR_INT, 0); - wrapper.set(Type.VAR_INT, 0, getNewSoundID(soundId)); - } - }); - } - }); // Player list header and footer registerOutgoing(State.PLAY, 0x4A, 0x4E, new PacketRemapper() { @Override @@ -1175,10 +1163,6 @@ public class Protocol1_13To1_12_2 extends Protocol { providers.register(PaintingProvider.class, new PaintingProvider()); } - private int getNewSoundID(final int oldID) { - return MappingData.soundMappings.getNewId(oldID); - } - // Based on method from https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/ChatColor.java public ChatColor getLastColor(String input) { int length = input.length(); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java index 071ee87a2..e45011be8 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java @@ -6,6 +6,7 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData; @@ -80,19 +81,8 @@ public class Protocol1_14To1_13_2 extends Protocol { registerOutgoing(State.PLAY, 0x4B, 0x4F); registerOutgoing(State.PLAY, 0x4C, 0x52); - // Sound Effect - registerOutgoing(State.PLAY, 0x4D, 0x51, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // Sound Id - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - wrapper.set(Type.VAR_INT, 0, getNewSoundId(wrapper.get(Type.VAR_INT, 0))); - } - }); - } - }); + new SoundRewriter(this, id -> MappingData.soundMappings.getNewId(id)).registerSound(0x4D, 0x51); + registerOutgoing(State.PLAY, 0x4E, 0x53); registerOutgoing(State.PLAY, 0x4F, 0x55); registerOutgoing(State.PLAY, 0x50, 0x56); @@ -262,15 +252,6 @@ public class Protocol1_14To1_13_2 extends Protocol { WorldPackets.caveAir = MappingData.blockStateMappings.getNewId(8592); } - public static int getNewSoundId(int id) { - int newId = MappingData.soundMappings.getNewId(id); - if (newId == -1) { - Via.getPlatform().getLogger().warning("Missing 1.14 sound for 1.13.2 sound " + id); - return 0; - } - return newId; - } - public static int getNewBlockStateId(int id) { int newId = MappingData.blockStateMappings.getNewId(id); if (newId == -1) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java index 88078cd08..0ddb1d23b 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java @@ -6,6 +6,7 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.rewriters.TagRewriter; import us.myles.ViaVersion.api.rewriters.TagType; import us.myles.ViaVersion.api.type.Type; @@ -36,33 +37,9 @@ public class Protocol1_15To1_14_4 extends Protocol { WorldPackets.register(this); InventoryPackets.register(this); - // Entity Sound Effect (added somewhere in 1.14) - registerOutgoing(State.PLAY, 0x50, 0x51, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // Sound Id - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - wrapper.set(Type.VAR_INT, 0, MappingData.soundMappings.getNewId(wrapper.get(Type.VAR_INT, 0))); - } - }); - } - }); - - // Sound Effect - registerOutgoing(State.PLAY, 0x51, 0x52, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // Sound Id - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - wrapper.set(Type.VAR_INT, 0, MappingData.soundMappings.getNewId(wrapper.get(Type.VAR_INT, 0))); - } - }); - } - }); + SoundRewriter soundRewriter = new SoundRewriter(this, id -> MappingData.soundMappings.getNewId(id)); + soundRewriter.registerSound(0x50, 0x51); // Entity Sound Effect (added somewhere in 1.14) + soundRewriter.registerSound(0x51, 0x52); // Edit Book registerIncoming(State.PLAY, 0x0C, 0x0C, new PacketRemapper() { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java index bc8f4396f..7e441fcd2 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java @@ -4,6 +4,7 @@ import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.rewriters.SoundRewriter; import us.myles.ViaVersion.api.rewriters.TagRewriter; import us.myles.ViaVersion.api.rewriters.TagType; import us.myles.ViaVersion.api.type.Type; @@ -60,23 +61,9 @@ public class Protocol1_16To1_15_2 extends Protocol { } }); - // Entity Sound Effect - registerOutgoing(State.PLAY, 0x51, 0x51, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // Sound Id - handler(wrapper -> wrapper.set(Type.VAR_INT, 0, MappingData.soundMappings.getNewId(wrapper.get(Type.VAR_INT, 0)))); - } - }); - - // Sound Effect - registerOutgoing(State.PLAY, 0x52, 0x52, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // Sound Id - handler(wrapper -> wrapper.set(Type.VAR_INT, 0, MappingData.soundMappings.getNewId(wrapper.get(Type.VAR_INT, 0)))); - } - }); + SoundRewriter soundRewriter = new SoundRewriter(this, id -> MappingData.soundMappings.getNewId(id)); + soundRewriter.registerSound(0x51, 0x51); + soundRewriter.registerSound(0x52, 0x52); // Edit Book registerIncoming(State.PLAY, 0x0C, 0x0C, new PacketRemapper() { From 799c933d958de4fefc17b5292469cf3b41207017 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Sat, 23 May 2020 11:14:50 +0200 Subject: [PATCH 2/2] Make SoundRewriter fields protected --- .../java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java index 4c795f10d..740ba84e2 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/SoundRewriter.java @@ -6,9 +6,9 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; public class SoundRewriter { - private final Protocol protocol; + protected final Protocol protocol; // Can't hold the mappings instance here since it's loaded later - private final IdRewriteFunction idRewriter; + protected final IdRewriteFunction idRewriter; public SoundRewriter(Protocol protocol, IdRewriteFunction idRewriter) { this.protocol = protocol;