diff --git a/patches/server/Book-size-limits.patch b/patches/server/Book-size-limits.patch index 3fde33a6c3..1a31776a61 100644 --- a/patches/server/Book-size-limits.patch +++ b/patches/server/Book-size-limits.patch @@ -5,6 +5,22 @@ Subject: [PATCH] Book size limits Puts some limits on the size of books. +diff --git a/src/main/java/net/minecraft/network/protocol/game/ServerboundEditBookPacket.java b/src/main/java/net/minecraft/network/protocol/game/ServerboundEditBookPacket.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/network/protocol/game/ServerboundEditBookPacket.java ++++ b/src/main/java/net/minecraft/network/protocol/game/ServerboundEditBookPacket.java +@@ -0,0 +0,0 @@ public record ServerboundEditBookPacket(int slot, List pages, Optional STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.VAR_INT, + ServerboundEditBookPacket::slot, +- ByteBufCodecs.stringUtf8(8192).apply(ByteBufCodecs.list(200)), ++ ByteBufCodecs.stringUtf8(net.minecraft.world.item.component.WritableBookContent.PAGE_EDIT_LENGTH).apply(ByteBufCodecs.list(net.minecraft.world.item.component.WritableBookContent.MAX_PAGES)), // Paper - limit books + ServerboundEditBookPacket::pages, +- ByteBufCodecs.stringUtf8(128).apply(ByteBufCodecs::optional), ++ ByteBufCodecs.stringUtf8(net.minecraft.world.item.component.WrittenBookContent.TITLE_MAX_LENGTH).apply(ByteBufCodecs::optional), // Paper - limit books + ServerboundEditBookPacket::title, + ServerboundEditBookPacket::new + ); diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java @@ -15,28 +31,31 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 public void handleEditBook(ServerboundEditBookPacket packet) { + // Paper start - Book size limits + if (!this.cserver.isPrimaryThread()) { -+ List pageList = packet.pages(); ++ final List pageList = packet.pages(); + long byteTotal = 0; -+ int maxBookPageSize = io.papermc.paper.configuration.GlobalConfiguration.get().itemValidation.bookSize.pageMax; -+ double multiplier = Math.clamp(io.papermc.paper.configuration.GlobalConfiguration.get().itemValidation.bookSize.totalMultiplier, 0.3D, 1D); ++ final int maxBookPageSize = io.papermc.paper.configuration.GlobalConfiguration.get().itemValidation.bookSize.pageMax; ++ final double multiplier = Math.clamp(io.papermc.paper.configuration.GlobalConfiguration.get().itemValidation.bookSize.totalMultiplier, 0.3D, 1D); + long byteAllowed = maxBookPageSize; -+ for (String testString : pageList) { -+ int byteLength = testString.getBytes(java.nio.charset.StandardCharsets.UTF_8).length; ++ for (final String page : pageList) { ++ final int byteLength = page.getBytes(java.nio.charset.StandardCharsets.UTF_8).length; + byteTotal += byteLength; -+ int length = testString.length(); -+ int multibytes = 0; ++ final int length = page.length(); ++ int multiByteCharacters = 0; + if (byteLength != length) { -+ for (char c : testString.toCharArray()) { ++ // Count the number of multi byte characters ++ for (final char c : page.toCharArray()) { + if (c > 127) { -+ multibytes++; ++ multiByteCharacters++; + } + } + } ++ ++ // Allow pages with fewer characters to consume less of the allowed byte quota + byteAllowed += maxBookPageSize * Math.clamp((double) length / 255D, 0.1D, 1) * multiplier; + -+ if (multibytes > 1) { -+ // penalize MB -+ byteAllowed -= multibytes; ++ if (multiByteCharacters > 1) { ++ // Penalize multibyte characters ++ byteAllowed -= multiByteCharacters; + } + } +