aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/api/0256-Add-PaperRegistry.patch
blob: 4cda0a858cbda680733e8f108846530120ff5e2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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 b24439b379be1a90dde4e6f4dbe5ca3fdd8face4..0697214210a6e87f690b9454d9651d06ca57a524 100644
--- a/src/main/java/org/bukkit/UnsafeValues.java
+++ b/src/main/java/org/bukkit/UnsafeValues.java
@@ -147,5 +147,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
 }