Expose a central place to access all of Vanilla and Forge's registries using the new registry API.

This commit is contained in:
LexManos 2016-04-09 22:33:39 -07:00
parent c560af69a5
commit 205f5c13c8
4 changed files with 62 additions and 2 deletions

View File

@ -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

View File

@ -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<Block> BLOCKS = GameRegistry.findRegistry(Block.class);
public static final IForgeRegistry<Item> ITEMS = GameRegistry.findRegistry(Item.class);
public static final IForgeRegistry<Potion> POTIONS = GameRegistry.findRegistry(Potion.class);
public static final IForgeRegistry<BiomeGenBase> BIOMES = GameRegistry.findRegistry(BiomeGenBase.class);
public static final IForgeRegistry<SoundEvent> SOUND_EVENTS = GameRegistry.findRegistry(SoundEvent.class);
public static final IForgeRegistry<PotionType> POTION_TYPES = GameRegistry.findRegistry(PotionType.class);
public static final IForgeRegistry<Enchantment> ENCHANTMENTS = GameRegistry.findRegistry(Enchantment.class);
public static final IForgeRegistry<VillagerProfession> 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();
}
}

View File

@ -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 <K extends IForgeRegistryEntry<K>> IForgeRegistry<K> findRegistry(Class<K> 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

View File

@ -152,6 +152,11 @@ public class PersistentRegistryManager
return registry;
}
static <V extends IForgeRegistryEntry<V>> IForgeRegistry<V> findRegistryByType(Class<V> registryType)
{
return PersistentRegistry.ACTIVE.getRegistry(registryType);
}
public static List<String> injectSnapshot(GameDataSnapshot snapshot, boolean injectFrozenData, boolean isLocalWorld)
{