Modding: Custom Sword

In this tutorial I will teach you how to create your own sword. 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;

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


To add a new sword you will have to add a new item. This is mostly the same as adding a normal item.
This is what you will have to add.


public static Item yoursword;

yoursword = new ItemYourSword(552, yourtoolmaterial).setIconIndex(4).setItemName(“yoursword”);

LanguageRegistry.addName(yoursword, “Your Sword”);

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


You should already know most of this code. The biggest part of this is a simple recipe and some basic item registering.
The only interesting part is the yourtoolmaterial. Right now this should be red underlined. This is because you haven’t created it just yet.
To add the tool material you have to add this.


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


EnumToolMaterial is the file where all the tool materials from minecraft are stored. To add something to that file you have to use EnumHelper.
EnumHelper.addToolMaterial adds a new tool material to the EnumToolMaterial file without changing it.
This material is used for every type of tool so it has several things that are not related to swords.
“yourmaterial” is the name of your tool material even though this is not used.
2 is the harvest level. This has to do with the blocks it can destroy. 3 can destroy things like diamond gear and 0 like wooden things.
500 is the durability.
7F is the speed it can destroy blocks.
2 is the damage in half hearts the tool does. There are 4 half hearts counted up with this value for swords.
14 is the enchantability. Nobody really knows what this number does. I suggest copying the number of the tool material that is the most the same as yours.

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 
{
public static Block oreblock;

public static Item youritem;
public static Item yourfood;

public static Item yoursword;

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

public static int oreblockId = 230;

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

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

yoursword = new ItemYourSword(552, yourtoolmaterial).setIconIndex(4).setItemName(“yoursword”);

LanguageRegistry.addName(yoursword, “Your Sword”);

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


Now for the sword file you should copy the ItemSword file from the source code. It should look like this.


package Tutorial.common;

import net.minecraft.src.EnumToolMaterial;
import net.minecraft.src.ItemSword;

public class ItemYourSword extends ItemSword
{
public ItemYourSword(int itemID, EnumToolMaterial toolMaterial)
{
super(itemID, toolMaterial);
}
}


This is all you will need for the sword to work.
The reason that this item will be a sword is because it extends ItemSword. This means that it will contain all the properties from ItemSword unless you copy them over to the file and change them in there.
To get the texture from your custom sprite sheet you have to add this.


public String getTextureFile()
    {
            return “/TutTextures.png”;
    }


The whole file should now look like this, but I suggest that you only copy this over if you can’t get it to work by doing it yourself. Doing things like this yourself like that is going to be very important with modding.


package Tutorial.common;

import net.minecraft.src.EnumToolMaterial;
import net.minecraft.src.ItemSword;

public class ItemYourSword extends ItemSword
{
public ItemYourSword(int itemID, EnumToolMaterial toolMaterial)
{
super(itemID, toolMaterial);
}

public String getTextureFile()
{
return “/TutTextures.png”;
}
}


 

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