Note
Before you follow this guide, make sure you have decent knowledge of Java and have already decompiled Minecraft. If you are using RetroMCP-Java, you may want to enable "keep resources" in order to get the terrain.png
texture used for this tutorial.
This guide will explain how to make a simple flower block and making it generate in the world.
You can optionally use this flower texture created by Tyy on Modification Station for the tutorial.
- Open your IDE of choice and create a new class extending named
BlockWitherRose
with this code:
public class BlockWitherRose extends BlockFlower {
public BlockWitherRose(int id, int textureIndex) {
super(id, textureIndex);
}
@Override
public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity) {
if (entity instanceof EntityLiving) {
EntityLiving livingEntity = (EntityLiving) entity;
livingEntity.heal(-1);
}
}
}
The onEntityCollidedWithBlock
will check when an animal/mob touches the block and will damage them by 1 heart.
- Open the
Block
class and register the block using this code:
public static final Block WITHER_ROSE = new BlockWitherRose(BLOCK_ID, TEXTURE_INDEX)
.setStepSound(soundGrassFootstep)
.setBlockName("wither_rose");
setStepSound
sets the sound that the block makes to a grass/plant sound, and setBlockName
will set the translation key to use so the block can have a name.
- To add a name to your block, edit the
lang/en_US.lang
file to have the following entry:
tile.wither_rose.name=Wither Rose
- Make sure to replace
BLOCK_ID
with the block ID you want to assign the block to. The block ID can be between 1 and 255 and must not be used by any other blocks.TEXTURE_INDEX
is the texture slot interrain.png
where your block texture is located. Note that the top left of the texture atlas is slot 0 and the bottom right is slot 255.
Note
Make sure to reference the Forge texture atlases page if you are using Forge, which allows one to use a custom texture atlas for their mod.
- To make your flower generate in the world, open
ChunkProviderGenerate
and search for whereWorldGenFlowers
is used, and add this code after it.
if (this.rand.nextInt(4) == 0) {
WorldGenFlowers generator = new WorldGenFlowers(Block.WITHER_ROSE.blockID);
int x = i4 + this.rand.nextInt(16) + 8;
int z = i5 + this.rand.nextInt(16) + 8;
generator.generate(this.worldObj, this.rand, x, this.rand.nextInt(128), z);
}
- When you recompile and start the game, you should notice your flower generating in the world.
This is a list of methods you can use when creating your block in order to customize its properties.
disableNeighborNotifyOnMetadataChange
setStepSound
setLightOpacity
setLightValue
setResistance
setHardness
setBlockUnbreakable
setTickOnLoad
setBlockBounds
setBlockName
disableStats
This is a list of methods that you can optionally override in your block class to give your block more functionality.
initializeBlock
- Anything you need to do after all the blocks have been registered can go here.renderAsNormalBlock
getRenderType
- This method makes your block render differently. See Render Types.shouldSideBeRendered
getIsBlockSolid
getBlockTexture
getBlockTextureFromSideAndMetadata
getBlockTextureFromSide
getCollidingBoundingBoxes
getSelectedBoundingBoxFromPool
isOpaqueCube
updateTick
randomDisplayTick
onBlockDestroyedByPlayer
onNeighborBlockChange
tickRate
onBlockAdded
onBlockRemoval
quantityDropped
idDropped
damageDropped
onBlockDestroyedByExplosion
getRenderBlockPass
canPlaceBlockOnSide
canPlaceBlockAt
blockActivated
onEntityWalking
onBlockPlaced
onBlockClicked
getRenderColor
colorMultiplier
isPoweringTo
canProvidePower
onEntityCollidedWithBlock
isIndirectlyPoweringTo
harvestBlock
onBlockPlacedBy