Basic Blocks
Blocks are very simple. There are several ways in which you can declare your block. It can be a normal block, an ore block, a leaf block, a flower block or a half slab block. This section will cover ore blocks.
To declare a block, simply put this section of code under your sided proxy:
public static final BlockOre TutorialBlock = (new BlockTutorialBlock(170, 0, Material.rock));
public static final BlockOre TutorialBlock defines the block's code name and type. new BlockTutorialBlock is the class you will need to make in order for the block to exist in the game. After that, the number 170 is it's ID, number 0 is the block ID that your uses to separate it from other blocks. Material.rock defines what type of tool is required to mine it, rock being pickaxes.
Now, paste this into your @init section:
LanguageRegistry.addName(TutorialBlock, "Epic Ore");
MinecraftForge.setBlockHarvestLevel(TutorialBlock, "pickaxe", 2);
GameRegistry.registerBlock(TutorialBlock);
HarvestLevel is the speed at which the tool can harvest your block. addName is for the in game name and registerBlock makes sure your block will be a part of the game.
This next section of code is the content of your BlockTutorialBlock class:
package tutorial.common;
import java.util.Random;
public class BlockTutorialBlock extends BlockOre
{
public BlockTutorialBlock(int id, int texture, Material material)
{
super(id, texture);
setHardness(4.0F);
setStepSound(Block.soundStoneFootstep);
setUnlocalizedName("TutorialBlock");
setCreativeTab(CreativeTabs.tabBlock);
}
@Override
public void registerIcons(IconRegister iconRegister)
{
blockIcon= iconRegister.registerIcon("blfngl" + ":" + this.getUnlocalizedName().substring(this.getUnlocalizedName().indexOf(".") + 1));
}
public int idDropped(int par1, Random random, int par2)
{
return TutorialMain.TutorialIngot.itemID;
}
}
Explanation:
First things first. In your super, setHardness determines what kind of tool material your block requires to be mined. 4.0F is at least iron. 5.0 is diamond, 3.0 is gold, 2.0 is stone and 1.0 is wood. If you want your block to be unbreakeable, set it to -1.0F. setBlockName is the code name, so make it what you defined it with public static final. setStepSound is what sound your block makes when you walk on it. setCreativeTab is pretty obvious.
Lastly, idDropped is what item your block will drop. If you want it to drop a vanilla item, it must be: Item.theitem.itemID. For a vanilla block, Block.theblock.blockID. For your block, TutorialMain.TutorialBlock.blockID.
Your final code should be:
Main File:
package tutorial.common;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.Mod.PostInit;
import cpw.mods.fml.common.Mod.PreInit;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
@Mod(modid="tutorial", name="Tutorial Mod", version="1.4.7")
@NetworkMod(clientSideRequired=true, serverSideRequired=false)
public class TutorialMain
{
// The instance of your mod that Forge uses, in my case tutorial.
@Instance("tutorial")
public static TutorialMain instance;
// Says where the client and server 'proxy' code is loaded.
@SidedProxy(clientSide="com.tutorial.common.client.TutorialClientProxy", serverSide="com.tutorial.common.TutorialCommonProxy")
public static TutorialCommonProxy proxy;
public static final Item = (new YourIBase(902)).setUnlocalizedName("TutorialIngot").setCreativeTab(CreativeTabs.tabMisc);
public static final Item = (new YourIBase(903)).setUnlocalizedName("TutorialChunk").setCreativeTab(CreativeTabs.tabMisc);
public static final BlockOre TutorialBlock = (new BlockTutorialBlock(170, 0, Material.rock));
@PreInit
public void preInit(FMLPreInitializationEvent event)
{
// Stub Method
}
@Init
public void load(FMLInitializationEvent event)
{
LanguageRegistry.addName(TutorialIngot, "Epic Ingot");
LanguageRegistry.addName(TutorialChunk, "Failing Ingot");
LanguageRegistry.addName(TutorialBlock, "Epic Ore");
MinecraftForge.setBlockHarvestLevel(TutorialBlock, "pickaxe", 2);
GameRegistry.registerBlock(TutorialBlock);
proxy.registerRenderers();
}
@PostInit
public void postInit(FMLPostInitializationEvent event)
{
// Stub Method
}
}
And that's all there is to a basic block. Now on to textures.
To declare a block, simply put this section of code under your sided proxy:
public static final BlockOre TutorialBlock = (new BlockTutorialBlock(170, 0, Material.rock));
public static final BlockOre TutorialBlock defines the block's code name and type. new BlockTutorialBlock is the class you will need to make in order for the block to exist in the game. After that, the number 170 is it's ID, number 0 is the block ID that your uses to separate it from other blocks. Material.rock defines what type of tool is required to mine it, rock being pickaxes.
Now, paste this into your @init section:
LanguageRegistry.addName(TutorialBlock, "Epic Ore");
MinecraftForge.setBlockHarvestLevel(TutorialBlock, "pickaxe", 2);
GameRegistry.registerBlock(TutorialBlock);
HarvestLevel is the speed at which the tool can harvest your block. addName is for the in game name and registerBlock makes sure your block will be a part of the game.
This next section of code is the content of your BlockTutorialBlock class:
package tutorial.common;
import java.util.Random;
public class BlockTutorialBlock extends BlockOre
{
public BlockTutorialBlock(int id, int texture, Material material)
{
super(id, texture);
setHardness(4.0F);
setStepSound(Block.soundStoneFootstep);
setUnlocalizedName("TutorialBlock");
setCreativeTab(CreativeTabs.tabBlock);
}
@Override
public void registerIcons(IconRegister iconRegister)
{
blockIcon= iconRegister.registerIcon("blfngl" + ":" + this.getUnlocalizedName().substring(this.getUnlocalizedName().indexOf(".") + 1));
}
public int idDropped(int par1, Random random, int par2)
{
return TutorialMain.TutorialIngot.itemID;
}
}
Explanation:
First things first. In your super, setHardness determines what kind of tool material your block requires to be mined. 4.0F is at least iron. 5.0 is diamond, 3.0 is gold, 2.0 is stone and 1.0 is wood. If you want your block to be unbreakeable, set it to -1.0F. setBlockName is the code name, so make it what you defined it with public static final. setStepSound is what sound your block makes when you walk on it. setCreativeTab is pretty obvious.
Lastly, idDropped is what item your block will drop. If you want it to drop a vanilla item, it must be: Item.theitem.itemID. For a vanilla block, Block.theblock.blockID. For your block, TutorialMain.TutorialBlock.blockID.
Your final code should be:
Main File:
package tutorial.common;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.Mod.PostInit;
import cpw.mods.fml.common.Mod.PreInit;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
@Mod(modid="tutorial", name="Tutorial Mod", version="1.4.7")
@NetworkMod(clientSideRequired=true, serverSideRequired=false)
public class TutorialMain
{
// The instance of your mod that Forge uses, in my case tutorial.
@Instance("tutorial")
public static TutorialMain instance;
// Says where the client and server 'proxy' code is loaded.
@SidedProxy(clientSide="com.tutorial.common.client.TutorialClientProxy", serverSide="com.tutorial.common.TutorialCommonProxy")
public static TutorialCommonProxy proxy;
public static final Item = (new YourIBase(902)).setUnlocalizedName("TutorialIngot").setCreativeTab(CreativeTabs.tabMisc);
public static final Item = (new YourIBase(903)).setUnlocalizedName("TutorialChunk").setCreativeTab(CreativeTabs.tabMisc);
public static final BlockOre TutorialBlock = (new BlockTutorialBlock(170, 0, Material.rock));
@PreInit
public void preInit(FMLPreInitializationEvent event)
{
// Stub Method
}
@Init
public void load(FMLInitializationEvent event)
{
LanguageRegistry.addName(TutorialIngot, "Epic Ingot");
LanguageRegistry.addName(TutorialChunk, "Failing Ingot");
LanguageRegistry.addName(TutorialBlock, "Epic Ore");
MinecraftForge.setBlockHarvestLevel(TutorialBlock, "pickaxe", 2);
GameRegistry.registerBlock(TutorialBlock);
proxy.registerRenderers();
}
@PostInit
public void postInit(FMLPostInitializationEvent event)
{
// Stub Method
}
}
And that's all there is to a basic block. Now on to textures.