diff --git a/src/main/java/net/minecraftforge/fml/common/registry/GameData.java b/src/main/java/net/minecraftforge/fml/common/registry/GameData.java index 5847b7cde..5aad04802 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/GameData.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/GameData.java @@ -32,6 +32,7 @@ import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.ModContainer; import com.google.common.collect.BiMap; +import org.apache.logging.log4j.Level; public class GameData { @@ -211,6 +212,27 @@ public class GameData } static > K register(K object, ResourceLocation location) { + if (object == null) + { + FMLLog.getLogger().log(Level.ERROR, "Attempt to register a null object"); + throw new NullPointerException("Attempt to register a null object"); + } + object.setRegistryName(location); + return register(object); + } + + static > K register(K object) + { + if (object == null) + { + FMLLog.getLogger().log(Level.ERROR, "Attempt to register a null object"); + throw new NullPointerException("Attempt to register a null object"); + } + if (object.getRegistryName() == null) + { + FMLLog.getLogger().log(Level.ERROR, "Attempt to register object without having set a registry name {} (type {})", object, object.getClass().getName()); + throw new IllegalArgumentException(String.format("No registry name set for object %s (%s)", object, object.getClass().getName())); + } final IForgeRegistry registry = PersistentRegistryManager.findRegistry(object); registry.register(object); return object; 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 873c897a3..41bf24b7d 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/GameRegistry.java @@ -60,6 +60,7 @@ import com.google.common.collect.ObjectArrays; import com.google.common.collect.Sets; import com.google.common.primitives.Ints; +@SuppressWarnings({"WeakerAccess", "unused"}) public class GameRegistry { private static Set worldGenerators = Sets.newHashSet(); @@ -127,9 +128,34 @@ public class GameRegistry sortedGeneratorList = ImmutableList.copyOf(list); } - public static > K register(K object, ResourceLocation location) + /** + * Register the previously named {@link IForgeRegistry} object with the registry system. + * Always ensure the object is already named using {@link IForgeRegistryEntry#setRegistryName(ResourceLocation)} + * or another mechanism. + * + * @param object The object to register with a registry + * @param The registry supertype + * @throws IllegalArgumentException if the object is not yet named (use {@link #register(IForgeRegistryEntry, ResourceLocation)} instead) + * @return The object + */ + public static > K register(K object) { - return GameData.register(object, location); + return GameData.register(object); + } + + /** + * Register the unnamed {@link IForgeRegistry} object with the registry system. + * Always make sure you have not previously named the object. + * + * @param object The object to register + * @param name The name to register it with + * @param The registry supertype + * @throws IllegalStateException if the object already has an existing name (use {@link #register(IForgeRegistryEntry)} instead) + * @return The object + */ + public static > K register(K object, ResourceLocation name) + { + return GameData.register(object, name); } /** * Register an item with the item registry with a the name specified in Item.getRegistryName()