aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/removed/1.20.5/0259-Add-API-for-CanPlaceOn-and-CanDestroy-NBT-values.patch
blob: f179b48bd74e55b1cac7cdf3f0e58ac601baeffa (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Mark Vainomaa <mikroskeem@mikroskeem.eu>
Date: Wed, 12 Sep 2018 18:53:55 +0300
Subject: [PATCH] Add API for CanPlaceOn and CanDestroy NBT values


diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
index 1920cf7ad846f57cd278cb9a72dce03f3d014fbb..7cf1153ae532a9d53ee85b05f77ed74b94cf5fbc 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
@@ -85,6 +85,12 @@ import org.bukkit.persistence.PersistentDataContainer;
 import static org.spigotmc.ValidateUtils.*;
 // Spigot end
 
+// Paper start
+import com.destroystokyo.paper.Namespaced;
+import com.destroystokyo.paper.NamespacedTag;
+import java.util.Collections;
+// Paper end
+
 /**
  * Children must include the following:
  *
@@ -273,6 +279,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
     @Specific(Specific.To.NBT)
     static final ItemMetaKey BLOCK_DATA = new ItemMetaKey("BlockStateTag");
     static final ItemMetaKey BUKKIT_CUSTOM_TAG = new ItemMetaKey("PublicBukkitValues");
+    // Paper start - Add API for CanPlaceOn and CanDestroy NBT values
+    static final ItemMetaKey CAN_DESTROY = new ItemMetaKey("CanDestroy");
+    static final ItemMetaKey CAN_PLACE_ON = new ItemMetaKey("CanPlaceOn");
+    // Paper end - Add API for CanPlaceOn and CanDestroy NBT values
 
     // We store the raw original JSON representation of all text data. See SPIGOT-5063, SPIGOT-5656, SPIGOT-5304
     private String displayName;
@@ -286,6 +296,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
     private int hideFlag;
     private boolean unbreakable;
     private int damage;
+    // Paper start - Add API for CanPlaceOn and CanDestroy NBT values
+    private Set<Namespaced> placeableKeys = Sets.newHashSet();
+    private Set<Namespaced> destroyableKeys = Sets.newHashSet();
+    // Paper end - Add API for CanPlaceOn and CanDestroy NBT values
 
     private static final Set<String> HANDLED_TAGS = Sets.newHashSet();
     private static final CraftPersistentDataTypeRegistry DATA_TYPE_REGISTRY = new CraftPersistentDataTypeRegistry();
@@ -323,6 +337,15 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
         this.hideFlag = meta.hideFlag;
         this.unbreakable = meta.unbreakable;
         this.damage = meta.damage;
+        // Paper start - Add API for CanPlaceOn and CanDestroy NBT values
+        if (meta.hasPlaceableKeys()) {
+            this.placeableKeys = new java.util.HashSet<>(meta.placeableKeys);
+        }
+
+        if (meta.hasDestroyableKeys()) {
+            this.destroyableKeys = new java.util.HashSet<>(meta.destroyableKeys);
+        }
+        // Paper end - Add API for CanPlaceOn and CanDestroy NBT values
         this.unhandledTags.putAll(meta.unhandledTags);
         this.persistentDataContainer.putAll(meta.persistentDataContainer.getRaw());
 
@@ -386,6 +409,31 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
                 this.persistentDataContainer.put(key, compound.get(key).copy());
             }
         }
+        // Paper start - Add API for CanPlaceOn and CanDestroy NBT values
+        if (tag.contains(CAN_DESTROY.NBT)) {
+            ListTag list = tag.getList(CAN_DESTROY.NBT, CraftMagicNumbers.NBT.TAG_STRING);
+            for (int i = 0; i < list.size(); i++) {
+                Namespaced namespaced = this.blockKeyFromString(list.getString(i));
+                if (namespaced == null) {
+                    continue;
+                }
+
+                this.destroyableKeys.add(namespaced);
+            }
+        }
+
+        if (tag.contains(CAN_PLACE_ON.NBT)) {
+            ListTag list = tag.getList(CAN_PLACE_ON.NBT, CraftMagicNumbers.NBT.TAG_STRING);
+            for (int i = 0; i < list.size(); i++) {
+                Namespaced namespaced = this.blockKeyFromString(list.getString(i));
+                if (namespaced == null) {
+                    continue;
+                }
+
+                this.placeableKeys.add(namespaced);
+            }
+        }
+        // Paper end - Add API for CanPlaceOn and CanDestroy NBT values
 
         Set<String> keys = tag.getAllKeys();
         for (String key : keys) {
@@ -524,6 +572,34 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
             this.setDamage(damage);
         }
 
+        // Paper start - Add API for CanPlaceOn and CanDestroy NBT values
+        Iterable<?> canPlaceOnSerialized = SerializableMeta.getObject(Iterable.class, map, CAN_PLACE_ON.BUKKIT, true);
+        if (canPlaceOnSerialized != null) {
+            for (Object canPlaceOnElement : canPlaceOnSerialized) {
+                String canPlaceOnRaw = (String) canPlaceOnElement;
+                Namespaced value = this.blockKeyFromString(canPlaceOnRaw);
+                if (value == null) {
+                    continue;
+                }
+
+                this.placeableKeys.add(value);
+            }
+        }
+
+        Iterable<?> canDestroySerialized = SerializableMeta.getObject(Iterable.class, map, CAN_DESTROY.BUKKIT, true);
+        if (canDestroySerialized != null) {
+            for (Object canDestroyElement : canDestroySerialized) {
+                String canDestroyRaw = (String) canDestroyElement;
+                Namespaced value = this.blockKeyFromString(canDestroyRaw);
+                if (value == null) {
+                    continue;
+                }
+
+                this.destroyableKeys.add(value);
+            }
+        }
+        // Paper end - Add API for CanPlaceOn and CanDestroy NBT values
+
         String internal = SerializableMeta.getString(map, "internal", true);
         if (internal != null) {
             ByteArrayInputStream buf = new ByteArrayInputStream(Base64.getDecoder().decode(internal));
@@ -652,6 +728,23 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
         if (this.hasDamage()) {
             itemTag.putInt(CraftMetaItem.DAMAGE.NBT, this.damage);
         }
+        // Paper start - Add API for CanPlaceOn and CanDestroy NBT values
+        if (hasPlaceableKeys()) {
+            List<String> items = this.placeableKeys.stream()
+                .map(this::serializeNamespaced)
+                .collect(java.util.stream.Collectors.toList());
+
+            itemTag.put(CAN_PLACE_ON.NBT, createNonComponentStringList(items));
+        }
+
+        if (hasDestroyableKeys()) {
+            List<String> items = this.destroyableKeys.stream()
+                .map(this::serializeNamespaced)
+                .collect(java.util.stream.Collectors.toList());
+
+            itemTag.put(CAN_DESTROY.NBT, createNonComponentStringList(items));
+        }
+        // Paper end - Add API for CanPlaceOn and CanDestroy NBT values
 
         for (Map.Entry<String, Tag> e : this.unhandledTags.entrySet()) {
             itemTag.put(e.getKey(), e.getValue());
@@ -668,6 +761,21 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
         }
     }
 
+    // Paper start - Add API for CanPlaceOn and CanDestroy NBT values
+    static ListTag createNonComponentStringList(List<String> list) {
+        if (list == null || list.isEmpty()) {
+            return null;
+        }
+
+        ListTag tagList = new ListTag();
+        for (String value : list) {
+            tagList.add(StringTag.valueOf(value)); // Paper - NBTTagString.of(String str)
+        }
+
+        return tagList;
+    }
+    // Paper end - Add API for CanPlaceOn and CanDestroy NBT values
+
     ListTag createStringList(List<String> list) {
         if (list == null) {
             return null;
@@ -751,7 +859,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
 
     @Overridden
     boolean isEmpty() {
-        return !(this.hasDisplayName() || this.hasLocalizedName() || this.hasEnchants() || (this.lore != null) || this.hasCustomModelData() || this.hasBlockData() || this.hasRepairCost() || !this.unhandledTags.isEmpty() || !this.persistentDataContainer.isEmpty() || this.hideFlag != 0 || this.isUnbreakable() || this.hasDamage() || this.hasAttributeModifiers());
+        return !(this.hasDisplayName() || this.hasLocalizedName() || this.hasEnchants() || (this.lore != null) || this.hasCustomModelData() || this.hasBlockData() || this.hasRepairCost() || !this.unhandledTags.isEmpty() || !this.persistentDataContainer.isEmpty() || this.hideFlag != 0 || this.isUnbreakable() || this.hasDamage() || this.hasAttributeModifiers() || this.hasPlaceableKeys() || this.hasDestroyableKeys()); // Paper - Implement an API for CanPlaceOn and CanDestroy NBT values
     }
 
     // Paper start
@@ -1223,7 +1331,11 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
                 && (this.hideFlag == that.hideFlag)
                 && (this.isUnbreakable() == that.isUnbreakable())
                 && (this.hasDamage() ? that.hasDamage() && this.damage == that.damage : !that.hasDamage())
-                && (this.version == that.version);
+                && (this.version == that.version)
+                // Paper start - Implement an API for CanPlaceOn and CanDestroy NBT values
+                && (this.hasPlaceableKeys() ? that.hasPlaceableKeys() && this.placeableKeys.equals(that.placeableKeys) : !that.hasPlaceableKeys())
+                && (this.hasDestroyableKeys() ? that.hasDestroyableKeys() && this.destroyableKeys.equals(that.destroyableKeys) : !that.hasDestroyableKeys());
+                // Paper end
     }
 
     /**
@@ -1258,6 +1370,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
         hash = 61 * hash + (this.hasDamage() ? this.damage : 0);
         hash = 61 * hash + (this.hasAttributeModifiers() ? this.attributeModifiers.hashCode() : 0);
         hash = 61 * hash + this.version;
+        // Paper start - Implement an API for CanPlaceOn and CanDestroy NBT values
+        hash = 61 * hash + (this.hasPlaceableKeys() ? this.placeableKeys.hashCode() : 0);
+        hash = 61 * hash + (this.hasDestroyableKeys() ? this.destroyableKeys.hashCode() : 0);
+        // Paper end
         return hash;
     }
 
@@ -1282,6 +1398,14 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
             clone.unbreakable = this.unbreakable;
             clone.damage = this.damage;
             clone.version = this.version;
+            // Paper start - Implement an API for CanPlaceOn and CanDestroy NBT values
+            if (this.placeableKeys != null) {
+                clone.placeableKeys = Sets.newHashSet(this.placeableKeys);
+            }
+            if (this.destroyableKeys != null) {
+                clone.destroyableKeys = Sets.newHashSet(this.destroyableKeys);
+            }
+            // Paper end
             return clone;
         } catch (CloneNotSupportedException e) {
             throw new Error(e);
@@ -1339,6 +1463,23 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
             builder.put(CraftMetaItem.DAMAGE.BUKKIT, this.damage);
         }
 
+        // Paper start - Implement an API for CanPlaceOn and CanDestroy NBT values
+        if (this.hasPlaceableKeys()) {
+            List<String> cerealPlaceable = this.placeableKeys.stream()
+                .map(this::serializeNamespaced)
+                .collect(java.util.stream.Collectors.toList());
+
+            builder.put(CAN_PLACE_ON.BUKKIT, cerealPlaceable);
+        }
+
+        if (this.hasDestroyableKeys()) {
+            List<String> cerealDestroyable = this.destroyableKeys.stream()
+                .map(this::serializeNamespaced)
+                .collect(java.util.stream.Collectors.toList());
+
+            builder.put(CAN_DESTROY.BUKKIT, cerealDestroyable);
+        }
+        // Paper end
         final Map<String, Tag> internalTags = new HashMap<String, Tag>(this.unhandledTags);
         this.serializeInternal(internalTags);
         if (!internalTags.isEmpty()) {
@@ -1516,6 +1657,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
                         CraftMetaArmorStand.SHOW_ARMS.NBT,
                         CraftMetaArmorStand.SMALL.NBT,
                         CraftMetaArmorStand.MARKER.NBT,
+                        CAN_DESTROY.NBT,
+                        CAN_PLACE_ON.NBT,
                         // Paper end
                         CraftMetaCompass.LODESTONE_DIMENSION.NBT,
                         CraftMetaCompass.LODESTONE_POS.NBT,
@@ -1545,4 +1688,141 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
     }
     // Paper end
 
+    // Paper start - Implement an API for CanPlaceOn and CanDestroy NBT values
+    @Override
+    @SuppressWarnings("deprecation")
+    public Set<Material> getCanDestroy() {
+        return !hasDestroyableKeys() ? Collections.emptySet() : legacyGetMatsFromKeys(this.destroyableKeys);
+    }
+
+    @Override
+    @SuppressWarnings("deprecation")
+    public void setCanDestroy(Set<Material> canDestroy) {
+        Preconditions.checkArgument(canDestroy != null, "Cannot replace with null set!");
+        legacyClearAndReplaceKeys(this.destroyableKeys, canDestroy);
+    }
+
+    @Override
+    @SuppressWarnings("deprecation")
+    public Set<Material> getCanPlaceOn() {
+        return !hasPlaceableKeys() ? Collections.emptySet() : legacyGetMatsFromKeys(this.placeableKeys);
+    }
+
+    @Override
+    @SuppressWarnings("deprecation")
+    public void setCanPlaceOn(Set<Material> canPlaceOn) {
+        Preconditions.checkArgument(canPlaceOn != null, "Cannot replace with null set!");
+        legacyClearAndReplaceKeys(this.placeableKeys, canPlaceOn);
+    }
+
+    @Override
+    public Set<Namespaced> getDestroyableKeys() {
+        return !hasDestroyableKeys() ? Collections.emptySet() : Sets.newHashSet(this.destroyableKeys);
+    }
+
+    @Override
+    public void setDestroyableKeys(Collection<Namespaced> canDestroy) {
+        Preconditions.checkArgument(canDestroy != null, "Cannot replace with null collection!");
+        Preconditions.checkArgument(ofAcceptableType(canDestroy), "Can only use NamespacedKey or NamespacedTag objects!");
+        this.destroyableKeys.clear();
+        this.destroyableKeys.addAll(canDestroy);
+    }
+
+    @Override
+    public Set<Namespaced> getPlaceableKeys() {
+        return !hasPlaceableKeys() ? Collections.emptySet() : Sets.newHashSet(this.placeableKeys);
+    }
+
+    @Override
+    public void setPlaceableKeys(Collection<Namespaced> canPlaceOn) {
+        Preconditions.checkArgument(canPlaceOn != null, "Cannot replace with null collection!");
+        Preconditions.checkArgument(ofAcceptableType(canPlaceOn), "Can only use NamespacedKey or NamespacedTag objects!");
+        this.placeableKeys.clear();
+        this.placeableKeys.addAll(canPlaceOn);
+    }
+
+    @Override
+    public boolean hasPlaceableKeys() {
+        return this.placeableKeys != null && !this.placeableKeys.isEmpty();
+    }
+
+    @Override
+    public boolean hasDestroyableKeys() {
+        return this.destroyableKeys != null && !this.destroyableKeys.isEmpty();
+    }
+
+    @Deprecated
+    private void legacyClearAndReplaceKeys(Collection<Namespaced> toUpdate, Collection<Material> beingSet) {
+        if (beingSet.stream().anyMatch(Material::isLegacy)) {
+            throw new IllegalArgumentException("Set must not contain any legacy materials!");
+        }
+
+        toUpdate.clear();
+        toUpdate.addAll(beingSet.stream().map(Material::getKey).collect(java.util.stream.Collectors.toSet()));
+    }
+
+    @Deprecated
+    private Set<Material> legacyGetMatsFromKeys(Collection<Namespaced> names) {
+        Set<Material> mats = Sets.newHashSet();
+        for (Namespaced key : names) {
+            if (!(key instanceof org.bukkit.NamespacedKey)) {
+                continue;
+            }
+
+            Material material = Material.matchMaterial(key.toString(), false);
+            if (material != null) {
+                mats.add(material);
+            }
+        }
+
+        return mats;
+    }
+
+    private @Nullable Namespaced blockKeyFromString(String raw) {
+        boolean isTag = !raw.isEmpty() && raw.codePointAt(0) == '#';
+        com.mojang.datafixers.util.Either<net.minecraft.commands.arguments.blocks.BlockStateParser.BlockResult, net.minecraft.commands.arguments.blocks.BlockStateParser.TagResult> result;
+        try {
+            result = net.minecraft.commands.arguments.blocks.BlockStateParser.parseForTesting(net.minecraft.core.registries.BuiltInRegistries.BLOCK.asLookup(), raw, false);
+        } catch (com.mojang.brigadier.exceptions.CommandSyntaxException e) {
+            return null;
+        }
+
+        net.minecraft.resources.ResourceLocation key = null;
+        if (isTag && result.right().isPresent() && result.right().get().tag() instanceof net.minecraft.core.HolderSet.Named<net.minecraft.world.level.block.Block> namedSet) {
+            key = namedSet.key().location();
+        } else if (result.left().isPresent()) {
+            key = net.minecraft.core.registries.BuiltInRegistries.BLOCK.getKey(result.left().get().blockState().getBlock());
+        }
+
+        if (key == null) {
+            return null;
+        }
+
+        try {
+            if (isTag) {
+                return new NamespacedTag(key.getNamespace(), key.getPath());
+
+            }
+            return CraftNamespacedKey.fromMinecraft(key);
+        } catch (IllegalArgumentException ignored) {
+            return null;
+        }
+    }
+
+    private @Nonnull String serializeNamespaced(Namespaced resource) {
+        return resource.toString();
+    }
+
+    // not a fan of this
+    private boolean ofAcceptableType(Collection<Namespaced> namespacedResources) {
+        
+        for (Namespaced resource : namespacedResources) {
+            if (!(resource instanceof org.bukkit.NamespacedKey || resource instanceof com.destroystokyo.paper.NamespacedTag)) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+    // Paper end
 }