Minecraft Modding: Custom Log / Wood Block

Background


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

In vanilla there are actually two log classes ("old" and "new") that extend the abstract class BlockLog and use a property to distinguish types. You can make a custom log by similarly extending BlockLog. A log  has an orientation based on a LOG_AXIS property that affect how the texture looks (i.e. you can see the "tree rings" on two sides and the bark on the remaining sides). 

Tip: Look at the vanilla BlockLog 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 BlockLog has a LOG_AXIS property but does not map it into metadata, so you will need to @Override the relevant methods.

Recommended Approach


Basically, the approach to simply create a class that extends BlockLog.
  1. Create class: Create a custom class that extends BlockLog 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 logs 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 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.
    • Recipe JSONs: Make any recipe files you want. For example you might want to make planks.
    • Example asset files
Warning: You probably want your custom log to be usable as a fuel, just like vanilla logs. To do this, you need to @Override the getItemBurnTime() method as an anonymous class when you instantiate the ItemBlock. For example, when you create the new instance it should be something like this:
     new ItemBlock(cloud_log) {
          @Override
          public int getItemBurnTime(ItemStack itemStack)
          {
               return 300; // same value as vanilla log
          }
     }

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.
  • The block can be used in recipes as expected.
  • The block can be used as furnace fuel.

Conclusion


A log is a fairly simple block, but has the complexity of having an orientation property which needs to be handled in the metadata and the corresponding model. Hope you had fun creating it. Happy modding! 




1 comment:

  1. hello mr jabelar is there any other way to do this? getstate from meta will be removed in 1.13 and datafixing is currently terrible

    ReplyDelete