Modding: Custom crop part 4: Seed item

In this tutorial I will show you how to create your own seed item to plant your crop. To do this you will have to add a new item in your mod file. This is the mod file I will start with.


package Tutorial.common;

import net.minecraft.src.Block;
import net.minecraft.src.EnumToolMaterial;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraftforge.common.DungeonHooks;
import net.minecraftforge.common.EnumHelper;
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 
{
//blocks
public static Block oreblock;
public static Block cropblock;

public static int oreblockId = 230;

//items
public static Item youritem;
public static Item yourfood;
public static Item yoursword;
public static Item yourpick;
public static Item yourshovel;

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

//enums
static EnumToolMaterial yourtoolmaterial = EnumHelper.addToolMaterial(“yourmaterial”, 2, 500, 7F, 2, 14);

@Init
public void load(FMLInitializationEvent event)
{
//proxy
proxy.registerRenderThings();

//blocks
oreblock = new BlockOres(oreblockId, 0).setStepSound(Block.soundStoneFootstep).setHardness(3F).setResistance(1.0F).setBlockName(“oreblock”);
cropblock = new BlockCropTutorial(231, 1).setStepSound(Block.soundGrassFootstep).setHardness(0.0F).setBlockName(“cropblock”);

GameRegistry.registerBlock(cropblock);

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

//block names
LanguageRegistry.instance().addStringLocalization(“tile.oreblock.ore.name”, “Your Ore”);
LanguageRegistry.instance().addStringLocalization(“tile.oreblock.netherore.name”, “Your Nether Ore”);
LanguageRegistry.addName(cropblock, “Custom Crop”);

//items
youritem = new ItemTutorial(550).setIconIndex(1).setItemName(“youritem”);
yourfood = new ItemTutorialFood(551, 8, true).setIconIndex(3).setItemName(“yourfood”);
yoursword = new ItemYourSword(552, yourtoolmaterial).setIconIndex(4).setItemName(“yoursword”);
yourpick = new ItemYourPick(553, yourtoolmaterial).setIconIndex(5).setItemName(“yourpick”);
yourshovel = new ItemYourShovel(554, yourtoolmaterial).setIconIndex(6).setItemName(“yourshovel”);

//item names
LanguageRegistry.addName(youritem, “Your Item”);
LanguageRegistry.addName(yourfood, “Your Food”);
LanguageRegistry.addName(yoursword, “Your Sword”);
LanguageRegistry.addName(yourpick, “Your Pickaxe”);
LanguageRegistry.addName(yourshovel, “Your Shovel”);

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

//game registry
GameRegistry.registerFuelHandler(new TutorialFuel());
GameRegistry.registerWorldGenerator(new WorldgeneratorTutorial());

//recipes
GameRegistry.addRecipe(new ItemStack(yoursword), new Object[]
{
“X”, “X”, “Z”, ‘X’, youritem, ‘Z’, Item.stick
});
GameRegistry.addRecipe(new ItemStack(yourpick), new Object[]
{
“XXX”, ” Z “, ” Z “, ‘X’, youritem, ‘Z’, Item.stick
});
GameRegistry.addRecipe(new ItemStack(yourshovel), new Object[]
{
“X”, “Z”, “Z”, ‘X’, youritem, ‘Z’, Item.stick
});
}
}


In the last tutorial I showed you how to do the crop block. In there I also added code for the cropseed item. That is the item that I will add right now.
This is the code that you will need for a seed item.


public static Item cropseed;
cropseed = new Itemcropseed(555, cropblock.blockID, Block.grass.blockID).setIconIndex(7).setItemName(“flaxseed”);
LanguageRegistry.addName(cropseed, “Your Crop Seed”);


Most of this code should be famillair to you. If it isn’t I suggest reading through the item tutorial.
The only different is the thing that is behind Itemcropseed. The first parameter is the id, the second id is the block it should place and the last one is where it should be able to place it on.

The whole file should now look like this.


package Tutorial.common;

