Skip to content

Interaction API

The Interaction API lets you control any interaction. This includes block placing, attacking, etc.

If you're here to search for a specific method or just a quick explanation of how something works, you may find it useful to scroll through your IDE tooltips of your Interaction instance. This is a good method for discovering all possibilities you have, as Actor also has extensive Javadocs.

Obtaining the Interaction

You can obtain the API surface like this:

java
var interaction = YOUR_ACTOR.getInteraction();

Using the Interaction

Here is some info on how to use the Interaction API:

WARNING

Generally, when using the Interaction API and you activate it through a Command, you may want to wait for a single tick before executing your logic, since Minecraft ensures you can't interact after closing a GUI if the interaction started inside the GUI.

Interacting

You can interact (right click) once like this:

java
interaction.interact();

This will make you able to use rockets, place a block, etc.
If you want to interact for a specified amount of ticks or while a boolean supplier returns true, you can use this:

java
// Interact for 20 ticks
interaction.interact(20);
// OR
// Interact while the player is on ground
interaction.interact(() -> player.onGround());

WARNING

The above methods will make the Player spam interact every tick and not hold an interaction.
To hold an interaction you must use different methods.

You can hold interact (right click) like this:

java
// Hold interact for 20 ticks
interaction.holdInteract(20);
// OR
// Hold interact while the player is on ground
interaction.holdInteract(() -> player.onGround());

This will make you block with a shield, eat food, load a bow, etc.

Attacking

You can attack (left click) once like this:

java
interaction.attack();

This will make the Player destroy a block (if the block is minable in 1 tick), etc.
If you want to attack for a specified amount of ticks or while a boolean supplier returns true, you can use this:

java
// Attack for 20 ticks
interaction.attack(20);
// OR
// Attack while the player is in creative mode
interaction.attack(() -> player.isCreative());

WARNING

The above methods will make the Player spam attack every tick and not hold attack.
To hold an attack you must use different methods.

You can hold attack (left click) like this:

java
// Hold attack for 20 ticks
interaction.holdAttack(20);
// OR
// Hold attack while the player is in creative mode
interaction.holdAttack(() -> player.isCreative());

This will make the Player destroy blocks that take longer than 1 tick to mine, etc.

Persistent flags

You also have the option to set an interaction flag once and make every tick after that subject to that flag. These flags stay active until you change them.

java
interaction.setInteracting(true);
interaction.setAttacking(true);
// AND
interaction.setHoldingInteract(true);
interaction.setHoldingAttack(true);

The boolean you pass is just whether the flag should be true or false (e.g. attacking or not attacking).