forge

Forge Lesson 02 – Creating a block

In lesson two we are going to add a block, which if right clicked will spawn a sheep. We will continue with the workspace from last time in which we created an item which spawns pigs. You can download the code at the end of the previous lesson

We will start with creating a new class named BlockSpawn in the noppes.tutorials package and make it extend the Block class.

package noppes.tutorials;

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

public class BlockSpawn extends Block{

	public BlockSpawn() {
		super(Material.rock);
	}
}

Next we need to create a new BlockSpawn object and register the block in the GameRegistry in the Tutorial class init function.

Block spawn = new BlockSpawn().setBlockName("tutorial_spawn").setCreativeTab(CreativeTabs.tabTools);
GameRegistry.registerBlock(spawn, "tutorial_spawn");

Run minecraft and you will see the block in the tools creative tab. Again it will have no texture and no name.

You can give it a name like with the item by adding the following line to your en_US.lang

tile.tutorial_spawn.name=Tutorial Block

Now to fix the texture you will need to create a new package: assets.tutorial.textures.blocks and download the block texture or create your own and put it in the package. To point the block texture to the correct location call setTextureName with a string containing the modid:texturename

Block spawn = new BlockSpawn().setBlockName("tutorial_spawn").
    setBlockTextureName("tutorial:spawn").setCreativeTab(CreativeTabs.tabTools);

If you run minecraft again the name and the texture should display correctly.

Blocks have an onBlockActivated functions which is called when a player right clicks a block. To make the block spawn sheep we will need to overwrite that function.

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9){
	if(!world.isRemote){
		EntitySheep sheep = new EntitySheep(world);
		sheep.setPosition(x + 0.5, y + 1, z + 0.5);
		//gives the sheep a random color
		sheep.setFleeceColor(world.rand.nextInt(16));
		world.spawnEntityInWorld(sheep);
	}
	return true;
}

Just like with the item we dont want it to spawn client side otherwise it will ghost. To give it a random color we use world.rand.nextInt(16) is for. That gives a random integer between 0 and 16 not including 16 itself.

Note: In 1.6 setBlockName used to be setUnlocalizedName and setBlockTextureName use to be setTextureName. Basically the same as with Item. I wouldn’t be surprised if mcp changes it back in the future.

Download the Code

 

Forge Lesson 01 – Creating an item

The first lesson will be to create a simple mod, which adds an item that can spawn pigs.

First start with creating a package for our mod (in eclipse right click the src/main/java folder -> new -> package). Call the package noppes.tutorials.  Inside this package create new class called Tutorials (right click the package -> new -> Class).

To the top of the Class add the annotation @Mod with the parameters modid=”tutorial”, name=”Tutorial”, version=”1″. Inside of the class create a new function called init, with the annotation @EventHandler which takes the paramater FMLPreInitializationEvent ev.

Your class should now look like:

package noppes.tutorials;

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

@Mod(modid="tutorial",name="Tutorial",version="1")
public class Tutorial {

	@EventHandler
	public void init(FMLPreInitializationEvent ev) {

	}
}

Next create a new class called ItemWand which extends Item:

package noppes.tutorials;

import net.minecraft.item.Item;

public class ItemWand extends Item{

}

Inside your init function in the Tutorial class create a new ItemWand object with the integer 4000. Also call setUnlocalizedName with an unique item name and setCreativeTab with the value CreativeTabs.tabTools.

new ItemWand().setUnlocalizedName("tutorial_wand").setCreativeTab(CreativeTabs.tabTools);

Next run minecraft, open a creative world and in the tools tab you will see your item. It will however not have a proper name and no proper texture. Lets fix that next.

First create two new packages: assets.tutorial.lang and assets.tutorial.textures.items in the src/java/resources folder. Next download the wand texture or create your own and put it in the items package. After that create a new file in the lang package called en_US.lang.

When you ran minecraft and checked the name of the item, it should have shown something like item.tutorialwand.name. The tutorialwand is the name we gave it in the setUnlocalizedName function earlier. Now open your en_US.lang with a text editor and add the line:

item.tutorial_wand.name=Tutorial Wand

To point the items texture to the correct location call setTextureName with a string containing the modid:texturename

new ItemWand().setUnlocalizedName("tutorial_wand").
    setCreativeTab(CreativeTabs.tabTools).setTextureName("tutorial:wand");

When you run minecraft again and check the tools tab, it should show the proper name and texture.

All thats left is to make the item spawn pigs. To do that add the next function to your ItemWand:

@Override
public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int x, int y, int z, int face, float par8, float par9, float par10) {
	if (face != 1)
		return false;
	if (!par3World.isRemote) {
		EntityPig pig = new EntityPig(par3World);
		pig.setPosition(x + 0.5, y + 1, z + 0.5);
		par3World.spawnEntityInWorld(pig);
	}
	return true;
}

The onItemUse is called when a player clicks on the ground. Depending on what part of a block (top, bottom, sides) is clicked the face integer has a different value. When the top of a block is pressed the face value is 1, since we only want to spawn it when the top is pressed we return false if the top isnt pressed. The world.isRemote is to check if its called client or server side. You should only spawn new entities server side otherwise ghost entities will appear.

Side note: language files always go into the assets.<modid>. lang and item textures in the assets.<modid>.textures.items. Language files always need the en_US.lang, which is used by default if it cant find the selected minecraft language.

Download the Code

 

Forge Getting Started

To get started with creating forge mods, you need a couple of things first:

The howto install JDK and eclipse are found in the Java Getting Starter. Follow that tutorial before continuing here.

Installing gradle for eclipse

Open eclipse, select a random workspace it doesn’t really matter. Then press Help -> Eclipse Marketplace -> search for gradle -> Install Gradle integration for eclipse. After installing close eclipse.

Installing Forge

To install forge you first need to download the Forge src (source), from the Forge Files page. After downloading create a new folder, extract the files to that folder. Now open a cmd prompt and browse to the folder (or hold shift -> right click inside the folder -> open command window here) and run the command:

gradlew setupDecompWorkspace eclipse

This will take 5-30 minutes depending on your computer/internet speed. It downloads and sets up everything you need. For those interested gradlew tasks, will give you all options it has, but for now we dont need to do anything else.

Now you can open eclipse and set the workspace to /eclipse. Press the run button to launch minecraft.

When this is done you can start Forge Lesson 1

note: Renaming, copying or placing the folder somewhere else will mess things up and you will be unable to open your eclipse workspace.