ModderDojo Java Modding 13: All Our Mod Code So Far

firstmod

Overview

Here is all of the code for our mods to define our first item, first block, and first recipe. In addition to 3 Java files, there is 1 language resource file (en_US.lang) and also 2 PNG files that are not included below: one for the block texture and one for the item texture.

Refer to the image below and to earlier posts in this series to see how these files should be arranged in a package.

MyFirstItem.java

// Michael Madden Feb 2014.
// Ref: http://www.minecraftforge.net/wiki/Basic_Items 
// And: https://cdathenry.wordpress.com/2014/02/15/modderdojo-java-modding-8-a-simple-item-mod/ 

package modderdojo.firstitem;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;

public class MyFirstItem extends Item 
{
 public MyFirstItem() 
 {
 // This is the constructor for our new item. Set basic properties in it.
 setMaxStackSize(64);
 setCreativeTab(CreativeTabs.tabTransport);
 setUnlocalizedName("MichaelFirstItem"); 
 }
}

 

MyFirstBlock.java

// Michael Madden March 2014.

// Ref: http://www.minecraftforge.net/wiki/Basic_Items
// And: https://cdathenry.wordpress.com/2014/03/29/modderdojo-java-modding-12-creating-our-first-block/

// Thanks to Ailish and Eli in CoderDojo Athenry who figured this out originally. 

package modderdojo.firstitem;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;

public class MyFirstBlock extends Block 
{
 public MyFirstBlock() 
 {
 // This is the constructor for our new item. Set basic properties in it.
 super(Material.iron); // Copy the properties of iron
 
 setCreativeTab(CreativeTabs.tabBlock);
 setBlockName("MichaelFirstBlock"); // This is the internal name, not display name
 }
}

 

MyFirstMod.java

// Michael Madden, March 2014.
// Registers an Item mod and a Block mod and creates a recipe.
// Ref: https://cdathenry.wordpress.com/2014/01/19/modderdojo-java-modding-6-creating-our-first-mod-adding-a-crafting-recipe/
// And: https://cdathenry.wordpress.com/category/modderdojo/

package modderdojo.firstitem;

import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid = "firstitem", name = "First Mod from ModderDojo by Michael", version = "1.0")
public class MyFirstMod
{
 @EventHandler
 public void preInit(FMLPreInitializationEvent event) 
 {
 // Register my item and set its texture
 MyFirstItem first = new MyFirstItem();
 first.setTextureName("modderdojo:md2");
 GameRegistry.registerItem(first, "MichaelFirstItem"); // This name doesn't have to be same as unlocalised name, but no harm in them being the same
 
 // Register my block and set its texture
 MyFirstBlock b = new MyFirstBlock();
 b.setBlockTextureName("modderdojo:mmblock"); // requires .png file with this name
 GameRegistry.registerBlock(b, "MichaelFirstBlock");
 
 // Add recipe to turn 2 of my items into diamond
 ItemStack myStack = new ItemStack(first);
 ItemStack diamond = new ItemStack(Items.diamond, 1);
 
 GameRegistry.addRecipe(diamond, 
 "x ", // 2x2 recipe
 " x", 
 'x', first);
 }
}

 

en_US.lang

item.MichaelFirstItem.name=Nice Name For Michael's First Item
tile.MichaelFirstBlock.name=Michael's First Block Display Name

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s