-
Notifications
You must be signed in to change notification settings - Fork 0
Custom Blocks
Custom Blocks are based on a CustomBlockType. These consist of a base material, to be used when it's placed in the world, as well as a hardness value. Hardness works just like in vanilla and is responsible for the base breaking time of a block. A list for all vanilla materials can be found on the Minecraft Wiki.
There are two main ways of creating a CustomBlockType. You can either define a custom hardness value or copy it from another material.
CustomBlockType LIGHT_OBSIDIAN = CustomBlockType.of(Material.OBSIDIAN, 1.5f); // 1.5 is the Hardness of StoneCustomBlockType LIGHT_OBSIDIAN = CustomBlockType.of(Material.OBSIDIAN, Material.STONE); // Equates to 1.5Warning
Registration of any kind must always happen during STARTUP. Attempting to register entries later could lead to data loss and unexpected behaviour!
You will need to reference your CustomBlockType for registering and actually placing your Custom Blocks later, which is why an easy way for accessing them will be of tremendous help. We will start off with a CustomBlocks class including some helper methods for registering. One for a direct hardness value and another to copy the hardness.
Very important! We will need to call the static init() method in our onEnable(), as due to how Java behaves and loads classes, our CustomBlockTypes would not get registered until accessed which will throw upon loading chunks as MoonBreak is not aware of our types yet.
public class CustomBlocks {
public static void init() {}
private static CustomBlockType register(Key key, Material material, float hardness) {
CustomBlockType type = CustomBlockType.of(material, hardness);
BuiltinRegistries.BLOCK_TYPE.register(key, type);
return type;
}
private static CustomBlockType register(Key key, Material material, Material copyHardness) {
CustomBlockType type = CustomBlockType.of(material, copyHardness);
BuiltinRegistries.BLOCK_TYPE.register(key, type);
return type;
}
}Now we can easily register our CustomBlockType.
public static final CustomBlockType LIGHT_OBSIDIAN = register(Key.key("test:light_obsidian"), Material.OBSIDIAN, 1.5f);Now that we have successfully created and registered our CustomBlockType, we can actually place our Custom Blocks and place them in the world. For this, we will need the CustomBlockManager which is easily obtained by calling CustomBlockManager.manager().
CustomBlockManager.manager().place(yourLocation, LIGHT_OBSIDIAN);Our Custom Block is now placed in-world and we can finally break it! It should behave the same way as a stone block, but drop obsidian instead.
insert GIF