summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/LambdaCommand.java
blob: 5f55122f53e4181302181fadd064c58111edc833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package io.dico.dicore.command;

import io.dico.dicore.exceptions.checkedfunctions.CheckedBiFunction;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;

import java.util.List;
import java.util.Objects;
import java.util.function.BiFunction;

public class LambdaCommand extends ExtendedCommand<LambdaCommand> {
    private CheckedBiFunction<CommandSender, ExecutionContext, String, CommandException> executor;
    private BiFunction<CommandSender, ExecutionContext, List<String>> completer;

    public LambdaCommand executor(CheckedBiFunction<CommandSender, ExecutionContext, String, CommandException> executor) {
        this.executor = Objects.requireNonNull(executor);
        return this;
    }

    public LambdaCommand completer(BiFunction<CommandSender, ExecutionContext, List<String>> completer) {
        this.completer = Objects.requireNonNull(completer);
        return this;
    }

    @Override
    public String execute(CommandSender sender, ExecutionContext context) throws CommandException {
        return executor.checkedApply(sender, context);
    }

    @Override
    public List<String> tabComplete(CommandSender sender, ExecutionContext context, Location location) {
        return completer == null ? super.tabComplete(sender, context, location) : completer.apply(sender, context);
    }

}