Paper/Spigot-Server-Patches/0350-Implement-an-API-for-CanPlaceOn-and-CanDestroy-NBT-v.patch

417 lines
18 KiB
Diff
Raw Normal View History

From 79274884292748c5830bd23cdd4b329093e56e7e 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/net/minecraft/server/ArgumentBlock.java b/src/main/java/net/minecraft/server/ArgumentBlock.java
index 8e8390282..f52936581 100644
--- a/src/main/java/net/minecraft/server/ArgumentBlock.java
+++ b/src/main/java/net/minecraft/server/ArgumentBlock.java
@@ -43,7 +43,7 @@ public class ArgumentBlock {
private final boolean j;
private final Map<IBlockState<?>, Comparable<?>> k = Maps.newLinkedHashMap(); // CraftBukkit - stable
private final Map<String, String> l = Maps.newHashMap();
- private MinecraftKey m = new MinecraftKey("");
+ private MinecraftKey m = new MinecraftKey(""); public MinecraftKey getBlockKey() { return this.m; } // Paper - OBFHELPER
private BlockStateList<Block, IBlockData> n;
private IBlockData o;
@Nullable
@@ -72,11 +72,13 @@ public class ArgumentBlock {
return this.p;
}
+ public @Nullable MinecraftKey getTagKey() { return d(); } // Paper - OBFHELPER
@Nullable
public MinecraftKey d() {
return this.q;
}
+ public ArgumentBlock parse(boolean parseTile) throws CommandSyntaxException { return this.a(parseTile); } // Paper - OBFHELPER
public ArgumentBlock a(boolean flag) throws CommandSyntaxException {
this.s = this::l;
Updated Upstream (Bukkit/CraftBukkit/Spigot) Upstream has released updates that appears to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Warning: this commit contains more mapping changes from upstream, As always, ensure that you have working backups and test this build before deployment; Developers working on paper will, yet again, need to delete their work/Minecraft/1.13.2 folder Bukkit Changes: 7fca5fd4 SPIGOT-4558: Preserve user order in the face of copied defaults in configurations 15c9b1eb Ignore spurious slot IDs sent by client, e.g. in enchanting tables 5d2a10c5 SPIGOT-3747: Add API for force loaded chunks d6dd2bb3 SPIGOT-3538: Add getHitBlockFace for ProjectileHitEvent 771db4aa SPIGOT-794: Call EntityPlaceEvent for Minecart placement 55462509 Add InventoryView#getSlotType 2f3ce5b6 Remove EntityTransformEvent and CustomItemTagContainer from draft API f04ad7b6 Make ProjectileLaunchEvent extend EntitySpawnEvent ccb85808 Define EntitySpawnEvent b8cc3ebe Add PlayerItemDamageEvent 184a495d Ease ClassLoader Deadlocks Where Possible 11ac4728 Expand Boolean Prompt Values in Conversation API aae62d51 Added getAllSessionData() to the Conversation API. 9290ff91 Add InventoryView#getInventory API 995e530f Add API to get / set base arrow damage CraftBukkit Changes: c4a67eed SPIGOT-4556: Fix plugins closing inventory during drop events 5be2ddcb Replace version constants with methods to prevent compiler inlining a5b9c7b3 Use API method to create offset command completions 2bc7d1df SPIGOT-3747: Add API for force loaded chunks a408f375 SPIGOT-3538: Add getHitBlockFace for ProjectileHitEvent b54b9409 SPIGOT-2864: Make Arrow / Item setTicksLived behave like FallingBlock 79ded7a8 SPIGOT-1811: Death message not shown on respawn screen b4a4f15d SPIGOT-943: InventoryCloseEvent called on death regardless of open inventory 0afed592 SPIGOT-794: Call EntityPlaceEvent for Minecart placement 2b2d084a Add InventoryView#getSlotType 01a9959a Do not use deprecated ItemSpawnEvent constructor 9642498d SPIGOT-4547: Call EntitySpawnEvent as general spawn fallback event 963f4a5f Add PlayerItemDamageEvent 63db0445 Add API to get / set base arrow damage 531c25d7 Add CraftMagicNumbers.MAPPINGS_VERSION for use by NMS plugins d05c8b14 Mappings Update bd36e200 SPIGOT-4551: Ignore invalid attribute modifier slots Spigot Changes: 518206a1 Remove redundant trove depend 1959ad21 MC-11211,SPIGOT-4552: Fix placing double slabs at y = 255 29ab5e43 SPIGOT-3661: Allow arguments in restart-script 7cc46316 SPIGOT-852: Growth modifiers for beetroots, potatoes, carrots 82e117e1 Squelch "fatal: Resolve operation not in progress" message 0a1a68e7 Mappings Update & Patch Rebuild
2019-01-01 04:15:55 +01:00
if (this.i.canRead() && this.i.peek() == '#') {
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
index eaf4cd11c..be2e736eb 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
@@ -81,6 +81,12 @@ import javax.annotation.Nullable;
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:
*
@@ -256,6 +262,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
@Specific(Specific.To.NBT)
static final ItemMetaKey DAMAGE = new ItemMetaKey("Damage");
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
private IChatBaseComponent displayName;
private IChatBaseComponent locName;
@@ -266,6 +276,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
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 CraftCustomTagTypeRegistry TAG_TYPE_REGISTRY = new CraftCustomTagTypeRegistry();
@@ -298,6 +312,15 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
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.publicItemTagContainer.putAll(meta.publicItemTagContainer.getRaw());
@@ -361,6 +384,31 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
publicItemTagContainer.put(key, compound.get(key));
}
}
+ // Paper start - Implement an API for CanPlaceOn and CanDestroy NBT values
+ if (tag.hasKey(CAN_DESTROY.NBT)) {
+ NBTTagList 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.hasKey(CAN_PLACE_ON.NBT)) {
+ NBTTagList 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.getKeys();
for (String key : keys) {
@@ -488,6 +536,34 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
setDamage(damage);
}
+ // 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.decodeBase64(internal));
@@ -608,6 +684,23 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
if (hasDamage()) {
itemTag.setInt(DAMAGE.NBT, damage);
}
+ // 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.set(CAN_PLACE_ON.NBT, createStringList(items));
+ }
+
+ if (hasDestroyableKeys()) {
+ List<String> items = this.destroyableKeys.stream()
+ .map(this::serializeNamespaced)
+ .collect(java.util.stream.Collectors.toList());
+
+ itemTag.set(CAN_DESTROY.NBT, createStringList(items));
+ }
+ // Paper end
for (Map.Entry<String, NBTBase> e : unhandledTags.entrySet()) {
itemTag.set(e.getKey(), e.getValue());
@@ -706,7 +799,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
@Overridden
boolean isEmpty() {
- return !(hasDisplayName() || hasLocalizedName() || hasEnchants() || hasLore() || hasRepairCost() || !unhandledTags.isEmpty() || !publicItemTagContainer.isEmpty() || hideFlag != 0 || isUnbreakable() || hasDamage() || hasAttributeModifiers());
+ return !(hasDisplayName() || hasLocalizedName() || hasEnchants() || hasLore() || hasRepairCost() || !unhandledTags.isEmpty() || !publicItemTagContainer.isEmpty() || hideFlag != 0 || isUnbreakable() || hasDamage() || hasAttributeModifiers()
+ || hasPlaceableKeys() || hasDestroyableKeys()); // Paper - Implement an API for CanPlaceOn and CanDestroy NBT values
}
public String getDisplayName() {
@@ -1048,7 +1142,11 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
&& (this.publicItemTagContainer.equals(that.publicItemTagContainer))
&& (this.hideFlag == that.hideFlag)
&& (this.isUnbreakable() == that.isUnbreakable())
- && (this.hasDamage() ? that.hasDamage() && this.damage == that.damage : !that.hasDamage());
+ && (this.hasDamage() ? that.hasDamage() && this.damage == that.damage : !that.hasDamage())
+ // 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
}
/**
@@ -1080,6 +1178,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
hash = 61 * hash + (isUnbreakable() ? 1231 : 1237);
hash = 61 * hash + (hasDamage() ? this.damage : 0);
hash = 61 * hash + (hasAttributeModifiers() ? this.attributeModifiers.hashCode() : 0);
+ // Paper start - Implement an API for CanPlaceOn and CanDestroy NBT values
+ hash = 61 * hash + (hasPlaceableKeys() ? this.placeableKeys.hashCode() : 0);
+ hash = 61 * hash + (hasDestroyableKeys() ? this.destroyableKeys.hashCode() : 0);
+ // Paper end
return hash;
}
@@ -1100,6 +1202,15 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
clone.hideFlag = this.hideFlag;
clone.unbreakable = this.unbreakable;
clone.damage = this.damage;
+ // 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);
@@ -1149,6 +1260,24 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
builder.put(DAMAGE.BUKKIT, damage);
}
+ // Paper start - Implement an API for CanPlaceOn and CanDestroy NBT values
+ if (hasPlaceableKeys()) {
+ List<String> cerealPlaceable = this.placeableKeys.stream()
+ .map(this::serializeNamespaced)
+ .collect(java.util.stream.Collectors.toList());
+
+ builder.put(CAN_PLACE_ON.BUKKIT, cerealPlaceable);
+ }
+
+ if (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, NBTBase> internalTags = new HashMap<String, NBTBase>(unhandledTags);
serializeInternal(internalTags);
if (!internalTags.isEmpty()) {
@@ -1298,7 +1427,9 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
CraftMetaArmorStand.NO_BASE_PLATE.NBT,
CraftMetaArmorStand.SHOW_ARMS.NBT,
CraftMetaArmorStand.SMALL.NBT,
- CraftMetaArmorStand.MARKER.NBT
+ CraftMetaArmorStand.MARKER.NBT,
+ CAN_DESTROY.NBT,
+ CAN_PLACE_ON.NBT
// Paper end
));
}
@@ -1345,4 +1476,147 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable {
return spigot;
}
// Spigot 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) {
+ Validate.notNull(canDestroy, "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) {
+ Validate.notNull(canPlaceOn, "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) {
+ Validate.notNull(canDestroy, "Cannot replace with null collection!");
+ Validate.isTrue(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) {
+ Validate.notNull(canPlaceOn, "Cannot replace with null collection!");
+ Validate.isTrue(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 deserializeNamespaced(String raw) {
+ boolean isTag = raw.codePointAt(0) == '#';
+ net.minecraft.server.ArgumentBlock blockParser = new net.minecraft.server.ArgumentBlock(new com.mojang.brigadier.StringReader(raw), true);
+ try {
+ blockParser = blockParser.parse(false);
+ } catch (com.mojang.brigadier.exceptions.CommandSyntaxException e) {
+ e.printStackTrace();
+ return null;
+ }
+
+ net.minecraft.server.MinecraftKey key;
+ if (isTag) {
+ key = blockParser.getTagKey();
+ } else {
+ key = blockParser.getBlockKey();
+ }
+
+ 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.b(), key.getKey());
+ } 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) {
+ boolean valid = true;
+ for (Namespaced resource : namespacedResources) {
+ if (valid && !(resource instanceof org.bukkit.NamespacedKey || resource instanceof com.destroystokyo.paper.NamespacedTag)) {
+ valid = false;
+ }
+ }
+
+ return valid;
+ }
+ // Paper end
}
--
2.21.0