Commands

plugin.yml

As you would in a normal bukkit plugin, you need to configure your commands in the plugin.yml file

main: be.garagepoort.tubingexample.TubingExample
name: TubingExample
version: ${project.version}
description: Tubing simple example project
author: Garagepoort
api-version: 1.16
commands:
  broadcast:
    description: Broadcasts a message to all players
    usage: /<command>
    aliases: [br]

CommandExecutor

The command executor needs to be annotated with @IocBukkitCommandHandler. The handler annotation takes as parameter the id of the command as specified in the plugin.yml file.

@IocBukkitCommandHandler("broadcast")
public class BroadcastCmd implements CommandExecutor {

    private final MessageService messageService;
    private final BroadcastingService broadcastingService;

    public BroadcastCmd(MessageService messageService, BroadcastingService broadcastingService) {
        this.messageService = messageService;
        this.broadcastingService = broadcastingService;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
        try {
            if(args.length < 1) {
                throw new BusinessException("Invalid arguments given for broadcast. Must provide a message");
            }

            String message = JavaUtils.compileWords(args, 0);
            broadcastingService.broadcast(sender, message);
            return true;
        } catch (BusinessException e) {
            messageService.sendMessage(sender, "&6[Broadcasts] &C" + e.getMessage());
            return false;
        }
    }
}

Last updated