Code Patterns

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

← Back to Patterns
command

CommandContext Helpers

Useful helper methods on CommandContext for player commands.

Example Code

java
// Check if sender is a player
if (!context.isPlayer()) {
    context.sendMessage(Message.raw("Players only!"));
    return;
}

// Get player entity reference directly
Ref<EntityStore> ref = context.senderAsPlayerRef();
if (ref == null || !ref.isValid()) {
    context.sendMessage(Message.raw("Could not find player!"));
    return;
}

// Get store and world from ref
Store<EntityStore> store = ref.getStore();
EntityStore entityStore = (EntityStore) store.getExternalData();
World world = entityStore.getWorld();

// Now you can use world.execute() for ECS operations

Thread Safety

context.isPlayer() and context.senderAsPlayerRef() are thread-safe checks

Common Mistakes

Not checking isPlayer() before senderAsPlayerRef(). Not checking ref.isValid() before using ref.