Code Patterns
Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.
All
225
Command
60
Damage
2
Ecs
27
Entity
5
Gui
75
Interaction
2
Inventory
4
Moderation
3
Permission
2
Player
9
Plugin
4
Storage
2
Teleport
3
Ui
22
Util
5
← Back to Patterns
java
interaction
Custom Interaction
Create custom item interactions by extending SimpleInteraction. Register with codec registry.
Example Code
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().