Modding: Block metadata Part 1

In this tutorial I will teach you how to create your own blocks with metadata. This means that you can have up to 16 blocks in 1 block id. There are a lot of ways to make blocks with metadata. This is what I do.
This is the file I will start with.


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;

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

GameRegistry.registerBlock(oreblock);

LanguageRegistry.addName(oreblock, "Your 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());
}
}


And this is the 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 will have to do in the mod file is remove this.

GameRegistry.registerBlock(oreblock);

You will not need this anymore with metadata blocks.
You should also remove this.

LanguageRegistry.addName(oreblock, "Your Ore");

There is a new way of naming with metadata.
Now to make this simpler you will have to add a variable in the file. This is what you will have to add.

public static int oreblockId = 230;

This is the id of the ore block. You should make this a variable, because you will be using this several times and you don't want any bugs because you changed one of the id's, but not the others.
Now you will have to replace the 230 with the int you just made here.


oreblock = new BlockOres(oreblockId, 0).setStepSound(Block.soundStoneFootstep).setHardness(3F).setResistance(1.0F).setBlockName("oreblock");


To make this block have metadata you will have to add this one line in the file.

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

This makes the block have metadata. The ItemBlockOres handles with the metadata and it is also used with naming the blocks.
The whole 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");

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());
}
}



When you are done you should go back to the tutorials list here.

1 opmerking:

  1. ROBLOX is empowered by a growing membership base of over 300,000 creators who produce an infinite variety of highly immersive experiences.

    These experiences range from 3D multi-player games and contests, to interactive adventures where players can take on new personas to imagine what it would be like to be a dinosaur, a miner working a mine or an astronaut on a space exploration.

    BeantwoordenVerwijderen