split direct holder support up from ctor accepting Holder
Enchantment shouldn't support direct holders despite the ctor accepting a Holder type. We want to limit the types to ones that are actually used as direct holders in the game
This commit is contained in:
@@ -97,21 +97,21 @@ public final class PaperRegistries {
|
||||
start(Registries.MENU, RegistryKey.MENU).craft(MenuType.class, CraftMenuType::new).build(),
|
||||
start(Registries.ATTRIBUTE, RegistryKey.ATTRIBUTE).craft(Attribute.class, CraftAttribute::new).build(),
|
||||
start(Registries.FLUID, RegistryKey.FLUID).craft(Fluid.class, CraftFluid::new).build(),
|
||||
start(Registries.SOUND_EVENT, RegistryKey.SOUND_EVENT).craft(Sound.class, CraftSound::new).build(),
|
||||
start(Registries.SOUND_EVENT, RegistryKey.SOUND_EVENT).craft(Sound.class, CraftSound::new, true).build(),
|
||||
start(Registries.DATA_COMPONENT_TYPE, RegistryKey.DATA_COMPONENT_TYPE).craft(DataComponentTypes.class, PaperDataComponentType::of).build(),
|
||||
|
||||
// data-drivens
|
||||
start(Registries.BIOME, RegistryKey.BIOME).craft(Biome.class, CraftBiome::new).build().delayed(),
|
||||
start(Registries.STRUCTURE, RegistryKey.STRUCTURE).craft(Structure.class, CraftStructure::new).build().delayed(),
|
||||
start(Registries.TRIM_MATERIAL, RegistryKey.TRIM_MATERIAL).craft(TrimMaterial.class, CraftTrimMaterial::new).build().delayed(),
|
||||
start(Registries.TRIM_PATTERN, RegistryKey.TRIM_PATTERN).craft(TrimPattern.class, CraftTrimPattern::new).build().delayed(),
|
||||
start(Registries.TRIM_MATERIAL, RegistryKey.TRIM_MATERIAL).craft(TrimMaterial.class, CraftTrimMaterial::new, true).build().delayed(),
|
||||
start(Registries.TRIM_PATTERN, RegistryKey.TRIM_PATTERN).craft(TrimPattern.class, CraftTrimPattern::new, true).build().delayed(),
|
||||
start(Registries.DAMAGE_TYPE, RegistryKey.DAMAGE_TYPE).craft(DamageType.class, CraftDamageType::new).writable(PaperDamageTypeRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.WOLF_VARIANT, RegistryKey.WOLF_VARIANT).craft(Wolf.Variant.class, CraftWolf.CraftVariant::new).build().delayed(),
|
||||
start(Registries.ENCHANTMENT, RegistryKey.ENCHANTMENT).craft(Enchantment.class, CraftEnchantment::new).serializationUpdater(FieldRename.ENCHANTMENT_RENAME).writable(PaperEnchantmentRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.JUKEBOX_SONG, RegistryKey.JUKEBOX_SONG).craft(JukeboxSong.class, CraftJukeboxSong::new).build().delayed(),
|
||||
start(Registries.BANNER_PATTERN, RegistryKey.BANNER_PATTERN).craft(PatternType.class, CraftPatternType::new).writable(PaperBannerPatternRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.PAINTING_VARIANT, RegistryKey.PAINTING_VARIANT).craft(Art.class, CraftArt::new).writable(PaperPaintingVariantRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.INSTRUMENT, RegistryKey.INSTRUMENT).craft(MusicInstrument.class, CraftMusicInstrument::new).build().delayed(),
|
||||
start(Registries.BANNER_PATTERN, RegistryKey.BANNER_PATTERN).craft(PatternType.class, CraftPatternType::new, true).writable(PaperBannerPatternRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.PAINTING_VARIANT, RegistryKey.PAINTING_VARIANT).craft(Art.class, CraftArt::new, true).writable(PaperPaintingVariantRegistryEntry.PaperBuilder::new).delayed(),
|
||||
start(Registries.INSTRUMENT, RegistryKey.INSTRUMENT).craft(MusicInstrument.class, CraftMusicInstrument::new, true).build().delayed(),
|
||||
|
||||
// api-only
|
||||
start(Registries.ENTITY_TYPE, RegistryKey.ENTITY_TYPE).apiOnly(PaperSimpleRegistry::entityType),
|
||||
|
||||
@@ -107,7 +107,7 @@ public class PaperRegistryAccess implements RegistryAccess {
|
||||
|
||||
public <M> void lockReferenceHolders(final ResourceKey<? extends net.minecraft.core.Registry<M>> resourceKey) {
|
||||
final RegistryEntry<M, Keyed> entry = PaperRegistries.getEntry(resourceKey);
|
||||
if (entry == null || !(entry.meta() instanceof final RegistryEntryMeta.ServerSide<M, Keyed> serverSide) || !serverSide.registryTypeMapper().supportsDirectHolders()) {
|
||||
if (entry == null || !(entry.meta() instanceof final RegistryEntryMeta.ServerSide<M, Keyed> serverSide) || !serverSide.registryTypeMapper().constructorUsesHolder()) {
|
||||
return;
|
||||
}
|
||||
final CraftRegistry<?, M> registry = (CraftRegistry<?, M>) this.getRegistry(entry.apiKey());
|
||||
|
||||
@@ -24,7 +24,7 @@ public final class InlinedRegistryBuilderProviderImpl implements InlinedRegistry
|
||||
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()));
|
||||
return buildableMeta.registryTypeMapper().createBukkit(Holder.direct(builderFactory.requireBuilder().build()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -42,7 +42,11 @@ public class RegistryEntryBuilder<M, A extends Keyed> { // TODO remove Keyed
|
||||
}
|
||||
|
||||
public CraftStage<M, A> craft(final Class<?> classToPreload, final Function<Holder<M>, ? extends A> minecraftToBukkit) {
|
||||
return new CraftStage<>(this.mcKey, this.apiKey, classToPreload, new RegistryTypeMapper<>(minecraftToBukkit));
|
||||
return this.craft(classToPreload, minecraftToBukkit, false);
|
||||
}
|
||||
|
||||
public CraftStage<M, A> craft(final Class<?> classToPreload, final Function<Holder<M>, ? extends A> minecraftToBukkit, final boolean allowDirect) {
|
||||
return new CraftStage<>(this.mcKey, this.apiKey, classToPreload, new RegistryTypeMapper<>(minecraftToBukkit, allowDirect));
|
||||
}
|
||||
|
||||
public static final class CraftStage<M, A extends Keyed> extends RegistryEntryBuilder<M, A> { // TODO remove Keyed
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.papermc.paper.registry.entry;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.mojang.datafixers.util.Either;
|
||||
import io.papermc.paper.util.MCUtil;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import net.minecraft.core.Holder;
|
||||
@@ -9,29 +9,40 @@ import org.bukkit.NamespacedKey;
|
||||
|
||||
public final class RegistryTypeMapper<M, A> {
|
||||
|
||||
final Either<BiFunction<? super NamespacedKey, M, ? extends A>, Function<Holder<M>, ? extends A>> minecraftToBukkit;
|
||||
private static <M, A> Function<Holder<M>, ? extends A> wrap(final BiFunction<? super NamespacedKey, M, ? extends A> byValueCreator) {
|
||||
return holder -> {
|
||||
if (!(holder instanceof final Holder.Reference<M> reference)) {
|
||||
throw new IllegalArgumentException("This type does not support direct holders: " + holder);
|
||||
}
|
||||
return byValueCreator.apply(MCUtil.fromResourceKey(reference.key()), reference.value());
|
||||
};
|
||||
}
|
||||
|
||||
final Either<Function<Holder<M>, ? extends A>, BiFunction<? super NamespacedKey, M, ? extends A>> minecraftToBukkit;
|
||||
private final boolean supportsDirectHolders;
|
||||
|
||||
public RegistryTypeMapper(final BiFunction<? super NamespacedKey, M, ? extends A> byValueCreator) {
|
||||
this.minecraftToBukkit = Either.left(byValueCreator);
|
||||
this.minecraftToBukkit = Either.right(byValueCreator);
|
||||
this.supportsDirectHolders = false;
|
||||
}
|
||||
|
||||
public RegistryTypeMapper(final Function<Holder<M>, ? extends A> byHolderCreator) {
|
||||
this.minecraftToBukkit = Either.right(byHolderCreator);
|
||||
public RegistryTypeMapper(final Function<Holder<M>, ? extends A> byHolderCreator, final boolean supportsDirectHolders) {
|
||||
this.minecraftToBukkit = Either.left(byHolderCreator);
|
||||
this.supportsDirectHolders = supportsDirectHolders;
|
||||
}
|
||||
|
||||
public A createBukkit(final NamespacedKey key, final Holder<M> minecraft) {
|
||||
return this.minecraftToBukkit.map(
|
||||
minecraftToBukkit -> minecraftToBukkit.apply(key, minecraft.value()),
|
||||
minecraftToBukkit -> minecraftToBukkit.apply(minecraft)
|
||||
);
|
||||
public A createBukkit(final Holder<M> minecraft) {
|
||||
return this.minecraftToBukkit.<Function<Holder<M>, ? extends A>>map(
|
||||
Function.identity(),
|
||||
RegistryTypeMapper::wrap
|
||||
).apply(minecraft);
|
||||
}
|
||||
|
||||
public boolean supportsDirectHolders() {
|
||||
return this.minecraftToBukkit.right().isPresent();
|
||||
return this.supportsDirectHolders;
|
||||
}
|
||||
|
||||
public A convertDirectHolder(final Holder<M> directHolder) {
|
||||
Preconditions.checkArgument(this.supportsDirectHolders() && directHolder.kind() == Holder.Kind.DIRECT);
|
||||
return this.minecraftToBukkit.right().orElseThrow().apply(directHolder);
|
||||
public boolean constructorUsesHolder() {
|
||||
return this.minecraftToBukkit.left().isPresent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public interface Holderable<M> extends Handleable<M> {
|
||||
return this.getHolder().value();
|
||||
}
|
||||
|
||||
static <T extends org.bukkit.Keyed, M> @Nullable T fromBukkitSerializationObject(final Object deserialized, final Codec<? extends Holder<M>> codec, final RegistryKey<T> registryKey) { // TODO remove Keyed
|
||||
static <T extends org.bukkit.Keyed, M> @Nullable T fromBukkitSerializationObject(final Object deserialized, final Codec<M> directCodec, final RegistryKey<T> registryKey) { // TODO remove Keyed
|
||||
final Registry<T> registry = RegistryAccess.registryAccess().getRegistry(registryKey);
|
||||
return switch (deserialized) {
|
||||
case @Subst("key:value") final String string -> {
|
||||
@@ -41,18 +41,18 @@ public interface Holderable<M> extends Handleable<M> {
|
||||
throw new IllegalArgumentException("Cannot deserialize direct holders for " + registry);
|
||||
}
|
||||
final RegistryOps<JsonElement> ops = CraftRegistry.getMinecraftRegistry().createSerializationContext(JsonOps.INSTANCE);
|
||||
final Holder<M> holder = codec.decode(ops, element).getOrThrow().getFirst();
|
||||
yield ((CraftRegistry<T, M>) registry).convertDirectHolder(holder);
|
||||
final M holder = directCodec.decode(ops, element).getOrThrow().getFirst();
|
||||
yield ((CraftRegistry<T, M>) registry).createBukkit(Holder.direct(holder));
|
||||
}
|
||||
default -> throw new IllegalArgumentException("Cannot deserialize " + deserialized);
|
||||
};
|
||||
}
|
||||
|
||||
default Object toBukkitSerializationObject(final Codec<? super Holder<M>> codec) {
|
||||
default Object toBukkitSerializationObject(final Codec<? super M> directCodec) {
|
||||
return switch (this.getHolder()) {
|
||||
case final Holder.Direct<M> direct -> {
|
||||
final RegistryOps<JsonElement> ops = CraftRegistry.getMinecraftRegistry().createSerializationContext(JsonOps.INSTANCE);
|
||||
yield new JsonObjectWrapper(codec.encodeStart(ops, direct).getOrThrow().getAsJsonObject());
|
||||
yield new JsonObjectWrapper(directCodec.encodeStart(ops, direct.value()).getOrThrow().getAsJsonObject());
|
||||
}
|
||||
case final Holder.Reference<M> reference -> reference.key().location().toString();
|
||||
default -> throw new IllegalArgumentException("Cannot serialize " + this.getHolder());
|
||||
|
||||
Reference in New Issue
Block a user