From 72daae76ae205f2b4797101ce77696c56d867078 Mon Sep 17 00:00:00 2001 From: Anthony Hilyard Date: Mon, 20 Sep 2021 20:17:15 -0700 Subject: Added renderer support for auto registry system. --- .../iceberg/registry/AutoRegistry.java | 19 +++++++++++++++---- .../iceberg/registry/RendererRegistrar.java | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/anthonyhilyard/iceberg/registry/RendererRegistrar.java diff --git a/src/main/java/com/anthonyhilyard/iceberg/registry/AutoRegistry.java b/src/main/java/com/anthonyhilyard/iceberg/registry/AutoRegistry.java index dd7da65..c42a8d0 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/registry/AutoRegistry.java +++ b/src/main/java/com/anthonyhilyard/iceberg/registry/AutoRegistry.java @@ -14,7 +14,6 @@ import net.minecraft.util.ResourceLocation; import net.minecraft.util.SoundEvent; import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.fml.client.registry.IRenderFactory; -import net.minecraftforge.fml.client.registry.RenderingRegistry; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.IForgeRegistry; @@ -32,6 +31,8 @@ public abstract class AutoRegistry private static Map, Supplier> entityAttributes = new HashMap<>(); + private static Map> registeredEntityTypes = new HashMap<>(); + public static void init(String ModID) { MODID = ModID; @@ -60,6 +61,17 @@ public abstract class AutoRegistry } } + public static boolean isEntityTypeRegistered(String name) + { + return registeredEntityTypes.containsKey(name); + } + + @SuppressWarnings("unchecked") + public static EntityType getEntityType(String name) + { + return (EntityType) registeredEntityTypes.getOrDefault(name, null); + } + @SuppressWarnings("unchecked") private final > void registerAllOfType(Class> type, RegistryEvent.Register event) { @@ -111,8 +123,8 @@ public abstract class AutoRegistry ResourceLocation resourceLocation = new ResourceLocation(MODID, name); EntityType entityType = (EntityType) builder.build(name).setRegistryName(resourceLocation); - // Register the rendering handler. - RenderingRegistry.registerEntityRenderingHandler(entityType, renderFactory); + // Add this entity type to the registered hashmap. + registeredEntityTypes.put(name, entityType); // Store mob attributes if provided. These will be added in the attribute creation event below. if (attributes != null) @@ -120,7 +132,6 @@ public abstract class AutoRegistry entityAttributes.put(entityType, attributes); } - return entityType; } diff --git a/src/main/java/com/anthonyhilyard/iceberg/registry/RendererRegistrar.java b/src/main/java/com/anthonyhilyard/iceberg/registry/RendererRegistrar.java new file mode 100644 index 0000000..e77fd1f --- /dev/null +++ b/src/main/java/com/anthonyhilyard/iceberg/registry/RendererRegistrar.java @@ -0,0 +1,21 @@ +package com.anthonyhilyard.iceberg.registry; + +import net.minecraft.entity.Entity; +import net.minecraftforge.fml.client.registry.IRenderFactory; +import net.minecraftforge.fml.client.registry.RenderingRegistry; + +public abstract class RendererRegistrar +{ + protected static void registerRenderer(String name, IRenderFactory renderFactory) + { + if (AutoRegistry.isEntityTypeRegistered(name)) + { + // Register the rendering handler. + RenderingRegistry.registerEntityRenderingHandler(AutoRegistry.getEntityType(name), renderFactory); + } + else + { + throw new RuntimeException("Tried to register a renderer for an unregistered entity type! Make sure you register renderers after entities."); + } + } +} -- cgit