Finished up tab-complete improvements, added "examples" at start of command (#72, #91), changed version number to 1.6

This commit is contained in:
Artemis-the-gr8 2022-07-12 12:54:08 +02:00
parent ec13ec51b8
commit aa8f31c6fb
11 changed files with 67 additions and 58 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.gmail.artemis-the-gr8</groupId>
<artifactId>PlayerStats</artifactId>
<version>1.5</version>
<version>1.6</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -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;

View File

@ -41,33 +41,30 @@ public class TabCompleter implements org.bukkit.command.TabCompleter {
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
List<String> 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<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().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;

View File

@ -11,7 +11,7 @@ import java.util.stream.Collectors;
public final class TabCompleteHelper {
private static List<String> itemBrokenSuggestions;
private static List<String> entityKilledSuggestions;
private static List<String> entitySuggestions;
public TabCompleteHelper() {
prepareLists();
@ -29,12 +29,13 @@ public final class TabCompleteHelper {
return EnumHandler.getBlockNames();
}
public List<String> getEntityKilledSuggestions() {
return entityKilledSuggestions;
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)
@ -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)

View File

@ -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 {
<p>PlayerStats 1.3: "config-version" is 3. </P>
<p>PlayerStats 1.4: "config-version" is 4.</p>*/
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();
}

View File

@ -11,7 +11,7 @@ import com.tchristofferson.configupdater.ConfigUpdater;
public class ConfigUpdateHandler {
/** Add new key-value pairs to the config without losing comments, using <a href="https://github.com/tchristofferson/Config-Updater">tchristofferson's Config-Updater</a> */
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);

View File

@ -16,4 +16,4 @@ public final class FontUtils {
return (int) Math.round((130.0 - (MinecraftFont.Font.getWidth(displayText) * 1.5))/2);
}
}
}
}

View File

@ -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) {

View File

@ -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<String> blockNames;
@ -50,11 +55,22 @@ public final class EnumHandler {
.collect(Collectors.toList());
}
/** Returns all block-names in lowercase */
public static List<String> getBlockNames() {
return blockNames;
}
/** Returns all item-names in lowercase*/
public static List<String> getItemNames() {
return itemNames;
}
/** Returns corresponding item enum constant for an itemName
/** Returns all statistic-names in lowercase */
public static List<String> 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<String> 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<String> 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<String> 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());
}
}

View File

@ -1,7 +1,7 @@
# ------------------------------------------------------------------------------------------------------ #
# PlayerStats Configuration #
# ------------------------------------------------------------------------------------------------------ #
config-version: 5
config-version: 6
# # ------------------------------- # #

View File

@ -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