From 2194c968838ed248506e3cf5fff2e9ecd5dd9864 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Tue, 11 May 2021 14:02:12 +0200 Subject: [PATCH] Handle new map colors Co-authored-by: Gerrygames --- build.gradle.kts | 2 +- .../api/rewriters/MapColorRewriter.java | 61 +++++++++++++++++++ ...lorRewriter.java => MapColorRewrites.java} | 2 +- .../packets/BlockItemPackets1_16.java | 31 +--------- .../Protocol1_16_4To1_17.java | 17 ------ .../data/MapColorRewrites.java | 45 ++++++++++++++ .../packets/BlockItemPackets1_17.java | 19 ++++++ gradle/libs.versions.toml | 2 +- 8 files changed, 131 insertions(+), 48 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/api/rewriters/MapColorRewriter.java rename common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/{MapColorRewriter.java => MapColorRewrites.java} (98%) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/data/MapColorRewrites.java diff --git a/build.gradle.kts b/build.gradle.kts index 8aab044d..f1fd1044 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { allprojects { group = "com.viaversion" - version = "4.0.0-21w17a" + version = "4.0.0-21w18a" description = "Allow older clients to join newer server versions." } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/MapColorRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/MapColorRewriter.java new file mode 100644 index 00000000..d4cb39f3 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/MapColorRewriter.java @@ -0,0 +1,61 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2021 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.api.rewriters; + +import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.rewriter.IdRewriteFunction; + +public final class MapColorRewriter { + + /** + * Returns a packethandler to rewrite map data color ids. Reading starts from the icon count. + * + * @param rewriter id rewriter returning mapped colors, or -1 if unmapped + * @return packethandler to rewrite map data color ids + */ + public static PacketHandler getRewriteHandler(IdRewriteFunction rewriter) { + return wrapper -> { + int iconCount = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < iconCount; i++) { + wrapper.passthrough(Type.VAR_INT); // Type + wrapper.passthrough(Type.BYTE); // X + wrapper.passthrough(Type.BYTE); // Z + wrapper.passthrough(Type.BYTE); // Direction + if (wrapper.passthrough(Type.BOOLEAN)) { + wrapper.passthrough(Type.COMPONENT); // Display Name + } + } + + short columns = wrapper.passthrough(Type.UNSIGNED_BYTE); + if (columns < 1) return; + + wrapper.passthrough(Type.UNSIGNED_BYTE); // Rows + wrapper.passthrough(Type.UNSIGNED_BYTE); // X + wrapper.passthrough(Type.UNSIGNED_BYTE); // Z + byte[] data = wrapper.passthrough(Type.BYTE_ARRAY_PRIMITIVE); + for (int i = 0; i < data.length; i++) { + int color = data[i] & 0xFF; + int mappedColor = rewriter.rewrite(color); + if (mappedColor != -1) { + data[i] = (byte) mappedColor; + } + } + }; + } +} diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/MapColorRewriter.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/MapColorRewrites.java similarity index 98% rename from common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/MapColorRewriter.java rename to common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/MapColorRewrites.java index ce17bb0a..555bc4e9 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/MapColorRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/data/MapColorRewrites.java @@ -20,7 +20,7 @@ package com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data; import com.viaversion.viaversion.libs.fastutil.ints.Int2IntMap; import com.viaversion.viaversion.libs.fastutil.ints.Int2IntOpenHashMap; -public class MapColorRewriter { +public final class MapColorRewrites { private static final Int2IntMap MAPPINGS = new Int2IntOpenHashMap(); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java index ab4b6cd4..bb7d6474 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java @@ -18,9 +18,10 @@ package com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.packets; import com.viaversion.viabackwards.api.rewriters.EnchantmentRewriter; +import com.viaversion.viabackwards.api.rewriters.MapColorRewriter; import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; -import com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.MapColorRewriter; +import com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.MapColorRewrites; import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; @@ -224,33 +225,7 @@ public class BlockItemPackets1_16 extends com.viaversion.viabackwards.api.rewrit map(Type.BYTE); // Scale map(Type.BOOLEAN); // Tracking Position map(Type.BOOLEAN); // Locked - handler(wrapper -> { - int iconCount = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < iconCount; i++) { - wrapper.passthrough(Type.VAR_INT); // Type - wrapper.passthrough(Type.BYTE); // X - wrapper.passthrough(Type.BYTE); // Z - wrapper.passthrough(Type.BYTE); // Direction - if (wrapper.passthrough(Type.BOOLEAN)) { - wrapper.passthrough(Type.COMPONENT); // Display Name - } - } - - short columns = wrapper.passthrough(Type.UNSIGNED_BYTE); - if (columns < 1) return; - - wrapper.passthrough(Type.UNSIGNED_BYTE); // Rows - wrapper.passthrough(Type.UNSIGNED_BYTE); // X - wrapper.passthrough(Type.UNSIGNED_BYTE); // Z - byte[] data = wrapper.passthrough(Type.BYTE_ARRAY_PRIMITIVE); - for (int i = 0; i < data.length; i++) { - int color = data[i] & 0xFF; - int mappedColor = MapColorRewriter.getMappedColor(color); - if (mappedColor != -1) { - data[i] = (byte) mappedColor; - } - } - }); + handler(MapColorRewriter.getRewriteHandler(MapColorRewrites::getMappedColor)); } }); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java index 06034913..6be4f816 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java @@ -149,23 +149,6 @@ public class Protocol1_16_4To1_17 extends BackwardsProtocol { - wrapper.passthrough(Type.VAR_INT); - wrapper.passthrough(Type.BYTE); - wrapper.write(Type.BOOLEAN, true); // Tracking position - wrapper.passthrough(Type.BOOLEAN); - - boolean hasMarkers = wrapper.read(Type.BOOLEAN); - if (!hasMarkers) { - wrapper.write(Type.VAR_INT, 0); // Array size - } - }); - } - }); - registerClientbound(ClientboundPackets1_17.EXPLOSION, new PacketRemapper() { @Override public void registerMap() { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/data/MapColorRewrites.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/data/MapColorRewrites.java new file mode 100644 index 00000000..3a6dcdf3 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/data/MapColorRewrites.java @@ -0,0 +1,45 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2021 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_16_4to1_17.data; + +import com.viaversion.viaversion.libs.fastutil.ints.Int2IntMap; +import com.viaversion.viaversion.libs.fastutil.ints.Int2IntOpenHashMap; + +public final class MapColorRewrites { + + private static final Int2IntMap MAPPINGS = new Int2IntOpenHashMap(); + + static { + MAPPINGS.put(236, 85); // (70, 70, 70) -> (65, 65, 65) + MAPPINGS.put(237, 27); // (86, 86, 86) -> (88, 88, 88) + MAPPINGS.put(238, 45); // (100, 100, 100) -> (96, 96, 96) + MAPPINGS.put(239, 84); // (52, 52, 52) -> (53, 53, 53) + MAPPINGS.put(240, 144); // (152, 123, 103) -> (147, 124, 113) + MAPPINGS.put(241, 145); // (186, 150, 126) -> (180, 152, 138) + MAPPINGS.put(242, 146); // (216, 175, 147) -> (209, 177, 161) + MAPPINGS.put(243, 147); // (114, 92, 77) -> (110, 93, 85) + MAPPINGS.put(244, 127); // (89, 117, 105) -> (48, 115, 112) + MAPPINGS.put(245, 226); // (109, 144, 129) -> (58, 142, 140) + MAPPINGS.put(246, 124); // (127, 167, 150) -> (64, 154, 150) + MAPPINGS.put(247, 227); // (67, 88, 79) -> (30, 75, 74) + } + + public static int getMappedColor(int color) { + return MAPPINGS.getOrDefault(color, -1); + } +} diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/BlockItemPackets1_17.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/BlockItemPackets1_17.java index 41231179..e4754ae1 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/BlockItemPackets1_17.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/BlockItemPackets1_17.java @@ -18,8 +18,10 @@ package com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.packets; import com.viaversion.viabackwards.api.entities.storage.EntityTracker; +import com.viaversion.viabackwards.api.rewriters.MapColorRewriter; import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.Protocol1_16_4To1_17; +import com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.data.MapColorRewrites; import com.viaversion.viaversion.api.minecraft.BlockChangeRecord; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; @@ -292,6 +294,23 @@ public class BlockItemPackets1_17 extends com.viaversion.viabackwards.api.rewrit }); } }); + + protocol.registerClientbound(ClientboundPackets1_17.MAP_DATA, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // Map ID + map(Type.BYTE); // Scale + create(wrapper -> wrapper.write(Type.BOOLEAN, true)); // Tracking position + map(Type.BOOLEAN); // Locked + handler(wrapper -> { + boolean hasMarkers = wrapper.read(Type.BOOLEAN); + if (!hasMarkers) { + wrapper.write(Type.VAR_INT, 0); // Array size + } + }); + handler(MapColorRewriter.getRewriteHandler(MapColorRewrites::getMappedColor)); + } + }); } private int cutLightMask(long[] mask, int startFromSection) { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f1022566..cfc54659 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,7 +3,7 @@ metadata.format.version = "1.0" [versions] # ViaVersion -viaver = "4.0.0-21w17a" +viaver = "4.0.0-21w18a" # Common provided netty = "4.0.20.Final"