Ticked Blocks - CansteinBerlin/CustomBlocksApi GitHub Wiki

Sometimes you might need your block to be ticked let's say every tick to update some values/render some more complex animations/and so on...
To do so your block has to implement the ITickable interface.

Ticking your block

When implementing the ITickable interface you have to implement two functions.

  • int getTickDelay()
  • void onTick(TickState)

getTickDelay

This method describes how many ticks should be waited between each tick. So a return value of 1 means that the block will be ticked every tick. A return value of 20 means that the block is ticked every second.

onTick

This method is called when the block is ticked. The Tickstate provides the Context of the block. That means it provide the CustomBlockState currently ticked (getCustomBlockState()), and how often the block has been ticked (getCount()).

Example

Say we wanted to create a block that prints every second to the console how often it has been ticked. For that we would create a new block class that extends CustomBlock and implements ITickable

public class ExampleTickedBlock extends CustomBlock implements ITickable {
    [...]
}

We then have to override the getTickDelay Method

public class ExampleTickedBlock extends CustomBlock implements ITickable {
    [...]
    @Override
    public int getTickDelay() {
        return 20; //20 Ticks = One second.
    }
}

After that we override the onTick Method

public class ExampleTickedBlock extends CustomBlock implements ITickable {
    [...]
    @Override
    public void onTick(TickState tickState) { 
        // The following code is ran every 20 ticks/1 second
        // Print the name of the block and how often it has ticked to the console
        Bukkit.broadcast(Component.text(tickState.getCustomBlockState().getParentBlock().getSettings().getName() + " ticked " + tickState.getCount() + " times"));
    }
}

The last thing to do is to register the block as usual and test it out.