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
This commit is contained in:
Shane Freeder 2019-01-12 13:59:06 +00:00
parent c42ec4782e
commit a5e0335065
No known key found for this signature in database
GPG key ID: A3F61EA5A085289C
2 changed files with 30 additions and 17 deletions

View file

@ -1,4 +1,4 @@
From 903f06b5ea3616e0b4054b28c28d9d0b4b42a1aa Mon Sep 17 00:00:00 2001
From 74ffd7f2292be3294d8d8c5f75525b6960266933 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Fri, 16 Nov 2018 23:08:50 -0500
Subject: [PATCH] Book Size Limits
@ -22,10 +22,19 @@ 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
@@ -747,6 +747,38 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
@@ -12,6 +12,8 @@ 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;
@@ -747,6 +749,42 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
}
public void a(PacketPlayInBEdit packetplayinbedit) {
@ -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;
+ int length = testString.length();
+ 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;
+
+ if (multibytes > 1) {
+ // 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;
+ }
+ 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;
+ }
+ byteAllowed += (maxBookPageSize * Math.min(1, Math.max(0.1D, (double) length / 255D))) * multiplier;
+ multiplier *= multiplier;
+
+ if (multibytes > 1) {
+ // penalize MB some more
+ byteAllowed -= length;
+ }
+ }
+ }
+ // Paper end
// CraftBukkit start

View file

@ -1,4 +1,4 @@
From 84094387063f1158ef9192709e67e222cb39c5ed Mon Sep 17 00:00:00 2001
From 73b51681652bfcf42e775aaff54db6c4d29b6ce9 Mon Sep 17 00:00:00 2001
From: Michael Himing <mhiming@gmail.com>
Date: Sun, 16 Dec 2018 13:07:33 +1100
Subject: [PATCH] Fix PlayerEditBookEvent
@ -10,10 +10,10 @@ 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
@@ -816,10 +816,13 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
@@ -822,10 +822,13 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
this.player.setSlot(enumitemslot, CraftEventFactory.handleEditBookEvent(player, enumitemslot, itemstack1, itemstack2)); // CraftBukkit
} else {