Lab 3: Basic Blocks

Objective

If you want to modify the Minecraft world, you'll need to be able to work with blocks – the fundamental building unit of the MC universe. For this lab, you'll be building our first custom block.

Notes

Extending Block

You need to import net.minecraft.block.Block.

Extending BlockFalling

You need to import net.minecraft.block.BlockFalling.

Tasks

Part 1 — Create a simple block that has no function

  1. Find the AmethystOre class in tealsmc/mods/blocks/AmethystOre.java. You will modify this class.
  2. To start with, it has a public constructor that takes no parameters. Inside the constructor, add a call the superclass's constructor with Material.rock.
  3. Go to the BlocksModule class. Notice that AmethystOre has been registered to the parentMod class, just as you did with items.
  4. Now go back to the Amethystore class. In the constructor, set the creative tab (setCreativeTab) to CreativeTabs.tabBlock.
  5. Go in-game, open your inventory (press 'e'), and scroll to the bottom of the blocks tab. You should see the Amethyst block near the bottom of the blocks creative tab (it's a gray block with purple splotches). Click on the block to select it, and then click on an open slot at the bottom row of your inventory to put the Amethyst block there. Hit the escape key to go back to the game. Now select the Amethyst block by either clicking on the slot you put it in, or by pressing the number corresponding to the proper slot (the leftmost slot is number 1, the rightmost slot is number 9). Aim at a spot on the ground where you want to place the block, and right click. You can then put a block on top of the block you just placed, or just about anywhere else. Left-click on a block to destroy it.

Part 2 — Create a block that gravity applies to (falling block)

  1. Modify the block you just created to now extend the BlockFalling class (early versions already have this modification in place).
  2. In game, place your block next to another block, with nothing beneath it. If the block falls down until it rests on something, it works. Another way to test this is to make a stack of two Amethyst blocks, and then destroy (right-click) the bottom block. The top block should fall. If it didn't, make sure you registered the correct block.

Part 3 — Create a block with multiple textures

  1. Create a new block class called BatteryBlock and extend the Block class.
  2. Implement a public constructor that takes no parameters. In the constructor, call the superclass's constructor with Material.rock. Set the creative tab to the block tab as above.
  3. In order to make multiple textures, you will need to create a private array of IIcon's (size 3) as a field. You will use this array to hold the three textures (images) of the top, bottom and sides of the battery.
  4. Implement the two methods public void registerBlockIcons(IIconRegister iconRegister) and public IIcon getIcon(int side, int meta).
  5. Inside registerBlockIcons, the IIconRegister parameter has a method registerIcon(String texture_name) that returns an IIcon reference. Use this method by passing each texture name as a parameter to return the associated IIcon object. Store these icon references in your icons array. The texture names are the following:

    • Common.MOD_ID + ":battery_top"
    • Common.MOD_ID + ":battery_bottom"
    • Common.MOD_ID + ":battery_side"
  6. The Minecraft client will continually query the block for its texture icons, using the getIcon method. Inside the getIcon method, the side parameter indicates which side of the cube it's querying:

    • 0: block bottom
    • 1: block top
    • 2, 3, 4, 5: block sides

    Implement the logic that uses the "side" parameter to look up the correct object in the IIcon array and return it to the calling method.

  7. As with the basic block, register your BatteryBlock. Use anything for the texture name, since you overrode the registerBlockIcons method).
  8. Head into the game and verify that your new battery block (top, bottom and sides) looks as expected.

Part 4 — Create a checkered block

  1. In the 'assets' directory of your installation, you'll find two images: white.png and black.png. Add those two files to your project in src/main/resources/assets.tealsmodloader.textures/blocks.
  2. A checkered block is like a battery block in that you'll need to assign textures to the block faces. However, a checkered block comes in two versions: all white and all black. When stacked or placed side-by-side, checkered blocks will form a checker pattern — black, white, black, white and so on. Start by creating a new block class, CheckeredBlock. The public checkered block constructor should take no parameters, will pass Material.rock to its superclass constructor, and will use the CreativeTabs.tabBlock.
  3. Just like the BatteryBlock, the CheckeredBlock class will need an array of IIcon to hold the face textures. In this case, you'll need to hold two different IIcon entries.
  4. As with BatteryBlock, implement the registerBlockIcons method (see above), and set icons[0] to Common.MOD_ID + ":black" and icons[1] to Common.MOD_ID + ":white". Again, refer to BatteryBlock for help.
  5. Now, you will need to implement two versions of the getIcon method. They have the following signatures:

    public IIcon getIcon (int side, int meta);

    and

    public IIcon getIcon (
        IBlockAccess worldAccess, int x, int y, int z, int meta);

  6. The game will use the first version of getIcon to retrieve the block textures when displaying it in your inventory. For this usage, just return the white icon for all sides.
  7. Now the fun part. The second version of getIcon takes in x, y and z parameters that correspond to the Minecraft coordinate system. The ground extends in the X and Z directions, and Y is used to represent up. You can also think of these three coordinates as the address of any given block. If you're unfamiliar with 3D coordinates, don't worry — it's just like the old familiar X and Y Cartesian coordinates, with a new coordinate that goes into and out of the screen, to cover 3D space.

    The puzzle you'll need to solve is how to figure out, from the three coordinate parameters alone, whether to return the black texture, or the white texture, so that the CheckeredBlock forms a checkerboard pattern when placed in the world.