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:

@Override
public void loadData(@NotNull JsonObject element) {
    // Getting info from "Some info" field.
    myField = element.get("Some info").getAsString();
}

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:

@Override
public void rightClickOnBlock(PlacedBlockData data, Player player) {
    player.sendMessage("Yo, how r u?");
    player.sendMessage(
        "We are in " + data.getLocation().getWorld().getName()
        );
}

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:

@Override
public void rightClickOnBlockWithItem(
        PlacedBlockData data, Player player, ItemStack item
    ) {
    
    player.sendMessage("Yo! That's " + item.getType().toString() +
        " in you'r hand? Cool!");
    
    if (CustomItemUtils.getKeyStatus(item).equals(CustomItemKeyStatus.FOUND)) {
        player.sendMessage("Aren't this item is custom? You're cool!!!");
    }
    
    // You got the idea.
    
}

Done.

Creation and deletion

Creation

Creation event is called when block is gonna be placed.

Example:

@Override
public void blockCreation(PlacedBlockData data, BlockPlaceEvent event) {
    // Bukkit event can be null if block was placed with code
    if (event != null) {
        Player creator = event.getPlayer();
        
        if (creator.getName().equals("Herobrine")) {
            creator.sendMessage("Nah, bro i'm not dealing with you.");
            event.setCancelled(true); // Canceling creation event.
            return;
        }
    }

    event.getPlayer().sendMessage("Thug shacker central is here.");
}

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

Deletion

Same thing like creation.

Example:

@Override
public void blockDestroy(PlacedBlockData data, BlockBreakEvent event) {
    if (event != null)
        event.getPlayer().sendMessage("Yo, i'm leaving.");
}

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:

@Override
public void validationFailed(PlacedBlockData data) {
    Debug.error("Im getting deleted from the world ):");
    // I have no idea what to put here.
}

Done

Last updated

Was this helpful?