Paper/patches/api/0257-Add-PaperRegistry.patch
Jake Potrebic 03a4e7ac75
Updated Upstream (Bukkit/CraftBukkit) (#8832)
Upstream has released updates that appear to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

Bukkit Changes:
37262de8 PR-812: Add Registry#match(String)
d6b40162 SPIGOT-4569: Add more BlockData API
f9691891 PR-809: Throw a more clear error for BlockIterators with zero direction, add Vector#isZero()
91e79e19 PR-804: Added methods to get translation keys for materials, itemstacks and more
426b00d3 PR-795: Add new BiomeParameterPoint passed to BiomeProvider#getBiome
0e91ea52 SPIGOT-7224: Add events for brewing stands and campfires starting their actions

CraftBukkit Changes:
a50301aa5 Fix issues with fluid tag conversion and fluid #isTagged
6aeb5e4c3 SPIGOT-4569: Implement more BlockData API
7dbf862c2 PR-1131: Added methods to get translation keys for materials, itemstacks and more
7167588b1 PR-1117: Add new BiomeParameterPoint passed to BiomeProvider#getBiome
7c44152eb SPIGOT-7224: Add events for brewing stands and campfires starting their actions
2023-02-15 14:10:14 -08:00

113 lines
3.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Wed, 2 Mar 2022 13:36:21 -0800
Subject: [PATCH] Add PaperRegistry
diff --git a/src/main/java/io/papermc/paper/registry/Reference.java b/src/main/java/io/papermc/paper/registry/Reference.java
new file mode 100644
index 0000000000000000000000000000000000000000..d880810cbf05bc45051fe29515054211572e33b4
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/Reference.java
@@ -0,0 +1,43 @@
+package io.papermc.paper.registry;
+
+import org.bukkit.Keyed;
+import org.bukkit.NamespacedKey;
+import org.bukkit.Registry;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Represents a reference to a server-backed registry value that may
+ * change.
+ *
+ * @param <T> type of the value
+ */
+public interface Reference<T extends Keyed> extends Keyed {
+
+ /**
+ * Gets the value from the registry with the key.
+ *
+ * @return the value
+ * @throws java.util.NoSuchElementException if there is no value with this key
+ */
+ @NotNull T value();
+
+ /**
+ * Gets the value from the registry with the key.
+ *
+ * @return the value or null if it doesn't exist
+ */
+ @Nullable T valueOrNull();
+
+ /**
+ * Creates a reference to a registered value.
+ *
+ * @param registry the registry the value is located in
+ * @param key the key to the value
+ * @param <T> the type of the value
+ * @return a reference
+ */
+ static <T extends Keyed> @NotNull Reference<T> create(@NotNull Registry<T> registry, @NotNull NamespacedKey key) {
+ return new ReferenceImpl<>(registry, key);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/ReferenceImpl.java b/src/main/java/io/papermc/paper/registry/ReferenceImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..f29e76a6b66ddfec12ddf8db6dcb2df6083b5982
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/ReferenceImpl.java
@@ -0,0 +1,31 @@
+package io.papermc.paper.registry;
+
+import org.bukkit.Keyed;
+import org.bukkit.NamespacedKey;
+import org.bukkit.Registry;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.NoSuchElementException;
+
+record ReferenceImpl<T extends Keyed>(@NotNull Registry<T> registry, @NotNull NamespacedKey key) implements Reference<T> {
+
+ @Override
+ public @NotNull T value() {
+ final T value = this.registry.get(this.key);
+ if (value == null) {
+ throw new NoSuchElementException("No such value with key " + this.key);
+ }
+ return value;
+ }
+
+ @Override
+ public @Nullable T valueOrNull() {
+ return this.registry.get(this.key);
+ }
+
+ @Override
+ public @NotNull NamespacedKey getKey() {
+ return this.key;
+ }
+}
diff --git a/src/main/java/org/bukkit/UnsafeValues.java b/src/main/java/org/bukkit/UnsafeValues.java
index 7a6c22b9845798580635a938c28adf01908e04a0..1b6fe8fadcf4860cd3411c0433bc775de3a870ad 100644
--- a/src/main/java/org/bukkit/UnsafeValues.java
+++ b/src/main/java/org/bukkit/UnsafeValues.java
@@ -128,5 +128,15 @@ public interface UnsafeValues {
* Use this when sending custom packets, so that there are no collisions on the client or server.
*/
public int nextEntityId();
+
+ /**
+ * Gets the server-backed registry for a type.
+ *
+ * @param classOfT type
+ * @param <T> type
+ * @return the server-backed registry
+ * @throws IllegalArgumentException if there isn't a registry for that type
+ */
+ <T extends Keyed> @org.jetbrains.annotations.NotNull Registry<T> registryFor(Class<T> classOfT);
// Paper end
}