Skip to content

Add simple implementation of CommandCallable #523

@boformer

Description

@boformer

It would be nice to have a simple abstract implementation of CommandCallable (like SimpleDispatcher implementation for Dispatcher).

The abstract class provides the basic functionality that is sufficient in many cases. The only method it should not implement is the call method. A command class extending the abstract class would look like this:

public class SendCommand extends AbstractCommand {
    public SendCommand() {
        super(
                "simplemail.write", // permission
                "Send a mail", // description
                Texts.of("Send a server mail to a player."), // help
                "<player> <msg>"); // usage
    }

    @Override
    public boolean call(CommandSource source, String arguments, List<String> parents) throws CommandException {
        // Command stuff...
        return true;
    }
}

This is the proposed abstract command class:

public abstract class AbstractCommand implements CommandCallable {
    private final String permission;
    private final String shortDescription;
    private final Text help;
    private final String usage;

    public AbstractCommand(String permission, String shortDescription, Text help, String usage) {
        this.permission = permission;
        this.shortDescription = shortDescription;
        this.help = help;
        this.usage = usage;
    }

    @Override
    public boolean testPermission(CommandSource source) {
        return source.hasPermission(this.permission);
    }

    @Override
    public String getShortDescription(CommandSource source) {
        return this.shortDescription;
    }

    @Override
    public Text getHelp(CommandSource source) {
        return this.help;
    }

    @Override
    public String getUsage(CommandSource source) {
        return this.usage;
    }

    @Override
    public List<String> getSuggestions(CommandSource source, String arguments) throws CommandException {
        return Collections.emptyList();
    }

}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions