2020-07-13 21:27:59 +02:00
|
|
|
package me.clip.placeholderapi.commands;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
|
|
|
import me.clip.placeholderapi.commands.command.*;
|
2020-07-14 16:21:29 +02:00
|
|
|
import me.clip.placeholderapi.commands.command.ecloud.EcloudInfoCommand;
|
|
|
|
import me.clip.placeholderapi.commands.command.ecloud.EcloudListCommand;
|
2020-07-13 21:27:59 +02:00
|
|
|
import me.clip.placeholderapi.commands.command.ecloud.*;
|
|
|
|
import me.clip.placeholderapi.util.Msg;
|
|
|
|
import org.bukkit.command.CommandExecutor;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
public final class CommandHandler implements CommandExecutor {
|
|
|
|
private static final Command DEFAULT = new VersionCommand();
|
|
|
|
|
|
|
|
private static final List<Command> COMMANDS = Lists.newArrayList(
|
2020-07-14 16:21:29 +02:00
|
|
|
new EcloudClearCommand(),
|
|
|
|
new EcloudDownloadCommand(),
|
|
|
|
new EcloudInfoCommand(),
|
|
|
|
new EcloudListCommand(),
|
|
|
|
new EcloudPlaceholdersCommand(),
|
|
|
|
new EcloudRefreshCommand(),
|
|
|
|
new EcloudStatusCommand(),
|
|
|
|
new EcloudVersionInfoCommand(),
|
2020-07-13 21:27:59 +02:00
|
|
|
new EcloudCommand(),
|
|
|
|
new BcParseCommand(),
|
|
|
|
new ParseCommand(),
|
|
|
|
new ParseRelCommand(),
|
|
|
|
new DisableEcloudCommand(),
|
|
|
|
new EnableCloudCommand(),
|
|
|
|
new HelpCommand(),
|
2020-07-14 16:21:29 +02:00
|
|
|
new InfoCommand(),
|
|
|
|
new ListCommand(),
|
2020-07-13 21:27:59 +02:00
|
|
|
new RegisterCommand(),
|
|
|
|
new ReloadCommand(),
|
|
|
|
DEFAULT,
|
|
|
|
new UnregisterCommand()
|
|
|
|
);
|
|
|
|
|
|
|
|
static {
|
|
|
|
COMMANDS.sort((command1, command2) -> {
|
|
|
|
final int comparison = Integer.compare(command1.getMatch().length(), command2.getMatch().length());
|
|
|
|
|
|
|
|
if (comparison == 1) return -1;
|
|
|
|
if (comparison == -1) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
2020-07-14 16:24:01 +02:00
|
|
|
Objects.requireNonNull(PlaceholderAPIPlugin.getInstance().getCommand("placeholderapi"))
|
|
|
|
.setTabCompleter(new CompletionHandler(COMMANDS));
|
2020-07-13 21:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static final Pattern SPACE_PATTERN = Pattern.compile(" ");
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCommand(@NotNull final CommandSender sender, @NotNull final org.bukkit.command.Command bukkitCommand,
|
|
|
|
@NotNull final String name, @NotNull String[] args) {
|
|
|
|
if (args.length == 0) {
|
|
|
|
DEFAULT.execute(sender, args);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String joined = String.join(" ", args).toLowerCase();
|
|
|
|
final Optional<Command> optional = COMMANDS.stream()
|
|
|
|
.filter(command -> joined.startsWith(command.getMatch()))
|
|
|
|
.findFirst();
|
|
|
|
|
|
|
|
if (!optional.isPresent()) {
|
|
|
|
sender.sendMessage("Specified command is not valid.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
final Command command = optional.get();
|
|
|
|
|
|
|
|
if (!command.getPermissions().isEmpty() && command.getPermissions().stream().noneMatch(sender::hasPermission)) {
|
|
|
|
sender.sendMessage("You do not have the permission to execute specified command.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
args = splitArguments(joined, command.getMatch());
|
|
|
|
|
|
|
|
if (args.length < command.getMinimumArguments()) {
|
|
|
|
Msg.msg(sender, command.getUsage());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
command.execute(sender, args);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static String[] splitArguments(@NotNull final String joinedArguments, @NotNull final String command) {
|
|
|
|
final String[] args = SPACE_PATTERN.split(joinedArguments.replace(command, "").trim());
|
|
|
|
return args.length == 1 && args[0].isEmpty() ? new String[]{} : args;
|
|
|
|
}
|
|
|
|
}
|