MSpace-Dev Posted November 15, 2017 Share Posted November 15, 2017 (edited) So, I am trying to get an ItemBlock to register alongside my Block when it gets registered. I have come up with this so far. Is there a better way to be doing this? If so, please show me some example code! I'm not really happy with the itemBlock.setRegistryName part inside registerBlock() ! public class ModBlocks { public static Block tinBlock; public static void init(){ tinBlock = new BlockTinBlock("tin_block", "tin_block"); } public static void register(){ registerBlock(tinBlock); } public static void registerRenders(){ registerRender(tinBlock); } public static void registerBlock(Block block){ GameRegistry.register(block); ItemBlock itemBlock = new ItemBlock(block); itemBlock.setRegistryName(Reference.MODID, block.getUnlocalizedName().substring(5)); GameRegistry.register(itemBlock); } public static void registerRender(Block block){ ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0, new ModelResourceLocation(new ResourceLocation(Reference.MODID, block.getUnlocalizedName().substring(5)), "inventory")); } } Solution by Ugdhar: [Register Items and Blocks like in the post below! Super neat and up to date!] Edited November 15, 2017 by MSpace-Dev Added solution 1 Quote Link to comment Share on other sites More sharing options...
MSpace-Dev Posted November 15, 2017 Author Share Posted November 15, 2017 (edited) So, I have actually been reading up on these, and I have no idea how to implement this within my code. I'm quite confused as to how and when these events get called. I've tried this, but it crashes. public class ModItems { public static Item tinIngot; public static void preInit(){ tinIngot = new ItemTinIngot("tin_ingot", "tin_ingot"); } @SubscribeEvent private static void registerItems(RegistryEvent.Register<Item> event){ event.getRegistry().register(tinIngot); } public static void registerRenders(){ registerRender(tinIngot); } public static void registerRender(Item item){ ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(new ResourceLocation(item.getRegistryName(), "inventory")); } } And here are the first couple lines of the crash: net.minecraftforge.fml.common.LoaderExceptionModCrash: Caught exception from Bit Of Everything (boe) Caused by: java.lang.NoClassDefFoundError: io/github/mspacedev/init/ModItems at io.github.mspacedev.BitOfEverything.preInit(BitOfEverything.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:602) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) Edited November 15, 2017 by MSpace-Dev 1 Quote Link to comment Share on other sites More sharing options...
Ugdhar Posted November 15, 2017 Share Posted November 15, 2017 This link has a great example of using registry events to register stuff. 2 Quote Link to comment Share on other sites More sharing options...
MSpace-Dev Posted November 15, 2017 Author Share Posted November 15, 2017 (edited) 8 minutes ago, Ugdhar said: This link has a great example of using registry events to register stuff. Thanks, it looks good so far! I'll try change up my mod's code and see if it works. This seems like a really clean way of doing things EDIT: Upon further reading, this method of doing it is suuuuper clean and nice. Thanks so much for the resource! Edited November 15, 2017 by MSpace-Dev 1 Quote Link to comment Share on other sites More sharing options...
MSpace-Dev Posted November 15, 2017 Author Share Posted November 15, 2017 Alright, I have set up everything and I must say that I'm impressed with how neat this solution is. Kudos! 1 Quote Link to comment Share on other sites More sharing options...
MSpace-Dev Posted November 15, 2017 Author Share Posted November 15, 2017 (edited) I have one more question that relates to all this. How would I get my creative tab to register before the items and blocks? Because, they are not getting put into the creative tab at the moment because I register the creative tab in preInit in my main mod folder, whereas all the items and blocks are being registered using the Register events which run before everything Edited November 15, 2017 by MSpace-Dev 1 Quote Link to comment Share on other sites More sharing options...
Ugdhar Posted November 15, 2017 Share Posted November 15, 2017 I've been creating my creative tab right in my mod class near the top: public static final CreativeTabs TEST_TAB = new CreativeTabs("test") { @SideOnly(Side.CLIENT) public ItemStack getTabIconItem() { return new ItemStack(Items.REDSTONE); } }; I haven't messed with it much, but I do recall it wouldn't use my test item for the tab icon because my items hadn't registered yet, and I haven't as of yet bothered to try and fix/change/figure it out. But besides that works fine as far as I can see. 2 Quote Link to comment Share on other sites More sharing options...
MSpace-Dev Posted November 15, 2017 Author Share Posted November 15, 2017 (edited) I have done that, yeah. But same as you, the items register before the tab. I guess I'll leave it then. I have NEI to get my items. Thanks for all the help! EDIT: Wait, your method worked perfectly! Haha, my items have shown up in the tab and my modded item is the icon too. Awesome Edited November 15, 2017 by MSpace-Dev 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.