Making more advanced block

Before reading this highly recommended to read Making simple block first.

Let's create a advanced block.

Blocks can handle safe/load, interraction, interraction with item in hand, creation, deletion, validation fail events.

I will show examples for every event here.

Safe/Load events

Safe

Safe event is calling before server shutdown and after saving. Blocks are saving into JSON data and Safe event is giving access to extra section of block save data.

Example:

@Override
public JsonElement saveData(PlacedBlockData data) {
    JsonObject obj = new JsonObject(); // Creating new JSON object

    obj.addProperty("Some info", "My saved data"); // Adding data to JSON object
    return obj; // Return JSON object.
}

If you need to save more than 1 property use more JSON objects and add them to main JSON object then just return the main one. Everything will be put into extra section in JSON file.

Load

Load event is called when loading your block on sertain position before block validation. In element argument you will receive your extra section data. Also this event will be called only if extra is not null.

Example:

Yeah, this was easy. But make sure those values are not null and you are SURE that you can make this block work without those data or recover/create new data.

Interraction

Every PlacedBlockData contain unquie UUID that represents placed block instance. Use UUID to define some special block with special interractions.

Without items

Interraction without items will call when player are not holding any items when clicked or you are not override rightClickOnBlockWithItem mehtod.

Example:

Done.

With items

Same thing as without items but now player holding an item. By default this method is calling rightClickOnBlock method for handling event.

Example:

Done.

Creation and deletion

Creation

Creation event is called when block is gonna be placed.

Example:

If event was cancelled then custom block will dissapear and return to the inventory.

Deletion

Same thing like creation.

Example:

This event can be also cancelled and block will not break.

Validation fail

When block fail validation its getting removed from the world without blockDestroy event. Validation will fail when block on location is air or not material that defined in block data.

This event is uncancellable and called before deletion so location is still valid.

Example:

Done

Last updated

Was this helpful?