Modding: Block metadata Part 3

To understand this tutorial you will have to be done with part 1 and 2 of this metadata tutorial.
In this tutorial I will teach you how what you will need in the ItemBlock class and how to name your blocks in the mod file.
First you should create the ItemBlock file and it should look something like this.


package Tutorial.common;

public class ItemBlockOres 
{

}


The first thing you will have to do is make it extend ItemBlock like this.


package Tutorial.common;

import net.minecraft.src.ItemBlock;

public class ItemBlockOres extends ItemBlock
{

}


Now you should add some methods to this file. First of the constructor.


public ItemBlockOres(int par1, Block block) 
{
super(par1);
setHasSubtypes(true);
}


This is just used to register the ItemBlock and is required when you extend the ItemBlock file.
The second method you will need in here is this one.


public int getMetadata(int par1)
    {
        return par1;
    }


This makes sure that minecraft knows that this ItemBlock has metadata.
There is one last method you will have to add and it should look something like this.


public String getItemNameIS(ItemStack itemstack) 
{
String name = “”;
switch(itemstack.getItemDamage()) 
{
case 0: 
{
name = “ore”;
break;
}
case 1: 
{
name = “netherore”; 
break;
}
default: name = “ore”;
}
return getItemName() + “.” + name;
}


This is used in the naming of those blocks.
The switch statement will check for the metadata.

The whole file should now look like this.


package Tutorial.common;

import net.minecraft.src.Block;
import net.minecraft.src.ItemBlock;
import net.minecraft.src.ItemStack;

public class ItemBlockOres extends ItemBlock
{
public ItemBlockOres(int par1, Block block) 
{
super(par1);
setHasSubtypes(true);
}

public String getItemNameIS(ItemStack itemstack) 
{
String name = “”;
switch(itemstack.getItemDamage()) 
{
case 0: 
{
name = “ore”;
break;
}
case 1: 
{
name = “netherore”; 
break;
}
default: name = “ore”;
}
return getItemName() + “.” + name;
}

public int getMetadata(int par1)
    {
        return par1;
    }
}


This is all you will need in this file.
Now for the naming in the mod file you have to add this in the mod file.


LanguageRegistry.instance().addStringLocalization(“tile.oreblock.ore.name”, “Your Ore”);
LanguageRegistry.instance().addStringLocalization(“tile.oreblock.netherore.name”, “Your Nether Ore”);


LanguageRegistry has a method called addStringLocalization, but it isn’t static. Because of this you will have to add the .instance() after it.
The 2 strings between the brackets make the name. The second one is the name of the block.
The first one is a bit more complicated. To name a block you will have to do it like this:
tile.[blockname].[name in getNameIS method].name
It is important to do it exactly like that or it will not work.

The whole mod file should now look like this.


package Tutorial.common;

import net.minecraft.src.Block;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraftforge.common.DungeonHooks;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = “YourName_ModName”, name = “ModName”, version = “Version number”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial 
{
public static Block oreblock;

public static Item youritem;

public static Item yourfood;

@SidedProxy(clientSide = “Tutorial.client.ClientProxyTutorial”, serverSide = “Tutorial.common.CommonProxyTutorial”)
    public static CommonProxyTutorial proxy;

public static int oreblockId = 230;

@Init
public void load(FMLInitializationEvent event) 
{
oreblock = new BlockOres(oreblockId, 0).setStepSound(Block.soundStoneFootstep).setHardness(3F).setResistance(1.0F).setBlockName(“oreblock”);

Item.itemsList[oreblockId] = new ItemBlockOres(oreblockId-256, oreblock).setItemName(“oreblock”);

LanguageRegistry.instance().addStringLocalization(“tile.oreblock.ore.name”, “Your Ore”);
LanguageRegistry.instance().addStringLocalization(“tile.oreblock.netherore.name”, “Your Nether Ore”);

youritem = new ItemTutorial(550).setIconIndex(1).setItemName(“youritem”);

LanguageRegistry.addName(youritem, “Your Item”);

proxy.registerRenderThings();

DungeonHooks.addDungeonLoot(new ItemStack(youritem), 10, 2, 5);
DungeonHooks.setDungeonLootTries(50);

yourfood = new ItemTutorialFood(551, 8, true).setIconIndex(3).setItemName(“yourfood”);
LanguageRegistry.addName(yourfood, “Your Food”);

GameRegistry.registerFuelHandler(new TutorialFuel());

GameRegistry.registerWorldGenerator(new WorldgeneratorTutorial());
}
}


 

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