From 4d6a7d8aa0a71f84bda17baa8eeae4ff32d988ca Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Tue, 24 Dec 2024 17:49:47 -0800 Subject: [PATCH] Add directly buildable Art --- .../data/InlinedRegistryBuilderProvider.java | 22 ++++++++++++ paper-api/src/main/java/org/bukkit/Art.java | 16 +++++++++ .../paper/registry/PaperRegistries.java | 13 +++++++ .../InlinedRegistryBuilderProviderImpl.java | 34 +++++++++++++++++++ .../org/bukkit/craftbukkit/CraftRegistry.java | 2 +- ...gistry.data.InlinedRegistryBuilderProvider | 1 + 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 paper-api/src/main/java/io/papermc/paper/registry/data/InlinedRegistryBuilderProvider.java create mode 100644 paper-server/src/main/java/io/papermc/paper/registry/data/InlinedRegistryBuilderProviderImpl.java create mode 100644 paper-server/src/main/resources/META-INF/services/io.papermc.paper.registry.data.InlinedRegistryBuilderProvider diff --git a/paper-api/src/main/java/io/papermc/paper/registry/data/InlinedRegistryBuilderProvider.java b/paper-api/src/main/java/io/papermc/paper/registry/data/InlinedRegistryBuilderProvider.java new file mode 100644 index 0000000000..554bda0341 --- /dev/null +++ b/paper-api/src/main/java/io/papermc/paper/registry/data/InlinedRegistryBuilderProvider.java @@ -0,0 +1,22 @@ +package io.papermc.paper.registry.data; + +import io.papermc.paper.registry.RegistryBuilderFactory; +import java.util.Optional; +import java.util.ServiceLoader; +import java.util.function.Consumer; +import org.bukkit.Art; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +@ApiStatus.NonExtendable +public interface InlinedRegistryBuilderProvider { + + static InlinedRegistryBuilderProvider instance() { + class Holder { + static final Optional INSTANCE = ServiceLoader.load(InlinedRegistryBuilderProvider.class).findFirst(); + } + return Holder.INSTANCE.orElseThrow(); + } + + Art createPaintingVariant(Consumer> value); +} diff --git a/paper-api/src/main/java/org/bukkit/Art.java b/paper-api/src/main/java/org/bukkit/Art.java index 5d3fb60a21..c6080c9d43 100644 --- a/paper-api/src/main/java/org/bukkit/Art.java +++ b/paper-api/src/main/java/org/bukkit/Art.java @@ -2,9 +2,14 @@ package org.bukkit; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; +import io.papermc.paper.registry.RegistryBuilderFactory; +import io.papermc.paper.registry.data.InlinedRegistryBuilderProvider; +import io.papermc.paper.registry.data.PaintingVariantRegistryEntry; import java.util.Locale; +import java.util.function.Consumer; import org.bukkit.packs.DataPack; import org.bukkit.util.OldEnum; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -18,6 +23,17 @@ import org.jetbrains.annotations.Nullable; */ public interface Art extends OldEnum, Keyed { + /** + * Create an inlined painting variant. + * + * @param value a consumer for the builder factory + * @return the created painting variant + */ + @ApiStatus.Experimental + static @NotNull Art create(final Consumer> value) { + return InlinedRegistryBuilderProvider.instance().createPaintingVariant(value); + } + Art KEBAB = getArt("kebab"); Art AZTEC = getArt("aztec"); Art ALBAN = getArt("alban"); diff --git a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java index a41356e136..7cef00f4ff 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java @@ -8,6 +8,7 @@ import io.papermc.paper.registry.data.PaperEnchantmentRegistryEntry; import io.papermc.paper.registry.data.PaperGameEventRegistryEntry; import io.papermc.paper.registry.data.PaperPaintingVariantRegistryEntry; import io.papermc.paper.registry.entry.RegistryEntry; +import io.papermc.paper.registry.entry.RegistryEntryMeta; import io.papermc.paper.registry.tag.TagKey; import java.util.Collections; import java.util.IdentityHashMap; @@ -136,6 +137,18 @@ public final class PaperRegistries { return (RegistryEntry) BY_REGISTRY_KEY.get(registryKey); } + @SuppressWarnings("unchecked") + public static > RegistryEntryMeta.Buildable getBuildableMeta(final ResourceKey> resourceKey) { + final RegistryEntry entry = getEntry(resourceKey); + if (entry == null) { + throw new IllegalArgumentException("No registry entry for " + resourceKey); + } + if (!(entry.meta() instanceof final RegistryEntryMeta.Buildable buildableMeta)) { + throw new IllegalArgumentException("Registry entry for " + resourceKey + " is not buildable"); + } + return (RegistryEntryMeta.Buildable) buildableMeta; + } + @SuppressWarnings("unchecked") public static RegistryKey registryFromNms(final ResourceKey> registryResourceKey) { return (RegistryKey) Objects.requireNonNull(BY_RESOURCE_KEY.get(registryResourceKey), registryResourceKey + " doesn't have an api RegistryKey").apiKey(); diff --git a/paper-server/src/main/java/io/papermc/paper/registry/data/InlinedRegistryBuilderProviderImpl.java b/paper-server/src/main/java/io/papermc/paper/registry/data/InlinedRegistryBuilderProviderImpl.java new file mode 100644 index 0000000000..8d7d75840a --- /dev/null +++ b/paper-server/src/main/java/io/papermc/paper/registry/data/InlinedRegistryBuilderProviderImpl.java @@ -0,0 +1,34 @@ +package io.papermc.paper.registry.data; + +import com.google.common.base.Preconditions; +import io.papermc.paper.registry.PaperRegistries; +import io.papermc.paper.registry.PaperRegistryBuilder; +import io.papermc.paper.registry.PaperRegistryBuilderFactory; +import io.papermc.paper.registry.RegistryBuilderFactory; +import io.papermc.paper.registry.data.util.Conversions; +import io.papermc.paper.registry.entry.RegistryEntryMeta; +import java.util.function.Consumer; +import net.minecraft.core.Holder; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; +import org.bukkit.Art; +import org.bukkit.Keyed; +import org.bukkit.craftbukkit.CraftRegistry; + +@SuppressWarnings("BoundedWildcard") +public final class InlinedRegistryBuilderProviderImpl implements InlinedRegistryBuilderProvider { + + private static > A create(final ResourceKey> registryKey, final Consumer> value) { + final RegistryEntryMeta.Buildable buildableMeta = PaperRegistries.getBuildableMeta(registryKey); + Preconditions.checkArgument(buildableMeta.registryTypeMapper().supportsDirectHolders(), "Registry type mapper must support direct holders"); + final PaperRegistryBuilderFactory builderFactory = new PaperRegistryBuilderFactory<>(Conversions.global(), buildableMeta.builderFiller(), CraftRegistry.getMinecraftRegistry(buildableMeta.mcKey())::getValue); + value.accept(builderFactory); + return buildableMeta.registryTypeMapper().convertDirectHolder(Holder.direct(builderFactory.requireBuilder().build())); + } + + @Override + public Art createPaintingVariant(final Consumer> value) { + return create(Registries.PAINTING_VARIANT, value::accept); + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java index 332215f8b4..a25f33307f 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java @@ -78,7 +78,7 @@ public class CraftRegistry implements Registry { return CraftRegistry.registry; } - public static net.minecraft.core.Registry getMinecraftRegistry(ResourceKey> key) { + public static net.minecraft.core.Registry getMinecraftRegistry(ResourceKey> key) { return CraftRegistry.getMinecraftRegistry().lookupOrThrow(key); } diff --git a/paper-server/src/main/resources/META-INF/services/io.papermc.paper.registry.data.InlinedRegistryBuilderProvider b/paper-server/src/main/resources/META-INF/services/io.papermc.paper.registry.data.InlinedRegistryBuilderProvider new file mode 100644 index 0000000000..9540695b2e --- /dev/null +++ b/paper-server/src/main/resources/META-INF/services/io.papermc.paper.registry.data.InlinedRegistryBuilderProvider @@ -0,0 +1 @@ +io.papermc.paper.registry.data.InlinedRegistryBuilderProviderImpl