From fc759da1004877a34a0eab1dcd0efcdf6c14e428 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Thu, 20 Oct 2022 15:58:30 +0200 Subject: [PATCH] Added TabCompleter for ExcludeCommand, moved command initializing to its own method in Main, and fixed bug where commandsender wasn't set for internal StatRequests (#88) --- dependency-reduced-pom.xml | 8 +-- .../com/artemis/the/gr8/playerstats/Main.java | 54 +++++++++++++------ .../gr8/playerstats/commands/StatCommand.java | 15 +++--- .../playerstats/commands/TabCompleter.java | 34 ++++++++---- .../playerstats/msg/msgutils/StringUtils.java | 16 +++++- .../statistic/PlayerStatRequest.java | 7 ++- .../statistic/ServerStatRequest.java | 7 ++- .../playerstats/statistic/TopStatRequest.java | 7 ++- 8 files changed, 107 insertions(+), 41 deletions(-) diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 991fecf..e1cc09b 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -4,7 +4,7 @@ io.github.ithotl PlayerStats PlayerStats - 1.8 + 2.0-SNAPSHOT Statistics Plugin https://www.spigotmc.org/resources/playerstats.102347/ @@ -60,15 +60,15 @@ net.kyori - com.artemis.the.gr8.lib.kyori + com.artemis.the.gr8.playerstats.lib.kyori com.tchristofferson - com.artemis.the.gr8.util.tchristofferson + com.artemis.the.gr8.playerstats.lib.tchristofferson org.bstats - com.artemis.the.gr8.util.bstats + com.artemis.the.gr8.playerstats.lib.bstats diff --git a/src/main/java/com/artemis/the/gr8/playerstats/Main.java b/src/main/java/com/artemis/the/gr8/playerstats/Main.java index d3e9f2b..3537658 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/Main.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/Main.java @@ -3,12 +3,9 @@ package com.artemis.the.gr8.playerstats; import com.artemis.the.gr8.playerstats.api.PlayerStats; import com.artemis.the.gr8.playerstats.api.StatFormatter; import com.artemis.the.gr8.playerstats.api.StatManager; +import com.artemis.the.gr8.playerstats.commands.*; import com.artemis.the.gr8.playerstats.statistic.RequestManager; import com.artemis.the.gr8.playerstats.msg.OutputManager; -import com.artemis.the.gr8.playerstats.commands.ReloadCommand; -import com.artemis.the.gr8.playerstats.commands.ShareCommand; -import com.artemis.the.gr8.playerstats.commands.StatCommand; -import com.artemis.the.gr8.playerstats.commands.TabCompleter; import com.artemis.the.gr8.playerstats.config.ConfigHandler; import com.artemis.the.gr8.playerstats.listeners.JoinListener; import com.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler; @@ -50,21 +47,10 @@ public final class Main extends JavaPlugin implements PlayerStats { @Override public void onEnable() { - //initialize all the Managers, singletons, ConfigHandler and the API initializeMainClasses(); + registerCommands(); setupMetrics(); - //register all commands and the tabCompleter - PluginCommand statcmd = this.getCommand("statistic"); - if (statcmd != null) { - statcmd.setExecutor(new StatCommand(outputManager, threadManager, config, offlinePlayerHandler, enumHandler)); - statcmd.setTabCompleter(new TabCompleter(enumHandler, offlinePlayerHandler)); - } - PluginCommand reloadcmd = this.getCommand("statisticreload"); - if (reloadcmd != null) reloadcmd.setExecutor(new ReloadCommand(threadManager)); - PluginCommand sharecmd = this.getCommand("statisticshare"); - if (sharecmd != null) sharecmd.setExecutor(new ShareCommand(shareManager, outputManager)); - //register the listener Bukkit.getPluginManager().registerEvents(new JoinListener(threadManager), this); @@ -109,6 +95,11 @@ public final class Main extends JavaPlugin implements PlayerStats { return playerStatsAPI; } + /** + * Initialize all classes that need initializing, + * and store references to classes that are + * needed for the Command classes or the API. + */ private void initializeMainClasses() { pluginInstance = this; playerStatsAPI = this; @@ -127,6 +118,37 @@ public final class Main extends JavaPlugin implements PlayerStats { threadManager = new ThreadManager(this, config, outputManager, statRequestManager); } + /** + * Register all commands and assign the tabCompleter + * to the relevant commands. + */ + private void registerCommands() { + TabCompleter tabCompleter = new TabCompleter(enumHandler, offlinePlayerHandler); + + PluginCommand statcmd = this.getCommand("statistic"); + if (statcmd != null) { + statcmd.setExecutor(new StatCommand(outputManager, threadManager, config, offlinePlayerHandler, enumHandler)); + statcmd.setTabCompleter(tabCompleter); + } + PluginCommand excludecmd = this.getCommand("statisticexclude"); + if (excludecmd != null) { + excludecmd.setExecutor(new ExcludeCommand(offlinePlayerHandler)); + excludecmd.setTabCompleter(tabCompleter); + } + + PluginCommand reloadcmd = this.getCommand("statisticreload"); + if (reloadcmd != null) { + reloadcmd.setExecutor(new ReloadCommand(threadManager)); + } + PluginCommand sharecmd = this.getCommand("statisticshare"); + if (sharecmd != null) { + sharecmd.setExecutor(new ShareCommand(shareManager, outputManager)); + } + } + + /** + * Setup bstats + */ private void setupMetrics() { new BukkitRunnable() { @Override diff --git a/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java b/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java index 5a3cdc4..6f76183 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java @@ -100,7 +100,9 @@ public final class StatCommand implements CommandExecutor { private final class ArgProcessor { + private final CommandSender sender; private String[] argsToProcess; + private Statistic statistic; private String subStatName; private Target target; @@ -108,11 +110,12 @@ public final class StatCommand implements CommandExecutor { private StatRequest request; private ArgProcessor(CommandSender sender, String[] args) { - argsToProcess = args; + this.sender = sender; + this.argsToProcess = args; extractStatistic(); extractSubStatistic(); - extractTarget(sender); + extractTarget(); combineProcessedArgsIntoRequest(); } @@ -124,9 +127,9 @@ public final class StatCommand implements CommandExecutor { RequestGenerator requestGenerator = switch (target) { - case PLAYER -> new PlayerStatRequest(playerName); - case SERVER -> new ServerStatRequest(); - case TOP -> new TopStatRequest(config.getTopListMaxSize()); + case PLAYER -> new PlayerStatRequest(sender, playerName); + case SERVER -> new ServerStatRequest(sender); + case TOP -> new TopStatRequest(sender, config.getTopListMaxSize()); }; switch (statistic.getType()) { @@ -152,7 +155,7 @@ public final class StatCommand implements CommandExecutor { } } - private void extractTarget(CommandSender sender) { + private void extractTarget() { String targetArg = null; for (String arg : argsToProcess) { Matcher matcher = pattern.matcher(arg); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java b/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java index e8169c1..c8e2a81 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java @@ -36,24 +36,38 @@ public final class TabCompleter implements org.bukkit.command.TabCompleter { //args[3] = playerName (length = 4) @Override public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if (args.length >= 1) { + if (command.getName().equalsIgnoreCase("statistic")) { + return getStatCommandSuggestions(args); + } + else if (command.getName().equalsIgnoreCase("statisticexclude")) { + return getExcludeCommandSuggestions(args); + } + return null; + } + + private @Nullable List getExcludeCommandSuggestions(@NotNull String[] args) { + if (args.length == 1) { + return getDynamicTabSuggestions(offlinePlayerHandler.getOfflinePlayerNames(), args[0]); + } + return null; + } + + private @Nullable List getStatCommandSuggestions(@NotNull String[] args) { + if (args.length == 1) { + return getFirstArgSuggestions(args[0]); + } + else if (args.length > 1) { String currentArg = args[args.length-1]; - - if (args.length == 1) { - return getFirstArgSuggestions(args[0]); - } - - //after checking if args[0] is a viable statistic, suggest sub-stat OR targets String previousArg = args[args.length-2]; + //after checking if args[0] is a viable statistic, suggest sub-stat or targets if (enumHandler.isStatistic(previousArg)) { Statistic stat = EnumHandler.getStatEnum(previousArg); if (stat != null) { - return getDynamicTabSuggestions(getAfterStatSuggestions(stat), currentArg); + return getDynamicTabSuggestions(getSuggestionsAfterStat(stat), currentArg); } } else if (previousArg.equalsIgnoreCase("player")) { - if (args.length >= 3 && enumHandler.isEntityStatistic(args[args.length-3])) { return targetSuggestions; //if arg before "player" was entity-sub-stat, suggest targets } @@ -89,7 +103,7 @@ public final class TabCompleter implements org.bukkit.command.TabCompleter { .collect(Collectors.toList()); } - private List getAfterStatSuggestions(@NotNull Statistic stat) { + private List getSuggestionsAfterStat(@NotNull Statistic stat) { switch (stat.getType()) { case BLOCK -> { return getAllBlockNames(); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/StringUtils.java b/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/StringUtils.java index ff62aec..d6cb464 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/StringUtils.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/StringUtils.java @@ -2,12 +2,22 @@ package com.artemis.the.gr8.playerstats.msg.msgutils; import com.artemis.the.gr8.playerstats.utils.MyLogger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * A small utility class that helps make enum constant * names prettier for output in stat-messages. */ public final class StringUtils { + private static final Pattern lowercaseLetterAfterSpace; + private static final Pattern underscore; + + static { + lowercaseLetterAfterSpace = Pattern.compile("(?<= )[a-z]"); + underscore = Pattern.compile("_"); + } private StringUtils() { } @@ -18,6 +28,7 @@ public final class StringUtils { */ public static String prettify(String input) { if (input == null) return null; + MyLogger.logHighLevelMsg("Prettifying [" + input + "]"); StringBuilder capitals = new StringBuilder(input.toLowerCase()); capitals.setCharAt(0, Character.toUpperCase(capitals.charAt(0))); @@ -28,9 +39,10 @@ public final class StringUtils { capitals.setCharAt(index, ' '); } - while (capitals.indexOf(" ") != -1) { + Matcher matcher = lowercaseLetterAfterSpace.matcher(capitals); + while (matcher.find()) { MyLogger.logHighLevelMsg("Capitalizing names..."); - int index = capitals.indexOf(" ") + 1; + int index = matcher.start(); capitals.setCharAt(index, Character.toUpperCase(capitals.charAt(index))); } return capitals.toString(); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/PlayerStatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/PlayerStatRequest.java index 52855a5..c8f9bc2 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/PlayerStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/PlayerStatRequest.java @@ -4,13 +4,18 @@ import com.artemis.the.gr8.playerstats.api.RequestGenerator; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Statistic; +import org.bukkit.command.CommandSender; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; public final class PlayerStatRequest extends StatRequest implements RequestGenerator { public PlayerStatRequest(String playerName) { - super(Bukkit.getConsoleSender()); + this(Bukkit.getConsoleSender(), playerName); + } + + public PlayerStatRequest(CommandSender sender, String playerName) { + super(sender); super.getSettings().configureForPlayer(playerName); } diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/ServerStatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/ServerStatRequest.java index 8170a61..239ef57 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/ServerStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/ServerStatRequest.java @@ -4,6 +4,7 @@ import com.artemis.the.gr8.playerstats.api.RequestGenerator; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Statistic; +import org.bukkit.command.CommandSender; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; @@ -11,7 +12,11 @@ public final class ServerStatRequest extends StatRequest implements Reques public ServerStatRequest() { - super(Bukkit.getConsoleSender()); + this(Bukkit.getConsoleSender()); + } + + public ServerStatRequest(CommandSender sender) { + super(sender); super.getSettings().configureForServer(); } diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/TopStatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/TopStatRequest.java index d911fb2..32a6dec 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/TopStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/TopStatRequest.java @@ -4,6 +4,7 @@ import com.artemis.the.gr8.playerstats.api.RequestGenerator; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Statistic; +import org.bukkit.command.CommandSender; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; @@ -12,7 +13,11 @@ import java.util.LinkedHashMap; public final class TopStatRequest extends StatRequest> implements RequestGenerator> { public TopStatRequest(int topListSize) { - super(Bukkit.getConsoleSender()); + this(Bukkit.getConsoleSender(), topListSize); + } + + public TopStatRequest(CommandSender sender, int topListSize) { + super(sender); super.getSettings().configureForTop(topListSize); }