Code Patterns

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

← Back to Patterns
command

Command with All Argument Types

Use various argument types: STRING, INTEGER, DOUBLE, BOOLEAN, PLAYER_REF, WORLD, ITEM_ASSET, BLOCK_TYPE_KEY, GAME_MODE, etc.

Example Code

java
public class GiveCommand extends AbstractPlayerCommand {

    public GiveCommand() {
        super("give", "Give items to a player");
        this.requirePermission("server.give");
    }

    // Required arguments
    RequiredArg<PlayerRef> targetArg = withRequiredArg("player", "target.player", ArgTypes.PLAYER_REF);
    RequiredArg<ItemAsset> itemArg = withRequiredArg("item", "item.type", ArgTypes.ITEM_ASSET);

    // Optional arguments
    OptionalArg<Integer> amountArg = withOptionalArg("amount", "item.amount", ArgTypes.INTEGER);

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

        PlayerRef target = targetArg.get(context);
        ItemAsset item = itemArg.get(context);
        Integer amount = amountArg.get(context);  // May be null

        int count = amount != null ? amount : 1;

        // Give items...
        context.sendMessage(Message.raw("Gave " + count + " items"));
    }
}

// Available ArgTypes:
// ArgTypes.STRING      - Text
// ArgTypes.INTEGER     - Whole numbers
// ArgTypes.DOUBLE      - Decimal numbers
// ArgTypes.BOOLEAN     - true/false
// ArgTypes.PLAYER_REF  - Online player
// ArgTypes.WORLD       - World reference
// ArgTypes.ITEM_ASSET  - Item type
// ArgTypes.BLOCK_TYPE_KEY - Block type
// ArgTypes.GAME_MODE   - GameMode enum
// ArgTypes.RELATIVE_POSITION - ~x ~y ~z coordinates
// ArgTypes.ROTATION    - Rotation values
// ArgTypes.ENTITY_ID   - Entity network ID