Step 4: Creating Your First Block!

Last note before we start: I encourage you to explore around the minecraft code for a while and see how it works... a lot of it will be intimidating unless you know java well, but don't worry. You can change some stuff for fun, but I wouldn't get in a habit of editing the base files. Open the "Minecraft" folder, open "common", then right-click anwhere in the common folder and go to NEW -> PACKAGE. Name this package whatever you want. I named mine "micdoodle8.mods.crossbowmod" for my mod. Next, right click on that package and click NEW -> CLASS. Again you can name this whatever you want, I named mine "CrossbowModCore" for my Crossbow Mod. None of the check marks need to be selected at the bottom. Now hit Finish. You'll see the pre-written java class code. Your first step is to let FML (Forge ModLoader) see this is a mod, so you'll want to copy the following line of code and paste it on the line ABOVE where it says "public class Whatever { " @Mod(modid = "NewMod", name = "My First Mod!", version = "1") Once you've pasted it, you'll need to hover over "@Mod" and click import. Your class should now look something like this: package micdoodle8.mods.minetorial; import cpw.mods.fml.common.Mod; @Mod(modid = "NewMod", name = "My First Mod!", version = "1") public class MinetorialCore {      }

Now you can start to add your block code. To start, add the following link to the top of your class (still in between the class brackets. This time you need to hover over and click import, but make sure you select the right one (net.minecraft.src).     public static Block yourFirstBlock = new BlockTutorial(blockID, 0).setHardness(2.5F).setBlockName("blockName");

public - Means that any class can access it. static - Any class can access it without creating a new instance of this class (See java tutorials for more info). Block - Means that this variable is using the class. yourfirstBlock - The variable name, doesn't matter what you call it. = new BlockTutorial - You are creating a new instance of the BlockTutorial class, which will cause an error because we haven't made that yet. You can name this whatever you want, as long as you use the same name later. (blockID, 0) - The information you are passing to the BlockTutorial class, which in this case is just the Block ID. Replace this with what you want your block ID to be. Also note that the later version of Forge include the block ID fix which allows you to set your block ID anywhere from 137 to 4096, but check here to see if it will be incompatible with other mods The 0 is the sprite index number, but we'll come back to that, just leave it at 0. .setHardness(2.5F) - This is setting the block with a hardness of 2.5, you can play around with it. .setStepSound(Block.soundWoodFootstep) - The step sound you want. You can change soundWoodFootstep to soundClothFootstep or whatever else you want .setBlockName("blockName"); - This doesn't matter, just make sure if you make another block that it's something different. Now you can create the new BlockTutorial class, or whatever you named it earlier. So that minecraft sees this is a new block, we need to extend the Block class. public class BlockTutorial extends Block {

Now let's create a contructor (read java tutorials) for the info we passed earlier. public BlockTutorial(int i, int j) {     super(i, j, Material.wood); }

BlockTutorial - Naming it the same as the class means we're making a constructor (int i, int j) - This is the information that we created earlier when we used (blockID, 0). This means the constructor is using two integers and naming them i and j. super - this means we are passing the information on to the superclass, which is Block, because that's what we extended in this class. This might be confusing, just read more java tutorials :). (i, j, Material.wood); - This is passing the integers i and j that we took from our mod class and passing them to block, we're also passing Material.wood, because I want this to break faster with an axe. Next you're going to want to add a Forge method for getting the texture file for this block. public String getTextureFile() {     return "/Mic'sMods/CrossbowMod/gui/Crossbow.png"; }

Note: You cannot change the first line of this method, as it is overriding (replacing) the forge method. This is an example of what I would return for my Crossbow Mod. This will be the location of your texture file INSIDE the minecraft.jar. You will obviously change this to whatever you want, but if (when you create your textures) this isn't the place where the texture is, it won't load it. Also, make sure you have the .png in the quotation marks. To add a block or item to the new creative interface, we can add the following line into the block's constructor: public BlockTutorial(int i, int j) {     super(i, j, Material.wood);     this.setCreativeTab(CreativeTabs.tabDeco); }

this.setCreativeTab - This is a method inside the Block class; it sets the creative tab that the item/block appears on in the game. We will want to create a method back in the base mod file that will do everything we ask it when the mod loads. To do this, first add a new method and then add the "@Init" annotation on the line above like so: @Init public void loadMod(FMLInitializationEvent event) { }

@Init - Telling forge modloader to run this when Minecraft is being loaded. loadMod - The method name, you can name this whatever you like. Now, the last part of coding this block, we need to go back to the base file and invoke (basically, use) the method to register this as a new block and add the name. In the loadMod method (or whatever you named it) add: GameRegistry.registerBlock(yourFirstBlock); LanguageRegistry.instance().addNameForObject(yourFirstBlock, "en_US", "Woohoo! My first Block :D");

GameRegistry.registerBlock - This is the method you are invoking (yourFirstBlock) - This is the block you are registering. You must provide the block instance that you created earlier. LanguageRegistry.instance().addNameForObject - Invoking another method, this time it is adding a name. (yourFirstBlock, "en_US", "Woohoo! My first Block :D"); - Here, you are providing the block instance that you want to add a name to, and then a String containing the block name. This is usually where you would put "Redstone Lamp" or "Diamond Ore", but this time we're just going to add our own fun name. Now start up minecraft by clicking the little greed "Run" button in the top of eclipse, start a new creative world, and look for your new Block! Give yourself a pat on the back, you added your first block to minecraft! It might be ugly and not seem like much, but it's your first step into modding and you did it! :D

Previous                 Back to Tutorials Index                 Next
Minecraft and Forge are always changing, found something wrong with this tutorial?

external links