Creating Custom Commands

Complete guide to creating custom commands with zAPI's command framework. Learn how to create commands, subcommands, and parameters without plugin.yml with automatic tab completion and cooldown suppor

zAPI provides a powerful command framework that eliminates the need for plugin.yml registration while offering advanced features like subcommands, parameters, cooldowns, and automatic tab completion.

Table of Contents

Overview

The zAPI command system consists of three main interfaces:

  • Command: Main commands with optional cooldowns

  • SubCommand: Sub-commands under main commands

  • Parameter: Optional flags/parameters (e.g., -force, -silent)

Key Features

  • No plugin.yml needed - Commands are registered dynamically

  • Automatic permission handling - Built-in permission checks

  • Cooldown support - Per-command cooldowns with bypass permissions

  • Parameter system - Unix-style parameters (e.g., -flag value)

  • Tab completion - Automatic subcommand completion

  • Message customization - Override default messages

  • Argument validation - Min/max argument counts

Basic Commands

Creating a Simple Command

Registering the Command

That's it! No plugin.yml entry needed. The command is now available as /hello.

circle-exclamation

Command Properties

Required Methods

Method
Return Type
Description

name()

String

The command name

execute()

void

Main execution logic

Optional Methods

Method
Return Type
Default
Description

description()

String

""

Command description

aliases()

List

[]

Command aliases

permission()

String

null

Required permission

playerOnly()

boolean

false

Can only be used by players

minArgs()

int

0

Minimum arguments required

maxArgs()

int

Integer.MAX_VALUE

Maximum arguments allowed

usage()

String

""

Usage message

permissionMessage()

String

Default

No permission message

playerOnlyMessage()

String

Default

Player-only message

cooldownTime()

double

0D

Cooldown in seconds

bypassCooldownPermission()

String

null

Permission to bypass cooldown

Complete Command Example

SubCommands

SubCommands allow you to create nested command structures like /homes set <name> or /homes delete <name>.

Creating a SubCommand

Registering SubCommands

SubCommands are registered to their parent command:

SubCommand Structure

Nested SubCommands

SubCommands can have their own SubCommands:

Structure:

Parameters

Parameters are optional flags that modify command behavior (e.g., -force, -silent, -amount 5).

Creating a Parameter

Registering Parameters

Using Parameters

Parameters with Arguments

Usage:

Accessing Parameter Values

Parameter Properties

Method
Return Type
Default
Description

name()

String

Required

Parameter name

aliases()

List

[]

Alternative names

permission()

String

null

Required permission

minArgs()

int

0

Minimum arguments

maxArgs()

int

Integer.MAX_VALUE

Maximum arguments

stopSubCommands()

boolean

false

Prevent subcommand execution

execute()

void

Empty

Parameter logic

Parameter Features

Stop SubCommand Execution

Useful for parameters like -help that should show help instead of executing the command.

Permission-Based Parameters

Players without permission won't see the parameter in tab completion and can't use it.

Tab Completion

Automatic Tab Completion

Tab completion is automatically provided for:

  • Main commands

  • SubCommands

  • Parameters (when typing -)

Custom Tab Completion

Override the tabComplete() method:

Tab Completion for SubCommands

SubCommands automatically appear in tab completion:

Tab Completion for Parameters

Parameters automatically appear when typing -:

Parameter Tab Completion

Parameters can provide custom tab completion for their arguments:

Usage:

Cooldown System

Commands can have cooldowns to prevent spam.

Basic Cooldown

Bypass Permission

Players with this permission skip the cooldown.

Cooldown Message

The default cooldown message is:

Customize it globally:

How Cooldowns Work

  1. Player executes command

  2. If cooldown is active, command is blocked

  3. Player sees cooldown message

  4. After cooldown expires, command can be used again

Cooldown Storage

Cooldowns are stored in memory and reset on server restart.

Complete Examples

Example 1: Homes Plugin Command Structure

Example 2: Admin Command with Nested Structure

Usage:

Example 3: Economy Command with Parameters

Usage:

Best Practices

  1. Use meaningful names - Clear command and subcommand names

  2. Provide descriptions - Help users understand commands

  3. Validate arguments - Check argument count and types

  4. Use parameters wisely - For optional flags and modifiers

  5. Implement tab completion - Improve user experience

  6. Set appropriate cooldowns - Prevent spam

  7. Use permissions - Control access to commands

  8. Provide clear usage messages - Help users when they make mistakes

  9. Handle errors gracefully - Don't crash on invalid input

  10. Keep commands focused - One command, one purpose

Common Patterns

Pattern 1: Confirmation Commands

Pattern 2: Player Target Commands

Pattern 3: Help Subcommand

See Also

Last updated

Was this helpful?