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
net.minecraft.block.Block.
Extending BlockFalling
net.minecraft.block.BlockFalling.
Tasks
Part 1 — Create a simple block that has no function
- Find the
AmethystOreclass intealsmc/mods/blocks/AmethystOre.java. You will modify this class. - 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. - Go to the
BlocksModuleclass. Notice thatAmethystOrehas been registered to theparentModclass, just as you did with items. - Now go back to the
Amethystoreclass. In the constructor, set the creative tab (setCreativeTab) toCreativeTabs.tabBlock. - 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)
- Modify the block you just created to now extend the
BlockFallingclass (early versions already have this modification in place). - 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
- Create a new block class called
BatteryBlockand extend theBlockclass. - Implement a
publicconstructor that takes no parameters. In the constructor, call the superclass's constructor withMaterial.rock. Set the creative tab to the block tab as above. - 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. - Implement the two methods
public void registerBlockIcons(IIconRegister iconRegister)andpublic IIcon getIcon(int side, int meta). Inside
registerBlockIcons, theIIconRegisterparameter has a methodregisterIcon(String texture_name)that returns anIIconreference. Use this method by passing each texture name as a parameter to return the associatedIIconobject. 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"
The Minecraft client will continually query the block for its texture icons, using the
getIconmethod. Inside thegetIconmethod, 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
IIconarray and return it to the calling method.- As with the basic block, register your
BatteryBlock. Use anything for the texture name, since you overrode theregisterBlockIconsmethod). - Head into the game and verify that your new battery block (top, bottom and sides) looks as expected.
Part 4 — Create a checkered block
- 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. - 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 passMaterial.rockto its superclass constructor, and will use theCreativeTabs.tabBlock. - Just like the
BatteryBlock, theCheckeredBlockclass will need an array ofIIconto hold the face textures. In this case, you'll need to hold two differentIIconentries. - As with
BatteryBlock, implement theregisterBlockIconsmethod (see above), and seticons[0]toCommon.MOD_ID + ":black"andicons[1]toCommon.MOD_ID + ":white". Again, refer to BatteryBlock for help. Now, you will need to implement two versions of the
getIconmethod. 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);- The game will use the first version of
getIconto retrieve the block textures when displaying it in your inventory. For this usage, just return the white icon for all sides. Now the fun part. The second version of
getIcontakes 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.