aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/1017-Proxy-ItemStack-to-CraftItemStack.patch
blob: 781b18ae7a7fc4a020bf56aa3d42a7258d7abf81 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Tue, 14 May 2024 11:57:43 -0700
Subject: [PATCH] Proxy ItemStack to CraftItemStack


diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
index efb7fb8dbaa7446e394f55b021692c11a25fd29f..a3c6d2cbdce60b1cf935d798568b8bb5d97e1229 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
@@ -26,15 +26,57 @@ import org.jetbrains.annotations.ApiStatus;
 @DelegateDeserialization(ItemStack.class)
 public final class CraftItemStack extends ItemStack {
 
-    // Paper start - MC Utils
-    public static net.minecraft.world.item.ItemStack unwrap(ItemStack bukkit) {
-        if (bukkit instanceof CraftItemStack craftItemStack) {
-            return craftItemStack.handle != null ? craftItemStack.handle : net.minecraft.world.item.ItemStack.EMPTY;
+    // Paper start - delegate api-ItemStack to CraftItemStack
+    private static final java.lang.invoke.VarHandle API_ITEM_STACK_CRAFT_DELEGATE_FIELD;
+    static {
+        try {
+            API_ITEM_STACK_CRAFT_DELEGATE_FIELD = java.lang.invoke.MethodHandles.privateLookupIn(
+                ItemStack.class,
+                java.lang.invoke.MethodHandles.lookup()
+            ).findVarHandle(ItemStack.class, "craftDelegate", ItemStack.class);
+        } catch (final IllegalAccessException | NoSuchFieldException exception) {
+            throw new RuntimeException(exception);
+        }
+    }
+
+    private static CraftItemStack getCraftStack(final ItemStack bukkit) {
+        if (bukkit instanceof final CraftItemStack craftItemStack) {
+            return craftItemStack;
         } else {
-            return asNMSCopy(bukkit);
+            return  (CraftItemStack) API_ITEM_STACK_CRAFT_DELEGATE_FIELD.get(bukkit);
         }
     }
 
+    @Override
+    public int hashCode() {
+        if (this.handle == null || this.handle.isEmpty()) {
+            return net.minecraft.world.item.ItemStack.EMPTY.hashCode();
+        } else {
+            int hash = net.minecraft.world.item.ItemStack.hashItemAndComponents(this.handle);
+            hash = hash * 31 + this.handle.getCount();
+            return hash;
+        }
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (!(obj instanceof final org.bukkit.inventory.ItemStack bukkit)) return false;
+        final CraftItemStack craftStack = getCraftStack(bukkit);
+        if (this.handle == craftStack.handle) return true;
+        else if (this.handle == null || craftStack.handle == null) return false;
+        else if (this.handle.isEmpty() && craftStack.handle.isEmpty()) return true;
+        else return net.minecraft.world.item.ItemStack.matches(this.handle, craftStack.handle);
+    }
+    // Paper end
+
+    // Paper start - MC Utils
+    public static net.minecraft.world.item.ItemStack unwrap(ItemStack bukkit) {
+        // Paper start - re-implement after delegating all api ItemStack calls to CraftItemStack
+        final CraftItemStack craftItemStack = getCraftStack(bukkit);
+        return craftItemStack.handle == null ? net.minecraft.world.item.ItemStack.EMPTY : craftItemStack.handle;
+        // Paper end - re-implement after delegating all api ItemStack calls to CraftItemStack
+    }
+
     public static net.minecraft.world.item.ItemStack getOrCloneOnMutation(ItemStack old, ItemStack newInstance) {
         return old == newInstance ? unwrap(old) : asNMSCopy(newInstance);
     }
@@ -48,25 +90,13 @@ public final class CraftItemStack extends ItemStack {
     // Paper end - override isEmpty to use vanilla's impl
 
     public static net.minecraft.world.item.ItemStack asNMSCopy(ItemStack original) {
-        if (original instanceof CraftItemStack) {
-            CraftItemStack stack = (CraftItemStack) original;
-            return stack.handle == null ? net.minecraft.world.item.ItemStack.EMPTY : stack.handle.copy();
-        }
-        if (original == null || original.isEmpty()) { // Paper - override isEmpty to use vanilla's impl; use isEmpty
+        // Paper start - re-implement after delegating all api ItemStack calls to CraftItemStack
+        if (original == null || original.isEmpty()) {
             return net.minecraft.world.item.ItemStack.EMPTY;
         }
-
-        Item item = CraftItemType.bukkitToMinecraft(original.getType());
-
-        if (item == null) {
-            return net.minecraft.world.item.ItemStack.EMPTY;
-        }
-
-        net.minecraft.world.item.ItemStack stack = new net.minecraft.world.item.ItemStack(item, original.getAmount());
-        if (original.hasItemMeta()) {
-            CraftItemStack.setItemMeta(stack, original.getItemMeta());
-        }
-        return stack;
+        final CraftItemStack stack = getCraftStack(original);
+        return stack.handle == null ? net.minecraft.world.item.ItemStack.EMPTY : stack.handle.copy();
+        // Paper end - re-implement after delegating all api ItemStack calls to CraftItemStack
     }
 
     // Paper start
@@ -89,14 +119,10 @@ public final class CraftItemStack extends ItemStack {
      * Copies the NMS stack to return as a strictly-Bukkit stack
      */
     public static ItemStack asBukkitCopy(net.minecraft.world.item.ItemStack original) {
-        if (original.isEmpty()) {
-            return new ItemStack(Material.AIR);
-        }
-        ItemStack stack = new ItemStack(CraftItemType.minecraftToBukkit(original.getItem()), original.getCount());
-        if (CraftItemStack.hasItemMeta(original)) {
-            stack.setItemMeta(CraftItemStack.getItemMeta(original));
-        }
-        return stack;
+        // Paper start - no such thing as a "strictly-Bukkit stack" anymore
+        // we copy the stack since it should be a complete copy not a mirror
+        return asCraftMirror(original.copy());
+        // Paper end
     }
 
     public static CraftItemStack asCraftMirror(net.minecraft.world.item.ItemStack original) {
@@ -317,11 +343,7 @@ public final class CraftItemStack extends ItemStack {
 
     @Override
     public CraftItemStack clone() {
-        CraftItemStack itemStack = (CraftItemStack) super.clone();
-        if (this.handle != null) {
-            itemStack.handle = this.handle.copy();
-        }
-        return itemStack;
+        return new org.bukkit.craftbukkit.inventory.CraftItemStack(this.handle != null ? this.handle.copy() : null); // Paper
     }
 
     @Override
@@ -424,22 +446,14 @@ public final class CraftItemStack extends ItemStack {
         if (stack == this) {
             return true;
         }
-        if (!(stack instanceof CraftItemStack)) {
-            return stack.getClass() == ItemStack.class && stack.isSimilar(this);
-        }
-
-        CraftItemStack that = (CraftItemStack) stack;
+        final CraftItemStack that = getCraftStack(stack); // Paper - re-implement after delegating all api ItemStack calls to CraftItemStack
         if (this.handle == that.handle) {
             return true;
         }
         if (this.handle == null || that.handle == null) {
             return false;
         }
-        Material comparisonType = CraftLegacy.fromLegacy(that.getType()); // This may be called from legacy item stacks, try to get the right material
-        if (!(comparisonType == this.getType() && this.getDurability() == that.getDurability())) {
-            return false;
-        }
-        return this.hasItemMeta() ? that.hasItemMeta() && this.handle.getComponents().equals(that.handle.getComponents()) : !that.hasItemMeta();
+        return net.minecraft.world.item.ItemStack.isSameItemSameComponents(this.handle, that.handle); // Paper - re-implement after delegating all api ItemStack calls to CraftItemStack
     }
 
     @Override
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
index d03f4a767f6c7fe7d6bcef20e6676c39d9657584..bae3dd5fc67e6b3d98a5e63ffbf639c5042f8843 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
@@ -100,13 +100,14 @@ public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Han
     @NotNull
     @Override
     public ItemStack createItemStack(final int amount, @Nullable final Consumer<? super M> metaConfigurator) {
-        final ItemStack itemStack = new ItemStack(this.asMaterial(), amount);
+        // Paper start - re-implement to return CraftItemStack
+        final net.minecraft.world.item.ItemStack stack = new net.minecraft.world.item.ItemStack(this.item, amount);
+        final CraftItemStack mirror = CraftItemStack.asCraftMirror(stack);
         if (metaConfigurator != null) {
-            final ItemMeta itemMeta = itemStack.getItemMeta();
-            metaConfigurator.accept((M) itemMeta);
-            itemStack.setItemMeta(itemMeta);
+            mirror.editMeta(this.getItemMetaClass(), metaConfigurator);
         }
-        return itemStack;
+        return mirror;
+        // Paper start - reimplement to return CraftItemStack
     }
 
     @Override
diff --git a/src/main/java/org/bukkit/craftbukkit/legacy/MaterialRerouting.java b/src/main/java/org/bukkit/craftbukkit/legacy/MaterialRerouting.java
index 6cc9d7a9e6d4bfdc27e52fc581b2bb832616f121..6930d0afb230a88aa813b02e4d55c95d3a049688 100644
--- a/src/main/java/org/bukkit/craftbukkit/legacy/MaterialRerouting.java
+++ b/src/main/java/org/bukkit/craftbukkit/legacy/MaterialRerouting.java
@@ -678,4 +678,16 @@ public class MaterialRerouting {
         return itemStack.withType(material);
     }
     // Paper end - register paper API specific material consumers in rerouting
+
+    // Paper start - methods added post 1.13, no-op
+    @RerouteStatic("org/bukkit/inventory/ItemStack")
+    public static ItemStack of(final Material material) {
+        return ItemStack.of(material);
+    }
+
+    @RerouteStatic("org/bukkit/inventory/ItemStack")
+    public static ItemStack of(final Material material, final int amount) {
+        return ItemStack.of(material, amount);
+    }
+    // Paper end
 }
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
index 68a6cd43042e87501f5bd48565222638dd58a1cf..6adc18c40d5d62e2ebc8deec197cec630a366937 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -689,6 +689,13 @@ public final class CraftMagicNumbers implements UnsafeValues {
     }
     // Paper end - hack to get tags for non server-backed registries
 
+    // Paper start - proxy ItemStack
+    @Override
+    public org.bukkit.inventory.ItemStack createEmptyStack() {
+        return CraftItemStack.asCraftMirror(null);
+    }
+    // Paper end - proxy ItemStack
+
     /**
      * This helper class represents the different NBT Tags.
      * <p>
diff --git a/src/test/java/io/papermc/paper/configuration/ConfigurationSectionTest.java b/src/test/java/io/papermc/paper/configuration/ConfigurationSectionTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..ed45bfa577579afcbd54d655c3b5d05d6c6f3e86
--- /dev/null
+++ b/src/test/java/io/papermc/paper/configuration/ConfigurationSectionTest.java
@@ -0,0 +1,53 @@
+package io.papermc.paper.configuration;
+
+import org.bukkit.Material;
+import org.bukkit.configuration.ConfigurationSection;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.support.environment.VanillaFeature;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public abstract class ConfigurationSectionTest {
+    public abstract ConfigurationSection getConfigurationSection();
+
+    @Test
+    public void testGetItemStack_String() {
+        ConfigurationSection section = getConfigurationSection();
+        String key = "exists";
+        ItemStack value = new ItemStack(Material.ACACIA_WOOD, 50);
+
+        section.set(key, value);
+
+        assertEquals(value, section.getItemStack(key));
+        assertNull(section.getString("doesntExist"));
+    }
+
+    @Test
+    public void testGetItemStack_String_ItemStack() {
+        ConfigurationSection section = getConfigurationSection();
+        String key = "exists";
+        ItemStack value = new ItemStack(Material.ACACIA_WOOD, 50);
+        ItemStack def = new ItemStack(Material.STONE, 1);
+
+        section.set(key, value);
+
+        assertEquals(value, section.getItemStack(key, def));
+        assertEquals(def, section.getItemStack("doesntExist", def));
+    }
+
+    @Test
+    public void testIsItemStack() {
+        ConfigurationSection section = getConfigurationSection();
+        String key = "exists";
+        ItemStack value = new ItemStack(Material.ACACIA_WOOD, 50);
+
+        section.set(key, value);
+
+        assertTrue(section.isItemStack(key));
+        assertFalse(section.isItemStack("doesntExist"));
+    }
+}
diff --git a/src/test/java/io/papermc/paper/configuration/MemorySectionTest.java b/src/test/java/io/papermc/paper/configuration/MemorySectionTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..c00085328ce8a00fc274632a556ab27660fa57ed
--- /dev/null
+++ b/src/test/java/io/papermc/paper/configuration/MemorySectionTest.java
@@ -0,0 +1,13 @@
+package io.papermc.paper.configuration;
+
+import org.bukkit.configuration.ConfigurationSection;
+import org.bukkit.configuration.MemoryConfiguration;
+import org.bukkit.support.environment.Normal;
+
+@Normal
+public class MemorySectionTest extends ConfigurationSectionTest {
+    @Override
+    public ConfigurationSection getConfigurationSection() {
+        return new MemoryConfiguration().createSection("section");
+    }
+}