Modding: Block metadata Part 2

Before you do this tutorial make sure you have read and done part 1.
The mod file is finished now. In this part I will show you what you should change in the block file. This is the normal block file.


package Tutorial.common;

import java.util.Random;

import net.minecraft.src.Block;
import net.minecraft.src.CreativeTabs;
import net.minecraft.src.Item;
import net.minecraft.src.Material;

public class BlockOres extends Block
{
    public BlockOres(int par1, int par2)
    {
        super(par1, par2, Material.rock);
        this.setCreativeTab(CreativeTabs.tabBlock);
    }
    
    public String getTextureFile()
    {
            return “/TutTextures.png”;
    }
    
    public int idDropped(int par1, Random par2Random, int par3)
    {
        return Tutorial.youritem.shiftedIndex;
    }
    
    public int quantityDropped(Random par1Random)
    {
        return par1Random.nextInt(2) + 1;
    }
}


The first thing you have to add is this.


this.setRequiresSelfNotify();


You have to place this under the creative tabs method. The setRequiresSelfNotify() is needed for metadata to work.
With 2 blocks in 1 id there is a big chance that you want it to have 2 different textures.
This is what you have to add for it.


public int getBlockTextureFromSideAndMetadata(int par1, int par2) 
{
switch (par2) 
{
case 0:
return 0;
case 1:
return 2;
default:
return 0;
}
}


This method handles the textures. Par1 is for sides and par2 is for metadata.
The number you return is the location of the texture on the sprite sheet.
The default is needed so that the method always gets a number back.

Another thing that a lot of people want is that the 2 blocks drop different things. par1 in idDropped is the metadata of the block. To get different drops you can simply do this.


public int idDropped(int par1, Random par2Random, int par3)
    {
switch(par1)
{
case 0:
return Tutorial.youritem.shiftedIndex;
case 1:
return Tutorial.yourfood.shiftedIndex;
default: 
return Tutorial.youritem.shiftedIndex;
}
    }


So the first block you already had will keep dropping the item, but the block with the metadata of 1 will now drop the food.
The last thing you will have to add is a little fix for the creative inventory. Normally the inventory will not search for more than just the metadata of 0 and because of that it will only show you one of the blocks. To fix that you will have to add this.


@SideOnly(Side.CLIENT)
    public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
    {
        for (int var4 = 0; var4 < 2; ++var4)
        {
            par3List.add(new ItemStack(par1, 1, var4));
        }
    }


@SideOnly(Side.CLIENT) makes sure that this method only gets called on the client. This is required, because the inventory only gets filled on the client.
The for loop inside of the method makes sure that all the items get added. Right now it will add the metadata’s 0 and 1, but if you have a third block in there you need to change the 2 into a 3.

The whole file should now look like this.


package Tutorial.common;

import java.util.List;
import java.util.Random;

import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;

import net.minecraft.src.Block;
import net.minecraft.src.CreativeTabs;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.Material;

public class BlockOres extends Block
{
    public BlockOres(int par1, int par2)
    {
        super(par1, par2, Material.rock);
        this.setCreativeTab(CreativeTabs.tabBlock);
        this.setRequiresSelfNotify();
    }
    
    public int getBlockTextureFromSideAndMetadata(int par1, int par2) 
{
switch (par2) 
{
case 0:
return 0;
case 1:
return 1;
default:
return 0;
}
}
    
    public String getTextureFile()
    {
            return “/TutTextures.png”;
    }
    
    public int idDropped(int par1, Random par2Random, int par3)
    {
switch(par1)
{
case 0:
return Tutorial.youritem.shiftedIndex;
case 1:
return Tutorial.yourfood.shiftedIndex;
default: 
return Tutorial.youritem.shiftedIndex;
}
    }
    
    public int quantityDropped(Random par1Random)
    {
        return par1Random.nextInt(2) + 1;
    }
    
    
    @SideOnly(Side.CLIENT)
    public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
    {
        for (int var4 = 0; var4 < 2; ++var4)
        {
            par3List.add(new ItemStack(par1, 1, var4));
        }
    }
}


 

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="">