Paper/patches/server/0255-Implement-an-API-for-CanPlaceOn-and-CanDestroy-NBT-v.patch

403 lines
18 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
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] Implement an 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
2023-12-05 23:21:44 +01:00
index 4c66a0357c06e0b286e38624c874e33e45933fc9..4e1f436cbe47ba08c731be781fb372b20c497de6 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
2023-12-05 23:21:44 +01:00
@@ -85,6 +85,12 @@ import org.bukkit.persistence.PersistentDataContainer;
2021-06-11 14:02:28 +02:00
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:
*
2023-12-05 23:21:44 +01:00
@@ -273,6 +279,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
@Specific(Specific.To.NBT)
static final ItemMetaKey BLOCK_DATA = new ItemMetaKey("BlockStateTag");
static final ItemMetaKey BUKKIT_CUSTOM_TAG = new ItemMetaKey("PublicBukkitValues");
+ // Paper start - Implement an 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
// We store the raw original JSON representation of all text data. See SPIGOT-5063, SPIGOT-5656, SPIGOT-5304
private String displayName;
2023-12-05 23:21:44 +01:00
@@ -286,6 +296,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
private int hideFlag;
private boolean unbreakable;
private int damage;
+ // Paper start - Implement an API for CanPlaceOn and CanDestroy NBT values
+ private Set<Namespaced> placeableKeys = Sets.newHashSet();
+ private Set<Namespaced> destroyableKeys = Sets.newHashSet();
+ // Paper end
private static final Set<String> HANDLED_TAGS = Sets.newHashSet();
private static final CraftPersistentDataTypeRegistry DATA_TYPE_REGISTRY = new CraftPersistentDataTypeRegistry();
2023-12-05 23:21:44 +01:00
@@ -323,6 +337,15 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
this.hideFlag = meta.hideFlag;
this.unbreakable = meta.unbreakable;
this.damage = meta.damage;
+ // Paper start - Implement an 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
this.unhandledTags.putAll(meta.unhandledTags);
this.persistentDataContainer.putAll(meta.persistentDataContainer.getRaw());
2023-12-05 23:21:44 +01:00
@@ -386,6 +409,31 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
this.persistentDataContainer.put(key, compound.get(key).copy());
2021-06-11 14:02:28 +02:00
}
}
+ // Paper start - Implement an 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.deserializeNamespaced(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.deserializeNamespaced(list.getString(i));
+ if (namespaced == null) {
+ continue;
+ }
+
+ this.placeableKeys.add(namespaced);
+ }
+ }
+ // Paper end
Set<String> keys = tag.getAllKeys();
for (String key : keys) {
2023-12-05 23:21:44 +01:00
@@ -524,6 +572,34 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-13 06:03:02 +02:00
this.setDamage(damage);
2021-06-11 14:02:28 +02:00
}
+ // Paper start - Implement an 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.deserializeNamespaced(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.deserializeNamespaced(canDestroyRaw);
+ if (value == null) {
+ continue;
+ }
+
+ this.destroyableKeys.add(value);
+ }
+ }
+ // Paper end
+
String internal = SerializableMeta.getString(map, "internal", true);
if (internal != null) {
ByteArrayInputStream buf = new ByteArrayInputStream(Base64.getDecoder().decode(internal));
2023-12-05 23:21:44 +01:00
@@ -652,6 +728,23 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-13 06:03:02 +02:00
if (this.hasDamage()) {
2023-10-27 01:34:58 +02:00
itemTag.putInt(CraftMetaItem.DAMAGE.NBT, this.damage);
2021-06-11 14:02:28 +02:00
}
+ // Paper start - Implement an 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
2021-06-13 06:03:02 +02:00
for (Map.Entry<String, Tag> e : this.unhandledTags.entrySet()) {
2021-06-11 14:02:28 +02:00
itemTag.put(e.getKey(), e.getValue());
2023-12-05 23:21:44 +01:00
@@ -668,6 +761,21 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
}
}
+ // Paper start
+ 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
+
ListTag createStringList(List<String> list) {
if (list == null) {
return null;
2023-12-05 23:21:44 +01:00
@@ -751,7 +859,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
@Overridden
boolean isEmpty() {
2021-06-13 06:03:02 +02:00
- 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
2021-06-11 14:02:28 +02:00
}
// Paper start
2023-12-05 23:21:44 +01:00
@@ -1183,7 +1291,11 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
&& (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
}
/**
2023-12-05 23:21:44 +01:00
@@ -1218,6 +1330,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-13 06:03:02 +02:00
hash = 61 * hash + (this.hasDamage() ? this.damage : 0);
hash = 61 * hash + (this.hasAttributeModifiers() ? this.attributeModifiers.hashCode() : 0);
hash = 61 * hash + this.version;
2021-06-11 14:02:28 +02:00
+ // Paper start - Implement an API for CanPlaceOn and CanDestroy NBT values
2021-06-13 06:03:02 +02:00
+ hash = 61 * hash + (this.hasPlaceableKeys() ? this.placeableKeys.hashCode() : 0);
+ hash = 61 * hash + (this.hasDestroyableKeys() ? this.destroyableKeys.hashCode() : 0);
2021-06-11 14:02:28 +02:00
+ // Paper end
return hash;
}
2023-12-05 23:21:44 +01:00
@@ -1242,6 +1358,14 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
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);
2023-12-05 23:21:44 +01:00
@@ -1299,6 +1423,23 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2023-10-27 01:34:58 +02:00
builder.put(CraftMetaItem.DAMAGE.BUKKIT, this.damage);
2021-06-11 14:02:28 +02:00
}
+ // Paper start - Implement an API for CanPlaceOn and CanDestroy NBT values
2021-06-13 06:03:02 +02:00
+ if (this.hasPlaceableKeys()) {
2021-06-11 14:02:28 +02:00
+ List<String> cerealPlaceable = this.placeableKeys.stream()
+ .map(this::serializeNamespaced)
+ .collect(java.util.stream.Collectors.toList());
+
+ builder.put(CAN_PLACE_ON.BUKKIT, cerealPlaceable);
+ }
+
2021-06-13 06:03:02 +02:00
+ if (this.hasDestroyableKeys()) {
2021-06-11 14:02:28 +02:00
+ List<String> cerealDestroyable = this.destroyableKeys.stream()
+ .map(this::serializeNamespaced)
+ .collect(java.util.stream.Collectors.toList());
+
+ builder.put(CAN_DESTROY.BUKKIT, cerealDestroyable);
+ }
+ // Paper end
2021-06-13 06:03:02 +02:00
final Map<String, Tag> internalTags = new HashMap<String, Tag>(this.unhandledTags);
this.serializeInternal(internalTags);
2021-06-11 14:02:28 +02:00
if (!internalTags.isEmpty()) {
2023-12-05 23:21:44 +01:00
@@ -1471,6 +1612,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
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,
2023-12-05 23:21:44 +01:00
@@ -1500,4 +1643,146 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
}
// 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!");
2021-06-11 14:02:28 +02:00
+ 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!");
2021-06-11 14:02:28 +02:00
+ 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!");
2021-06-11 14:02:28 +02:00
+ 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!");
2021-06-11 14:02:28 +02:00
+ 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 deserializeNamespaced(String raw) {
+ boolean isTag = raw.length() > 0 && raw.codePointAt(0) == '#';
2022-06-08 03:13:05 +02:00
+ com.mojang.datafixers.util.Either<net.minecraft.commands.arguments.blocks.BlockStateParser.BlockResult, net.minecraft.commands.arguments.blocks.BlockStateParser.TagResult> result;
2021-06-11 14:02:28 +02:00
+ try {
2022-12-08 05:24:59 +01:00
+ result = net.minecraft.commands.arguments.blocks.BlockStateParser.parseForTesting(net.minecraft.core.registries.BuiltInRegistries.BLOCK.asLookup(), raw, false);
2021-06-11 14:02:28 +02:00
+ } catch (com.mojang.brigadier.exceptions.CommandSyntaxException e) {
+ return null;
+ }
+
2022-02-28 22:43:31 +01:00
+ net.minecraft.resources.ResourceLocation key = null;
2022-06-08 03:13:05 +02:00
+ 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()) {
2022-12-08 05:24:59 +01:00
+ key = net.minecraft.core.registries.BuiltInRegistries.BLOCK.getKey(result.left().get().blockState().getBlock());
2021-06-11 14:02:28 +02:00
+ }
+
+ if (key == null) {
+ return null;
+ }
+
+ // don't DC the player if something slips through somehow
+ Namespaced resource = null;
+ try {
+ if (isTag) {
+ resource = new NamespacedTag(key.getNamespace(), key.getPath());
+ } else {
+ resource = CraftNamespacedKey.fromMinecraft(key);
+ }
+ } catch (IllegalArgumentException ex) {
+ org.bukkit.Bukkit.getLogger().warning("Namespaced resource does not validate: " + key.toString());
+ ex.printStackTrace();
+ }
+
+ return resource;
+ }
+
+ 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
}