From 455bb546f0606927038a0a86cda0de1cd474bb2d Mon Sep 17 00:00:00 2001 From: kennytv Date: Sat, 17 Jul 2021 11:11:40 +0200 Subject: [PATCH] Add option to replace ping packets by using container acks Closes ViaVersion/ViaVersion#2624 --- .../viabackwards/ViaBackwardsConfig.java | 7 ++++ .../viabackwards/api/ViaBackwardsConfig.java | 2 ++ .../packets/BlockItemPackets1_12.java | 2 +- .../Protocol1_16_4To1_17.java | 18 +++++++++- .../packets/BlockItemPackets1_17.java | 25 +++++++++++++- .../storage/PingRequests.java | 34 +++++++++++++++++++ .../resources/assets/viabackwards/config.yml | 6 +++- 7 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/storage/PingRequests.java diff --git a/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java b/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java index 2dd7bce9..ae08adfe 100644 --- a/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java +++ b/common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java @@ -31,6 +31,7 @@ public class ViaBackwardsConfig extends Config implements com.viaversion.viaback private boolean addTeamColorToPrefix; private boolean fix1_13FacePlayer; private boolean alwaysShowOriginalMobName; + private boolean handlePingsAsInvAcknowledgements; public ViaBackwardsConfig(File configFile) { super(configFile); @@ -47,6 +48,7 @@ public class ViaBackwardsConfig extends Config implements com.viaversion.viaback addTeamColorToPrefix = getBoolean("add-teamcolor-to-prefix", true); fix1_13FacePlayer = getBoolean("fix-1_13-face-player", false); alwaysShowOriginalMobName = getBoolean("always-show-original-mob-name", true); + handlePingsAsInvAcknowledgements = getBoolean("handle-pings-as-inv-acknowledgements", false); } @Override @@ -69,6 +71,11 @@ public class ViaBackwardsConfig extends Config implements com.viaversion.viaback return alwaysShowOriginalMobName; } + @Override + public boolean handlePingsAsInvAcknowledgements() { + return handlePingsAsInvAcknowledgements || Boolean.getBoolean("com.viaversion.handlePingsAsInvAcknowledgements"); + } + @Override public URL getDefaultConfigURL() { return getClass().getClassLoader().getResource("assets/viabackwards/config.yml"); diff --git a/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsConfig.java b/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsConfig.java index b7aeccc1..1d8740aa 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsConfig.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsConfig.java @@ -46,4 +46,6 @@ public interface ViaBackwardsConfig { * @return true if enabled */ boolean alwaysShowOriginalMobName(); + + boolean handlePingsAsInvAcknowledgements(); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java index ec5b3642..cdac3627 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/BlockItemPackets1_12.java @@ -145,7 +145,7 @@ public class BlockItemPackets1_12 extends LegacyBlockItemRewriter { wrapper.cancel(); - // Plugins expecting a real response will have to handle this accordingly themselves int id = wrapper.read(Type.INT); + short shortId = (short) id; + if (id == shortId && ViaBackwards.getConfig().handlePingsAsInvAcknowledgements()) { + wrapper.user().get(PingRequests.class).addId(shortId); + + // Send inventory acknowledgement to replace ping packet functionality in the unsigned byte range + PacketWrapper acknowledgementPacket = wrapper.create(ClientboundPackets1_16_2.WINDOW_CONFIRMATION); + acknowledgementPacket.write(Type.UNSIGNED_BYTE, (short) 0); // Inventory id + acknowledgementPacket.write(Type.SHORT, shortId); // Confirmation id + acknowledgementPacket.write(Type.BOOLEAN, false); // Accepted + acknowledgementPacket.send(Protocol1_16_4To1_17.class); + return; + } + + // Plugins expecting a real response will have to handle this accordingly themselves PacketWrapper pongPacket = wrapper.create(ServerboundPackets1_17.PONG); pongPacket.write(Type.INT, id); pongPacket.sendToServer(Protocol1_16_4To1_17.class); @@ -233,6 +248,7 @@ public final class Protocol1_16_4To1_17 extends BackwardsProtocol { + wrapper.cancel(); + if (!ViaBackwards.getConfig().handlePingsAsInvAcknowledgements()) { + return; + } + + // Handle ping packet replacement + short inventoryId = wrapper.read(Type.UNSIGNED_BYTE); + short confirmationId = wrapper.read(Type.SHORT); + boolean accepted = wrapper.read(Type.BOOLEAN); + if (inventoryId == 0 && accepted && wrapper.user().get(PingRequests.class).removeId(confirmationId)) { + PacketWrapper pongPacket = wrapper.create(ServerboundPackets1_17.PONG); + pongPacket.write(Type.INT, (int) confirmationId); + pongPacket.sendToServer(Protocol1_16_4To1_17.class); + } + }); + } + }); protocol.registerClientbound(ClientboundPackets1_17.SPAWN_PARTICLE, new PacketRemapper() { @Override diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/storage/PingRequests.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/storage/PingRequests.java new file mode 100644 index 00000000..8c260579 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/storage/PingRequests.java @@ -0,0 +1,34 @@ +/* + * 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_16_4to1_17.storage; + +import com.viaversion.viaversion.api.connection.StorableObject; +import com.viaversion.viaversion.libs.fastutil.ints.IntOpenHashSet; +import com.viaversion.viaversion.libs.fastutil.ints.IntSet; + +public final class PingRequests implements StorableObject { + private final IntSet ids = new IntOpenHashSet(); + + public void addId(short id) { + ids.add(id); + } + + public boolean removeId(short id) { + return ids.remove(id); + } +} diff --git a/common/src/main/resources/assets/viabackwards/config.yml b/common/src/main/resources/assets/viabackwards/config.yml index a0a99605..9abbdbb5 100644 --- a/common/src/main/resources/assets/viabackwards/config.yml +++ b/common/src/main/resources/assets/viabackwards/config.yml @@ -11,4 +11,8 @@ add-custom-enchants-into-lore: true add-teamcolor-to-prefix: true # # Converts the 1.13 face look-at packet for 1.12- players. Requires a bit of extra caching. -fix-1_13-face-player: false \ No newline at end of file +fix-1_13-face-player: false +# +# Sends inventory acknowledgement packets to act as a replacement for ping packets for sub 1.17 clients. +# This only takes effect for ids in the short range. Useful for anticheat compatibility. +handle-pings-as-inv-acknowledgements: false \ No newline at end of file