Jump to content

Registering Blocks in GameRegistry


somarani

Recommended Posts

Hi, I'm updating my mod from forge 1.6.4 to forge 1.7.2 and I'm following some tutorials online. In almost all the tutorial I've seen, the blocks are registered under the init method. But whenever I did that, the block texture wouldn't show. The only way I could get the block texture to load was to register my block the my main mod class constructor. Which is the better way to do it?

 

Here is what I have:

 

public SoulCraft(){      //This is my constructor

   

    GameRegistry.registerBlock(infusedStone, "infusedstone");

   

    }

Link to comment
Share on other sites

My blocks are registered in the static method of my main class(technically inside the static method of a different class that is initialized by the static method of my main class), but the init method should be equally valid. Can't imagine why it would effect your textures but, if for curiosity's sake, you tried registering them in preInit I would like to know if you get the same problem.

Link to comment
Share on other sites

Here is an example modfile:

package com.kwibble.mod;

// imports. I can't remember what they are

@Mod(modid = "dummymod", name = "Dummy Mod", version = "1.0.0")
public class ModDummy
{
        @Mod.Instance("dummymod")
        public static ModDummy instance;

        public static Block dummy_block;

        @EventHandler
        public void preInit(FMLPreInitializationEvent event)
        {
                dummy_block = ( new BlockDummy()).setCreativeTab(CreativeTabs.tabBlock);
        }
}

 

Here is BlockDummy:

package com.kwibble.mod.block;

// imports

public class BlockDummy extends Block
{
        public BlockDummy()
        {
                this.setBlockName("dummy_block");
                this.setTextureName("dummy_block");
                // Other stuff you/need to make this block. Or you could chain them when you instantiate the dummy_block variable
                GameRegistry.registerBlock(this, "block_dummy_block");
        }

        // Other block stuff if you want it
}

 

This is basically the setup that I use (aside from the bad coding practise shown in that example... Bad Kwibble) and it works perfectly fine for me.

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

My blocks are registered in the static method of my main class(technically inside the static method of a different class that is initialized by the static method of my main class), but the init method should be equally valid. Can't imagine why it would effect your textures but, if for curiosity's sake, you tried registering them in preInit I would like to know if you get the same problem.

 

Okay, first thins first. NONE OF YOUR INITLIAZATION EVENTS (PRE, POST, OR OTHERWISE) SHOULD BE STATIC.

Now that that is taken care of... Always register your blocks/items in the PRE initialization event, unless they are reliant on another mods items/blocks.

 

 

@OP I also forgot to mention, you don't need a constructor in your mod class. At all. Forge "constructs" your class via the initialization methods, so a constructor is not needed (at least to my knowledge).

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

If I register the block in the preInit method, the texture doesn't show up, also, If I create the object in the preInit method the texutre doesn't show up either. To get the texture to work, I either have to do this line - "GameRegistry.registerBlock(this, "infusedstone");" in the constructor for the block, or the constructor of the Main mod class.

 

This is what I have now and it works, is this the bad way of doing it, and is there a better way?

 

Main Mod Class - http://pastebin.com/kbPPv4Ee

 

Block class - http://pastebin.com/69haC0bu

Link to comment
Share on other sites

I've been told that registering your block during construction is technically incorrect. You can get around this by having a method

public Block register(){
    GameRegistry.register(this, this.getUnlocalizedName());
    return this;
}

and when you create your block:

    public static final Block myBlock = new MyBlock().register();

because you can guarantee that the Block is created before you call GameRegistry.register(args).

Link to comment
Share on other sites

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.