aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0534-Update-itemstack-legacy-name-and-lore.patch
blob: 934e8b519ee6b9ac40f168fd364cd9f28c9e5f38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Wed, 1 Jul 2020 11:57:40 -0500
Subject: [PATCH] Update itemstack legacy name and lore


diff --git a/src/main/java/net/minecraft/server/ItemStack.java b/src/main/java/net/minecraft/server/ItemStack.java
index 9a9678ed76b95131df0c12af5bba3440789699aa..3c56053e3075173edd22eccb277f51a12287ab56 100644
--- a/src/main/java/net/minecraft/server/ItemStack.java
+++ b/src/main/java/net/minecraft/server/ItemStack.java
@@ -83,6 +83,44 @@ public final class ItemStack {
             list.sort((Comparator<? super NBTBase>) enchantSorter); // Paper
         } catch (Exception ignored) {}
     }
+
+    private void processText() {
+        NBTTagCompound display = getSubTag("display");
+        if (display != null) {
+            if (display.hasKeyOfType("Name", 8)) {
+                String json = display.getString("Name");
+                if (json != null && json.contains("\u00A7")) {
+                    try {
+                        display.set("Name", convert(json));
+                    } catch (JsonParseException jsonparseexception) {
+                        display.remove("Name");
+                    }
+                }
+            }
+            if (display.hasKeyOfType("Lore", 9)) {
+                NBTTagList list = display.getList("Lore", 8);
+                for (int index = 0; index < list.size(); index++) {
+                    String json = list.getString(index);
+                    if (json != null && json.contains("\u00A7")) { // Only try if it has legacy in the unparsed json
+                        try {
+                            list.set(index, convert(json));
+                        } catch (JsonParseException e) {
+                            list.set(index, NBTTagString.create(org.bukkit.craftbukkit.util.CraftChatMessage.toJSON(new ChatComponentText(""))));
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private NBTTagString convert(String json) {
+        IChatBaseComponent component = IChatBaseComponent.ChatSerializer.jsonToComponent(json);
+        if (component instanceof ChatComponentText && component.getText().contains("\u00A7") && component.getSiblings().isEmpty()) {
+            // Only convert if the root component is a single comp with legacy in it, don't convert already normal components
+            component = org.bukkit.craftbukkit.util.CraftChatMessage.fromString(component.getText())[0];
+        }
+        return NBTTagString.create(org.bukkit.craftbukkit.util.CraftChatMessage.toJSON(component));
+    }
     // Paper end
 
     public ItemStack(IMaterial imaterial) {
@@ -128,6 +166,7 @@ public final class ItemStack {
             // CraftBukkit start - make defensive copy as this data may be coming from the save thread
             this.tag = (NBTTagCompound) nbttagcompound.getCompound("tag").clone();
             processEnchantOrder(this.tag); // Paper
+            processText(); // Paper
             this.getItem().b(this.tag);
             // CraftBukkit end
         }
@@ -611,6 +650,7 @@ public final class ItemStack {
         }
     }
 
+    @Nullable public NBTTagCompound getSubTag(String s) { return b(s); } // Paper - OBFHELPER
     @Nullable
     public NBTTagCompound b(String s) {
         return this.tag != null && this.tag.hasKeyOfType(s, 10) ? this.tag.getCompound(s) : null;
diff --git a/src/main/java/net/minecraft/server/NBTTagString.java b/src/main/java/net/minecraft/server/NBTTagString.java
index 7ef2378311edd0645472d895cf3756426f25605d..a4747d5dc04ff1e1ec5fd35f927db7d452ea5ae4 100644
--- a/src/main/java/net/minecraft/server/NBTTagString.java
+++ b/src/main/java/net/minecraft/server/NBTTagString.java
@@ -40,6 +40,7 @@ public class NBTTagString implements NBTBase {
         this.data = s;
     }
 
+    public static NBTTagString create(String s) { return a(s); } // Paper - OBFHELPER
     public static NBTTagString a(String s) {
         return s.isEmpty() ? NBTTagString.b : new NBTTagString(s);
     }