diff --git a/src/main/java/net/minecraftforge/fml/common/FMLContainer.java b/src/main/java/net/minecraftforge/fml/common/FMLContainer.java index d92e7fc26..397266bff 100644 --- a/src/main/java/net/minecraftforge/fml/common/FMLContainer.java +++ b/src/main/java/net/minecraftforge/fml/common/FMLContainer.java @@ -35,6 +35,7 @@ import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.network.NetworkCheckHandler; import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.internal.FMLNetworkHandler; +import net.minecraftforge.fml.common.registry.ForgeRegistries; import net.minecraftforge.fml.common.registry.PersistentRegistryManager; import net.minecraftforge.fml.common.registry.VillagerRegistry; import net.minecraftforge.fml.relauncher.Side; @@ -84,8 +85,9 @@ public final class FMLContainer extends DummyModContainer implements WorldAccess @Subscribe public void modPreinitialization(FMLPreInitializationEvent evt) { - // Initialize the villager registry - VillagerRegistry.instance(); + // Initialize all Forge/Vanilla registries {invoke the static init) + if (ForgeRegistries.ITEMS == null) + throw new RuntimeException("Something horrible went wrong in init, ForgeRegistres didn't create..."); } @NetworkCheckHandler diff --git a/src/main/java/net/minecraftforge/fml/common/registry/ForgeRegistries.java b/src/main/java/net/minecraftforge/fml/common/registry/ForgeRegistries.java new file mode 100644 index 000000000..74917b1c9 --- /dev/null +++ b/src/main/java/net/minecraftforge/fml/common/registry/ForgeRegistries.java @@ -0,0 +1,40 @@ +package net.minecraftforge.fml.common.registry; + +import net.minecraft.block.Block; +import net.minecraft.enchantment.Enchantment; +import net.minecraft.item.Item; +import net.minecraft.potion.Potion; +import net.minecraft.potion.PotionType; +import net.minecraft.util.SoundEvent; +import net.minecraft.world.biome.BiomeGenBase; +import net.minecraftforge.fml.common.registry.VillagerRegistry.VillagerProfession; + +/** + * A class that exposes static references to all vanilla and Forge registries. + * Created to have a central place to access the registries directly if modders need. + * It is still advised that if you are registering things to go through {@link GameRegistry.register} but queries and iterations can use this. + */ +public class ForgeRegistries +{ + static { init(); } // This must be above the fields so we guarantee it's run before findRegistry is called. Yay static inializers + + public static final IForgeRegistry BLOCKS = GameRegistry.findRegistry(Block.class); + public static final IForgeRegistry ITEMS = GameRegistry.findRegistry(Item.class); + public static final IForgeRegistry POTIONS = GameRegistry.findRegistry(Potion.class); + public static final IForgeRegistry BIOMES = GameRegistry.findRegistry(BiomeGenBase.class); + public static final IForgeRegistry SOUND_EVENTS = GameRegistry.findRegistry(SoundEvent.class); + public static final IForgeRegistry POTION_TYPES = GameRegistry.findRegistry(PotionType.class); + public static final IForgeRegistry ENCHANTMENTS = GameRegistry.findRegistry(Enchantment.class); + public static final IForgeRegistry VILLAGER_PROFESSIONS = GameRegistry.findRegistry(VillagerProfession.class); + + + /** + * This function is just to make sure static inializers in other classes have run and setup their registries before we query them. + */ + private static void init() + { + GameData.getMain(); + VillagerRegistry.instance(); + } + +} diff --git a/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java b/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java index ab0d43eb2..232e6053e 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java @@ -184,6 +184,19 @@ public class GameRegistry return block; } + /** + * Retrieves the registry associated with this super class type. + * If the return is non-null it is HIGHLY recommended that modders cache this + * value as the return will never change for a given type in a single run of Minecraft once set. + * + * @param registryType The base class of items in this registry. + * @return The registry, Null if none is registered. + */ + public static > IForgeRegistry findRegistry(Class registryType) + { + return PersistentRegistryManager.findRegistryByType(registryType); + } + /** * Add a forced persistent substitution alias for the block or item to another block or item. This will have * the effect of using the substituted block or item instead of the original, where ever it is diff --git a/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java b/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java index 03e2257ed..0001b425f 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/PersistentRegistryManager.java @@ -152,6 +152,11 @@ public class PersistentRegistryManager return registry; } + static > IForgeRegistry findRegistryByType(Class registryType) + { + return PersistentRegistry.ACTIVE.getRegistry(registryType); + } + public static List injectSnapshot(GameDataSnapshot snapshot, boolean injectFrozenData, boolean isLocalWorld) {