From 0e7c9559ecc4ba329d205f6812195f11b04cee63 Mon Sep 17 00:00:00 2001 From: Shane Freeder Date: Sat, 12 Jan 2019 13:59:06 +0000 Subject: [PATCH] Address some issues with book limits (#1798) The multibyte calculation has been fixed, now we actually work out which characters take more than a byte. Diminishing returns has been modified, previously the multiplier would zero itself out due to floating point limitations --- Spigot-Server-Patches/Book-Size-Limits.patch | 37 +++++++++++++------ .../Fix-PlayerEditBookEvent.patch | 2 +- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/Spigot-Server-Patches/Book-Size-Limits.patch b/Spigot-Server-Patches/Book-Size-Limits.patch index e07f2c5877..01800cec4b 100644 --- a/Spigot-Server-Patches/Book-Size-Limits.patch +++ b/Spigot-Server-Patches/Book-Size-Limits.patch @@ -22,9 +22,18 @@ index fef899ae0..468aff713 100644 + } } diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java -index 7ae374085..bbdf729c3 100644 +index cb6d1deb8..8d06b23ea 100644 --- a/src/main/java/net/minecraft/server/PlayerConnection.java +++ b/src/main/java/net/minecraft/server/PlayerConnection.java +@@ -0,0 +0,0 @@ import java.util.Collections; + import java.util.Iterator; + import java.util.Set; + import javax.annotation.Nullable; ++ ++import org.apache.commons.lang3.StringEscapeUtils; + import org.apache.commons.lang3.StringUtils; + import org.apache.logging.log4j.LogManager; + import org.apache.logging.log4j.Logger; @@ -0,0 +0,0 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable { } @@ -41,24 +50,28 @@ index 7ae374085..bbdf729c3 100644 + String testString = pageList.getString(i); + int byteLength = testString.getBytes(java.nio.charset.StandardCharsets.UTF_8).length; + byteTotal += byteLength; -+ if (byteTotal > byteAllowed) { -+ PlayerConnection.LOGGER.warn(this.player.getName() + " tried to send too large of a book. Book Size: " + byteTotal + " - Allowed: "+ byteAllowed + " - Pages: " + pageList.size()); -+ minecraftServer.postToMainThread(() -> this.disconnect("Book too large!")); -+ return; -+ } + int length = testString.length(); -+ int multibytes = byteLength == length ? byteLength : (int) Math.round((double) byteLength / (double) length); -+ for (int x = 1; x < multibytes; x++) { -+ multiplier *= multiplier; ++ int multibytes = 0; ++ if (byteLength != length) { ++ for (char c : testString.toCharArray()) { ++ if (c > 127) { ++ multibytes++; ++ } ++ } + } + byteAllowed += (maxBookPageSize * Math.min(1, Math.max(0.1D, (double) length / 255D))) * multiplier; -+ multiplier *= multiplier; + + if (multibytes > 1) { -+ // penalize MB some more -+ byteAllowed -= length; ++ // penalize MB ++ byteAllowed -= multibytes; + } + } ++ ++ if (byteTotal > byteAllowed) { ++ PlayerConnection.LOGGER.warn(this.player.getName() + " tried to send too large of a book. Book Size: " + byteTotal + " - Allowed: "+ byteAllowed + " - Pages: " + pageList.size()); ++ minecraftServer.postToMainThread(() -> this.disconnect("Book too large!")); ++ return; ++ } + } + // Paper end // CraftBukkit start diff --git a/Spigot-Server-Patches/Fix-PlayerEditBookEvent.patch b/Spigot-Server-Patches/Fix-PlayerEditBookEvent.patch index 804d9e0819..6423deeadb 100644 --- a/Spigot-Server-Patches/Fix-PlayerEditBookEvent.patch +++ b/Spigot-Server-Patches/Fix-PlayerEditBookEvent.patch @@ -10,7 +10,7 @@ it impossible to properly cancel the event or modify the book meta cancelled writing diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java -index 74c570615..9741d3b23 100644 +index 8d06b23ea..a8a6e236e 100644 --- a/src/main/java/net/minecraft/server/PlayerConnection.java +++ b/src/main/java/net/minecraft/server/PlayerConnection.java @@ -0,0 +0,0 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {