Skip to content

Commit

Permalink
Make RegistryObject.getHolder lazy, Should help cases where vanilla r…
Browse files Browse the repository at this point in the history
…egistries use holders from other vanilla registries. Closes #9961
  • Loading branch information
LexManos committed May 12, 2024
1 parent dffa3dd commit c700b4e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@
}

return this;
@@ -435,5 +_,20 @@
@@ -435,5 +_,24 @@
@Override
public HolderLookup.RegistryLookup<T> asLookup() {
return this.lookup;
+ }
+
+ public boolean isIntrusive() {
+ return this.unregisteredIntrusiveHolders != null;
+ }
+
+ private static final Set<ResourceLocation> KNOWN = new java.util.LinkedHashSet<>();
+ public static Set<ResourceLocation> getKnownRegistries() {
+ return java.util.Collections.unmodifiableSet(KNOWN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ boolean isFrozen() {
return this.frozen;
}

boolean isIntrusive() {
@Override
public boolean isIntrusive() {
return this.intrusiveHolderCallback != null && this.stage == RegistryManager.ACTIVE;
}

Expand Down
30 changes: 10 additions & 20 deletions src/main/java/net/minecraftforge/registries/NewRegistryEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
/**
* Register new registries when you receive this event through {@link RegistryBuilder} and {@link #create(RegistryBuilder)}.
*/
public class NewRegistryEvent extends Event implements IModBusEvent
{
public class NewRegistryEvent extends Event implements IModBusEvent {
private static final Logger LOGGER = LogUtils.getLogger();
private final List<RegistryData<?>> registries = new ArrayList<>();

Expand All @@ -35,8 +34,7 @@ public NewRegistryEvent() {}
* @param builder The builder to turn into a {@link IForgeRegistry}
* @return A supplier of the {@link IForgeRegistry} created by the builder. Resolving too early will return null.
*/
public <V> Supplier<IForgeRegistry<V>> create(RegistryBuilder<V> builder)
{
public <V> Supplier<IForgeRegistry<V>> create(RegistryBuilder<V> builder) {
return create(builder, null);
}

Expand All @@ -47,8 +45,7 @@ public <V> Supplier<IForgeRegistry<V>> create(RegistryBuilder<V> builder)
* @param onFill Called when the returned supplier is filled with the registry
* @return a supplier of the {@link IForgeRegistry} created by the builder. Resolving too early will return null.
*/
public <V> Supplier<IForgeRegistry<V>> create(RegistryBuilder<V> builder, @Nullable Consumer<IForgeRegistry<V>> onFill)
{
public <V> Supplier<IForgeRegistry<V>> create(RegistryBuilder<V> builder, @Nullable Consumer<IForgeRegistry<V>> onFill) {
RegistryHolder<V> registryHolder = new RegistryHolder<>();

registries.add(new RegistryData<>(builder, registryHolder, onFill));
Expand All @@ -57,20 +54,17 @@ public <V> Supplier<IForgeRegistry<V>> create(RegistryBuilder<V> builder, @Nulla
}

@SuppressWarnings("deprecation")
void fill()
{
void fill() {
RuntimeException aggregate = new RuntimeException();
Map<RegistryBuilder<?>, IForgeRegistry<?>> builtRegistries = new IdentityHashMap<>();

if (BuiltInRegistries.REGISTRY instanceof MappedRegistry<?> rootRegistry)
rootRegistry.unfreeze();

for (RegistryData<?> data : this.registries)
{
for (RegistryData<?> data : this.registries) {
try {
buildRegistry(builtRegistries, data);
} catch (Throwable t)
{
} catch (Throwable t) {
aggregate.addSuppressed(t);
return;
}
Expand All @@ -83,8 +77,7 @@ void fill()
LOGGER.error(LogUtils.FATAL_MARKER, "Failed to create some forge registries, see suppressed exceptions for details", aggregate);
}

private <T> void buildRegistry(Map<RegistryBuilder<?>, IForgeRegistry<?>> builtRegistries, RegistryData<T> data)
{
private <T> void buildRegistry(Map<RegistryBuilder<?>, IForgeRegistry<?>> builtRegistries, RegistryData<T> data) {
RegistryBuilder<T> builder = data.builder;
IForgeRegistry<T> registry = builder.create();

Expand All @@ -104,20 +97,17 @@ private record RegistryData<V>(
Consumer<IForgeRegistry<V>> onFill
) {}

private static class RegistryHolder<V> implements Supplier<IForgeRegistry<V>>
{
private static class RegistryHolder<V> implements Supplier<IForgeRegistry<V>> {
IForgeRegistry<V> registry = null;

@Override
public IForgeRegistry<V> get()
{
public IForgeRegistry<V> get() {
return this.registry;
}
}

@Override
public String toString()
{
public String toString() {
return "RegistryEvent.NewRegistry";
}
}

0 comments on commit c700b4e

Please sign in to comment.