diff --git a/pom.xml b/pom.xml index c05ba46..9cdbb02 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.gmail.artemis-the-gr8 PlayerStats - 1.5 + 1.6 UTF-8 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 0c43767..d1a903c 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 @@ -7,13 +7,17 @@ import com.gmail.artemis.the.gr8.playerstats.commands.TabCompleter; import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler; 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 com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.command.PluginCommand; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; + public class Main extends JavaPlugin { private static BukkitAudiences adventure; 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 17cf909..b841197 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 @@ -41,33 +41,30 @@ public class TabCompleter implements org.bukkit.command.TabCompleter { public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { List tabSuggestions = new ArrayList<>(); - //after typing "stat", suggest a list of viable statistics if (args.length >= 1) { String currentArg = args[args.length -1]; - if (args.length == 1) { - tabSuggestions = getTabSuggestions(EnumHandler.getStatNames(), args[0]); + if (args.length == 1) { //after typing "stat", suggest a list of viable statistics + tabSuggestions = getFirstArgSuggestions(args[0]); } - //after checking if args[0] is a viable statistic, suggest substatistic OR commandOptions - else { + 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", suggest playerNames + //if previous arg = "player" else if (previousArg.equalsIgnoreCase("player")) { - //if args.length-3 is kill_entity or entity_killed_by + if (args.length >= 3 && EnumHandler.isEntityStatistic(args[args.length-3])) { - tabSuggestions = commandOptions; + tabSuggestions = commandOptions; //if arg before "player" was entity-stat, suggest commandOptions } - else { + else { //otherwise "player" is target-flag: suggest playerNames tabSuggestions = getTabSuggestions(offlinePlayerHandler.getOfflinePlayerNames(), currentArg); } } @@ -81,6 +78,13 @@ public class TabCompleter implements org.bukkit.command.TabCompleter { return tabSuggestions; } + private List getFirstArgSuggestions(String currentArg) { + List suggestions = EnumHandler.getStatNames(); + suggestions.add("examples"); + suggestions.add("help"); + return getTabSuggestions(suggestions, currentArg); + } + private List getTabSuggestions(List completeList, String currentArg) { return completeList.stream() .filter(item -> item.toLowerCase().contains(currentArg.toLowerCase())) @@ -95,14 +99,12 @@ public class TabCompleter implements org.bukkit.command.TabCompleter { 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(); + return tabCompleteHelper.getEntitySuggestions(); } default -> { return commandOptions; 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 dca6308..7a1345f 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 @@ -11,7 +11,7 @@ import java.util.stream.Collectors; public final class TabCompleteHelper { private static List itemBrokenSuggestions; - private static List entityKilledSuggestions; + private static List entitySuggestions; public TabCompleteHelper() { prepareLists(); @@ -29,12 +29,13 @@ public final class TabCompleteHelper { return EnumHandler.getBlockNames(); } - public List getEntityKilledSuggestions() { - return entityKilledSuggestions; + public List getEntitySuggestions() { + return entitySuggestions; } private static void prepareLists() { + //breaking an item means running its durability negative itemBrokenSuggestions = Arrays.stream(Material.values()) .parallel() .filter(Material::isItem) @@ -43,7 +44,8 @@ public final class TabCompleteHelper { .map(String::toLowerCase) .collect(Collectors.toList()); - entityKilledSuggestions = Arrays.stream(EntityType.values()) + //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) diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java index 75387af..ff4ad31 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java @@ -13,7 +13,7 @@ import java.io.File; public class ConfigHandler { private static Main plugin; - private static double configVersion; + private static int configVersion; private File configFile; private FileConfiguration config; @@ -35,7 +35,7 @@ public class ConfigHandler {

PlayerStats 1.3: "config-version" is 3.

PlayerStats 1.4: "config-version" is 4.

*/ private void checkConfigVersion() { - if (!config.contains("config-version") || config.getDouble("config-version") != configVersion) { + if (!config.contains("config-version") || config.getInt("config-version") != configVersion) { new ConfigUpdateHandler(plugin, configFile, configVersion); reloadConfig(); } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java index a614420..a067623 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java @@ -11,7 +11,7 @@ import com.tchristofferson.configupdater.ConfigUpdater; public class ConfigUpdateHandler { /** Add new key-value pairs to the config without losing comments, using tchristofferson's Config-Updater */ - public ConfigUpdateHandler(Main plugin, File configFile, double configVersion) { + public ConfigUpdateHandler(Main plugin, File configFile, int configVersion) { YamlConfiguration configuration = YamlConfiguration.loadConfiguration(configFile); updateTopListDefault(configuration); updateDefaultColors(configuration); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/FontUtils.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/FontUtils.java index 0ebbc02..31e0287 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/FontUtils.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/FontUtils.java @@ -16,4 +16,4 @@ public final class FontUtils { return (int) Math.round((130.0 - (MinecraftFont.Font.getWidth(displayText) * 1.5))/2); } } -} +} \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java index afecbe1..b42982d 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java @@ -70,13 +70,13 @@ public class StatThread extends Thread { } } - Target selection = request.getSelection(); long lastCalc = ThreadManager.getLastRecordedCalcTime(); if (lastCalc > 2000) { adventure.sender(request.getCommandSender()).sendMessage( messageWriter.waitAMoment(lastCalc > 20000, request.isBukkitConsoleSender())); } + Target selection = request.getSelection(); TextComponent statResult; try { statResult = switch (selection) { 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 1a581ca..08259f9 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 @@ -12,6 +12,11 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; +/** This class deals with Bukkit Enumerators. It holds private lists of all + block-, item-, entity- and statistic-names, and has one big list of all + possible sub-statistic-entries (block/item/entity). It can give the names + of all aforementioned enums, check if something is a valid enum constant, + and turn a name into its corresponding enum constant. */ public final class EnumHandler { private final static List blockNames; @@ -50,11 +55,22 @@ public final class EnumHandler { .collect(Collectors.toList()); } + /** Returns all block-names in lowercase */ + public static List getBlockNames() { + return blockNames; + } + + /** Returns all item-names in lowercase*/ public static List getItemNames() { return itemNames; } - /** Returns corresponding item enum constant for an itemName + /** Returns all statistic-names in lowercase */ + public static List getStatNames() { + return statNames; + } + + /** Returns the corresponding Material enum constant for an itemName @param itemName String, case-insensitive @return Material enum constant, uppercase */ public static @Nullable Material getItemEnum(String itemName) { @@ -64,12 +80,7 @@ public final class EnumHandler { return (item != null && item.isItem()) ? item : null; } - /** Returns all entitytype names in lowercase */ - public static List getEntityNames() { - return entityNames; - } - - /** Returns corresponding EntityType enum constant for an entityName + /** Returns the corresponding EntityType enum constant for an entityName @param entityName String, case-insensitive @return EntityType enum constant, uppercase */ public static @Nullable EntityType getEntityEnum(String entityName) { @@ -81,12 +92,7 @@ public final class EnumHandler { } } - /** Returns all block names in lowercase */ - public static List getBlockNames() { - return blockNames; - } - - /** Returns corresponding block enum constant for a materialName + /** Returns the corresponding Material enum constant for a materialName @param materialName String, case-insensitive @return Material enum constant, uppercase */ public static @Nullable Material getBlockEnum(String materialName) { @@ -96,6 +102,17 @@ public final class EnumHandler { return (block != null && block.isBlock()) ? block : null; } + /** Returns the statistic enum constant, or null if that failed. + @param statName String, case-insensitive */ + public static @Nullable Statistic getStatEnum(@NotNull String statName) { + try { + return Statistic.valueOf(statName.toUpperCase()); + } + catch (IllegalArgumentException e) { + return null; + } + } + /** Checks if string is a valid statistic @param statName String, case-insensitive */ public static boolean isStatistic(@NotNull String statName) { @@ -108,20 +125,10 @@ public final class EnumHandler { statName.equalsIgnoreCase(Statistic.KILL_ENTITY.toString()); } - /** Returns the names of all general statistics in lowercase */ - public static List getStatNames() { - return statNames; - } - - /** Returns the statistic enum constant, or null if that failed. - @param statName String, case-insensitive */ - public static @Nullable Statistic getStatEnum(@NotNull String statName) { - try { - return Statistic.valueOf(statName.toUpperCase()); - } - catch (IllegalArgumentException e) { - return null; - } + /** Checks if this statistic is a subStatEntry, meaning it is a block, item or entity + @param statName String, case-insensitive*/ + public static boolean isSubStatEntry(@NotNull String statName) { + return subStatNames.contains(statName.toLowerCase()); } /** Returns "block", "entity", "item", or "sub-statistic" if the provided Type is null. */ @@ -135,10 +142,4 @@ public final class EnumHandler { } return subStat; } - - /** Checks if this statistic is a subStatEntry, meaning it is a block, item or entity - @param statName String, case-insensitive*/ - public static boolean isSubStatEntry(@NotNull String statName) { - return subStatNames.contains(statName.toLowerCase()); - } } \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b501218..ece4331 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,7 @@ # ------------------------------------------------------------------------------------------------------ # # PlayerStats Configuration # # ------------------------------------------------------------------------------------------------------ # -config-version: 5 +config-version: 6 # # ------------------------------- # # diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index edeef0d..f8408b2 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ main: com.gmail.artemis.the.gr8.playerstats.Main name: PlayerStats -version: 1.5 +version: 1.6 api-version: 1.18 description: adds commands to view player statistics in chat author: Artemis_the_gr8