Tags

, , , , , , , , , ,

Peters was waiting for me when I returned from work, leaping out from behind a shrub and brandishing a gun. ‘Make me another Minecraft Mod, or I will put a slug in you!’ Oh dear, here we go again…

The Minecraft Mod I built for my crazy lodger was a Magic 8-Ball. All that was required was the use of a Java Random class, to provide a mystical 8-Ball answer in the guise of either a number 0, 1 or 2. Armed with the answer (and Peters armed with a pistol), I set about selecting the texture for the ball and an associated sword, using a switch statement. The more positive the answer from our Magic 8-Ball, the better the sword we get.

I created my Magic 8-Ball textures using Nova Skin website.

Here’s the code, which I implemented in Eclipse (see my previous post regarding getting this all set up on your PC):

package tutorial.generic;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
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="peters", name="Peters", version="1.0.0")
public class Peters 
{
    public static Block petersBlock;
    public static Item petersItem;
    
    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
    	// get magic 8-ball answer
        int ballAnswer = new Random().nextInt(3);

        // get texture and material
        String blockTextureName;
        Item.ToolMaterial toolMaterial = null;
        switch (ballAnswer) 
        {
	        case 0:  blockTextureName = "peters:ballAnswer_0";
        			 toolMaterial = Item.ToolMaterial.WOOD;
	                 break;
	        case 1:  blockTextureName = "peters:ballAnswer_1";
	        		 toolMaterial = Item.ToolMaterial.IRON;
	                 break;
	        case 2:  blockTextureName = "peters:ballAnswer_2";
	        		 toolMaterial = Item.ToolMaterial.EMERALD;
	                 break;
	        default: blockTextureName = "peters:ballAnswer_none";
	        		 toolMaterial = Item.ToolMaterial.WOOD;
	                 break;
	    }
    	
        // create magic 8-ball
        petersBlock = new PetersBlock(blockTextureName);
        
        // create sword
        petersItem = new PetersItem(toolMaterial);

        GameRegistry.registerBlock(petersBlock, "petersBlock");
        GameRegistry.registerItem(petersItem, "petersItem");
    }
}
package tutorial.generic;

import net.minecraft.block.BlockStone;

public class PetersBlock extends BlockStone 
{
    public PetersBlock (String blockTextureName) 
    {
        setBlockName("Peters Stone");
        setBlockTextureName(blockTextureName);
    }
}
package tutorial.generic;

import net.minecraft.item.Item;
import net.minecraft.item.ItemSword;

public class PetersItem extends ItemSword 
{
    public PetersItem(Item.ToolMaterial toolMaterial) 
    {
    	super(toolMaterial);

    	setUnlocalizedName("Peters Sword");
        setTextureName("peters:superSword");
    }
}

Great. Now when I run Minecraft, I will get a block with one of three possible Magic 8-Ball answers. I’ll also get either a top emerald sword if I get a positive Yes answer, or a simple wood sword if I get a No.

Here’s some screenshots of our Magic 8-Ball in action:

ballAnswer_0_texture
ballAnswer_1_texture
ballAnswer_2_texture

Peters was beside himself with joy, putting his gun back into its holster and starting up a new Minecraft game. It granted me the opportunity to slip the gun from his decorative belt and use the butt to beat him about the head until he lay unconscious, blood oozing out of his right ear.

Still, once he came round we made up, taking it in turns to use our new Magic 8-Ball Minecraft Mod.

Ciao!

P.S. here’s the block texture images (.png 16×16 pixels):
ballAnswer_0
ballAnswer_1
ballAnswer_2

Advertisements