import net.minecraft.src.Block;
import net.minecraft.src.EnumToolMaterial;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraftforge.common.DungeonHooks;
import net.minecraftforge.common.EnumHelper;
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 
{
//blocks
public static Block oreblock;
public static Block cropblock;

public static int oreblockId = 230;

//items
public static Item youritem;
public static Item yourfood;
public static Item yoursword;
public static Item yourpick;
public static Item yourshovel;
public static Item cropseed;

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

//enums
static EnumToolMaterial yourtoolmaterial = EnumHelper.addToolMaterial(“yourmaterial”, 2, 500, 7F, 2, 14);

@Init
public void load(FMLInitializationEvent event)
{
//proxy
proxy.registerRenderThings();

//blocks
oreblock = new BlockOres(oreblockId, 0).setStepSound(Block.soundStoneFootstep).setHardness(3F).setResistance(1.0F).setBlockName(“oreblock”);
cropblock = new BlockCropTutorial(231, 1).setStepSound(Block.soundGrassFootstep).setHardness(0.0F).setBlockName(“cropblock”);

GameRegistry.registerBlock(cropblock);

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

//block names
LanguageRegistry.instance().addStringLocalization(“tile.oreblock.ore.name”, “Your Ore”);
LanguageRegistry.instance().addStringLocalization(“tile.oreblock.netherore.name”, “Your Nether Ore”);
LanguageRegistry.addName(cropblock, “Custom Crop”);

//items
youritem = new ItemTutorial(550).setIconIndex(1).setItemName(“youritem”);
yourfood = new ItemTutorialFood(551, 8, true).setIconIndex(3).setItemName(“yourfood”);
yoursword = new ItemYourSword(552, yourtoolmaterial).setIconIndex(4).setItemName(“yoursword”);
yourpick = new ItemYourPick(553, yourtoolmaterial).setIconIndex(5).setItemName(“yourpick”);
yourshovel = new ItemYourShovel(554, yourtoolmaterial).setIconIndex(6).setItemName(“yourshovel”);
cropseed = new Itemcropseed(555, cropblock.blockID, Block.grass.blockID).setIconIndex(7).setItemName(“flaxseed”);

//item names
LanguageRegistry.addName(youritem, “Your Item”);
LanguageRegistry.addName(yourfood, “Your Food”);
LanguageRegistry.addName(yoursword, “Your Sword”);
LanguageRegistry.addName(yourpick, “Your Pickaxe”);
LanguageRegistry.addName(yourshovel, “Your Shovel”);
LanguageRegistry.addName(cropseed, “Your Crop Seed”);

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

//game registry
GameRegistry.registerFuelHandler(new TutorialFuel());
GameRegistry.registerWorldGenerator(new WorldgeneratorTutorial());

//recipes
GameRegistry.addRecipe(new ItemStack(yoursword), new Object[]
{
“X”, “X”, “Z”, ‘X’, youritem, ‘Z’, Item.stick
});
GameRegistry.addRecipe(new ItemStack(yourpick), new Object[]
{
“XXX”, ” Z “, ” Z “, ‘X’, youritem, ‘Z’, Item.stick
});
GameRegistry.addRecipe(new ItemStack(yourshovel), new Object[]
{
“X”, “Z”, “Z”, ‘X’, youritem, ‘Z’, Item.stick
});
}
}


Now you have to create the Itemcropseed file.
That file should contain this.


package Tutorial.common;

import net.minecraft.src.CreativeTabs;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.World;

public class Itemcropseed extends Item
{
    /**
     * the type of block this seed turns into (wheat or pumpkin stems for instance)
     */
    private int blockType;

    /** BlockID of the block the seeds can be planted on. */
    private int soilBlockID;

    public String getTextureFile()
    {
            return “/TutTextures.png”;
    }
    
    public Itemcropseed(int par1, int par2, int par3)
    {
        super(par1);
        blockType = par2;
        soilBlockID = par3;
        this.setCreativeTab(CreativeTabs.tabMaterials);
    }
    
    public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    {
        if (par7 != 1)
        {
            return false;
        }
        else if (par2EntityPlayer.canPlayerEdit(par4, par5, par6) && par2EntityPlayer.canPlayerEdit(par4, par5 + 1, par6))
        {
            int var11 = par3World.getBlockId(par4, par5, par6);

            if (var11 == this.soilBlockID && par3World.isAirBlock(par4, par5 + 1, par6))
            {
                par3World.setBlockWithNotify(par4, par5 + 1, par6, this.blockType);
                –par1ItemStack.stackSize;
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}


Most of this file should be pretty simple. The file has 3 parameters just like in the mod file and there are 2 variables that get set to the id’s added in the parameters.
In the tryPlaceIntoWorld method checks if the block you click on is the block from the third parameter and if the block above is air. When it is it will place the block set in the second parameter.

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