Code Patterns

Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.

← Back to Patterns
interaction

Custom Interaction

Create custom item interactions by extending SimpleInteraction. Register with codec registry.

Example Code

java
public class MyItemInteraction extends SimpleInteraction {

    public static final BuilderCodec<MyItemInteraction> CODEC =
        BuilderCodec.builder(MyItemInteraction.class, MyItemInteraction::new).build();

    @Override
    public void handle(@Nonnull Ref<EntityStore> ref, boolean firstRun, float time,
                       @Nonnull InteractionType type, @Nonnull InteractionContext context) {
        super.handle(ref, firstRun, time, type, context);

        var store = ref.getStore();
        var player = store.getComponent(ref, Player.getComponentType());

        switch (type) {
            case Primary -> player.sendMessage(Message.raw("Left click!"));
            case Secondary -> player.sendMessage(Message.raw("Right click!"));
            case Ability1 -> player.sendMessage(Message.raw("Ability 1!"));
            case Ability2 -> player.sendMessage(Message.raw("Ability 2!"));
            case Ability3 -> player.sendMessage(Message.raw("Ability 3!"));
            case Pick -> player.sendMessage(Message.raw("Pick/Middle click!"));
        }
    }
}

// Register in plugin setup():
this.getCodecRegistry(Interaction.CODEC).register(
    "MyPlugin_MyItemInteraction",
    MyItemInteraction.class,
    MyItemInteraction.CODEC
);

Thread Safety

Interactions run on the world thread.

Common Mistakes

Forgetting to register the interaction codec. Not calling super.handle().