diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java index 449eb42..5d6d15a 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java @@ -5,8 +5,10 @@ import com.gmail.artemis.the.gr8.playerstats.commands.StatCommand; import com.gmail.artemis.the.gr8.playerstats.commands.TabCompleter; import com.gmail.artemis.the.gr8.playerstats.commands.cmdutils.TabCompleteHelper; import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler; +import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel; import com.gmail.artemis.the.gr8.playerstats.listeners.JoinListener; import com.gmail.artemis.the.gr8.playerstats.msg.MessageWriter; +import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; @@ -39,6 +41,7 @@ public class Main extends JavaPlugin { //initialize the threadManager ThreadManager threadManager = new ThreadManager(adventure(), config, messageWriter, this); TabCompleteHelper tab = new TabCompleteHelper(); + Bukkit.getLogger().info(tab.getEntityKilledSuggestions().toString()); //register all commands and the tabCompleter PluginCommand statcmd = this.getCommand("statistic"); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/TabCompleter.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/TabCompleter.java index c21f223..46123c8 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/TabCompleter.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/TabCompleter.java @@ -1,5 +1,6 @@ package com.gmail.artemis.the.gr8.playerstats.commands; +import com.gmail.artemis.the.gr8.playerstats.commands.cmdutils.TabCompleteHelper; import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler; import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import org.bukkit.Statistic; @@ -14,6 +15,7 @@ import java.util.stream.Collectors; public class TabCompleter implements org.bukkit.command.TabCompleter { private final List commandOptions; + private final TabCompleteHelper tabCompleteHelper; //TODO add "example" to the list public TabCompleter() { @@ -22,6 +24,8 @@ public class TabCompleter implements org.bukkit.command.TabCompleter { commandOptions.add("player"); commandOptions.add("server"); commandOptions.add("me"); + + tabCompleteHelper = new TabCompleteHelper(); } //args[0] = statistic (length = 1) @@ -49,18 +53,13 @@ public class TabCompleter implements org.bukkit.command.TabCompleter { Statistic stat = EnumHandler.getStatEnum(previousArg); if (stat != null) { - tabSuggestions = switch (stat.getType()) { - case UNTYPED -> commandOptions; - case BLOCK -> getTabSuggestions(EnumHandler.getBlockNames(), currentArg); - case ITEM -> getTabSuggestions(EnumHandler.getItemNames(), currentArg); - case ENTITY -> getTabSuggestions(EnumHandler.getEntityNames(), currentArg); - }; + tabSuggestions = getTabSuggestions(getRelevantList(stat), currentArg); } } //if previous arg = "player", suggest playerNames else if (previousArg.equalsIgnoreCase("player")) { - if (args.length >= 3 && EnumHandler.getEntitySubStatNames().contains(args[args.length-3].toLowerCase())) { + if (args.length >= 3 && EnumHandler.getEntityTypeStatNames().contains(args[args.length-3].toLowerCase())) { tabSuggestions = commandOptions; } else { @@ -82,4 +81,27 @@ public class TabCompleter implements org.bukkit.command.TabCompleter { .filter(item -> item.toLowerCase().contains(currentArg.toLowerCase())) .collect(Collectors.toList()); } + + private List getRelevantList(Statistic stat) { + switch (stat.getType()) { + case BLOCK -> { + return tabCompleteHelper.getAllBlockNames(); + } + case ITEM -> { + if (stat == Statistic.BREAK_ITEM) { + return tabCompleteHelper.getItemBrokenSuggestions(); + } else if (stat == Statistic.CRAFT_ITEM) { + return tabCompleteHelper.getAllItemNames(); //TODO fix + } else { + return tabCompleteHelper.getAllItemNames(); + } + } + case ENTITY -> { + return tabCompleteHelper.getEntityKilledSuggestions(); + } + default -> { + return commandOptions; + } + } + } } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/cmdutils/TabCompleteHelper.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/cmdutils/TabCompleteHelper.java index af2d0ff..760894a 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/cmdutils/TabCompleteHelper.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/cmdutils/TabCompleteHelper.java @@ -2,28 +2,53 @@ package com.gmail.artemis.the.gr8.playerstats.commands.cmdutils; import com.gmail.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.stream.Collectors; public class TabCompleteHelper { private List itemBrokenSuggestions; + private List entityKilledSuggestions; public TabCompleteHelper() { prepareLists(); } - private void prepareLists() { - itemBrokenSuggestions = EnumHandler.getItems() - .parallelStream() - .filter(item -> item.getMaxDurability() != 0) - .map(Material::toString) - .map(String::toLowerCase) - .collect(Collectors.toList()); + public List getAllItemNames() { + return EnumHandler.getItemNames(); } public List getItemBrokenSuggestions() { return itemBrokenSuggestions; } + + public List getAllBlockNames() { + return EnumHandler.getBlockNames(); + } + + public List getEntityKilledSuggestions() { + return entityKilledSuggestions; + } + + + private void prepareLists() { + itemBrokenSuggestions = Arrays.stream(Material.values()) + .parallel() + .filter(Material::isItem) + .filter(item -> item.getMaxDurability() != 0) + .map(Material::toString) + .map(String::toLowerCase) + .collect(Collectors.toList()); + + entityKilledSuggestions = Arrays.stream(EntityType.values()) + .parallel() + .filter(EntityType::isAlive) + .filter(entityType -> entityType != EntityType.ARMOR_STAND) + .map(EntityType::toString) + .map(String::toLowerCase) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/Unit.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/Unit.java index 8c7de2f..8e8274a 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/Unit.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/Unit.java @@ -95,48 +95,34 @@ public enum Unit { } public double getSeconds() { - switch (this) { - case DAY -> { - return 86400; - } - case HOUR -> { - return 3600; - } - case MINUTE -> { - return 60; - } - case SECOND -> { - return 1; - } - case TICK -> { - return 1 / 20.0; - } - default -> { - return -1; - } - } + return switch (this) { + case DAY -> 86400; + case HOUR -> 3600; + case MINUTE -> 60; + case SECOND -> 1; + case TICK -> 1 / 20.0; + default -> -1; + }; } /** Returns the Unit corresponding to the given String. This String does NOT need to match exactly (it can be "day" or "days", for example), and is case-insensitive. @param unitName an approximation of the name belonging to the desired Unit, case-insensitive */ public static @NotNull Unit fromString(@NotNull String unitName) { - Unit unit; - switch (unitName.toLowerCase()) { - case "cm" -> unit = Unit.CM; - case "m", "block", "blocks" -> unit = Unit.BLOCK; - case "mile", "miles" -> unit = Unit.MILE; - case "km" -> unit = Unit.KM; - case "hp" -> unit = Unit.HP; - case "heart", "hearts" -> unit = Unit.HEART; - case "day", "days" -> unit = Unit.DAY; - case "hour", "hours" -> unit = Unit.HOUR; - case "minute", "minutes", "min" -> unit = Unit.MINUTE; - case "second", "seconds", "sec" -> unit = Unit.SECOND; - case "tick", "ticks" -> unit = Unit.TICK; - default -> unit = Unit.NUMBER; - } - return unit; + return switch (unitName.toLowerCase()) { + case "cm" -> Unit.CM; + case "m", "block", "blocks" -> Unit.BLOCK; + case "mile", "miles" -> Unit.MILE; + case "km" -> Unit.KM; + case "hp" -> Unit.HP; + case "heart", "hearts" -> Unit.HEART; + case "day", "days" -> Unit.DAY; + case "hour", "hours" -> Unit.HOUR; + case "minute", "minutes", "min" -> Unit.MINUTE; + case "second", "seconds", "sec" -> Unit.SECOND; + case "tick", "ticks" -> Unit.TICK; + default -> Unit.NUMBER; + }; } /** Returns the Unit.Type of this Statistic, which can be Untyped, Distance, Damage, or Time. diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/NumberFormatter.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/NumberFormatter.java index a3021fc..eaad3ff 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/NumberFormatter.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/NumberFormatter.java @@ -23,17 +23,11 @@ public class NumberFormatter { public String format(long number, Unit statUnit, Unit smallTimeUnit) { if (smallTimeUnit == null) { - switch (statUnit.getType()) { - case DISTANCE -> { - return formatDistance(number, statUnit); - } - case DAMAGE -> { - return formatDamage(number, statUnit); - } - default -> { - return format.format(number); - } - } + return switch (statUnit.getType()) { + case DISTANCE -> formatDistance(number, statUnit); + case DAMAGE -> formatDamage(number, statUnit); + default -> format.format(number); + }; } else { return formatTime(number, statUnit, smallTimeUnit); } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java index 8d14094..f890638 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java @@ -17,9 +17,8 @@ public class EnumHandler { private final static List blockNames; private final static List entityNames; private final static List itemNames; - private final static List items; private final static List statNames; - private final static List entitySubStatNames; + private final static List entityTypeStatNames; private final static List subStatNames; static { @@ -41,8 +40,9 @@ public class EnumHandler { .map(String::toLowerCase) .collect(Collectors.toList()); - items = Arrays.stream(Material.values()) - .filter(Material::isItem) + subStatNames = Stream.of(blockNames, entityNames, itemNames) + .flatMap(Collection::stream) + .distinct() .collect(Collectors.toList()); statNames = Arrays.stream(Statistic.values()) @@ -50,25 +50,16 @@ public class EnumHandler { .map(String::toLowerCase) .collect(Collectors.toList()); - entitySubStatNames = Arrays.stream(Statistic.values()) + entityTypeStatNames = Arrays.stream(Statistic.values()) .filter(statistic -> statistic.getType().equals(Statistic.Type.ENTITY)) .map(Statistic::toString) .map(String::toLowerCase) .collect(Collectors.toList()); - - subStatNames = Stream.of(blockNames, entityNames, itemNames) - .flatMap(Collection::stream) - .collect(Collectors.toList()); } private EnumHandler() { } - /** Returns all item names in lowercase */ - public static List getItems() { - return items; - } - public static List getItemNames() { return itemNames; } @@ -144,7 +135,7 @@ public class EnumHandler { } /** Returns all statistics that have type entities, in lowercase */ - public static List getEntitySubStatNames() { - return entitySubStatNames; + public static List getEntityTypeStatNames() { + return entityTypeStatNames; } } \ No newline at end of file