Skip to content

Add block change listener API for third-party integrations - #69

Open
HezyyyCode wants to merge 1 commit into
Moulberry:masterfrom
HezyyyCode:feat/block-change-listeners
Open

HezyyyCode wants to merge 1 commit into
Moulberry:masterfrom
HezyyyCode:feat/block-change-listeners

Conversation

@HezyyyCode

Copy link
Copy Markdown

Motivation

Integration.CustomIntegration exposes canBreakBlock, canPlaceBlock and checkSection. That lets a plugin veto an edit, but there is no way to observe one. The only existing sink is CoreProtect, and it is hardcoded inside the write paths (SetBlockBufferOperation, SetBlockPacketListener).

That gap is visible on real servers. A region/plot protection plugin can deny an Axiom edit, but an auditing plugin (Prism, or any CoreProtect-style block log) cannot record one, so Axiom edits stay invisible to block history and rollbacks while WorldEdit/FAWE edits are recorded. This cannot be fixed from the consumer side: the coordinates and the old/new states only exist inside Axiom's write loop.

What this adds

Integration.BlockChangeListener:

public interface BlockChangeListener {
    void blockChanged(Player player, World world, int x, int y, int z, BlockState oldState, BlockState newState);
}

plus registerBlockChangeListener(Plugin, BlockChangeListener), unregisterBlockChangeListener(Plugin), hasBlockChangeListeners() and an internal blockChanged(...) dispatcher.

Registration is keyed by plugin name, following the existing customIntegrationsByPlugin pattern, so re-registering replaces the previous listener and a real unregister is possible.

Dispatch points

All three block write paths are covered:

Path Location
Batched chunk edits SetBlockBufferOperation
updateNeighbors branches (both) SetBlockPacketListener#onReceive
delayedSetWithoutUpdates / preventUpdatesAt SetBlockPacketListener#setWithoutUpdates

Each call sits next to the existing CoreProtect calls and only fires when the state actually changed.

Properties

  • No cost when unused. The extra getBlockState read and the dispatch are both behind hasBlockChangeListeners(), so servers with no listener take exactly today's path. CoreProtect keeps its own isEnabled() check.
  • No locking on the write path. The listener array is copy-on-write (volatile BlockChangeListener[], rebuilt under a lock only on register/unregister), so the hot loop just reads a field.
  • A failing listener cannot abort a world edit. Each callback is wrapped in try/catch (Throwable) and logged at SEVERE.
  • Cleanup on plugin disable. onPluginUnload calls unregisterBlockChangeListener, matching the existing ImplServerCustomBlocks / ImplServerCustomDisplays handling.
  • CoreProtect behaviour is unchanged. Same logRemoval/logPlacement pairs, same order, same guards.

Notes for consumers

Listeners receive the raw NMS BlockState before and after the change, mirroring the CoreProtectIntegration signatures; convert with CraftBlockData.fromData(...) when Bukkit BlockData is needed. Callbacks run on the server thread inside the write loop, so anything that touches a database should be handed to an async queue.

Verification

Existing installs behave identically: the CoreProtect path is preserved and every new read/dispatch is short-circuited when no listener is registered. CI compiles the module.

A downstream consumer is a Paper plugin that uses Prism for block history: with this hook it can record Axiom edits as first-class block actions instead of being blind to them.

Axiom currently exposes canBreakBlock/canPlaceBlock/checkSection, so plugins
can veto edits but cannot observe them. Servers that need to audit or protect
Axiom edits (logging plugins such as Prism, region protection) have no
supported hook, and the only existing sink - CoreProtect - is hardcoded
inside the write paths.

Add Integration.BlockChangeListener, registered per plugin and dispatched from
every block write path:

- SetBlockBufferOperation: the batched chunk path
- SetBlockPacketListener: both updateNeighbors branches
- SetBlockPacketListener#setWithoutUpdates: the delayed/preventUpdates path

Listeners receive the NMS BlockState before and after the change, matching the
existing CoreProtectIntegration signatures, so consumers can convert with
CraftBlockData.fromData if needed. Dispatch is short-circuited by
hasBlockChangeListeners(), which keeps the current zero-overhead behaviour
when nothing is registered, and the volatile array copy means the hot write
loop never takes a lock. Listener exceptions are caught and logged so a
misbehaving plugin cannot abort a world edit.

Listeners are unregistered automatically in onPluginUnload, following the
existing ImplServerCustomBlocks/ImplServerCustomDisplays pattern.

CoreProtect integration is unchanged: it keeps its own enabled check and
records exactly the same callbacks as before.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant