Skip to content

Commit

Permalink
get values for builders in a safer way
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Jan 6, 2025
1 parent d4e8d54 commit 84790d4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@
public ResourceKey<? extends Registry<? extends T>> key() {
return registryKey;
}
@@ -121,6 +_,13 @@
public <T> Optional<RegistryOps.RegistryInfo<T>> lookup(ResourceKey<? extends Registry<? extends T>> registryKey) {
return getEntry(registryKey).map(Entry::opsInfo);
}
+
+ // Paper start - add method to get the value for pre-filling builders in the reg mod API
+ @Override
+ public HolderLookup.Provider lookupForValueCopyViaBuilders() {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ // Paper end - add method to get the value for pre-filling builders in the reg mod API
});
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
--- a/net/minecraft/resources/RegistryDataLoader.java
+++ b/net/minecraft/resources/RegistryDataLoader.java
@@ -179,11 +_,21 @@
final Map<ResourceKey<? extends Registry<?>>, RegistryOps.RegistryInfo<?>> map = new HashMap<>();
registryLookups.forEach(registryLookup -> map.put(registryLookup.key(), createInfoForContextRegistry((HolderLookup.RegistryLookup<?>)registryLookup)));
loaders.forEach(loader -> map.put(loader.registry.key(), createInfoForNewRegistry(loader.registry)));
+ // this creates a HolderLookup.Provider to be used exclusively to obtain real instances of values to pre-populate builders
+ // for the Registry Modification API. This concatenates the context registry lookups and the empty registries about to be filled.
+ final HolderLookup.Provider providerForBuilders = HolderLookup.Provider.create(java.util.stream.Stream.concat(registryLookups.stream(), loaders.stream().map(Loader::registry))); // Paper - add method to get the value for pre-filling builders in the reg mod API
return new RegistryOps.RegistryInfoLookup() {
@Override
public <T> Optional<RegistryOps.RegistryInfo<T>> lookup(ResourceKey<? extends Registry<? extends T>> registryKey) {
return Optional.ofNullable((RegistryOps.RegistryInfo<T>)map.get(registryKey));
}
+
+ // Paper start - add method to get the value for pre-filling builders in the reg mod API
+ @Override
+ public HolderLookup.Provider lookupForValueCopyViaBuilders() {
+ return providerForBuilders;
+ }
+ // Paper end - add method to get the value for pre-filling builders in the reg mod API
};
}

@@ -247,13 +_,14 @@
RegistryOps<JsonElement> ops,
ResourceKey<E> resourceKey,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--- a/net/minecraft/resources/RegistryOps.java
+++ b/net/minecraft/resources/RegistryOps.java
@@ -117,6 +_,13 @@
public int hashCode() {
return this.lookupProvider.hashCode();
}
+
+ // Paper start - add method to get the value for pre-filling builders in the reg mod API
+ @Override
+ public HolderLookup.Provider lookupForValueCopyViaBuilders() {
+ return this.lookupProvider;
+ }
+ // Paper end - add method to get the value for pre-filling builders in the reg mod API
}

public record RegistryInfo<T>(HolderOwner<T> owner, HolderGetter<T> getter, Lifecycle elementsLifecycle) {
@@ -127,5 +_,7 @@

public interface RegistryInfoLookup {
<T> Optional<RegistryOps.RegistryInfo<T>> lookup(ResourceKey<? extends Registry<? extends T>> registryKey);
+
+ HolderLookup.Provider lookupForValueCopyViaBuilders(); // Paper - add method to get the value for pre-filling builders in the reg mod API
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
import io.papermc.paper.registry.PaperRegistryBuilder;
import io.papermc.paper.registry.PaperRegistryBuilderFactory;
import io.papermc.paper.registry.entry.RegistryEntryMeta;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import net.kyori.adventure.text.Component;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.RegistryOps;
Expand Down Expand Up @@ -87,14 +84,8 @@ public <M, A extends Keyed, B extends PaperRegistryBuilder<M, A>> Holder<M> crea
final ResourceKey<? extends Registry<M>> registryKey,
final RegistryEntryMeta.Buildable<M, A, B> buildableMeta
) {
final RegistryOps.RegistryInfo<M> info = this.lookup.lookup(registryKey).orElseThrow();
final Function<ResourceKey<M>, Optional<M>> existingValueGetter;
if (info.getter() instanceof final HolderLookup.RegistryLookup<M> registryLookup) {
existingValueGetter = registryLookup::getValueForCopying;
} else {
existingValueGetter = ((MappedRegistry<M>) info.owner())::getValueForCopying;
}
return new PaperRegistryBuilderFactory<>(registryKey, this, buildableMeta.builderFiller(), existingValueGetter);
final HolderLookup.RegistryLookup<M> lookupForBuilders = this.lookup.lookupForValueCopyViaBuilders().lookupOrThrow(registryKey);
return new PaperRegistryBuilderFactory<>(registryKey, this, buildableMeta.builderFiller(), lookupForBuilders::getValueForCopying);
}

}

0 comments on commit 84790d4

Please sign in to comment.