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)

This commit is contained in:
Artemis-the-gr8 2022-10-20 15:58:30 +02:00
parent 51a9140a9a
commit fc759da100
8 changed files with 107 additions and 41 deletions

View File

@ -4,7 +4,7 @@
<groupId>io.github.ithotl</groupId>
<artifactId>PlayerStats</artifactId>
<name>PlayerStats</name>
<version>1.8</version>
<version>2.0-SNAPSHOT</version>
<description>Statistics Plugin</description>
<url>https://www.spigotmc.org/resources/playerstats.102347/</url>
<developers>
@ -60,15 +60,15 @@
<relocations>
<relocation>
<pattern>net.kyori</pattern>
<shadedPattern>com.artemis.the.gr8.lib.kyori</shadedPattern>
<shadedPattern>com.artemis.the.gr8.playerstats.lib.kyori</shadedPattern>
</relocation>
<relocation>
<pattern>com.tchristofferson</pattern>
<shadedPattern>com.artemis.the.gr8.util.tchristofferson</shadedPattern>
<shadedPattern>com.artemis.the.gr8.playerstats.lib.tchristofferson</shadedPattern>
</relocation>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>com.artemis.the.gr8.util.bstats</shadedPattern>
<shadedPattern>com.artemis.the.gr8.playerstats.lib.bstats</shadedPattern>
</relocation>
</relocations>
<filters>

View File

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

View File

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

View File

@ -36,24 +36,38 @@ public final class TabCompleter implements org.bukkit.command.TabCompleter {
//args[3] = playerName (length = 4)
@Override
public @Nullable List<String> 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<String> getExcludeCommandSuggestions(@NotNull String[] args) {
if (args.length == 1) {
return getDynamicTabSuggestions(offlinePlayerHandler.getOfflinePlayerNames(), args[0]);
}
return null;
}
private @Nullable List<String> 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<String> getAfterStatSuggestions(@NotNull Statistic stat) {
private List<String> getSuggestionsAfterStat(@NotNull Statistic stat) {
switch (stat.getType()) {
case BLOCK -> {
return getAllBlockNames();

View File

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

View File

@ -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<Integer> implements RequestGenerator<Integer> {
public PlayerStatRequest(String playerName) {
super(Bukkit.getConsoleSender());
this(Bukkit.getConsoleSender(), playerName);
}
public PlayerStatRequest(CommandSender sender, String playerName) {
super(sender);
super.getSettings().configureForPlayer(playerName);
}

View File

@ -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<Long> implements Reques
public ServerStatRequest() {
super(Bukkit.getConsoleSender());
this(Bukkit.getConsoleSender());
}
public ServerStatRequest(CommandSender sender) {
super(sender);
super.getSettings().configureForServer();
}

View File

@ -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<LinkedHashMap<String, Integer>> implements RequestGenerator<LinkedHashMap<String, Integer>> {
public TopStatRequest(int topListSize) {
super(Bukkit.getConsoleSender());
this(Bukkit.getConsoleSender(), topListSize);
}
public TopStatRequest(CommandSender sender, int topListSize) {
super(sender);
super.getSettings().configureForTop(topListSize);
}