Skip to content

Commit

Permalink
Renames and helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
lynxplay committed Dec 26, 2024
1 parent 6f7cabf commit 64e8731
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* A factory to create a {@link RegistryBuilder} for a given {@link TypedKey}. For
* each instance of this class, once either {@link #empty()} or {@link #copyOf(TypedKey)}
* each instance of this class, once either {@link #empty()} or {@link #copyFrom(TypedKey)}
* is called once, any future calls to either method will throw an {@link IllegalStateException}.
*
* @param <T> The type of the registry
Expand All @@ -21,7 +21,7 @@ public interface RegistryBuilderFactory<T, B extends RegistryBuilder<T>> {
* Creates a new empty {@link RegistryBuilder}.
*
* @return A new empty {@link RegistryBuilder}
* @throws IllegalStateException if this method or {@link #copyOf(TypedKey)}) has already been called once
* @throws IllegalStateException if this method or {@link #copyFrom(TypedKey)}) has already been called once
*/
@Contract("-> new")
B empty();
Expand All @@ -35,5 +35,5 @@ public interface RegistryBuilderFactory<T, B extends RegistryBuilder<T>> {
* @throws IllegalArgumentException if key doesn't exist
*/
@Contract("_ -> new")
B copyOf(TypedKey<T> key);
B copyFrom(TypedKey<T> key);
}
22 changes: 22 additions & 0 deletions paper-api/src/main/java/io/papermc/paper/registry/RegistryKey.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.papermc.paper.registry;

import io.papermc.paper.datacomponent.DataComponentType;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.KeyPattern;
import net.kyori.adventure.key.Keyed;
import org.bukkit.Art;
import org.bukkit.Fluid;
Expand Down Expand Up @@ -200,4 +202,24 @@ public sealed interface RegistryKey<T> extends Keyed permits RegistryKeyImpl {
RegistryKey<Particle> PARTICLE_TYPE = create("particle_type");
RegistryKey<PotionType> POTION = create("potion");
RegistryKey<MemoryKey<?>> MEMORY_MODULE_TYPE = create("memory_module_type");

/**
* Constructs a new {@link TypedKey} for this registry given the typed key's key.
*
* @param key the key of the typed key.
* @return the constructed typed key.
*/
default TypedKey<T> typedKey(final Key key) {
return TypedKey.create(this, key);
}

/**
* Constructs a new {@link TypedKey} for this registry given the typed key's key.
*
* @param key the string representation of the key that will be passed to {@link Key#key(String)}.
* @return the constructed typed key.
*/
default TypedKey<T> typedKey(final @KeyPattern String key) {
return TypedKey.create(this, key);
}
}
15 changes: 15 additions & 0 deletions paper-api/src/main/java/io/papermc/paper/registry/TypedKey.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.papermc.paper.registry;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.KeyPattern;
import net.kyori.adventure.key.Keyed;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;
Expand Down Expand Up @@ -42,4 +43,18 @@ public sealed interface TypedKey<T> extends Key permits TypedKeyImpl {
static <T> TypedKey<T> create(final RegistryKey<T> registryKey, final Key key) {
return new TypedKeyImpl<>(key, registryKey);
}

/**
* Create a typed key from a string and a registry key.
*
* @param registryKey the registry this key is for
* @param key the string version of a {@link Key} that will be passed to {@link Key#key(String)} for parsing.
* @param <T> value type
* @return a new key for the value key and registry key
* @see Key#key(String)
*/
@ApiStatus.Experimental
static <T> TypedKey<T> create(final RegistryKey<T> registryKey, final @KeyPattern String key) {
return create(registryKey, Key.key(key));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface WritableRegistry<T, B extends RegistryBuilder<T>> {
* @param value a consumer for the entry's builder
*/
default void register(final TypedKey<T> key, final Consumer<? super B> value) {
this.factoryRegister(key, factory -> value.accept(factory.empty()));
this.registerWith(key, factory -> value.accept(factory.empty()));
}

/**
Expand All @@ -38,5 +38,5 @@ default void register(final TypedKey<T> key, final Consumer<? super B> value) {
* @param key the entry's key (must be unique from others)
* @param value a consumer of a builder factory
*/
void factoryRegister(TypedKey<T> key, Consumer<RegistryBuilderFactory<T, B>> value);
void registerWith(TypedKey<T> key, Consumer<RegistryBuilderFactory<T, B>> value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public B empty() {
}

@Override
public B copyOf(final TypedKey<A> key) {
public B copyFrom(final TypedKey<A> key) {
this.validate();
final M existing = this.existingValueGetter.apply(PaperAdventure.asVanilla(key));
if (existing == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ApiWritableRegistry(final Conversions conversions) {
}

@Override
public void factoryRegister(final TypedKey<T> key, final Consumer<RegistryBuilderFactory<T, B>> value) {
public void registerWith(final TypedKey<T> key, final Consumer<RegistryBuilderFactory<T, B>> value) {
WritableCraftRegistry.this.register(key, value, this.conversions);
}
}
Expand Down

0 comments on commit 64e8731

Please sign in to comment.