From 666315a8163c866a7e89c3f07b47bc8339f54312 Mon Sep 17 00:00:00 2001 From: TheDoctor1138 Date: Fri, 30 Aug 2024 05:14:48 +0200 Subject: [PATCH] Switched to EBF Registration system --- .../java/tcce/client/core/ClientProxy.java | 2 + src/main/java/tcce/common/TCCE.java | 36 ++- .../java/tcce/common/core/CommonProxy.java | 5 + .../java/tcce/common/items/TCCEItems.java | 7 +- .../tcce/common/library/TCCERegistry.java | 252 ++++++++++++++++++ 5 files changed, 277 insertions(+), 25 deletions(-) diff --git a/src/main/java/tcce/client/core/ClientProxy.java b/src/main/java/tcce/client/core/ClientProxy.java index 6c85154..44d43cc 100644 --- a/src/main/java/tcce/client/core/ClientProxy.java +++ b/src/main/java/tcce/client/core/ClientProxy.java @@ -28,6 +28,8 @@ public static boolean isAnniversary() { return(cal.get(Calendar.MONTH) == Calendar.AUGUST && cal.get(Calendar.DATE) == 29); } + public static boolean preRenderModels = false; + @Override public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { diff --git a/src/main/java/tcce/common/TCCE.java b/src/main/java/tcce/common/TCCE.java index 761e70a..e7e91e0 100644 --- a/src/main/java/tcce/common/TCCE.java +++ b/src/main/java/tcce/common/TCCE.java @@ -30,7 +30,7 @@ public class TCCE { public static CommonProxy proxy; /* TrainCraft Logger */ - public static Logger tcLog = LogManager.getLogger(TCCEInfo.modName); + public static Logger tcceLog = LogManager.getLogger(TCCEInfo.modName); // public static File configDirectory; @@ -41,45 +41,36 @@ public class TCCE { @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { - tcLog.info("Starting Traincraft Community Edition Addon " + TCCEInfo.modVersion + "!"); + tcceLog.info("Starting PreInitialization"); + tcceLog.info("Starting Traincraft Community Edition" + TCCEInfo.modVersion + "!"); if (!Loader.isModLoaded("tc")) { - tcLog.error("Traincraft not found"); + tcceLog.error("Traincraft is not installed!"); } - - /* Config handler */ // configDirectory= event.getModConfigurationDirectory(); - - /* Register Items, Blocks, ... */ - tcLog.info("Initialize Blocks, Items, ..."); - - //TCCETab = new TCCETab(CreativeTabs.getNextID(), "TCCE"); - - - + tcceLog.info("Initialising Entities."); EntityHandler.init(); /* Other Proxy init */ - tcLog.info("Initialize Renderer and Events"); + tcceLog.info("Initialize Renderer and Events"); registry = new TCCERegistry(); - - - tcLog.info("Finished PreInitialization"); + tcceLog.info("Finished PreInitialization"); } @Mod.EventHandler public void init(FMLInitializationEvent event) { - tcLog.info("Start Initialization"); + tcceLog.info("Start Initialization"); + tcceLog.info("Initialize Blocks, Items, ..."); TCCEItems.init(); - tcLog.info("Initialize Gui"); + tcceLog.info("Initialize Gui"); NetworkRegistry.INSTANCE.registerGuiHandler(instance, proxy); - tcLog.info("Initialize Recipes"); + tcceLog.info("Initialize Recipes"); TCCERecipeHandler.initBlockRecipes(); TCCERecipeHandler.initItemRecipes(); TCCERecipeHandler.initSmeltingRecipes(); @@ -87,7 +78,7 @@ public void init(FMLInitializationEvent event) { proxy.registerBookHandler(); registry.init(); - tcLog.info("Finished Initialization"); + tcceLog.info("Finished Initialization"); } @@ -98,6 +89,9 @@ public void load(FMLInitializationEvent event) { @Mod.EventHandler public void postInit(FMLPostInitializationEvent evt) { + tcceLog.info("Start to PostInitialize"); + TCCERegistry.endRegistration(); + tcceLog.info("Finished PostInitialization"); } @Mod.EventHandler diff --git a/src/main/java/tcce/common/core/CommonProxy.java b/src/main/java/tcce/common/core/CommonProxy.java index 9632516..86d7e7d 100644 --- a/src/main/java/tcce/common/core/CommonProxy.java +++ b/src/main/java/tcce/common/core/CommonProxy.java @@ -19,6 +19,11 @@ public Object getClientGuiElement(int ID, EntityPlayer player, World world, int } + public boolean isClient(){ + return false; + } + public Object getTESR(){return null;} + public GuiScreen getCurrentScreen() { return null; } diff --git a/src/main/java/tcce/common/items/TCCEItems.java b/src/main/java/tcce/common/items/TCCEItems.java index f698185..4da6b7c 100644 --- a/src/main/java/tcce/common/items/TCCEItems.java +++ b/src/main/java/tcce/common/items/TCCEItems.java @@ -7,9 +7,9 @@ */ package tcce.common.items; -import cpw.mods.fml.common.registry.GameRegistry; import tcce.common.library.TCCEInfo; import tcce.common.library.TCCEItemIDs; +import tcce.common.library.TCCERegistry; import train.common.Traincraft; public class TCCEItems { @@ -21,15 +21,14 @@ public static void init() { private static void loadItems() { - TCCEItemIDs.recipeBook.item = new ItemRecipeBook().setCreativeTab(Traincraft.tcCommunityTab); + TCCEItemIDs.recipeBook.item = new ItemRecipeBook(); } private static void registerItems() { for (TCCEItemIDs itemId : TCCEItemIDs.values()) { if (itemId.item != null) { if (itemId.className != null) { - GameRegistry.registerItem(itemId.item, itemId.name()); - itemId.item.setUnlocalizedName(TCCEInfo.modID + ":" + itemId.name()); + TCCERegistry.registerItem(itemId.item, TCCEInfo.modID, itemId.name(), Traincraft.tcCommunityTab); } diff --git a/src/main/java/tcce/common/library/TCCERegistry.java b/src/main/java/tcce/common/library/TCCERegistry.java index e4eb7ed..328476e 100644 --- a/src/main/java/tcce/common/library/TCCERegistry.java +++ b/src/main/java/tcce/common/library/TCCERegistry.java @@ -2,14 +2,36 @@ import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import ebf.tim.render.CustomItemModel; +import ebf.tim.utility.DebugUtil; +import fexcraft.tmt.slim.ModelBase; +import net.minecraft.block.Block; +import net.minecraft.block.ITileEntityProvider; +import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; +import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.StatCollector; +import net.minecraftforge.client.IItemRenderer; +import net.minecraftforge.client.MinecraftForgeClient; +import net.minecraftforge.oredict.OreDictionary; +import tcce.client.core.ClientProxy; import tcce.client.render.TCCERenderEnum; +import tcce.common.TCCE; import train.common.Traincraft; import train.common.api.TrainRecord; import train.common.api.TrainRenderRecord; import train.common.api.TrainSoundRecord; +import train.common.api.blocks.BlockDynamic; +import train.common.api.blocks.TileRenderFacing; +import train.common.items.ItemRollingStock; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -74,4 +96,234 @@ public void registerTrainSoundRecord(TrainSoundRecord sound) { trainSoundRecords.add(sound); } + private static List usedNames = new ArrayList<>(); + private static List redundantTiles = new ArrayList<>(); + private static List redundantBlocks = new ArrayList<>(); + + public static int registryPosition = 18; + + + //Item and Block registration: + /** + * @param block the block in question + * @param tab the creative tab to put the block's item into, leave null for no creative tab entry. + * @param MODID modid of the host add-on, used for translations, texture names, and generic identification. + * @param unlocalisedName unlocalized name, used for translations, texture names, and generic identification. + * @param oreDictionaryName ore directory name, used for other mods to identify type, mainly used for ingots, ores, and wood. + * @param render a ModelBase instance for rendering the tile entity. + * Can instead be a TESR instance for more rendering control. + * Null will fallback to a normal textured block render. + * @return + */ + + /**Blocks That allow for Models, oreDictionary:*/ + public static Block registerBlock(Block block, CreativeTabs tab, String MODID, String unlocalizedName, @Nullable String oreDictionaryName, @Nullable Object render) { + if (render instanceof ModelBase) { + return registerBlock(block, tab, MODID, unlocalizedName, oreDictionaryName, TCCE.proxy.getTESR(), (ModelBase) render, unlocalizedName); + } else { + return registerBlock(block, tab, MODID, unlocalizedName, oreDictionaryName, render, null, unlocalizedName); + } + } + /**Blocks That allow for Models, oredictionary, but require texture name*/ + public static Block registerBlock(Block block, CreativeTabs tab, String MODID, String unlocalizedName, @Nullable String oreDictionaryName, @Nullable Object render, String textureName) { + if (render instanceof ModelBase) { + return registerBlock(block, tab, MODID, unlocalizedName, oreDictionaryName, TCCE.proxy.getTESR(), (ModelBase) render, textureName); + } else { + return registerBlock(block, tab, MODID, unlocalizedName, oreDictionaryName, render, null, textureName); + } + } + /**Most basic block:*/ + public static Block registerBlock(Block block, CreativeTabs tab, String modid, String unlocalizedName) { + return registerBlock(block, tab, modid, unlocalizedName, null, TCCE.proxy.getTESR()); + } + /**Most basic block that allows for oreDictionary:*/ + public static Block registerBlock(Block block, CreativeTabs tab, String modid, String unlocalizedName, @Nullable String oreDictionaryName) { + return registerBlock(block, tab, modid, unlocalizedName, oreDictionaryName, TCCE.proxy.getTESR()); + } + /**Block with Model:*/ + public static Block registerBlock(Block block, CreativeTabs tab, String unlocalizedName, @Nullable String oreDictionaryName, @Nullable Object render) { + return registerBlock(block, tab, null, unlocalizedName, oreDictionaryName, render); + } + /**Other:*/ + public static Block registerBlock(Block block, CreativeTabs tab, String MODID, String unlocalizedName, @Nullable String oreDictionaryName, @Nullable Object TESR, @Nullable ModelBase model) { + return registerBlock(block, tab, MODID, unlocalizedName, oreDictionaryName, TESR, model, unlocalizedName); + } + + + /**Registration of Block*/ + + public static Block registerBlock(Block block, CreativeTabs tab, String MODID, String unlocalizedName, @Nullable String oreDictionaryName, @Nullable Object TESR, @Nullable ModelBase model, String textureName) { + if (usedNames.contains(unlocalizedName)) { + DebugUtil.println("ERROR: ", "attempted to register Block with a used unlocalizedName", unlocalizedName); + DebugUtil.throwStackTrace(); + } + if (tab != null) { + block.setCreativeTab(tab); + } + if (!unlocalizedName.isEmpty()) { + block.setBlockName(unlocalizedName); + GameRegistry.registerBlock(block, null, unlocalizedName); + if(model!=null || (block instanceof ITileEntityProvider && ((ITileEntityProvider) block).createNewTileEntity(null,0) instanceof TileRenderFacing)) { + registerItem(new ItemBlock(block), MODID, unlocalizedName, oreDictionaryName + ".item", tab, null, ebf.tim.render.CustomItemModel.instance, textureName); + if(TCCE.proxy.isClient() && TESR==null){ + TESR=new ebf.tim.render.BlockTESR(); + } + } else { + registerItem(new ItemBlock(block), MODID, unlocalizedName, oreDictionaryName + ".item", tab, null, null, textureName); + } + usedNames.add(unlocalizedName); + } else { + DebugUtil.println("ERROR: ", "attempted to register Block with no unlocalizedName"); + DebugUtil.throwStackTrace(); + } + + if (TCCE.proxy.isClient() && MODID != null) { + block.setBlockTextureName(MODID + ":" + textureName); + } + if (oreDictionaryName != null) { + OreDictionary.registerOre(oreDictionaryName, block); + } + if (DebugUtil.dev && TCCE.proxy.isClient() && block.getUnlocalizedName().equals(StatCollector.translateToLocal(block.getUnlocalizedName() +".name"))) { + DebugUtil.println("Block missing lang entry: " + block.getUnlocalizedName()); + } + if (block instanceof ITileEntityProvider) { + Class tile = ((ITileEntityProvider) block).createNewTileEntity(null, 0).getClass(); + if (!redundantTiles.contains(tile.getName())) { + GameRegistry.registerTileEntity(tile, unlocalizedName + "tile"); + redundantTiles.add(tile.getName()); + redundantTiles.add(unlocalizedName + "tile"); + if (TCCE.proxy.isClient() && TESR!=null) { + regTileRender(MODID,unlocalizedName,block,tile,model,TESR); + } + } else if(!redundantTiles.contains(unlocalizedName + "tile")) { + if (TCCE.proxy.isClient() && TESR != null) { + regTileRender(MODID,unlocalizedName,block, tile, model, TESR); + } + } else { + DebugUtil.println("redundant tile name found",tile.getName(), unlocalizedName + "tile"); + DebugUtil.printStackTrace(); + } + } + return block; + } + + + /**Most basic Item*/ + public static Item registerItem(Item item, String MODID, String unlocalizedName, CreativeTabs tab) { + return registerItem(item, MODID, unlocalizedName, null, tab, null, null); + } + /**Most basic Item that has custom texture*/ + public static Item registerItem(Item item, String MODID, String unlocalizedName, CreativeTabs tab, String textureName) { + return registerItem(item, MODID, unlocalizedName, null, tab, null, null, textureName); + } + + /**Item with oreDictionary compatibility, has ItemRender for itemModel, if container item is used, it will return said item when crafted with (eg milk returns empty bucket after crafting cake */ + public static Item registerItem(Item item, String MODID, String unlocalizedName, @Nullable String oreDictionaryName, @Nullable CreativeTabs tab, @Nullable Item container, @Nullable Object itemRender) { + return registerItem(item, MODID, unlocalizedName, oreDictionaryName, tab, container, itemRender, unlocalizedName.replace("item.", "")); + } + + public static Item registerItem(Item item, String MODID, String unlocalizedName, @Nullable String oreDictionaryName, @Nullable CreativeTabs tab, @Nullable Item container, @Nullable Object itemRender, String textureName) { + if (usedNames.contains(unlocalizedName)) { + DebugUtil.println("ERROR: ", "attempted to register Item with a used unlocalizedName", unlocalizedName); + DebugUtil.throwStackTrace(); + } + if (tab != null) { + item.setCreativeTab(tab); + } + if (container != null) { + item.setContainerItem(container); + } + if (!unlocalizedName.equals("")) { + item.setUnlocalizedName(MODID + ":" + unlocalizedName); + usedNames.add(unlocalizedName); + } else { + DebugUtil.println("ERROR: ", "attempted to register Item with no unlocalizedName"); + DebugUtil.throwStackTrace(); + } + GameRegistry.registerItem(item, unlocalizedName); + if (oreDictionaryName != null) { + OreDictionary.registerOre(oreDictionaryName, item); + } + if (DebugUtil.dev && TCCE.proxy != null && TCCE.proxy.isClient() && item.getUnlocalizedName().equals(StatCollector.translateToLocal(item.getUnlocalizedName()+".name"))) { + DebugUtil.println("Item missing lang entry: " + item.getUnlocalizedName()); + } + if (TCCE.proxy.isClient() && itemRender != null) { + MinecraftForgeClient.registerItemRenderer(item, (IItemRenderer) itemRender); + if (ClientProxy.preRenderModels) { + ebf.tim.render.CustomItemModel.instance.renderItem(IItemRenderer.ItemRenderType.INVENTORY, new ItemStack(item)); + } + } else if (TCCE.proxy.isClient() && item instanceof ItemRollingStock) { + MinecraftForgeClient.registerItemRenderer(item, ebf.tim.render.CustomItemModel.instance); + if (ClientProxy.preRenderModels) { + ebf.tim.render.CustomItemModel.instance.renderItem(IItemRenderer.ItemRenderType.INVENTORY, new ItemStack(item)); + } + } else if(TCCE.proxy.isClient()){ + item.setTextureName(MODID+ ":" + textureName); + } + return item; + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @SideOnly(Side.CLIENT) + private static void regTileRender(String MODID, String unlocalizedName, Block block, Class tile, ModelBase model, Object TESR) { + + if (block instanceof BlockDynamic) { + if (model != null) { + ((BlockDynamic) block).setModel(model); + } else if (TESR != null) { + ((BlockDynamic) block).setTESR(TESR); + } + } + if (TESR != null) { + cpw.mods.fml.client.registry.ClientRegistry.bindTileEntitySpecialRenderer(tile, (TileEntitySpecialRenderer) TESR); + MinecraftForgeClient.registerItemRenderer(Item.getItemFromBlock(block), CustomItemModel.instance); + CustomItemModel.registerBlockTextures(Item.getItemFromBlock(block), ((ITileEntityProvider) block).createNewTileEntity(null, 0)); + } else { + cpw.mods.fml.client.registry.ClientRegistry.bindTileEntitySpecialRenderer(tile, (TileEntitySpecialRenderer) Traincraft.proxy.getTESR()); + MinecraftForgeClient.registerItemRenderer(Item.getItemFromBlock(block), CustomItemModel.instance); + CustomItemModel.registerBlockTextures(Item.getItemFromBlock(block), ((ITileEntityProvider) block).createNewTileEntity(null, 0)); + } + } + + public static void endRegistration() { + usedNames = null; + registryPosition = -1; + redundantTiles = null; + } + + + + + }