From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
Date: Tue, 3 Nov 2020 01:25:06 -0600
Subject: [PATCH] Allow color codes in books


diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index e71f9a3256eed2361ac8c5c058ceaeeeabbeefa3..e5c08a0305aed04906e5a5578023c9b0b0821381 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -1200,13 +1200,16 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
                 itemstack1.setTag(nbttagcompound.copy());
             }
 
+            // Purpur start
+            boolean hasPerm = getCraftPlayer().hasPermission("purpur.book.color.edit") || getCraftPlayer().hasPermission("purpur.book.color.sign"); // Purpur
             itemstack1.addTagElement("author", (Tag) StringTag.valueOf(this.player.getName().getString()));
             if (this.player.isTextFilteringEnabled()) {
-                itemstack1.addTagElement("title", (Tag) StringTag.valueOf(title.getFiltered()));
+                itemstack1.addTagElement("title", (Tag) StringTag.valueOf(color(title.getFiltered(), hasPerm)));
             } else {
-                itemstack1.addTagElement("filtered_title", (Tag) StringTag.valueOf(title.getFiltered()));
-                itemstack1.addTagElement("title", (Tag) StringTag.valueOf(title.getRaw()));
+                itemstack1.addTagElement("filtered_title", (Tag) StringTag.valueOf(color(title.getFiltered(), hasPerm)));
+                itemstack1.addTagElement("title", (Tag) StringTag.valueOf(color(title.getRaw(), hasPerm)));
             }
+            // Purpur end
 
             this.a(pages, (s) -> {
                 return Component.Serializer.toJson((Component) (new TextComponent(s)));
@@ -1218,10 +1221,13 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
     private void a(List<TextFilter.FilteredText> list, UnaryOperator<String> unaryoperator, ItemStack itemstack, int slot, ItemStack handItem) { // CraftBukkit
         ListTag nbttaglist = new ListTag();
 
+        // Purpur start
+        boolean hasPerm = getCraftPlayer().hasPermission("purpur.book.color.edit");
         if (this.player.isTextFilteringEnabled()) {
-            Stream<StringTag> stream = list.stream().map((itextfilter_a) -> { // CraftBukkit - decompile error
-                return StringTag.valueOf((String) unaryoperator.apply(itextfilter_a.getFiltered()));
+            Stream<StringTag> stream = list.stream().map(s -> color(s.getFiltered(), hasPerm, false)).map((s) -> { // CraftBukkit - decompile error
+                return StringTag.valueOf((String) unaryoperator.apply(s));
             });
+            // Purpur end
 
             Objects.requireNonNull(nbttaglist);
             stream.forEach(nbttaglist::add);
@@ -1231,10 +1237,10 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
 
             for (int j = list.size(); i < j; ++i) {
                 TextFilter.FilteredText itextfilter_a = (TextFilter.FilteredText) list.get(i);
-                String s = itextfilter_a.getRaw();
+                String s = color(itextfilter_a.getRaw(), hasPerm, false);
 
                 nbttaglist.add(StringTag.valueOf((String) unaryoperator.apply(s)));
-                String s1 = itextfilter_a.getFiltered();
+                String s1 = color(itextfilter_a.getFiltered(), hasPerm, false);
 
                 if (!s.equals(s1)) {
                     nbttagcompound.putString(String.valueOf(i), (String) unaryoperator.apply(s1));
@@ -1250,6 +1256,16 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
         this.player.getInventory().setItem(slot, CraftEventFactory.handleEditBookEvent(player, slot, handItem, itemstack)); // CraftBukkit // Paper - Don't ignore result (see other callsite for handleEditBookEvent)
     }
 
+    // Purpur start
+    private String color(String str, boolean hasPerm) {
+        return color(str, hasPerm, true);
+    }
+
+    private String color(String str, boolean hasPerm, boolean parseHex) {
+        return hasPerm ? org.bukkit.ChatColor.color(str, parseHex) : str;
+    }
+    // Purpur end
+
     @Override
     public void handleEntityTagQuery(ServerboundEntityTagQuery packet) {
         PacketUtils.ensureRunningOnSameThread(packet, this, this.player.getLevel());
