Code Patterns

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

← Back to Patterns
command

Command with Arguments

Create a command with required and optional arguments using ArgTypes.

Example Code

java
public class TeleportCommand extends AbstractPlayerCommand {

    public TeleportCommand() {
        super("tp", "Teleport to coordinates");
    }

    RequiredArg<Integer> xArg = this.withRequiredArg("x", "X coordinate", ArgTypes.INTEGER);
    RequiredArg<Integer> yArg = this.withRequiredArg("y", "Y coordinate", ArgTypes.INTEGER);
    RequiredArg<Integer> zArg = this.withRequiredArg("z", "Z coordinate", ArgTypes.INTEGER);

    @Override
    protected void execute(@Nonnull CommandContext context, @Nonnull Store<EntityStore> store,
            @Nonnull Ref<EntityStore> ref, @Nonnull PlayerRef playerRef, @Nonnull World world) {

        int x = xArg.get(context);
        int y = yArg.get(context);
        int z = zArg.get(context);

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

        world.execute(() -> {
            Teleport teleport = Teleport.createForPlayer(world, new Vector3d(x, y, z), new Vector3f(0, 0, 0));
            store.addComponent(ref, Teleport.getComponentType(), teleport);
            player.sendMessage(Message.raw("Teleported to " + x + ", " + y + ", " + z));
        });
    }
}

Thread Safety

AbstractPlayerCommand extends AbstractAsyncCommand - execute() runs off main thread. Use world.execute() for ECS modifications.

Common Mistakes

Forgetting world.execute() when modifying components. Not handling null arguments for optional args.