Paper/patches/api/0430-Improve-Registry.patch
2024-10-22 18:43:46 +02:00

240 lines
10 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Wed, 20 Dec 2023 02:03:10 -0800
Subject: [PATCH] Improve Registry
Adds Registry#getKey(Object) which should be the
primary way people get the key for an object. Registry
items need to exist without having a key and so
getKey() methods on Keyed objects that have a registry
are marked as Deprecated or Obsolete.
diff --git a/src/main/java/org/bukkit/Art.java b/src/main/java/org/bukkit/Art.java
index 042d1d932a33022e4fc873652f70dc6ed342d46a..e57e34064262b90221b0621f1d13e9705e68421a 100644
--- a/src/main/java/org/bukkit/Art.java
+++ b/src/main/java/org/bukkit/Art.java
@@ -103,6 +103,13 @@ public enum Art implements Keyed {
return id;
}
+ // Paper start - deprecate getKey
+ /**
+ * @deprecated use {@link Registry#getKey(Keyed)}, {@link io.papermc.paper.registry.RegistryAccess#getRegistry(io.papermc.paper.registry.RegistryKey)},
+ * and {@link io.papermc.paper.registry.RegistryKey#PAINTING_VARIANT}. Painting variants can exist without a key.
+ */
+ @Deprecated(since = "1.21")
+ // Paper end - deprecate getKey
@NotNull
@Override
public NamespacedKey getKey() {
diff --git a/src/main/java/org/bukkit/MusicInstrument.java b/src/main/java/org/bukkit/MusicInstrument.java
index c9f02466a04d20579fe2258bb02acf98e163ca81..bffd4ab2d08e5c3f83a49a31e1e55cc1eab7b319 100644
--- a/src/main/java/org/bukkit/MusicInstrument.java
+++ b/src/main/java/org/bukkit/MusicInstrument.java
@@ -47,6 +47,16 @@ public abstract class MusicInstrument implements Keyed, net.kyori.adventure.tran
return Registry.INSTRUMENT.getOrThrow(NamespacedKey.minecraft(key));
}
+ // Paper start - deprecate getKey
+ /**
+ * @deprecated use {@link Registry#getKey(Keyed)} and {@link Registry#INSTRUMENT}. MusicInstruments
+ * can exist without a key.
+ */
+ @Deprecated(forRemoval = true, since = "1.20.5")
+ @Override
+ public abstract @NotNull NamespacedKey getKey();
+ // Paper end - deprecate getKey
+
// Paper start - translation key
@Override
public @NotNull String translationKey() {
diff --git a/src/main/java/org/bukkit/Registry.java b/src/main/java/org/bukkit/Registry.java
index 6112db5d1153d045f2271038bada6b46d1a6a051..67cf3fcad21a8977d6fad172cc776b628ab68f25 100644
--- a/src/main/java/org/bukkit/Registry.java
+++ b/src/main/java/org/bukkit/Registry.java
@@ -379,6 +379,79 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
@Nullable
T get(@NotNull NamespacedKey key);
+ // Paper start - improve Registry
+ /**
+ * Gets the object by its key or throws if it doesn't exist.
+ *
+ * @param key the key to get the object of in this registry
+ * @return the object for the key
+ * @throws java.util.NoSuchElementException if the key doesn't point to an object in the registry
+ */
+ default @NotNull T getOrThrow(final net.kyori.adventure.key.@NotNull Key key) {
+ final T value = this.get(key);
+ if (value == null) {
+ throw new java.util.NoSuchElementException("No value for " + key + " in " + this);
+ }
+ return value;
+ }
+
+ /**
+ * Gets the object by its key or throws if it doesn't exist.
+ *
+ * @param key the key to get the object of in this registry
+ * @return the object for the key
+ * @throws java.util.NoSuchElementException if the key doesn't point to an object in the registry
+ */
+ default @NotNull T getOrThrow(final io.papermc.paper.registry.@NotNull TypedKey<T> key) {
+ final T value = this.get(key);
+ if (value == null) {
+ throw new java.util.NoSuchElementException("No value for " + key + " in " + this);
+ }
+ return value;
+ }
+
+ /**
+ * Gets the key for this object or throws if it doesn't exist.
+ * <p>
+ * Some types can exist without being in a registry
+ * and such will have no key associated with them. This
+ * method throw an exception if it isn't in this registry.
+ *
+ * @param value the value to get the key of in this registry
+ * @return the key for the value
+ * @throws java.util.NoSuchElementException if the value doesn't exist in this registry
+ * @see #getKey(Keyed)
+ */
+ default @NotNull NamespacedKey getKeyOrThrow(final @NotNull T value) {
+ Preconditions.checkArgument(value != null, "value cannot be null");
+ final NamespacedKey key = this.getKey(value);
+ if (key == null) {
+ throw new java.util.NoSuchElementException(value + " has no key in " + this);
+ }
+ return key;
+ }
+
+ /**
+ * Get the key for this object.
+ * <p>
+ * Some types can exist without being in a registry
+ * and such will have no key associated with them. This
+ * method will return null.
+ *
+ * @param value the value to get the key of in this registry
+ * @return the key for the value or null if not in the registry
+ * @see #getKeyOrThrow(Keyed)
+ */
+ default @Nullable NamespacedKey getKey(final @NotNull T value) {
+ Preconditions.checkArgument(value != null, "value cannot be null");
+ //noinspection ConstantValue (it might not be in the future...)
+ if (value instanceof Keyed) {
+ return value.getKey();
+ }
+ return null;
+ }
+ // Paper end - improve Registry
+
/**
* Get the object by its key.
*
@@ -475,5 +548,12 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
public Class<T> getType() {
return this.type;
}
+
+ // Paper start - improve Registry
+ @Override
+ public @NotNull NamespacedKey getKey(final @NotNull T value) {
+ return value.getKey();
+ }
+ // Paper end - improve Registry
}
}
diff --git a/src/main/java/org/bukkit/Sound.java b/src/main/java/org/bukkit/Sound.java
index cf17af024b1953b6f21f18885411ea6a0baa1d4c..f32a035317f3f8e200bb8076e6a326cb1e87cfe1 100644
--- a/src/main/java/org/bukkit/Sound.java
+++ b/src/main/java/org/bukkit/Sound.java
@@ -1655,6 +1655,13 @@ public enum Sound implements Keyed, net.kyori.adventure.sound.Sound.Type { // Pa
this.key = NamespacedKey.minecraft(key);
}
+ // Paper start - deprecate getKey
+ /**
+ * @deprecated use {@link Registry#getKey(Keyed)} and {@link Registry#SOUNDS}. Sounds
+ * can exist without a key.
+ */
+ @Deprecated(since = "1.20.5")
+ // Paper end - deprecate getKey
@NotNull
@Override
public NamespacedKey getKey() {
diff --git a/src/main/java/org/bukkit/block/banner/PatternType.java b/src/main/java/org/bukkit/block/banner/PatternType.java
index eaf6cd758344eeba29f00f822a50c93704af8bda..eb192030832e1741850871bec9bf999f014b6fc1 100644
--- a/src/main/java/org/bukkit/block/banner/PatternType.java
+++ b/src/main/java/org/bukkit/block/banner/PatternType.java
@@ -56,6 +56,13 @@ public interface PatternType extends OldEnum<PatternType>, Keyed {
PatternType FLOW = getType("flow");
PatternType GUSTER = getType("guster");
+ // Paper start - deprecate getKey
+ /**
+ * @deprecated use {@link Registry#getKey(Keyed)}, {@link io.papermc.paper.registry.RegistryAccess#getRegistry(io.papermc.paper.registry.RegistryKey)},
+ * and {@link io.papermc.paper.registry.RegistryKey#BANNER_PATTERN}. PatternTypes can exist without a key.
+ */
+ @Deprecated(since = "1.20.5")
+ // Paper end - deprecate getKey
@Override
@NotNull
public NamespacedKey getKey();
diff --git a/src/main/java/org/bukkit/generator/structure/Structure.java b/src/main/java/org/bukkit/generator/structure/Structure.java
index 20a7fd27ba3a029d58dd18ad9b470ffaed8c9578..a773550549b4093a5fb4ef47284c438dcddd99ab 100644
--- a/src/main/java/org/bukkit/generator/structure/Structure.java
+++ b/src/main/java/org/bukkit/generator/structure/Structure.java
@@ -61,4 +61,13 @@ public abstract class Structure implements Keyed {
*/
@NotNull
public abstract StructureType getStructureType();
+ // Paper start - deprecate getKey
+ /**
+ * @deprecated use {@link Registry#getKey(Keyed)} and {@link Registry#STRUCTURE}. Structures
+ * can exist without a key.
+ */
+ @Override
+ @Deprecated(since = "1.20.4")
+ public abstract @NotNull NamespacedKey getKey();
+ // Paper end - deprecate getKey
}
diff --git a/src/main/java/org/bukkit/inventory/meta/trim/TrimMaterial.java b/src/main/java/org/bukkit/inventory/meta/trim/TrimMaterial.java
index 804cb7162b34225d5dd84aada283de568d7d6592..2a7bedf850896e358213c2a201ca3d611cb9ea67 100644
--- a/src/main/java/org/bukkit/inventory/meta/trim/TrimMaterial.java
+++ b/src/main/java/org/bukkit/inventory/meta/trim/TrimMaterial.java
@@ -74,4 +74,14 @@ public interface TrimMaterial extends Keyed, Translatable {
@Deprecated(forRemoval = true)
@org.jetbrains.annotations.NotNull String getTranslationKey();
// Paper end - adventure
+
+ // Paper start - Registry#getKey
+ /**
+ * @deprecated use {@link Registry#getKey(Keyed)}, {@link io.papermc.paper.registry.RegistryAccess#getRegistry(io.papermc.paper.registry.RegistryKey)},
+ * and {@link io.papermc.paper.registry.RegistryKey#TRIM_MATERIAL}. TrimMaterials can exist without a key.
+ */
+ @Deprecated(forRemoval = true, since = "1.20.4")
+ @Override
+ org.bukkit.@org.jetbrains.annotations.NotNull NamespacedKey getKey();
+ // Paper end - Registry#getKey
}
diff --git a/src/main/java/org/bukkit/inventory/meta/trim/TrimPattern.java b/src/main/java/org/bukkit/inventory/meta/trim/TrimPattern.java
index 56cfe665daba1754e41f633d7c18172bebf87028..db657206c158e755227e8ef8e102a2ee2cc3bf0a 100644
--- a/src/main/java/org/bukkit/inventory/meta/trim/TrimPattern.java
+++ b/src/main/java/org/bukkit/inventory/meta/trim/TrimPattern.java
@@ -106,4 +106,14 @@ public interface TrimPattern extends Keyed, Translatable {
@Deprecated(forRemoval = true)
@org.jetbrains.annotations.NotNull String getTranslationKey();
// Paper end - adventure
+
+ // Paper start - Registry#getKey
+ /**
+ * @deprecated use {@link Registry#getKey(Keyed)}, {@link io.papermc.paper.registry.RegistryAccess#getRegistry(io.papermc.paper.registry.RegistryKey)},
+ * and {@link io.papermc.paper.registry.RegistryKey#TRIM_PATTERN}. TrimPatterns can exist without a key.
+ */
+ @Deprecated(forRemoval = true, since = "1.20.4")
+ @Override
+ org.bukkit.@org.jetbrains.annotations.NotNull NamespacedKey getKey();
+ // Paper end - Registry#getKey
}