Tutorials: Blocks

Paragrahobia Blocks are the basic unit of structure in Minecraft. When used together, they make up the in-game landscape and can be used to make various structures. Most of the modding that's going to be done in this section is very similar to that done in the Creating Items tutorial, so I will be referring to that page often.

To create blocks we are going to:


 * 1) Register and render the block in the game
 * 2) Create a .json file to give the block a texture

Initializing the Block variable
So now you should have an error around the Block declaration and something that looks like this: public class ModBlocks { public static Block test_block; public static void init { test_block = new Block(error here).setUnlocalizedName("test_block"); } public static void register { } In the parentheses where the error is put this code: test_block = new Block(Material.cloth).setUnlocalizedName("test_block"); You can substitute cloth for whatever material you want, but basically the material sets the type of block it is, how hard it is to break, and how much light it gives off, and so on.
 * 1) Create a class called ModBlocks in the init package.
 * 2) Make the init and register methods.
 * 3) Create the Block object and give it an UnlocalizedName

Creating the class
We are now going to have to make our own Block class since the one used in the game is protected, meaning that only classes that extend it can use it. Since the Block class has way too many options to use in just the registry class, we are going to make a separate class to handle it. Go to your com.name.mod package and create a new package called '''blocks. '''In the new package create a new block class and call it whatever you want except "Block" as this is the name of the vanilla class and could cause some overlap. I'm going to call my class BlockTest for this tutorial.

In the BlockTest class, add a class extension to the Block class, import the class and add the constructor. You can delete the TODO comment and change the 'protected modifier to 'public' so it should look something like this: public class BlockTest extends Block { public BlockTest(Material materialIn) { super(materialIn); } } Now go back to the ModBlocks class and replace Block with BlockTest or whatever your name is for your class and import it: test_block = new BlockTest(Material.cloth).setUnlocalizedName("test_block");

Registering/Rendering the Block
Inside the register method, enter the line of code that registers the block similar to the items tutorial: GameRegistry.registerBlock(test_block, test_block.getUnlocalizedName.substring(5)); Now we are going to make the method to render the block in the inventory. This is almost exactly the same as the rendering from the items tutorial, so just create the registerRenders and registerRender methods and add the other render line plus a new line for sampling the block item: public static void registerRenders { } public static void registerRender(Block block) { Item item = Item.getItemFromBlock(block); Minecraft.getMinecraft.getRenderItem.getItemModelMesher.register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName.substring(5),"inventory")); } Finally inside of the registerRender method add the register line for the block: registerRender(test_block); Again, like in the items tutorial, go to the ClientProxy class and add the following line: ModBlocks.registerRenders; Once again in the ModClass call the init and register method above the item methods in the preInit method: ModBlocks.init; ModBlocks.register; Now you should be able to spawn your untextured block in using the 'give' command from the item tutorial: /give  :

Saving the texture
Once again, you willl need to create a texture, making sure it's 16 pixels by 16 pixels. Make a new folder called blocks in the textures folder and save it too this directory: /src/main/resources/assets//textures/blocks Be sure to name your block texture the exact same name as your block variable in the code. For example, mine would be test_block.

Making the .json Files
Go into Eclipse and refresh your package. We need to make two models for blocks: one for the block inside the inventory, and one for the actually entity within the game world. First we are going to make the world model, so go to the src/main/resources package and find the models.item package therein. Create a new sub-package in that package and call it block, so now you should have a directory inside the src/main/resources package called assets.tm.models.block. Now create a new untitled text file inside the block package and copy some simple block data from one of the stock blocks in: Referenced Libraries/forgeSrc/assets/minecraft/models/block I'm going to use the 'dirt' block information for this tutorial. Copy the data into your text file and change this line: "all": "blocks/dirt" To this line: "all": ":blocks/ " Now save the file in Minecraft/src/main/resources/assets//models/block and give your variable name again, followed by a '.json'.

Now we have to make the inventory model. Go to this directory now (notice how we are going to the 'items' folder instead of the 'models' folder): Referenced Libraries/forgeSrc/assets/minecraft/models/item Find a simple block to use. To keep things consistent, I'm going to use the 'dirt' block again, but you can use any simple block. Copy the data, create a new Untitled Text File in the assets..models.item package and paste it in. Change this line: "parent": "block/dirt" To this line: "parent": ":block/ " Save it to the Minecraft/src/main/resources/assets//models/item directory and call it your variable name with '.json' after it.

Assigning the Simple Blockstate
Blockstates are what allow a specific model to render in the game. If an item has more than one "sub-model" you will need to assign a blockstate to specify which model to use on placement. Some blocks, like wheat have multiple blockstates, but other blocks, like dirt or stone, have only one. We will get into how to assign more complex blockstates in a later tutorial. Regardless of how many states your block has, you will need to tell the block the state to use or it will not be placed.

So, to give our block a blockstate, relocate to your assets.tm package in the Package Explorer and create a new package within it called blockstates. Inside that package create a new Untitled Text File and put this code into it: {     "variants": { "normal": {"model": ": "} } } Save that file to Minecraft/src/main/resources/assets//blockstates with the variable name followed by '.json'.

Now if you start the game in Eclipse you should be able to spawn in the block with the same command as earlier, and you should be able to place it in the world and break it. We will be going over how to give blocks properties in a later tutorial, as well as giving blocks a complex model and texture both in the inventory and the world and make them craftable, so stay tuned for that. Happy modding!