Java server development on Paper · 3 min read

Paper events: handle only permitted block breaks

BlockBreakEvent reports when a player attempts to break a block. For a hint after an allowed action, register a listener and filter world, mode and material; We will not change the cancellation of protection.

LonelyCraft editors · Updated

Desktop with computer and world Minecraft on screen
Illustration: desktop with a computer and world Minecraft on the screen. This is an artistic scene, not a snapshot of a specific server.

Determine which action counts

The example for Paper 26.3 and Java 25 uses base Gradle project. In world world the Survival player breaks STONE and receives a message. A player-placed stone is also suitable: the origin of the block is not stored here.

First check the actual world name via the console minecraft:list and your world settings: the arena folder name may be different from world. The conditions in the code should describe the desired world, and not a random current server.

Sequence of listener filters from cancellation to message to the player
Any failure terminates the handler before the message. Checking the origin of the block is not included in this example.Scroll the diagram horizontally.Open full-size diagram

Register a listener instance

Replace the main Java file with the complete class below. registerEvents is called once in onEnable. On MONITOR, the handler observes the state and does not cancel the action; ignoreCancelled=true skips an attempt that is already prohibited.

package ru.example.trailnotes;

import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.plugin.java.JavaPlugin;

public final class TrailNotesPlugin extends JavaPlugin implements Listener {
    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onBreak(BlockBreakEvent event) {
        if (!event.getBlock().getWorld().getName().equals("world")) return;
        if (event.getPlayer().getGameMode() != GameMode.SURVIVAL) return;
        if (event.getBlock().getType() != Material.STONE) return;
        event.getPlayer().sendMessage("Разрешённое разрушение камня учтено.");
    }
}

Check positive and negative cases

Build the JAR, install it in a separate test Paper and log in without OP. First, make sure there is no plugin enable error in the log. Then perform four experiments.

ExperienceExpected
STONE, Survival, world, destruction allowedOne message
DIRT under the same conditionsNo message
STONE in Creative or other worldNo message
STONE is regionally bannedThe block remains, there is no message

Don't turn a notification into a repeatable reward

If there is no message, temporarily add a log before each early return: you will see which filter did not pass. If the message is repeated twice, check that the listener is registered twice and that there are two copies of the plugin. Synchronous SQL in each destruction will add latency to the game flow.

The BlockBreakEvent contract contains neither a block’s origin nor a payment operation ID. Send the confirmed event to a quest progress service; for a one-time reward, use a persistent key combining the player and task.

A unique key is needed when repeating an event and after a restart. Protect one-time reward.

The file and database are read separately from the game tick, checking the relevance of the response. Put up with slow work.

Frequently asked questions

Does the annotation itself connect the listener?

No. Without registerEvents, the server does not have a registered instance to which to pass the event.

Does MONITOR guarantee the origin of the stone?

No. It determines the moment when an event is observed; the placed stone remains STONE.

Why should region protection remain enabled in the test?

It reproduces the cancellation of the event. The test under OP often bypasses the protection and does not check ignoreCancelled.

Views: … · One view per browser per day

Was this useful?Total score: …

One reaction per browser. Click again to remove it, or choose the other reaction.

Sources and documentation

Minecraftplugin developmentPaper

Read on

Paper Events: How to account for allowed block destruction - LonelyCraft — Lonely Craft