In this tutorial I’m going to show you how to make a block that has different textures for the different sides of the block. For this tutorial I’m going to change one of the metadata’s in the tutorial block to have a different texture, but it’s pretty much the same for every Block. All the code for this is done in the Block file. This is the BlockTutorialBlock file I will start with.
Adding sided textures to a Block is really easy. First of you will need the Icon and change the registerIcons method a bit so that it registers for each index in the array, but we already have that for this block. The only thing we will have to change is the size of the Icon. For this tutorial I will go with 1 texture for the top, another for the bottom and a last one for the sides. The metadata I’m going to change is number 1.
So we now need 4 textures. 3 for metadata 1 and the other one for metadata 0. The names for the extra 2 textures are the same as the 2 we had already. It will be tutorialBlock2.png and tutorialBlock3.png.
Now we have 4 textures registering, but we are still using just the 2. To make sure that metadata 1 uses a different texture for each side we will have to change the getIcon method. First of you might want to change it into this.
This does exactly the same as earlier, except for invalid metadata’s. It used to crash with an invalid metadata which is not that bad, because you can’t get the bad metadata. However, this is slightly better. It will print out to the console that there is a bad block and then run the code anyway.
Next up is to change case 1 to have different textures for each side. GetIcon has 2 parameters. The second one is the metadata and the first one is the side. icons will be the bottom
Each side of a block in Minecraft has a number. The bottom is 0, the top is 1 and the other sides go from 2-5.
So when we want a different texture for the top, bottom and all the sides together it has to look something like this.
The way this method works is that it will first look at the metadata. If it is 0 it will just return icons for each side. When it is 1 it will take a look at the sides. If it is the bottom (case 0) it will return icons and the top will get icons. In the case that the side requesting the texture isn’t the top or the bottom it will go to default which is icons. If for some reason the metadata is invalid it will print that to the console and return icons to prevent a crash.
The whole file should now look like this.
The only difference between doing this for a metadata block and a non metadata block is that you don’t have the metadata (par2) switch first. You can simply use just the par1 switch. The code for Icon registry and the icon is the same.
You can download the source code and the assets folder over here.