From d62165bf51ea0a43382bb3e5da9362967a63a20b Mon Sep 17 00:00:00 2001 From: KennyTV Date: Thu, 10 Jun 2021 18:51:12 +0200 Subject: [PATCH] 1.17.1-pre1 --- build.gradle.kts | 2 +- .../api/ViaBackwardsPlatform.java | 2 + .../Protocol1_17To1_17_1.java | 132 ++++++++++++++++++ .../storage/InventoryStateIds.java | 39 ++++++ gradle/libs.versions.toml | 2 +- settings.gradle.kts | 1 + 6 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17to1_17_1/Protocol1_17To1_17_1.java create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17to1_17_1/storage/InventoryStateIds.java diff --git a/build.gradle.kts b/build.gradle.kts index 7084ea11..a8cec13f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { allprojects { group = "com.viaversion" - version = "4.0.1-SNAPSHOT" + version = "4.0.1-1.17.1-pre1-SNAPSHOT" description = "Allow older clients to join newer server versions." } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsPlatform.java b/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsPlatform.java index c5906428..63f936a6 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsPlatform.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsPlatform.java @@ -43,6 +43,7 @@ import com.viaversion.viabackwards.protocol.protocol1_16_2to1_16_3.Protocol1_16_ import com.viaversion.viabackwards.protocol.protocol1_16_3to1_16_4.Protocol1_16_3To1_16_4; import com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.Protocol1_16_4To1_17; import com.viaversion.viabackwards.protocol.protocol1_16to1_16_1.Protocol1_16To1_16_1; +import com.viaversion.viabackwards.protocol.protocol1_17to1_17_1.Protocol1_17To1_17_1; import com.viaversion.viabackwards.protocol.protocol1_9_4to1_10.Protocol1_9_4To1_10; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.protocol.ProtocolManager; @@ -104,6 +105,7 @@ public interface ViaBackwardsPlatform { protocolManager.registerProtocol(new Protocol1_16_3To1_16_4(), ProtocolVersion.v1_16_3, ProtocolVersion.v1_16_4); protocolManager.registerProtocol(new Protocol1_16_4To1_17(), ProtocolVersion.v1_16_4, ProtocolVersion.v1_17); + protocolManager.registerProtocol(new Protocol1_17To1_17_1(), ProtocolVersion.v1_17, ProtocolVersion.v1_17_1); } /** diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17to1_17_1/Protocol1_17To1_17_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17to1_17_1/Protocol1_17To1_17_1.java new file mode 100644 index 00000000..d6642824 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17to1_17_1/Protocol1_17To1_17_1.java @@ -0,0 +1,132 @@ +/* + * 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_17to1_17_1; + +import com.viaversion.viabackwards.api.BackwardsProtocol; +import com.viaversion.viabackwards.protocol.protocol1_17to1_17_1.storage.InventoryStateIds; +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.item.Item; +import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; +import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17; +import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17; + +public final class Protocol1_17To1_17_1 extends BackwardsProtocol { + + private static final int MAX_PAGE_LENGTH = 8192; + private static final int MAX_TITLE_LENGTH = 128; + + public Protocol1_17To1_17_1() { + super(ClientboundPackets1_17.class, ClientboundPackets1_17.class, ServerboundPackets1_17.class, ServerboundPackets1_17.class); + } + + @Override + protected void registerPackets() { + registerClientbound(ClientboundPackets1_17.SET_SLOT, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); + int stateId = wrapper.read(Type.VAR_INT); + wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId); + }); + } + }); + registerClientbound(ClientboundPackets1_17.WINDOW_ITEMS, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); + int stateId = wrapper.read(Type.VAR_INT); + wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId); + + // Length is encoded as a var int in 1.17.1 + wrapper.write(Type.FLAT_VAR_INT_ITEM_ARRAY, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT)); + + // Carried item - should work without adding it to the array above + wrapper.read(Type.FLAT_VAR_INT_ITEM); + }); + } + }); + + registerServerbound(ServerboundPackets1_17.CLICK_WINDOW, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE); + int stateId = wrapper.user().get(InventoryStateIds.class).removeStateId(containerId); + wrapper.write(Type.VAR_INT, stateId == Integer.MAX_VALUE ? 0 : stateId); + }); + } + }); + + registerServerbound(ServerboundPackets1_17.EDIT_BOOK, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM); + boolean signing = wrapper.read(Type.BOOLEAN); + wrapper.passthrough(Type.VAR_INT); // Slot comes first + + CompoundTag tag = item.tag(); + ListTag pagesTag; + StringTag titleTag = null; + if (tag == null || (pagesTag = tag.get("pages")) == null + || (signing && (titleTag = tag.get("title")) == null)) { + wrapper.write(Type.VAR_INT, 0); // Pages length + wrapper.write(Type.BOOLEAN, false); // Optional title + return; + } + + // Write pages + wrapper.write(Type.VAR_INT, pagesTag.size()); + for (Tag pageTag : pagesTag) { + String page = ((StringTag) pageTag).getValue(); + if (page.length() > MAX_PAGE_LENGTH) { + page = page.substring(0, MAX_PAGE_LENGTH); + } + wrapper.write(Type.STRING, page); + } + + // Write optional title + wrapper.write(Type.BOOLEAN, signing); + if (signing) { + if (titleTag == null) { + titleTag = tag.get("title"); + } + + String title = titleTag.getValue(); + if (title.length() > MAX_TITLE_LENGTH) { + title = title.substring(0, MAX_TITLE_LENGTH); + } + wrapper.write(Type.STRING, title); + } + }); + } + }); + } + + @Override + public void init(UserConnection connection) { + connection.put(new InventoryStateIds()); + } +} diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17to1_17_1/storage/InventoryStateIds.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17to1_17_1/storage/InventoryStateIds.java new file mode 100644 index 00000000..637505ab --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17to1_17_1/storage/InventoryStateIds.java @@ -0,0 +1,39 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * 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_17to1_17_1.storage; + +import com.viaversion.viaversion.api.connection.StorableObject; +import com.viaversion.viaversion.libs.fastutil.ints.Int2IntMap; +import com.viaversion.viaversion.libs.fastutil.ints.Int2IntOpenHashMap; + +public final class InventoryStateIds implements StorableObject { + private final Int2IntMap ids = new Int2IntOpenHashMap(); + + public InventoryStateIds() { + // Inventory ids are unsigned bytes, so this is safe to do + ids.defaultReturnValue(Integer.MAX_VALUE); + } + + public void setStateId(short containerId, int id) { + ids.put(containerId, id); + } + + public int removeStateId(short containerId) { + return ids.remove(containerId); + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7d93b567..0815a7c3 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.1-SNAPSHOT" +viaver = "4.0.1-1.17.1-pre1-SNAPSHOT" # Common provided netty = "4.0.20.Final" diff --git a/settings.gradle.kts b/settings.gradle.kts index 9bd54f5a..c504fdac 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -5,6 +5,7 @@ rootProject.name = "viabackwards-parent" dependencyResolutionManagement { repositories { + mavenLocal() maven("https://repo.viaversion.com") maven("https://papermc.io/repo/repository/maven-public/") maven("https://oss.sonatype.org/content/repositories/snapshots/")