2020-04-02 21:07:06 +00:00
|
|
|
From d6d9d785dfd611f7ce87e64bb2bfa54f08277eb0 Mon Sep 17 00:00:00 2001
|
2018-09-23 03:30:54 +00:00
|
|
|
From: Mark Vainomaa <mikroskeem@mikroskeem.eu>
|
|
|
|
Date: Wed, 12 Sep 2018 18:53:35 +0300
|
|
|
|
Subject: [PATCH] Add an API for CanPlaceOn and CanDestroy NBT values
|
|
|
|
|
|
|
|
|
2018-11-11 08:30:57 +00:00
|
|
|
diff --git a/src/main/java/com/destroystokyo/paper/Namespaced.java b/src/main/java/com/destroystokyo/paper/Namespaced.java
|
|
|
|
new file mode 100644
|
2020-04-02 21:07:06 +00:00
|
|
|
index 000000000..cd1a34b82
|
2018-11-11 08:30:57 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/com/destroystokyo/paper/Namespaced.java
|
2019-03-20 00:28:15 +00:00
|
|
|
@@ -0,0 +1,40 @@
|
2018-11-11 08:30:57 +00:00
|
|
|
+package com.destroystokyo.paper;
|
|
|
|
+
|
2019-03-20 00:28:15 +00:00
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
|
+
|
2018-11-11 08:30:57 +00:00
|
|
|
+/**
|
|
|
|
+ * Represents a namespaced resource, see {@link org.bukkit.NamespacedKey} for single elements
|
|
|
|
+ * or {@link com.destroystokyo.paper.NamespacedTag} for a collection of elements
|
|
|
|
+ *
|
|
|
|
+ * Namespaces may only contain lowercase alphanumeric characters, periods,
|
|
|
|
+ * underscores, and hyphens.
|
|
|
|
+ * <p>
|
|
|
|
+ * Keys may only contain lowercase alphanumeric characters, periods,
|
|
|
|
+ * underscores, hyphens, and forward slashes.
|
|
|
|
+ * <p>
|
|
|
|
+ * You should not be implementing this interface yourself, use {@link org.bukkit.NamespacedKey}
|
|
|
|
+ * or {@link com.destroystokyo.paper.NamespacedTag} as needed instead.
|
|
|
|
+ */
|
|
|
|
+public interface Namespaced {
|
|
|
|
+ /**
|
|
|
|
+ * Gets the namespace this resource is a part of
|
|
|
|
+ * <p>
|
|
|
|
+ * This is contractually obligated to only contain lowercase alphanumeric characters,
|
|
|
|
+ * periods, underscores, and hyphens.
|
|
|
|
+ *
|
|
|
|
+ * @return resource namespace
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-11-11 08:30:57 +00:00
|
|
|
+ String getNamespace();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Gets the key corresponding to this resource
|
|
|
|
+ * <p>
|
|
|
|
+ * This is contractually obligated to only contain lowercase alphanumeric characters,
|
|
|
|
+ * periods, underscores, hyphens, and forward slashes.
|
|
|
|
+ *
|
|
|
|
+ * @return resource key
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-11-11 08:30:57 +00:00
|
|
|
+ String getKey();
|
|
|
|
+}
|
|
|
|
diff --git a/src/main/java/com/destroystokyo/paper/NamespacedTag.java b/src/main/java/com/destroystokyo/paper/NamespacedTag.java
|
|
|
|
new file mode 100644
|
2020-04-02 21:07:06 +00:00
|
|
|
index 000000000..28f3fda95
|
2018-11-11 08:30:57 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/com/destroystokyo/paper/NamespacedTag.java
|
2019-03-20 00:28:15 +00:00
|
|
|
@@ -0,0 +1,142 @@
|
2018-11-11 08:30:57 +00:00
|
|
|
+package com.destroystokyo.paper;
|
|
|
|
+
|
|
|
|
+import com.google.common.base.Preconditions;
|
|
|
|
+import java.util.Locale;
|
|
|
|
+import java.util.UUID;
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
+import org.bukkit.plugin.Plugin;
|
2019-03-20 00:28:15 +00:00
|
|
|
+import org.jetbrains.annotations.NotNull;
|
2018-11-11 08:30:57 +00:00
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Represents a String based key pertaining to a tagged entry. Consists of two components - a namespace
|
|
|
|
+ * and a key.
|
|
|
|
+ * <p>
|
|
|
|
+ * Namespaces may only contain lowercase alphanumeric characters, periods,
|
|
|
|
+ * underscores, and hyphens.
|
|
|
|
+ * <p>
|
|
|
|
+ * Keys may only contain lowercase alphanumeric characters, periods,
|
|
|
|
+ * underscores, hyphens, and forward slashes.
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+// Paper - entire class, based on org.bukkit.NamespacedKey
|
|
|
|
+public final class NamespacedTag implements com.destroystokyo.paper.Namespaced {
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * The namespace representing all inbuilt keys.
|
|
|
|
+ */
|
|
|
|
+ public static final String MINECRAFT = "minecraft";
|
|
|
|
+ /**
|
|
|
|
+ * The namespace representing all keys generated by Bukkit for backwards
|
|
|
|
+ * compatibility measures.
|
|
|
|
+ */
|
|
|
|
+ public static final String BUKKIT = "bukkit";
|
|
|
|
+ //
|
|
|
|
+ private static final Pattern VALID_NAMESPACE = Pattern.compile("[a-z0-9._-]+");
|
|
|
|
+ private static final Pattern VALID_KEY = Pattern.compile("[a-z0-9/._-]+");
|
|
|
|
+ //
|
|
|
|
+ private final String namespace;
|
|
|
|
+ private final String key;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Create a key in a specific namespace.
|
|
|
|
+ *
|
|
|
|
+ * @param namespace String representing a grouping of keys
|
|
|
|
+ * @param key Name for this specific key
|
|
|
|
+ * @deprecated should never be used by plugins, for internal use only!!
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
2019-03-20 00:28:15 +00:00
|
|
|
+ public NamespacedTag(@NotNull String namespace, @NotNull String key) {
|
2018-11-11 08:30:57 +00:00
|
|
|
+ Preconditions.checkArgument(namespace != null && VALID_NAMESPACE.matcher(namespace).matches(), "Invalid namespace. Must be [a-z0-9._-]: %s", namespace);
|
|
|
|
+ Preconditions.checkArgument(key != null && VALID_KEY.matcher(key).matches(), "Invalid key. Must be [a-z0-9/._-]: %s", key);
|
|
|
|
+
|
|
|
|
+ this.namespace = namespace;
|
|
|
|
+ this.key = key;
|
|
|
|
+
|
|
|
|
+ String string = toString();
|
|
|
|
+ Preconditions.checkArgument(string.length() < 256, "NamespacedTag must be less than 256 characters", string);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Create a key in the plugin's namespace.
|
|
|
|
+ * <p>
|
|
|
|
+ * Namespaces may only contain lowercase alphanumeric characters, periods,
|
|
|
|
+ * underscores, and hyphens.
|
|
|
|
+ * <p>
|
|
|
|
+ * Keys may only contain lowercase alphanumeric characters, periods,
|
|
|
|
+ * underscores, hyphens, and forward slashes.
|
|
|
|
+ *
|
|
|
|
+ * @param plugin the plugin to use for the namespace
|
|
|
|
+ * @param key the key to create
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ public NamespacedTag(@NotNull Plugin plugin, @NotNull String key) {
|
2018-11-11 08:30:57 +00:00
|
|
|
+ Preconditions.checkArgument(plugin != null, "Plugin cannot be null");
|
|
|
|
+ Preconditions.checkArgument(key != null, "Key cannot be null");
|
|
|
|
+
|
|
|
|
+ this.namespace = plugin.getName().toLowerCase(Locale.ROOT);
|
|
|
|
+ this.key = key.toLowerCase().toLowerCase(Locale.ROOT);
|
|
|
|
+
|
|
|
|
+ // Check validity after normalization
|
|
|
|
+ Preconditions.checkArgument(VALID_NAMESPACE.matcher(this.namespace).matches(), "Invalid namespace. Must be [a-z0-9._-]: %s", this.namespace);
|
|
|
|
+ Preconditions.checkArgument(VALID_KEY.matcher(this.key).matches(), "Invalid key. Must be [a-z0-9/._-]: %s", this.key);
|
|
|
|
+
|
|
|
|
+ String string = toString();
|
|
|
|
+ Preconditions.checkArgument(string.length() < 256, "NamespacedTag must be less than 256 characters (%s)", string);
|
|
|
|
+ }
|
|
|
|
+
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-11-11 08:30:57 +00:00
|
|
|
+ public String getNamespace() {
|
|
|
|
+ return namespace;
|
|
|
|
+ }
|
|
|
|
+
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-11-11 08:30:57 +00:00
|
|
|
+ public String getKey() {
|
|
|
|
+ return key;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public int hashCode() {
|
|
|
|
+ int hash = 7;
|
|
|
|
+ hash = 47 * hash + this.namespace.hashCode();
|
|
|
|
+ hash = 47 * hash + this.key.hashCode();
|
|
|
|
+ return hash;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean equals(Object obj) {
|
|
|
|
+ if (obj == null) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ if (getClass() != obj.getClass()) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ final NamespacedTag other = (NamespacedTag) obj;
|
|
|
|
+ return this.namespace.equals(other.namespace) && this.key.equals(other.key);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String toString() {
|
|
|
|
+ return "#" + this.namespace + ":" + this.key;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Return a new random key in the {@link #BUKKIT} namespace.
|
|
|
|
+ *
|
|
|
|
+ * @return new key
|
|
|
|
+ * @deprecated should never be used by plugins, for internal use only!!
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
|
|
+ public static NamespacedTag randomKey() {
|
|
|
|
+ return new NamespacedTag(BUKKIT, UUID.randomUUID().toString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Get a key in the Minecraft namespace.
|
|
|
|
+ *
|
|
|
|
+ * @param key the key to use
|
|
|
|
+ * @return new key in the Minecraft namespace
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
|
|
|
+ public static NamespacedTag minecraft(@NotNull String key) {
|
2018-11-11 08:30:57 +00:00
|
|
|
+ return new NamespacedTag(MINECRAFT, key);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
diff --git a/src/main/java/org/bukkit/NamespacedKey.java b/src/main/java/org/bukkit/NamespacedKey.java
|
2020-04-02 21:07:06 +00:00
|
|
|
index 22eca2a18..620a962df 100644
|
2018-11-11 08:30:57 +00:00
|
|
|
--- a/src/main/java/org/bukkit/NamespacedKey.java
|
|
|
|
+++ b/src/main/java/org/bukkit/NamespacedKey.java
|
2019-03-20 00:28:15 +00:00
|
|
|
@@ -18,7 +18,7 @@ import org.jetbrains.annotations.NotNull;
|
2018-11-11 08:30:57 +00:00
|
|
|
* underscores, hyphens, and forward slashes.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
-public final class NamespacedKey {
|
|
|
|
+public final class NamespacedKey implements com.destroystokyo.paper.Namespaced { // Paper - implement namespaced
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The namespace representing all inbuilt keys.
|
2019-03-20 00:28:15 +00:00
|
|
|
@@ -83,11 +83,13 @@ public final class NamespacedKey {
|
2018-11-11 08:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 00:28:15 +00:00
|
|
|
@NotNull
|
2018-11-11 08:30:57 +00:00
|
|
|
+ @Override // Paper
|
|
|
|
public String getNamespace() {
|
|
|
|
return namespace;
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:28:15 +00:00
|
|
|
@NotNull
|
2018-11-11 08:30:57 +00:00
|
|
|
+ @Override // Paper
|
|
|
|
public String getKey() {
|
|
|
|
return key;
|
|
|
|
}
|
2018-09-23 03:30:54 +00:00
|
|
|
diff --git a/src/main/java/org/bukkit/inventory/meta/ItemMeta.java b/src/main/java/org/bukkit/inventory/meta/ItemMeta.java
|
2020-04-02 21:07:06 +00:00
|
|
|
index f2e9f2753..7e0b1d363 100644
|
2018-09-23 03:30:54 +00:00
|
|
|
--- a/src/main/java/org/bukkit/inventory/meta/ItemMeta.java
|
|
|
|
+++ b/src/main/java/org/bukkit/inventory/meta/ItemMeta.java
|
2019-12-10 23:56:33 +00:00
|
|
|
@@ -388,4 +388,87 @@ public interface ItemMeta extends Cloneable, ConfigurationSerializable, Persiste
|
|
|
|
@SuppressWarnings("javadoc")
|
2019-03-20 00:28:15 +00:00
|
|
|
@NotNull
|
2019-12-10 23:56:33 +00:00
|
|
|
ItemMeta clone();
|
|
|
|
+
|
2018-09-23 03:30:54 +00:00
|
|
|
+ // Paper start - Add an API for CanPlaceOn and CanDestroy NBT values
|
|
|
|
+ /**
|
|
|
|
+ * Gets set of materials what given item can destroy in {@link org.bukkit.GameMode#ADVENTURE}
|
|
|
|
+ *
|
|
|
|
+ * @return Set of materials
|
2018-11-11 08:30:57 +00:00
|
|
|
+ * @deprecated Minecraft does not limit this to the material enum, Use {@link #getDestroyableKeys()} as a replacement
|
2018-09-23 03:30:54 +00:00
|
|
|
+ */
|
2018-11-11 08:30:57 +00:00
|
|
|
+ @Deprecated
|
2018-09-23 03:30:54 +00:00
|
|
|
+ Set<org.bukkit.Material> getCanDestroy();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Sets set of materials what given item can destroy in {@link org.bukkit.GameMode#ADVENTURE}
|
|
|
|
+ *
|
|
|
|
+ * @param canDestroy Set of materials
|
2018-11-11 08:30:57 +00:00
|
|
|
+ * @deprecated Minecraft does not limit this to the material enum, Use {@link #setDestroyableKeys(Collection)} as a replacement
|
2018-09-23 03:30:54 +00:00
|
|
|
+ */
|
2018-11-11 08:30:57 +00:00
|
|
|
+ @Deprecated
|
2018-09-23 03:30:54 +00:00
|
|
|
+ void setCanDestroy(Set<org.bukkit.Material> canDestroy);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Gets set of materials where given item can be placed on in {@link org.bukkit.GameMode#ADVENTURE}
|
|
|
|
+ *
|
|
|
|
+ * @return Set of materials
|
2018-11-11 08:30:57 +00:00
|
|
|
+ * @deprecated Minecraft does not limit this to the material enum, Use {@link #getPlaceableKeys()} as a replacement
|
2018-09-23 03:30:54 +00:00
|
|
|
+ */
|
2018-11-11 08:30:57 +00:00
|
|
|
+ @Deprecated
|
2018-09-23 03:30:54 +00:00
|
|
|
+ Set<org.bukkit.Material> getCanPlaceOn();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Sets set of materials where given item can be placed on in {@link org.bukkit.GameMode#ADVENTURE}
|
|
|
|
+ *
|
|
|
|
+ * @param canPlaceOn Set of materials
|
2018-11-11 08:30:57 +00:00
|
|
|
+ * @deprecated Minecraft does not limit this to the material enum, Use {@link #setPlaceableKeys(Collection)} as a replacement
|
2018-09-23 03:30:54 +00:00
|
|
|
+ */
|
2018-11-11 08:30:57 +00:00
|
|
|
+ @Deprecated
|
2018-09-23 03:30:54 +00:00
|
|
|
+ void setCanPlaceOn(Set<org.bukkit.Material> canPlaceOn);
|
2018-11-11 08:30:57 +00:00
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Gets the collection of namespaced keys that the item can destroy in {@link org.bukkit.GameMode#ADVENTURE}
|
|
|
|
+ *
|
|
|
|
+ * @return Set of {@link com.destroystokyo.paper.Namespaced}
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-11-11 08:30:57 +00:00
|
|
|
+ Set<com.destroystokyo.paper.Namespaced> getDestroyableKeys();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Sets the collection of namespaced keys that the item can destroy in {@link org.bukkit.GameMode#ADVENTURE}
|
|
|
|
+ *
|
2018-11-22 03:25:30 +00:00
|
|
|
+ * @param canDestroy Collection of {@link com.destroystokyo.paper.Namespaced}
|
2018-11-11 08:30:57 +00:00
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ void setDestroyableKeys(@NotNull Collection<com.destroystokyo.paper.Namespaced> canDestroy);
|
2018-11-11 08:30:57 +00:00
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Gets the collection of namespaced keys that the item can be placed on in {@link org.bukkit.GameMode#ADVENTURE}
|
|
|
|
+ *
|
|
|
|
+ * @return Set of {@link com.destroystokyo.paper.Namespaced}
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-11-11 08:30:57 +00:00
|
|
|
+ Set<com.destroystokyo.paper.Namespaced> getPlaceableKeys();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Sets the set of namespaced keys that the item can be placed on in {@link org.bukkit.GameMode#ADVENTURE}
|
|
|
|
+ *
|
2018-11-22 03:25:30 +00:00
|
|
|
+ * @param canPlaceOn Collection of {@link com.destroystokyo.paper.Namespaced}
|
2018-11-11 08:30:57 +00:00
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
|
|
|
+ void setPlaceableKeys(@NotNull Collection<com.destroystokyo.paper.Namespaced> canPlaceOn);
|
2018-11-11 08:30:57 +00:00
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Checks for the existence of any keys that the item can be placed on
|
|
|
|
+ *
|
|
|
|
+ * @return true if this item has placeable keys
|
|
|
|
+ */
|
|
|
|
+ boolean hasPlaceableKeys();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Checks for the existence of any keys that the item can destroy
|
|
|
|
+ *
|
|
|
|
+ * @return true if this item has destroyable keys
|
|
|
|
+ */
|
|
|
|
+ boolean hasDestroyableKeys();
|
2018-09-23 03:30:54 +00:00
|
|
|
+ // Paper end
|
|
|
|
}
|
|
|
|
--
|
2020-04-02 21:07:06 +00:00
|
|
|
2.25.1
|
2018-09-23 03:30:54 +00:00
|
|
|
|