Wuppy’s Minecraft Forge Modding Tutorials for 1.6.2: Sided Textures

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.


package tutorial;

import java.util.List;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Icon;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class BlockTutorialBlock extends BlockGeneralTutorial
{
       public BlockTutorialBlock(int id, Material par2Material)
       {
             super(id, par2Material);
       }
      
       @SideOnly(Side.CLIENT)
       private Icon[] icons;
      
       @SideOnly(Side.CLIENT)
       public void registerIcons(IconRegister par1IconRegister)
       {
             icons = new Icon[2];
            
             for(int i = 0; i < icons.length; i++)
             {
                    icons[i] = par1IconRegister.registerIcon(Tutorial.modid + “:” + (this.getUnlocalizedName().substring(5)) + i);
             }
       }
      
       @SideOnly(Side.CLIENT)
       public Icon getIcon(int par1, int par2)
       {
             return icons[par2];
       }
      
       @SideOnly(Side.CLIENT)
       public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
       {
             for(int i = 0; i < 2; i++)
             {
                    par3List.add(new ItemStack(par1, 1, i));
             }
       }
}

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.


@SideOnly(Side.CLIENT)

public Icon getIcon(int par1, int par2)
{
       switch(par2)
       {
             case 0:
                    return icons[0];
             case 1:
             {
                    return icons[1];
             }
             default:
             {
                    System.out.println(“Invalid metadata for “ + this.getUnlocalizedName());
                    return icons[0];
             }
       }
}

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[1] 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.


@SideOnly(Side.CLIENT)

public Icon getIcon(int par1, int par2)
{
       switch(par2)
       {
             case 0:
                    return icons[0];
             case 1:
             {
                    switch(par1)
                    {
                           case 0:
                                 return icons[1];
                           case 1:
                                 return icons[2];
                           default:
                                 return icons[3];
                    }
             }
             default:
             {
                    System.out.println(“Invalid metadata for “ + this.getUnlocalizedName());
                    return icons[0];
             }
       }
}

The way this method works is that it will first look at the metadata. If it is 0 it will just return icons[0] 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[1] and the top will get icons[2]. In the case that the side requesting the texture isn’t the top or the bottom it will go to default which is icons[3]. If for some reason the metadata is invalid it will print that to the console and return icons[0] to prevent a crash.
The whole file should now look like this.


package tutorial;

import java.util.List;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Icon;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class BlockTutorialBlock extends BlockGeneralTutorial
{
       public BlockTutorialBlock(int id, Material par2Material)
       {
             super(id, par2Material);
       }
      
       @SideOnly(Side.CLIENT)
       private Icon[] icons;
      
       @SideOnly(Side.CLIENT)
       public void registerIcons(IconRegister par1IconRegister)
       {
             icons = new Icon[4];
            
             for(int i = 0; i < icons.length; i++)
             {
                    icons[i] = par1IconRegister.registerIcon(Tutorial.modid + “:” + (this.getUnlocalizedName().substring(5)) + i);
             }
       }
      
       @SideOnly(Side.CLIENT)
       public Icon getIcon(int par1, int par2)
       {
             switch(par2)
             {
                    case 0:
                           return icons[0];
                    case 1:
                    {
                           switch(par1)
                           {
                                  case 0:
                                        return icons[1];
                                  case 1:
                                        return icons[2];
                                  default:
                                        return icons[3];
                           }
                    }
                    default:
                    {
                           System.out.println(“Invalid metadata for “ + this.getUnlocalizedName());
                           return icons[0];
                    }
             }
       }
      
       @SideOnly(Side.CLIENT)
       public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
       {
             for(int i = 0; i < 2; i++)
             {
                    par3List.add(new ItemStack(par1, 1, i));
             }
       }
}

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">