mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-26 14:30:17 +01:00
Add directly buildable Art
This commit is contained in:
parent
6f7cabf7a6
commit
4d6a7d8aa0
6 changed files with 87 additions and 1 deletions
|
@ -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<InlinedRegistryBuilderProvider> INSTANCE = ServiceLoader.load(InlinedRegistryBuilderProvider.class).findFirst();
|
||||
}
|
||||
return Holder.INSTANCE.orElseThrow();
|
||||
}
|
||||
|
||||
Art createPaintingVariant(Consumer<RegistryBuilderFactory<Art, ? extends PaintingVariantRegistryEntry.Builder>> value);
|
||||
}
|
|
@ -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<Art>, 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<RegistryBuilderFactory<Art, ? extends PaintingVariantRegistryEntry.Builder>> value) {
|
||||
return InlinedRegistryBuilderProvider.instance().createPaintingVariant(value);
|
||||
}
|
||||
|
||||
Art KEBAB = getArt("kebab");
|
||||
Art AZTEC = getArt("aztec");
|
||||
Art ALBAN = getArt("alban");
|
||||
|
|
|
@ -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<M, T>) BY_REGISTRY_KEY.get(registryKey);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> RegistryEntryMeta.Buildable<M, T, B> getBuildableMeta(final ResourceKey<? extends Registry<M>> resourceKey) {
|
||||
final RegistryEntry<M, T> entry = getEntry(resourceKey);
|
||||
if (entry == null) {
|
||||
throw new IllegalArgumentException("No registry entry for " + resourceKey);
|
||||
}
|
||||
if (!(entry.meta() instanceof final RegistryEntryMeta.Buildable<M, T, ?> buildableMeta)) {
|
||||
throw new IllegalArgumentException("Registry entry for " + resourceKey + " is not buildable");
|
||||
}
|
||||
return (RegistryEntryMeta.Buildable<M, T, B>) buildableMeta;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <M, T> RegistryKey<T> registryFromNms(final ResourceKey<? extends Registry<M>> registryResourceKey) {
|
||||
return (RegistryKey<T>) Objects.requireNonNull(BY_RESOURCE_KEY.get(registryResourceKey), registryResourceKey + " doesn't have an api RegistryKey").apiKey();
|
||||
|
|
|
@ -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 <M, A extends Keyed, B extends PaperRegistryBuilder<M, A>> A create(final ResourceKey<? extends Registry<M>> registryKey, final Consumer<PaperRegistryBuilderFactory<M, A, B>> value) {
|
||||
final RegistryEntryMeta.Buildable<M, A, B> buildableMeta = PaperRegistries.getBuildableMeta(registryKey);
|
||||
Preconditions.checkArgument(buildableMeta.registryTypeMapper().supportsDirectHolders(), "Registry type mapper must support direct holders");
|
||||
final PaperRegistryBuilderFactory<M, A, B> 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<RegistryBuilderFactory<Art, ? extends PaintingVariantRegistryEntry.Builder>> value) {
|
||||
return create(Registries.PAINTING_VARIANT, value::accept);
|
||||
}
|
||||
}
|
|
@ -78,7 +78,7 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
|||
return CraftRegistry.registry;
|
||||
}
|
||||
|
||||
public static <E> net.minecraft.core.Registry<E> getMinecraftRegistry(ResourceKey<net.minecraft.core.Registry<E>> key) {
|
||||
public static <E> net.minecraft.core.Registry<E> getMinecraftRegistry(ResourceKey<? extends net.minecraft.core.Registry<E>> key) {
|
||||
return CraftRegistry.getMinecraftRegistry().lookupOrThrow(key);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
io.papermc.paper.registry.data.InlinedRegistryBuilderProviderImpl
|
Loading…
Reference in a new issue