Merged locale-bugfix into v2.0

This commit is contained in:
Artemis-the-gr8 2023-02-06 12:16:34 +01:00
parent 991ab06f9f
commit 6298bf075c
5 changed files with 6 additions and 212 deletions

View File

@ -1,116 +0,0 @@
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;
}
}
}
}

View File

@ -1,58 +0,0 @@
package com.artemis.the.gr8.playerstats.commands.cmdutils;
import com.artemis.the.gr8.playerstats.utils.EnumHandler;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
public final class TabCompleteHelper {
private final EnumHandler enumHandler;
private static List<String> itemBrokenSuggestions;
private static List<String> entitySuggestions;
public TabCompleteHelper(EnumHandler enumHandler) {
this.enumHandler = enumHandler;
prepareLists();
}
public List<String> getAllItemNames() {
return enumHandler.getItemNames();
}
public List<String> getItemBrokenSuggestions() {
return itemBrokenSuggestions;
}
public List<String> getAllBlockNames() {
return enumHandler.getBlockNames();
}
public List<String> getEntitySuggestions() {
return entitySuggestions;
}
private static void prepareLists() {
//breaking an item means running its durability negative
itemBrokenSuggestions = Arrays.stream(Material.values())
.parallel()
.filter(Material::isItem)
.filter(item -> item.getMaxDurability() != 0)
.map(Material::toString)
.map(string -> string.toLowerCase(Locale.ENGLISH))
.collect(Collectors.toList());
//the only statistics dealing with entities are killed_entity and entity_killed_by
entitySuggestions = Arrays.stream(EntityType.values())
.parallel()
.filter(EntityType::isAlive)
.map(EntityType::toString)
.map(string -> string.toLowerCase(Locale.ENGLISH))
.collect(Collectors.toList());
}
}

View File

@ -13,6 +13,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
public final class TabCompleter implements org.bukkit.command.TabCompleter {
@ -104,7 +105,7 @@ public final class TabCompleter implements org.bukkit.command.TabCompleter {
*/
private List<String> getDynamicTabSuggestions(@NotNull List<String> completeList, String currentArg) {
return completeList.stream()
.filter(item -> item.toLowerCase().contains(currentArg.toLowerCase()))
.filter(item -> item.toLowerCase(Locale.ENGLISH).contains(currentArg.toLowerCase(Locale.ENGLISH)))
.collect(Collectors.toList());
}
@ -147,7 +148,7 @@ public final class TabCompleter implements org.bukkit.command.TabCompleter {
.filter(Material::isItem)
.filter(item -> item.getMaxDurability() != 0)
.map(Material::toString)
.map(String::toLowerCase)
.map(string -> string.toLowerCase(Locale.ENGLISH))
.collect(Collectors.toList());
//the only statistics dealing with entities are killed_entity and entity_killed_by
@ -155,7 +156,7 @@ public final class TabCompleter implements org.bukkit.command.TabCompleter {
.parallel()
.filter(EntityType::isAlive)
.map(EntityType::toString)
.map(String::toLowerCase)
.map(string -> string.toLowerCase(Locale.ENGLISH))
.collect(Collectors.toList());
}
}

View File

@ -2,6 +2,7 @@ package com.artemis.the.gr8.playerstats.core.msg.msgutils;
import com.artemis.the.gr8.playerstats.core.utils.MyLogger;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -29,7 +30,7 @@ public final class StringUtils {
if (input == null) return null;
MyLogger.logHighLevelMsg("Prettifying [" + input + "]");
StringBuilder capitals = new StringBuilder(input.toLowerCase());
StringBuilder capitals = new StringBuilder(input.toLowerCase(Locale.ENGLISH));
capitals.setCharAt(0, Character.toUpperCase(capitals.charAt(0)));
while (capitals.indexOf("_") != -1) {

View File

@ -1,34 +0,0 @@
package com.artemis.the.gr8.playerstats.msg.msgutils;
import com.artemis.the.gr8.playerstats.utils.MyLogger;
import java.util.Locale;
/**
* A small utility class that helps make enum constant
* names prettier for output in stat-messages.
*/
public final class StringUtils {
private StringUtils() {
}
/**
* Replace "_" with " " and capitalize each first letter of the input.
*
* @param input String to prettify, case-insensitive
*/
public static String prettify(String input) {
if (input == null) return null;
StringBuilder capitals = new StringBuilder(input.toLowerCase(Locale.ENGLISH));
capitals.setCharAt(0, Character.toUpperCase(capitals.charAt(0)));
while (capitals.indexOf("_") != -1) {
MyLogger.logHighLevelMsg("Replacing underscores and capitalizing names...");
int index = capitals.indexOf("_");
capitals.setCharAt(index + 1, Character.toUpperCase(capitals.charAt(index + 1)));
capitals.setCharAt(index, ' ');
}
return capitals.toString();
}
}