116 lines
4.6 KiB
Java
116 lines
4.6 KiB
Java
package com.artemis.the.gr8.playerstats.commands;
|
|
|
|
import com.artemis.the.gr8.playerstats.utils.EnumHandler;
|
|
import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
|
import com.artemis.the.gr8.playerstats.commands.cmdutils.TabCompleteHelper;
|
|
import org.bukkit.Statistic;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.stream.Collectors;
|
|
|
|
public final class TabCompleter implements org.bukkit.command.TabCompleter {
|
|
|
|
private final EnumHandler enumHandler;
|
|
private final OfflinePlayerHandler offlinePlayerHandler;
|
|
private final TabCompleteHelper tabCompleteHelper;
|
|
|
|
private final List<String> commandOptions;
|
|
|
|
public TabCompleter(EnumHandler enumHandler, OfflinePlayerHandler offlinePlayerHandler) {
|
|
this.enumHandler = enumHandler;
|
|
this.offlinePlayerHandler = offlinePlayerHandler;
|
|
tabCompleteHelper = new TabCompleteHelper(enumHandler);
|
|
|
|
commandOptions = new ArrayList<>();
|
|
commandOptions.add("top");
|
|
commandOptions.add("player");
|
|
commandOptions.add("server");
|
|
commandOptions.add("me");
|
|
|
|
}
|
|
|
|
//args[0] = statistic (length = 1)
|
|
//args[1] = commandOption (top/player/me) OR substatistic (block/item/entitytype) (length = 2)
|
|
//args[2] = executorName OR commandOption (top/player/me) (length = 3)
|
|
//args[3] = executorName (length = 4)
|
|
|
|
@Override
|
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
|
List<String> tabSuggestions = new ArrayList<>();
|
|
|
|
if (args.length >= 1) {
|
|
String currentArg = args[args.length -1];
|
|
|
|
if (args.length == 1) { //after typing "stat", suggest a list of viable statistics
|
|
tabSuggestions = getFirstArgSuggestions(args[0]);
|
|
}
|
|
|
|
else { //after checking if args[0] is a viable statistic, suggest substatistic OR commandOptions
|
|
String previousArg = args[args.length -2];
|
|
|
|
if (enumHandler.isStatistic(previousArg)) {
|
|
Statistic stat = EnumHandler.getStatEnum(previousArg);
|
|
if (stat != null) {
|
|
tabSuggestions = getTabSuggestions(getRelevantList(stat), currentArg);
|
|
}
|
|
}
|
|
|
|
//if previous arg = "player"
|
|
else if (previousArg.equalsIgnoreCase("player")) {
|
|
|
|
if (args.length >= 3 && enumHandler.isEntityStatistic(args[args.length-3])) {
|
|
tabSuggestions = commandOptions; //if arg before "player" was entity-stat, suggest commandOptions
|
|
}
|
|
else { //otherwise "player" is target-flag: suggest playerNames
|
|
tabSuggestions = getTabSuggestions(offlinePlayerHandler.getOfflinePlayerNames(), currentArg);
|
|
}
|
|
}
|
|
|
|
//after a substatistic, suggest commandOptions
|
|
else if (enumHandler.isSubStatEntry(previousArg)) {
|
|
tabSuggestions = commandOptions;
|
|
}
|
|
}
|
|
}
|
|
return tabSuggestions;
|
|
}
|
|
|
|
private List<String> getFirstArgSuggestions(String currentArg) {
|
|
List<String> suggestions = enumHandler.getStatNames();
|
|
suggestions.add("examples");
|
|
suggestions.add("help");
|
|
return getTabSuggestions(suggestions, currentArg);
|
|
}
|
|
|
|
private List<String> getTabSuggestions(List<String> completeList, String currentArg) {
|
|
return completeList.stream()
|
|
.filter(item -> item.toLowerCase(Locale.ENGLISH).contains(currentArg.toLowerCase(Locale.ENGLISH)))
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
private List<String> getRelevantList(Statistic stat) {
|
|
switch (stat.getType()) {
|
|
case BLOCK -> {
|
|
return tabCompleteHelper.getAllBlockNames();
|
|
}
|
|
case ITEM -> {
|
|
if (stat == Statistic.BREAK_ITEM) {
|
|
return tabCompleteHelper.getItemBrokenSuggestions();
|
|
} else {
|
|
return tabCompleteHelper.getAllItemNames();
|
|
}
|
|
}
|
|
case ENTITY -> {
|
|
return tabCompleteHelper.getEntitySuggestions();
|
|
}
|
|
default -> {
|
|
return commandOptions;
|
|
}
|
|
}
|
|
}
|
|
} |