From e64a0fb62e8573096bee85581c2e5f263654591d Mon Sep 17 00:00:00 2001 From: MrMicky Date: Sun, 3 Oct 2021 10:11:34 +0200 Subject: [PATCH] Prevent kick when signing empty book (#2700) --- .../packets/PlayerPackets.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/PlayerPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/PlayerPackets.java index 39edcf712..c1ed43394 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/PlayerPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/PlayerPackets.java @@ -19,6 +19,7 @@ package com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.ListTag; +import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.minecraft.Position; @@ -31,6 +32,8 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; +import java.util.Collections; + public class PlayerPackets { public static void register(Protocol protocol) { @@ -58,19 +61,24 @@ public class PlayerPackets { Item item = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM); protocol.getItemRewriter().handleItemToServer(item); + if (item == null) return; + + CompoundTag tag = item.tag(); + if (tag == null) return; + + Tag pages = tag.get("pages"); + + // Fix for https://github.com/ViaVersion/ViaVersion/issues/2660 + if (pages == null) { + tag.put("pages", new ListTag(Collections.singletonList(new StringTag()))); + } + // Client limit when editing a book was upped from 50 to 100 in 1.14, but some anti-exploit plugins ban with a size higher than the old client limit - if (Via.getConfig().isTruncate1_14Books()) { - if (item == null) return; - CompoundTag tag = item.tag(); - - if (tag == null) return; - Tag pages = tag.get("pages"); - - if (!(pages instanceof ListTag)) return; - + if (Via.getConfig().isTruncate1_14Books() && pages instanceof ListTag) { ListTag listTag = (ListTag) pages; - if (listTag.size() <= 50) return; - listTag.setValue(listTag.getValue().subList(0, 50)); + if (listTag.size() > 50) { + listTag.setValue(listTag.getValue().subList(0, 50)); + } } } });