Fixed negative number issue for real now (#71) and abstracted a method in TabCompleter

This commit is contained in:
Artemis-the-gr8 2022-06-25 20:52:50 +02:00
parent 529be1c337
commit 8ca928df57
4 changed files with 30 additions and 31 deletions

View File

@ -39,32 +39,26 @@ public class StatCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
if (args.length == 0) { //in case of less than 1 argument, display the help message
if (args.length == 0 || args[0].equalsIgnoreCase("help")) { //in case of less than 1 argument or "help", display the help message
adventure.sender(sender).sendMessage(messageFactory.helpMsg(sender instanceof ConsoleCommandSender));
return true;
}
else if (args[0].equalsIgnoreCase("help")) {
adventure.sender(sender).sendMessage(messageFactory.helpMsg(sender instanceof ConsoleCommandSender));
return false;
}
else if (args[0].equalsIgnoreCase("examples") ||
args[0].equalsIgnoreCase("example")) { //in case of "statistic examples", show examples
adventure.sender(sender).sendMessage(messageFactory.usageExamples(sender instanceof ConsoleCommandSender));
return true;
}
else { //part 1: collecting all relevant information from the args
StatRequest request = generateRequest(sender, args);
if (isValidStatRequest(request)) { //part 2: sending the information to the StatThread
threadManager.startStatThread(request);
return true;
}
else { //part 2: or give feedback if request is invalid
adventure.sender(sender).sendMessage(getRelevantFeedback(request));
return false;
}
}
return true;
}
//test method
@ -172,6 +166,7 @@ public class StatCommand implements CommandExecutor {
return request;
}
//--> separate "fixing" potential broken object from checking if it is valid
//part 2: check whether all necessary ingredients are present to proceed with a lookup
private boolean isValidStatRequest(StatRequest request) {
if (request.getStatName() != null) {
@ -211,6 +206,7 @@ public class StatCommand implements CommandExecutor {
request.setSelection(Query.TOP);
}
//--> use this method to validate and immediately send feedback
//call this method when isValidStatRequest has returned false to get a relevant error-message
private TextComponent getRelevantFeedback(@NotNull StatRequest request) {
boolean isConsoleSender = request.getCommandSender() instanceof ConsoleCommandSender;

View File

@ -34,42 +34,47 @@ public class TabCompleter implements org.bukkit.command.TabCompleter {
//after typing "stat", suggest a list of viable statistics
if (args.length >= 1) {
String currentArg = args[args.length -1];
if (args.length == 1) {
tabSuggestions = EnumHandler.getStatNames().stream().filter(stat ->
stat.contains(args[0].toLowerCase())).collect(Collectors.toList());
tabSuggestions = getTabSuggestions(EnumHandler.getStatNames(), args[0]);
}
//after checking if args[0] is a viable statistic, suggest substatistic OR commandOptions
else {
if (EnumHandler.isStatistic(args[args.length-2])) {
tabSuggestions = switch (EnumHandler.getStatType(args[args.length-2])) {
String previousArg = args[args.length -2];
if (EnumHandler.isStatistic(previousArg)) {
tabSuggestions = switch (EnumHandler.getStatType(previousArg)) {
case UNTYPED -> commandOptions;
case BLOCK -> EnumHandler.getBlockNames().stream().filter(block ->
block.contains(args[args.length - 1])).collect(Collectors.toList());
case ITEM -> EnumHandler.getItemNames().stream().filter(item ->
item.contains(args[args.length - 1])).collect(Collectors.toList());
case ENTITY -> EnumHandler.getEntityNames().stream().filter(entity ->
entity.contains(args[args.length - 1])).collect(Collectors.toList());
case BLOCK -> getTabSuggestions(EnumHandler.getBlockNames(), currentArg);
case ITEM -> getTabSuggestions(EnumHandler.getItemNames(), currentArg);
case ENTITY -> getTabSuggestions(EnumHandler.getEntityNames(), currentArg);
};
}
//if previous arg = "player", suggest playerNames
else if (args[args.length-2].equalsIgnoreCase("player")) {
else if (previousArg.equalsIgnoreCase("player")) {
if (args.length >= 3 && EnumHandler.getEntitySubStatNames().contains(args[args.length-3].toLowerCase())) {
tabSuggestions = commandOptions;
}
else {
tabSuggestions = OfflinePlayerHandler.getOfflinePlayerNames().stream().filter(player ->
player.toLowerCase().contains(args[args.length-1].toLowerCase())).collect(Collectors.toList());
tabSuggestions = getTabSuggestions(OfflinePlayerHandler.getOfflinePlayerNames(), currentArg);
}
}
//after a substatistic, suggest commandOptions
else if (EnumHandler.isSubStatEntry(args[args.length-2])) {
else if (EnumHandler.isSubStatEntry(previousArg)) {
tabSuggestions = commandOptions;
}
}
}
return tabSuggestions;
}
private List<String> getTabSuggestions(List<String> completeList, String currentArg) {
return completeList.stream()
.filter(item -> item.toLowerCase().contains(currentArg.toLowerCase()))
.collect(Collectors.toList());
}
}

View File

@ -20,7 +20,6 @@ public class StatRequest {
private boolean playerFlag;
private Statistic statEnum;
private Statistic.Type statType;
private EntityType entity;
private Material block;
private Material item;
@ -35,17 +34,16 @@ public class StatRequest {
public void setStatName(String statName) {
this.statName = statName;
if (statName != null) {
setStatEnumAndType();
setStatEnum();
if (subStatEntry != null) {
extractSubStat();
}
}
}
private void setStatEnumAndType() throws IllegalArgumentException {
private void setStatEnum() throws IllegalArgumentException {
try {
statEnum = EnumHandler.getStatEnum(statName);
statType = statEnum.getType();
} catch (IllegalArgumentException e) {
Bukkit.getLogger().warning(e.toString());
}
@ -55,7 +53,7 @@ public class StatRequest {
If the subStatEntry is set to null, any present item/block/entity is set to null again. */
public void setSubStatEntry(String subStatEntry) {
this.subStatEntry = subStatEntry;
if (subStatEntry != null && statType != null) {
if (subStatEntry != null && statEnum != null) {
extractSubStat();
}
else if (subStatEntry == null) {
@ -66,7 +64,7 @@ public class StatRequest {
}
private void extractSubStat() {
switch (statType) {
switch (statEnum.getType()) {
case ENTITY -> {
try {
if (EnumHandler.isEntity(subStatEntry)) {
@ -120,7 +118,7 @@ public class StatRequest {
/** Returns the type of the stored statistic, or null if no statName has been set. */
public Statistic.Type getStatType() {
return statType;
return statEnum.getType();
}
public Statistic getStatEnum() {

View File

@ -128,7 +128,7 @@ public class StatThread extends Thread {
private long getServerTotal() {
List<Integer> numbers = getAllStats().values().stream().toList();
return numbers.parallelStream().mapToInt(Integer::intValue).sum();
return numbers.parallelStream().mapToLong(Integer::longValue).sum();
}
//invokes a bunch of worker pool threads to divide and conquer (get the statistics for all players in the list)