better support for future direct buildables

This commit is contained in:
Jake Potrebic 2024-12-24 17:50:15 -08:00
parent c7d0436d2b
commit 6f7cabf7a6
No known key found for this signature in database
GPG key ID: 27CC63F7CBC866C7
2 changed files with 6 additions and 7 deletions

View file

@ -2,7 +2,6 @@ package io.papermc.paper.registry;
import io.papermc.paper.adventure.PaperAdventure; import io.papermc.paper.adventure.PaperAdventure;
import io.papermc.paper.registry.data.util.Conversions; import io.papermc.paper.registry.data.util.Conversions;
import io.papermc.paper.registry.entry.RegistryEntryMeta;
import java.util.function.Function; import java.util.function.Function;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import org.bukkit.Keyed; import org.bukkit.Keyed;
@ -11,13 +10,13 @@ import org.jspecify.annotations.Nullable;
public class PaperRegistryBuilderFactory<M, A extends Keyed, B extends PaperRegistryBuilder<M, A>> implements RegistryBuilderFactory<A, B> { // TODO remove Keyed public class PaperRegistryBuilderFactory<M, A extends Keyed, B extends PaperRegistryBuilder<M, A>> implements RegistryBuilderFactory<A, B> { // TODO remove Keyed
private final Conversions conversions; private final Conversions conversions;
private final RegistryEntryMeta.Buildable<M, A, B> meta; private final PaperRegistryBuilder.Filler<M, A, B> builderFiller;
private final Function<? super ResourceLocation, ? extends @Nullable M> existingValueGetter; private final Function<? super ResourceLocation, ? extends @Nullable M> existingValueGetter;
private @Nullable B builder; private @Nullable B builder;
public PaperRegistryBuilderFactory(final Conversions conversions, final RegistryEntryMeta.Buildable<M, A, B> meta, final Function<? super ResourceLocation, ? extends @Nullable M> existingValueGetter) { public PaperRegistryBuilderFactory(final Conversions conversions, final PaperRegistryBuilder.Filler<M, A, B> builderFiller, final Function<? super ResourceLocation, ? extends @Nullable M> existingValueGetter) {
this.conversions = conversions; this.conversions = conversions;
this.meta = meta; this.builderFiller = builderFiller;
this.existingValueGetter = existingValueGetter; this.existingValueGetter = existingValueGetter;
} }
@ -37,7 +36,7 @@ public class PaperRegistryBuilderFactory<M, A extends Keyed, B extends PaperRegi
@Override @Override
public B empty() { public B empty() {
this.validate(); this.validate();
return this.builder = this.meta.builderFiller().create(this.conversions); return this.builder = this.builderFiller.create(this.conversions);
} }
@Override @Override
@ -47,6 +46,6 @@ public class PaperRegistryBuilderFactory<M, A extends Keyed, B extends PaperRegi
if (existing == null) { if (existing == null) {
throw new IllegalArgumentException("Key " + key + " doesn't exist"); throw new IllegalArgumentException("Key " + key + " doesn't exist");
} }
return this.builder = this.meta.builderFiller().fill(this.conversions, existing); return this.builder = this.builderFiller.fill(this.conversions, existing);
} }
} }

View file

@ -31,7 +31,7 @@ public class WritableCraftRegistry<M, T extends Keyed, B extends PaperRegistryBu
public void register(final TypedKey<T> key, final Consumer<RegistryBuilderFactory<T, B>> value, final Conversions conversions) { public void register(final TypedKey<T> key, final Consumer<RegistryBuilderFactory<T, B>> value, final Conversions conversions) {
final ResourceKey<M> resourceKey = PaperRegistries.toNms(key); final ResourceKey<M> resourceKey = PaperRegistries.toNms(key);
this.registry.validateWrite(resourceKey); this.registry.validateWrite(resourceKey);
final PaperRegistryBuilderFactory<M, T, B> builderFactory = new PaperRegistryBuilderFactory<>(conversions, this.meta, this.registry.temporaryUnfrozenMap::get); final PaperRegistryBuilderFactory<M, T, B> builderFactory = new PaperRegistryBuilderFactory<>(conversions, this.meta.builderFiller(), this.registry.temporaryUnfrozenMap::get);
value.accept(builderFactory); value.accept(builderFactory);
PaperRegistryListenerManager.INSTANCE.registerWithListeners( PaperRegistryListenerManager.INSTANCE.registerWithListeners(
this.registry, this.registry,