From 5c3dff17f2c2863b1fd99259fd9e6e387f84da2a Mon Sep 17 00:00:00 2001
From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
Date: Mon, 5 Apr 2021 18:46:31 -0700
Subject: [PATCH] Fix PlayerEditBookEvent (#5463)

---
 ...erEditBookEvent-to-fire-for-off-hand.patch | 19 +++++++++++++++++++
 ...ignore-result-of-PlayerEditBookEvent.patch | 19 +++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 Spigot-API-Patches/Allow-PlayerEditBookEvent-to-fire-for-off-hand.patch
 create mode 100644 Spigot-Server-Patches/Don-t-ignore-result-of-PlayerEditBookEvent.patch

diff --git a/Spigot-API-Patches/Allow-PlayerEditBookEvent-to-fire-for-off-hand.patch b/Spigot-API-Patches/Allow-PlayerEditBookEvent-to-fire-for-off-hand.patch
new file mode 100644
index 0000000000..8d1dbdeef7
--- /dev/null
+++ b/Spigot-API-Patches/Allow-PlayerEditBookEvent-to-fire-for-off-hand.patch
@@ -0,0 +1,19 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
+Date: Mon, 5 Apr 2021 18:35:37 -0700
+Subject: [PATCH] Allow PlayerEditBookEvent to fire for off hand
+
+
+diff --git a/src/main/java/org/bukkit/event/player/PlayerEditBookEvent.java b/src/main/java/org/bukkit/event/player/PlayerEditBookEvent.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/org/bukkit/event/player/PlayerEditBookEvent.java
++++ b/src/main/java/org/bukkit/event/player/PlayerEditBookEvent.java
+@@ -0,0 +0,0 @@ public class PlayerEditBookEvent extends PlayerEvent implements Cancellable {
+     public PlayerEditBookEvent(@NotNull Player who, int slot, @NotNull BookMeta previousBookMeta, @NotNull BookMeta newBookMeta, boolean isSigning) {
+         super(who);
+ 
+-        Validate.isTrue(slot >= -1 && slot <= 8, "Slot must be in range (-1)-8 inclusive");
++        Validate.isTrue(slot == 40 || slot >= -1 && slot <= 8, "Slot must be in range (-1)-8 inclusive"); // Paper - off hand is 40
+         Validate.notNull(previousBookMeta, "Previous book meta must not be null");
+         Validate.notNull(newBookMeta, "New book meta must not be null");
+ 
diff --git a/Spigot-Server-Patches/Don-t-ignore-result-of-PlayerEditBookEvent.patch b/Spigot-Server-Patches/Don-t-ignore-result-of-PlayerEditBookEvent.patch
new file mode 100644
index 0000000000..d7ceb2f270
--- /dev/null
+++ b/Spigot-Server-Patches/Don-t-ignore-result-of-PlayerEditBookEvent.patch
@@ -0,0 +1,19 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
+Date: Mon, 5 Apr 2021 18:35:15 -0700
+Subject: [PATCH] Don't ignore result of PlayerEditBookEvent
+
+
+diff --git a/src/main/java/net/minecraft/server/network/PlayerConnection.java b/src/main/java/net/minecraft/server/network/PlayerConnection.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/net/minecraft/server/network/PlayerConnection.java
++++ b/src/main/java/net/minecraft/server/network/PlayerConnection.java
+@@ -0,0 +0,0 @@ public class PlayerConnection implements PacketListenerPlayIn {
+             list.stream().map(NBTTagString::a).forEach(nbttaglist::add);
+             ItemStack old = itemstack.cloneItemStack(); // CraftBukkit
+             itemstack.a("pages", (NBTBase) nbttaglist);
+-            CraftEventFactory.handleEditBookEvent(player, i, old, itemstack); // CraftBukkit
++            this.player.inventory.setItem(i, CraftEventFactory.handleEditBookEvent(player, i, old, itemstack)); // CraftBukkit // Paper - Don't ignore result (see other callsite for handleEditBookEvent)
+         }
+     }
+