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.

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

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

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

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.

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:

Next create a new class called ItemWand which 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.

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:

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

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:

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:

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.