Mod: Something For Nothing

This is the eleventh lesson in a series introducing 10-year-olds to programming through Minecraft. Learn more here.

The mod is a suggestion from my class :)

Goal

Fill crafting table with ...

  • dirt and get 64 diamonds
  • sand and get 64 emeralds
  • clay (not blocks) and get 64 obsidian

Relevant Classes

The class corresponding to items is called ItemStack and it's located in the net.minecraft.item package.

We also need to use the GameRegistry class to add our new recipe to the game. Specifically, there is a static method called addShapelessRecipe

public static void addShapelessRecipe(ItemStack output, 
                                      Object... params)

What does the ... mean? It signifies we can pass in a list of objects (items) that the user needs to put on the crafting table in order to receive the output. Because we're filling the entire table, the recipe is called shapeless (it doesn't matter which item goes in which square).

How do we do it?

We need to add the new recipe to the load method.

ItemStack diamonds = new ItemStack(Item.diamond, 64);
ItemStack dirt = new ItemStack(Block.dirt);

GameRegistry.addShapelessRecipe(
    diamonds, 
    dirt, dirt, dirt, 
    dirt, dirt, dirt, 
    dirt, dirt, dirt);

We'll also need to import net.minecraft.block.Block, net.minecraft.item.Item, and net.minecraft.item.ItemStack. Eclipse makes this easy for us: if you however on a word with a red squiggly line, you will get a popup menu with "quick fixes". Most often, the required import will be the top one and you can select it.

Now if we run the game (green play button or Ctrl+F11) and play for a little bit, we should get this:

something-for-nothing

The other two recipes are left as an exercise to the reader :)

Extra Credit

In case you want to test our your recipes without having to actually collect all the required items, you can give your player inventory items for free :)

  • Add a new class (e.g. ConnectionHandler) that implements IConnectionHandler
  • In the override for playerLoggedIn, add the following code:

    EntityPlayerMP mp = (EntityPlayerMP)player;
    mp.inventory.addItemStackToInventory(
                    new ItemStack(...));
    
  • Finally, add the following line to your mod's load event

    NetworkRegistry.instance().registerConnectionHandler(
                    new ConnectionHandler());
    
comments powered by Disqus