Jump to content

Register 'ItemBlock' alongside 'Block' better than this?


Recommended Posts

Posted (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 by MSpace-Dev
Added solution
  • Like 1
Posted (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 by MSpace-Dev
  • Like 1
Posted (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 by MSpace-Dev
  • Like 1
Posted (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 by MSpace-Dev
  • Like 1
Posted

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.

  • Like 2
Posted (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 by MSpace-Dev
  • Like 1

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.