From 383281cb9fcd52c964cd9e01e70e928b61e80ff8 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 1 Dec 2022 17:21:37 +0100 Subject: [PATCH] 1.19.3-rc1 --- build.gradle.kts | 2 +- .../api/data/BackwardsMappings.java | 2 +- .../api/rewriters/SoundRewriter.java | 35 ++++++++- .../data/BackwardsMappings.java | 4 +- .../data/BackwardsMappings.java | 2 +- .../data/BackwardsMappings.java | 2 +- .../data/BackwardsMappings.java | 4 +- .../Protocol1_19_1To1_19_3.java | 74 ++++++++++++++++--- .../data/BackwardsMappings.java | 46 ++++++++++++ 9 files changed, 151 insertions(+), 20 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/data/BackwardsMappings.java diff --git a/build.gradle.kts b/build.gradle.kts index fa162573..dd7eef05 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { allprojects { group = "com.viaversion" - version = "4.5.0-1.19.3-pre3-SNAPSHOT" + version = "4.5.0-1.19.3-rc1-SNAPSHOT" description = "Allow older clients to join newer server versions." } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java index a9247796..25b6abb7 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java @@ -104,7 +104,7 @@ public class BackwardsMappings extends MappingDataBase { /** * To be overridden. */ - protected void loadVBExtras(JsonObject oldMappings, JsonObject newMappings) { + protected void loadVBExtras(JsonObject unmapped, JsonObject mapped) { } protected boolean shouldWarnOnMissing(String key) { diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/SoundRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/SoundRewriter.java index 5bb5b5d4..4a15b8d7 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/SoundRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/SoundRewriter.java @@ -55,7 +55,10 @@ public class SoundRewriter extends com.viaversion.viaversion.rewriter.SoundRewri return wrapper -> { String soundId = wrapper.get(Type.STRING, 0); String mappedId = protocol.getMappingData().getMappedNamedSound(soundId); - if (mappedId == null) return; + if (mappedId == null) { + return; + } + if (!mappedId.isEmpty()) { wrapper.set(Type.STRING, 0, mappedId); } else { @@ -89,4 +92,34 @@ public class SoundRewriter extends com.viaversion.viaversion.rewriter.SoundRewri } }; } + + public PacketHandler get1_19_3SoundHandler() { + return wrapper -> { + final int soundId = wrapper.read(Type.VAR_INT); + if (soundId != 0) { + final int mappedId = idRewriter.rewrite(soundId - 1); // Normalize the id + if (mappedId == -1) { + wrapper.cancel(); + return; + } + + wrapper.write(Type.VAR_INT, mappedId + 1); + } + + // Is followed by the resource loation + wrapper.write(Type.VAR_INT, 0); + + String soundIdentifier = wrapper.read(Type.STRING); + final String mappedIdentifier = protocol.getMappingData().getMappedNamedSound(soundIdentifier); + if (mappedIdentifier != null) { + if (mappedIdentifier.isEmpty()) { + wrapper.cancel(); + return; + } + + soundIdentifier = mappedIdentifier; + } + wrapper.write(Type.STRING, soundIdentifier); + }; + } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java index 4f8845ed..d76f851d 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java @@ -47,9 +47,9 @@ public class BackwardsMappings extends com.viaversion.viabackwards.api.data.Back } @Override - public void loadVBExtras(JsonObject oldMappings, JsonObject newMappings) { + public void loadVBExtras(JsonObject unmapped, JsonObject mapped) { enchantmentMappings = VBMappings.vbBuilder().warnOnMissing(false) - .unmapped(oldMappings.getAsJsonArray("enchantments")).mapped(newMappings.getAsJsonObject("legacy_enchantments")).build(); + .unmapped(unmapped.getAsJsonArray("enchantments")).mapped(mapped.getAsJsonObject("legacy_enchantments")).build(); for (Map.Entry entry : StatisticMappings.CUSTOM_STATS.entrySet()) { statisticMappings.put(entry.getValue().intValue(), entry.getKey()); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java index 8cd99e02..2a664441 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/BackwardsMappings.java @@ -31,7 +31,7 @@ public class BackwardsMappings extends com.viaversion.viabackwards.api.data.Back } @Override - protected void loadVBExtras(JsonObject oldMappings, JsonObject newMappings) { + protected void loadVBExtras(JsonObject unmapped, JsonObject mapped) { for (Map.Entry entry : Protocol1_16To1_15_2.MAPPINGS.getAttributeMappings().entrySet()) { attributeMappings.put(entry.getValue(), entry.getKey()); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/data/BackwardsMappings.java index 4686a8d4..0936669a 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/data/BackwardsMappings.java @@ -32,7 +32,7 @@ public final class BackwardsMappings extends com.viaversion.viabackwards.api.dat } @Override - protected void loadVBExtras(final JsonObject oldMappings, final JsonObject newMappings) { + protected void loadVBExtras(final JsonObject unmapped, final JsonObject mapped) { for (final Object2IntMap.Entry entry : Protocol1_18To1_17_1.MAPPINGS.blockEntityIds().object2IntEntrySet()) { blockEntities.put(entry.getIntValue(), entry.getKey()); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/data/BackwardsMappings.java index df36b3da..bdce2274 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/data/BackwardsMappings.java @@ -43,9 +43,9 @@ public final class BackwardsMappings extends com.viaversion.viabackwards.api.dat } @Override - protected void loadVBExtras(final JsonObject oldMappings, final JsonObject newMappings) { + protected void loadVBExtras(final JsonObject unmapped, final JsonObject mapped) { int i = 0; - final JsonArray types = oldMappings.getAsJsonArray("argumenttypes"); + final JsonArray types = unmapped.getAsJsonArray("argumenttypes"); this.argumentTypes = new String[types.size()]; for (final JsonElement element : types) { final String id = element.getAsString(); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java index 7fd97fbd..a9016664 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java @@ -19,9 +19,9 @@ package com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3; import com.google.common.base.Preconditions; import com.viaversion.viabackwards.api.BackwardsProtocol; -import com.viaversion.viabackwards.api.data.BackwardsMappings; import com.viaversion.viabackwards.api.rewriters.SoundRewriter; import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; +import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.data.BackwardsMappings; import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.packets.BlockItemPackets1_19_3; import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.packets.EntityPackets1_19_3; import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.storage.ChatSessionStorage; @@ -59,7 +59,7 @@ import java.util.BitSet; public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol { - public static final BackwardsMappings MAPPINGS = new BackwardsMappings("1.19.3", "1.19", Protocol1_19_3To1_19_1.class, true); + public static final BackwardsMappings MAPPINGS = new BackwardsMappings(); private static final ByteArrayType.OptionalByteArrayType OPTIONAL_SIGNATURE_BYTES_TYPE = new ByteArrayType.OptionalByteArrayType(256); private static final ByteArrayType SIGNATURE_BYTES_TYPE = new ByteArrayType(256); private final EntityPackets1_19_3 entityRewriter = new EntityPackets1_19_3(this); @@ -93,22 +93,74 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol { - final int soundId = wrapper.read(Type.VAR_INT); - if (soundId == 0) { - wrapper.passthrough(Type.STRING); // String identifier - wrapper.read(Type.OPTIONAL_FLOAT); // Fixed range - wrapper.setPacketType(ClientboundPackets1_19_1.NAMED_SOUND); + final int soundId = wrapper.read(Type.VAR_INT) - 1; // Normalize the id + if (soundId != -1) { + final int mappedId = MAPPINGS.getSoundMappings().getNewId(soundId); + if (mappedId == -1) { + wrapper.cancel(); + return; + } + + wrapper.write(Type.VAR_INT, mappedId + 1); return; } - // Normalize the sound id - wrapper.write(Type.VAR_INT, soundId - 1); + String soundIdentifier = wrapper.read(Type.STRING); + wrapper.read(Type.OPTIONAL_FLOAT); // Fixed range + final String mappedIdentifier = MAPPINGS.getMappedNamedSound(soundIdentifier); + if (mappedIdentifier != null) { + if (mappedIdentifier.isEmpty()) { + wrapper.cancel(); + return; + } + + soundIdentifier = mappedIdentifier; + } + + wrapper.write(Type.STRING, soundIdentifier); + wrapper.setPacketType(ClientboundPackets1_19_1.NAMED_SOUND); + }); + } + }); + registerClientbound(ClientboundPackets1_19_3.ENTITY_SOUND, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + final int soundId = wrapper.read(Type.VAR_INT) - 1; // Normalize the id + if (soundId != -1) { + final int mappedId = MAPPINGS.getSoundMappings().getNewId(soundId); + if (mappedId == -1) { + wrapper.cancel(); + return; + } + + wrapper.write(Type.VAR_INT, mappedId + 1); + } + + // Convert the resource location to the corresponding integer id + String soundIdentifier = wrapper.read(Type.STRING); + wrapper.read(Type.OPTIONAL_FLOAT); // Fixed range + final String mappedIdentifier = MAPPINGS.getMappedNamedSound(soundIdentifier); + if (mappedIdentifier != null) { + if (mappedIdentifier.isEmpty()) { + wrapper.cancel(); + return; + } + + soundIdentifier = mappedIdentifier; + } + + final int mappedId = MAPPINGS.mappedSound(soundIdentifier); + if (mappedId == -1) { + wrapper.cancel(); + return; + } + + wrapper.write(Type.VAR_INT, mappedId); }); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/data/BackwardsMappings.java new file mode 100644 index 00000000..9a11727a --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/data/BackwardsMappings.java @@ -0,0 +1,46 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2022 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.data; + +import com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap; +import com.viaversion.viaversion.libs.fastutil.objects.Object2IntOpenHashMap; +import com.viaversion.viaversion.libs.gson.JsonElement; +import com.viaversion.viaversion.libs.gson.JsonObject; +import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.Protocol1_19_3To1_19_1; + +public final class BackwardsMappings extends com.viaversion.viabackwards.api.data.BackwardsMappings { + + private final Object2IntMap mappedSounds = new Object2IntOpenHashMap<>(); + + public BackwardsMappings() { + super("1.19.3", "1.19", Protocol1_19_3To1_19_1.class, true); + mappedSounds.defaultReturnValue(-1); + } + + @Override + protected void loadVBExtras(final JsonObject unmapped, final JsonObject mapped) { + int i = 0; + for (final JsonElement sound : mapped.getAsJsonArray("sounds")) { + mappedSounds.put(sound.getAsString(), i++); + } + } + + public int mappedSound(final String sound) { + return mappedSounds.getInt(sound.replace("minecraft:", "")); + } +} \ No newline at end of file