Tutorial - Creation of a block
In this part we're going to create a simple block and add a property for the block's id to the mod's configuration file.
Let's start with the configuration property. By default, the mod's configuration file is named [modname]_CustomStuff.cfg, for example, our configuration file is named CSTutorial_CustomStuff.cfg.
If you want to change the name of the configuration file, you can edit it in the config.ini file located in the CSTutorial directory.
Now lets actually add the property to the configuration file. Open the mod.js file that is located in the CSTutorial directory.
The mod.js file is the most important file of your mod. It contains everything that adds, edits or removes things. Don't worry, there is a way to outsource the contents into multiple files
if you don't like huge files.
Add the following line to the mod.js file:
This line adds a block ID property to the configuration file.
The first argument is the name of the property. This name will appear in the configuration file.
The second argument is the default ID for the property. This means that it may not actually use that ID if it is already used by another block. The default ID has to be between 256 and 4095 (including both).
You can now start Minecraft. If it is fully loaded, you should see the configuration file in the config directory containing the created property.
Now that we've added the property, we can create our block.
In order to add a block, you have to do three things:
At first, you have to copy the textures for your block to the assets/CSTutorial/textures/blocks directory in the CSTutorial directory. If you want to use vanilla minecraft textures or textures from other mods, you can skip this.
For this block, use the platinumOre.png file.
Secondly, you have to create a file in the blocks directory of your mod. This file contains all attributes that describe the block, such as the ID, material or step sound.
Name the file platinumOre.js and add the following lines to it:
id = config.getBlockId("platinumOre");
material = "rock";
stepSound = "stone";
creativeTab = "buildingBlocks";
addToCreative[0] = true;
displayName[0] = "Platinum Ore";
hardness[0] = 3.0;
toolClass[0] = "pickaxe";
harvestLevel[0] = 2;
textureFileXP[0] = "platinumOre.png";
textureFileXN[0] = "platinumOre.png";
textureFileYP[0] = "platinumOre.png";
textureFileYN[0] = "platinumOre.png";
textureFileZP[0] = "platinumOre.png";
textureFileZN[0] = "platinumOre.png";
Attributes ending with [0] can have different values for different metadata versions of the block. Blocks can have a maximum of 16 metdata versions (0 to 15). It can have less, depending on the block type.
An example of metadata versions of blocks is wool. All wool blocks share the same block ID but they have different display names and different textures.
- The 'name' attribute defines the name of the block and should be unique. This is not the name that is displayed in the game.
- The 'id' attribute defines the block's ID. You see we're using the configuration property we've created earlier. You could also use a fixed ID but should only do that for testing purposes because by using configuration properties you should have almost no problems with ID conflicts.
- The 'material' attribute defines the block's materials. Different materials have different abilities, for example, rock makes the block not harvestable with bare hands.
- The 'stepSound' attribute defines the step and harvest sound of the block.
- The 'toolClass' attribute defines what tools can harvest the block.
- The 'harvestLevel' attribute defines what level the tool has to have to harvest the block. For example iron ore has harvest level 1 (needs at least a stone pickaxe) and obsidian has harvest level 3 (needs at least a diamond pickaxe). We use 2, so our block can only be harvested by at least an iron pickaxe.
- The texture file attributes define the textures for different sides. YP means y-positive and is the top side, YN means y-negative and is the bottom side, and so on.
The last thing to do is to add our block in the mod.js file. Add the following line to it:
The first argument is the name of the block's file inside the blocks directory.
The second argument is the block's type. In our case a normal block.
Make sure that you always add this line after the 'config.addBlockIdProperty()' line.
That's it. Run Minecraft and you'll find the block in the building blocks creative tab.
Continue with: Adding the block to the world generation