Skip to content

Commit

Permalink
RegistryValue API
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Nov 24, 2024
1 parent bf8405f commit 185a1ea
Show file tree
Hide file tree
Showing 2 changed files with 448 additions and 0 deletions.
200 changes: 200 additions & 0 deletions patches/api/0501-RegistryValue-API.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <[email protected]>
Date: Sun, 24 Nov 2024 02:38:39 -0800
Subject: [PATCH] RegistryValue API


diff --git a/src/main/java/io/papermc/paper/registry/DirectRegistryValue.java b/src/main/java/io/papermc/paper/registry/DirectRegistryValue.java
new file mode 100644
index 0000000000000000000000000000000000000000..5ed90654e673f7959a03f534e677bfae5dfaca41
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/DirectRegistryValue.java
@@ -0,0 +1,22 @@
+package io.papermc.paper.registry;
+
+import io.papermc.paper.registry.tag.TagKey;
+import net.kyori.adventure.key.Key;
+
+public record DirectRegistryValue<T>(T value) implements RegistryValue.Direct<T> {
+
+ @Override
+ public boolean is(final Key key) {
+ return false;
+ }
+
+ @Override
+ public boolean is(final TypedKey<T> typedKey) {
+ return false;
+ }
+
+ @Override
+ public boolean is(final TagKey<T> tagKey) {
+ return false;
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/RegistryLookup.java b/src/main/java/io/papermc/paper/registry/RegistryLookup.java
new file mode 100644
index 0000000000000000000000000000000000000000..9ca06481436767745afbfb8385e9820179e64686
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/RegistryLookup.java
@@ -0,0 +1,34 @@
+package io.papermc.paper.registry;
+
+import io.papermc.paper.registry.tag.Tag;
+import io.papermc.paper.registry.tag.TagKey;
+import org.bukkit.Keyed;
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
+
+@ApiStatus.Experimental
+@NullMarked
+@ApiStatus.NonExtendable
+public interface RegistryLookup {
+
+ /**
+ * Gets or creates a tag for the given tag key. This tag
+ * is then required to be filled either from the built-in or
+ * custom datapack.
+ *
+ * @param tagKey the tag key
+ * @return the tag
+ * @param <V> the tag value type
+ */
+ <V extends Keyed> Tag<V> getOrCreateTag(TagKey<V> tagKey);
+
+ /**
+ * Gets or creates a registry value for the given typed key. If
+ * it's created, it's required to be filled during some later event.
+ *
+ * @param typedKey the typed key
+ * @return the registry value
+ * @param <V> the value type
+ */
+ <V> RegistryValue.Reference<V> getOrCreateValue(TypedKey<V> typedKey);
+}
diff --git a/src/main/java/io/papermc/paper/registry/RegistryValue.java b/src/main/java/io/papermc/paper/registry/RegistryValue.java
new file mode 100644
index 0000000000000000000000000000000000000000..c47c192538750943c190f0aadd4e88bfa7356659
--- /dev/null
+++ b/src/main/java/io/papermc/paper/registry/RegistryValue.java
@@ -0,0 +1,43 @@
+package io.papermc.paper.registry;
+
+import io.papermc.paper.registry.tag.TagKey;
+import net.kyori.adventure.key.Key;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.Contract;
+import org.jspecify.annotations.NullMarked;
+
+@ApiStatus.Experimental
+@NullMarked
+@ApiStatus.NonExtendable
+public sealed interface RegistryValue<T> {
+
+ @Contract(value = "_ -> new", pure = true)
+ static <T> RegistryValue<T> direct(final T value) {
+ return new DirectRegistryValue<>(value);
+ }
+
+ @Contract(pure = true)
+ boolean is(Key key);
+
+ @Contract(pure = true)
+ boolean is(TypedKey<T> typedKey);
+
+ @Contract(pure = true)
+ boolean is(TagKey<T> tagKey);
+
+ @ApiStatus.Experimental
+ @ApiStatus.NonExtendable
+ non-sealed interface Reference<T> extends RegistryValue<T> {
+
+ @Contract(pure = true)
+ TypedKey<T> key();
+ }
+
+ @ApiStatus.Experimental
+ @ApiStatus.NonExtendable
+ sealed interface Direct<T> extends RegistryValue<T> permits DirectRegistryValue {
+
+ @Contract(pure = true)
+ T value();
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/event/RegistryEntryAddEvent.java b/src/main/java/io/papermc/paper/registry/event/RegistryEntryAddEvent.java
index 56468b311e40a6d1aa03c6d31328952b92e95027..98963c3c8a49375fc14e6673f1317f74dcccb833 100644
--- a/src/main/java/io/papermc/paper/registry/event/RegistryEntryAddEvent.java
+++ b/src/main/java/io/papermc/paper/registry/event/RegistryEntryAddEvent.java
@@ -1,6 +1,8 @@
package io.papermc.paper.registry.event;

import io.papermc.paper.registry.RegistryBuilder;
+import io.papermc.paper.registry.RegistryLookup;
+import io.papermc.paper.registry.RegistryValue;
import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.registry.tag.Tag;
import io.papermc.paper.registry.tag.TagKey;
@@ -19,7 +21,7 @@ import org.jspecify.annotations.NullMarked;
@ApiStatus.Experimental
@NullMarked
@ApiStatus.NonExtendable
-public interface RegistryEntryAddEvent<T, B extends RegistryBuilder<T>> extends RegistryEvent<T> {
+public interface RegistryEntryAddEvent<T, B extends RegistryBuilder<T>> extends RegistryEvent<T>, RegistryLookup {

/**
* Gets the builder for the entry being added to the registry.
@@ -34,15 +36,4 @@ public interface RegistryEntryAddEvent<T, B extends RegistryBuilder<T>> extends
* @return the key
*/
TypedKey<T> key();
-
- /**
- * Gets or creates a tag for the given tag key. This tag
- * is then required to be filled either from the built-in or
- * custom datapack.
- *
- * @param tagKey the tag key
- * @return the tag
- * @param <V> the tag value type
- */
- <V extends Keyed> Tag<V> getOrCreateTag(TagKey<V> tagKey);
}
diff --git a/src/main/java/io/papermc/paper/registry/event/RegistryFreezeEvent.java b/src/main/java/io/papermc/paper/registry/event/RegistryFreezeEvent.java
index 59e8ca6c5b7fa0424ad9b2c74545ec53444b5fcb..1a95ee135d25f0700e1d6e902c09c0a28092d36e 100644
--- a/src/main/java/io/papermc/paper/registry/event/RegistryFreezeEvent.java
+++ b/src/main/java/io/papermc/paper/registry/event/RegistryFreezeEvent.java
@@ -1,6 +1,9 @@
package io.papermc.paper.registry.event;

import io.papermc.paper.registry.RegistryBuilder;
+import io.papermc.paper.registry.RegistryLookup;
+import io.papermc.paper.registry.RegistryValue;
+import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.registry.tag.Tag;
import io.papermc.paper.registry.tag.TagKey;
import org.bukkit.Keyed;
@@ -18,7 +21,7 @@ import org.jspecify.annotations.NullMarked;
@ApiStatus.Experimental
@NullMarked
@ApiStatus.NonExtendable
-public interface RegistryFreezeEvent<T, B extends RegistryBuilder<T>> extends RegistryEvent<T> {
+public interface RegistryFreezeEvent<T, B extends RegistryBuilder<T>> extends RegistryEvent<T>, RegistryLookup {

/**
* Get the writable registry.
@@ -26,15 +29,4 @@ public interface RegistryFreezeEvent<T, B extends RegistryBuilder<T>> extends Re
* @return a writable registry
*/
WritableRegistry<T, B> registry();
-
- /**
- * Gets or creates a tag for the given tag key. This tag
- * is then required to be filled either from the built-in or
- * custom datapack.
- *
- * @param tagKey the tag key
- * @return the tag
- * @param <V> the tag value type
- */
- <V extends Keyed> Tag<V> getOrCreateTag(TagKey<V> tagKey);
}
Loading

0 comments on commit 185a1ea

Please sign in to comment.