Allow using DeferredRegisters for vanilla registries (#8527)

Catch and aggregate exceptions when applying object holders
This commit is contained in:
SizableShrimp authored and GitHub committed 2022-04-01 13:49:46 -07:00
1 parent 8f0d3da1e4
commit 1f031ad2c3
13 files changed
+550 -142

No files matched your search

@@ -10,6 +10,7 @@ import com.mojang.serialization.Lifecycle;
import java.util.*;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.world.level.block.AirBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
@@ -55,6 +56,7 @@ import net.minecraftforge.common.util.LogMessageAdapter;
import net.minecraftforge.common.world.ForgeWorldPreset;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.RegistryEvent.MissingMappings;
import net.minecraftforge.fml.ModLoader;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.IModStateTransition;
import net.minecraftforge.fml.StartupMessageManager;
@@ -372,12 +374,17 @@ public class GameData
final ResourceLocation rl = event.getName();
ForgeRegistry<?> fr = (ForgeRegistry<?>) event.getRegistry();
fr.freeze();
LOGGER.debug(REGISTRIES, "Applying holder lookups: {}", rl.toString());
ObjectHolderRegistry.applyObjectHolders(rl::equals);
LOGGER.debug(REGISTRIES, "Holder lookups applied: {}", rl.toString());
applyHolderLookups(rl);
}, executor).handle((v, t)->t != null ? Collections.singletonList(t): Collections.emptyList());
}
private static void applyHolderLookups(ResourceLocation registryName)
{
LOGGER.debug(REGISTRIES, "Applying holder lookups: {}", registryName);
ObjectHolderRegistry.applyObjectHolders(registryName::equals);
LOGGER.debug(REGISTRIES, "Holder lookups applied: {}", registryName);
}
@SuppressWarnings("deprecation")
public static CompletableFuture<List<Throwable>> checkForRevertToVanilla(final Executor executor, final CompletableFuture<List<Throwable>> listCompletableFuture) {
return listCompletableFuture.whenCompleteAsync((errors, except) -> {
@@ -386,11 +393,28 @@ public class GameData
revertTo(RegistryManager.VANILLA, false);
LOGGER.fatal("Detected errors during registry event dispatch, roll back to VANILLA complete");
} else {
RegistryManager.getVanillaRegistryKeys().forEach(registryName -> {
Registry<?> registry = Registry.REGISTRY.get(registryName);
if (registry == null)
return;
postVanillaRegisterEvent(registry);
});
BuiltinRegistries.REGISTRY.forEach(GameData::postVanillaRegisterEvent);
net.minecraftforge.common.ForgeHooks.modifyAttributes();
}
}, executor);
}
@SuppressWarnings("removal")
private static void postVanillaRegisterEvent(Registry<?> registry)
{
if (RegistryManager.ACTIVE.getRegistry(registry.key().location()) != null)
return;
ModLoader.get().postEvent(new VanillaRegisterEvent(registry));
applyHolderLookups(registry.key().location());
}
//Lets us clear the map so we can rebuild it.
private static class ClearableObjectIntIdentityMap<I> extends IdMapper<I>
{