Minecraft Modding: Custom Leaves

Background


Note: This tutorial is part of Jabelar's Custom Tree tutorial.

In vanilla there are actually two leaves classes ("old" and "new") that extend the abstract class BlockLeaves and use a property to distinguish types. You can make a custom log by similarly extending BlockLeaves. A leaves block has the ability to "decay" based on DECAYABLE and CHECK_DECAY properties that allow the leaves to naturally fall away and give drops. 

Tip: Look at the vanilla BlockLeaves class to understand how to implement equivalent methods. However, I suggest you do NOT implement the VARIANT property like vanilla but instead move to the flat approach with a separate class per type.

Warning: The abstract class BlockLeaves has the DECAYABLE and CHECK_DECAY boolean properites but does not map them into metadata, so you will need to @Override the relevant methods.

Recommended Approach


Basically, the approach to simply create a class that extends BlockLeaves.
  1. Create class: Create a custom class that extends BlockLeaves and:
    • @Override the various methods related to metadata such as getSubTypes()getStateFromMeta() and getMetaFromState() to use a metadata value of 0 (or default)
    • Do the other block stuff such as setting the hardness, creative tab, step sound, etc. Copy the values from other leaves unless you want to change them for some reason.
    • Example code
  2. Registration Of Block, ItemBlock and Related Models: Refer to official Forge documentation on registry with object holder approach.
    • Instantiate block singleton instance: Instantiate your block using @ObjectHolder annotation.
    • Register block: Handle the RegistryEvent.Register event to register your block instance.
    • Register block model: Handle the ModelRegistryEvent to register your block model.
    • Instantiate ItemBlock singleton instance: Instantiate your related ItemBlock using the @ObjectHolder annotation.
    • Register ItemBlock: Handle the RegistryEvent.Register event to register your ItemBlock instance.
    • Register ItemBlock model: Handle the ModelRegistryEvent to register your ItemBlock model.
    • Example code
  3. Create and Organize Resource Assets:
    • Blockstate JSON: Make sure you have a proper blockstates JSON file in proper location and with name that matches the registry name. It needs to handle the variant values of the LOG_AXIS property. 
    • Block model JSON: Make sure you have a proper model JSON file for your block in proper location and with name that matches the references in your blockstate file. 
    • ItemBlock model JSON: Make sure you have a proper model JSON file for your item in proper location and with name that matches the registry name in your assets. 
    • Texture asset: Make sure you have a texture PNG asset in the proper location that matches the references in your model JSON files. 
    • .lang files: Make sure your lang file(s) have localization for your block name.
    • Example asset files

Issue: Fancy Graphics Will Not Be Automatically Applied To Custom Leaves


As you may know, due to the fact that leaves are partially see through they create a performance issue for some low-end computers. Therefore, there is a game setting for "fancy graphics" which enables the see-through leaves, otherwise they are full, opaque cubes.

However, the setFancyGraphics() method is only called for vanilla leaves in the RenderGlobal#loadRenderers() method. 

Therefore, you must call the setFancyGraphics() method yourself in your leaves block constructor. However, the method is Side.CLIENT only which means you must create a method in your proxy which does nothing on server and calls setFancyGraphics() on the client.

See example code (look at constructor).

Testing


Always remember to thoroughly test your code. In this case I recommend testing that:
  • The block appears as expected in the creative tab.
  • The block appears as expected when held in the hand.
  • The block can be placed in the world.
  • The block can be broken and harvested in survival mode.

Conclusion


A leaves block is a fairly simple block, but has the complexity of having a tickable decay property which needs to be handled in the metadata and in the random ticking methods. Hope you had fun creating it. Happy modding! 

1 comment:

  1. Is there anyway to alter the decay rate of custom leaves, I have looked at the BlockLeaves class `BlockLeaves#updateTick` but can't see where the decay speed is set. Thanks for the explanation int you tutorials. Rather than here's code, copy it and go.

    ReplyDelete