Anvil
Building blocksPlayersCapabilities

Overview

Install and retrieve the typed actions and observations that a player journey needs.

Retrieve player behavior by its public capability type with player.capability(Type.class). Keep these instances with that player and retrieve new ones after creating a replacement.

The capabilities in this section extend PlayerCapability; a provider creates each instance for one player. They cover what a Minecraft client can normally do or see: its connection, the chat it receives, its inventory, the dialogs it is shown. What a server or proxy knows about a player, such as whether the player is on it, the profile it sees, or the backend a proxy connected the player to, belongs to that process's Players capability, and moving a player to another world to its Worlds capability. General server or proxy control uses the context's process API or a capability owned by the process and backed by its agent, such as Dispatch.

Choose the behavior

For the first journey, use Session to connect, the server's Players to observe arrival, and Messages to trigger commands and check replies. Movement, Inventory, Interaction, Commands, and Dialogs cover additional game actions; Cookies holds the cookies a server stores on the client; Overlays and Sounds record the titles, action bars, boss bars and sounds the server shows and plays, Entities follows the entities the server shows it, such as text above a player, World the dimension the server put the player in, with the time, weather, world border and spawn point around it, Packets the packets the client receives, in arrival order, and Vitals the player's health, food and deaths. Each capability is optional unless the test or another installed capability requires it.

CapabilityUse it toBundled requirement
SessionConnect, disconnect, rejoin, and observe kicks.MCProtocol
MessagesSend chat and commands; await received text; check what the chat still shows.MCProtocol and Session
MovementSend absolute position and view changes.MCProtocol and Session
InventoryFind and click menu items by name, lore and type, follow screens, and read inventory state.MCProtocol and Session
InteractionUse items and interact with blocks or entities.MCProtocol and Session
CommandsRead the command tree and check commands as the client does.MCProtocol and Session
DialogsObserve dialogs, fill their inputs, and press their buttons.MCProtocol and Session, Minecraft 1.21.6+; Commands for buttons that run commands
CookiesHold cookies, answer the server's cookie requests, and observe them.MCProtocol and Session, Minecraft 1.20.5+
OverlaysAwait titles, action bars, and boss bars.MCProtocol and Session
SoundsAwait the sounds the server plays.MCProtocol and Session
EntitiesFollow the entities the server shows: positions, passengers, names, display text, items and flags.MCProtocol and Session
WorldRead the dimension the player is in, its type, environment and generation, the server's worlds, and the time, weather, world border and spawn point; await a change.MCProtocol and Session
PacketsMatch the packets the client receives in arrival order; record whole streams and compare two builds.MCProtocol
VitalsRead health and food, await deaths, and respawn.MCProtocol and Session

Packet-backed capabilities operate through the player's protocol library. The built-in capabilities are installed on every MCProtocolLib release Anvil ships, from Minecraft 1.16.5 to 26.3, except Dialogs, which needs a release with dialogs: 1.21.11 and newer, and Cookies, which needs a release with cookies: 1.20.6 and newer.

Install built-in capabilities

The me.whereareiam.anvil.capability.default Gradle plugin includes these player capabilities. For a smaller JUnit setup, this plugin block selects connection and messages behavior, and the process-owned Players capability that reports who is on each server:

plugins {
	java
	id("me.whereareiam.anvil.junit") version "0.0.1"
	id("me.whereareiam.anvil.capability.session") version "0.0.1"
	id("me.whereareiam.anvil.capability.messages") version "0.0.1"
	id("me.whereareiam.anvil.capability.players") version "0.0.1"
	id("me.whereareiam.anvil.platform.paper") version "0.0.1"
}

Keep the repositories, protocol dependency, and anvil configuration from installation. The remaining unit suffixes are movement, inventory, interaction, commands, dialogs, cookies, overlays, sounds, entities, world, packets, and vitals. Keep unit versions aligned with Anvil.

Retrieve a capability

In a running test, import me.whereareiam.anvil.capability.messages.Messages and use a connected player:

Messages messages = alice.capability(Messages.class);
messages.chat("hello-from-anvil");

Retrieval fails with CapabilityUnavailableException if the runtime does not supply that capability. Use hasCapability(type) only when absence is an intentional supported branch of the test. If the journey requires messages, let a missing Messages implementation fail rather than silently skipping the assertion.

Anvil selects each player's protocol library before composing its capabilities. Another library needs its own implementations of the capability APIs it supports; the bundled MCProtocol adapters are specific to MCProtocolLib. When the player's worker cannot install a capability, for example because the release lacks a class it needs, retrieval throws CapabilityUnavailableException with that reason while the player's other capabilities keep working.

Add an external capability

Add its consumer wiring artifact to anvilImplementation:

dependencies {
	add("anvilImplementation", "com.example:combat:1.0.0")
}

This coordinate is an example; use the artifact supplied by the capability author. See creating capabilities to publish one yourself.

On this page