mirror of
https://github.com/itHotL/PlayerStats.git
synced 2025-02-17 01:51:35 +01:00
Fixed negative number issue for real now (#71) and abstracted a method in TabCompleter
This commit is contained in:
parent
529be1c337
commit
8ca928df57
@ -39,32 +39,26 @@ public class StatCommand implements CommandExecutor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||||
if (args.length == 0) { //in case of less than 1 argument, display the help message
|
if (args.length == 0 || args[0].equalsIgnoreCase("help")) { //in case of less than 1 argument or "help", display the help message
|
||||||
adventure.sender(sender).sendMessage(messageFactory.helpMsg(sender instanceof ConsoleCommandSender));
|
adventure.sender(sender).sendMessage(messageFactory.helpMsg(sender instanceof ConsoleCommandSender));
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else if (args[0].equalsIgnoreCase("help")) {
|
|
||||||
adventure.sender(sender).sendMessage(messageFactory.helpMsg(sender instanceof ConsoleCommandSender));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (args[0].equalsIgnoreCase("examples") ||
|
else if (args[0].equalsIgnoreCase("examples") ||
|
||||||
args[0].equalsIgnoreCase("example")) { //in case of "statistic examples", show examples
|
args[0].equalsIgnoreCase("example")) { //in case of "statistic examples", show examples
|
||||||
adventure.sender(sender).sendMessage(messageFactory.usageExamples(sender instanceof ConsoleCommandSender));
|
adventure.sender(sender).sendMessage(messageFactory.usageExamples(sender instanceof ConsoleCommandSender));
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else { //part 1: collecting all relevant information from the args
|
else { //part 1: collecting all relevant information from the args
|
||||||
StatRequest request = generateRequest(sender, args);
|
StatRequest request = generateRequest(sender, args);
|
||||||
|
|
||||||
if (isValidStatRequest(request)) { //part 2: sending the information to the StatThread
|
if (isValidStatRequest(request)) { //part 2: sending the information to the StatThread
|
||||||
threadManager.startStatThread(request);
|
threadManager.startStatThread(request);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else { //part 2: or give feedback if request is invalid
|
else { //part 2: or give feedback if request is invalid
|
||||||
adventure.sender(sender).sendMessage(getRelevantFeedback(request));
|
adventure.sender(sender).sendMessage(getRelevantFeedback(request));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//test method
|
//test method
|
||||||
@ -172,6 +166,7 @@ public class StatCommand implements CommandExecutor {
|
|||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--> separate "fixing" potential broken object from checking if it is valid
|
||||||
//part 2: check whether all necessary ingredients are present to proceed with a lookup
|
//part 2: check whether all necessary ingredients are present to proceed with a lookup
|
||||||
private boolean isValidStatRequest(StatRequest request) {
|
private boolean isValidStatRequest(StatRequest request) {
|
||||||
if (request.getStatName() != null) {
|
if (request.getStatName() != null) {
|
||||||
@ -211,6 +206,7 @@ public class StatCommand implements CommandExecutor {
|
|||||||
request.setSelection(Query.TOP);
|
request.setSelection(Query.TOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--> use this method to validate and immediately send feedback
|
||||||
//call this method when isValidStatRequest has returned false to get a relevant error-message
|
//call this method when isValidStatRequest has returned false to get a relevant error-message
|
||||||
private TextComponent getRelevantFeedback(@NotNull StatRequest request) {
|
private TextComponent getRelevantFeedback(@NotNull StatRequest request) {
|
||||||
boolean isConsoleSender = request.getCommandSender() instanceof ConsoleCommandSender;
|
boolean isConsoleSender = request.getCommandSender() instanceof ConsoleCommandSender;
|
||||||
|
@ -34,42 +34,47 @@ public class TabCompleter implements org.bukkit.command.TabCompleter {
|
|||||||
|
|
||||||
//after typing "stat", suggest a list of viable statistics
|
//after typing "stat", suggest a list of viable statistics
|
||||||
if (args.length >= 1) {
|
if (args.length >= 1) {
|
||||||
|
String currentArg = args[args.length -1];
|
||||||
|
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
tabSuggestions = EnumHandler.getStatNames().stream().filter(stat ->
|
tabSuggestions = getTabSuggestions(EnumHandler.getStatNames(), args[0]);
|
||||||
stat.contains(args[0].toLowerCase())).collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//after checking if args[0] is a viable statistic, suggest substatistic OR commandOptions
|
//after checking if args[0] is a viable statistic, suggest substatistic OR commandOptions
|
||||||
else {
|
else {
|
||||||
if (EnumHandler.isStatistic(args[args.length-2])) {
|
String previousArg = args[args.length -2];
|
||||||
tabSuggestions = switch (EnumHandler.getStatType(args[args.length-2])) {
|
|
||||||
|
if (EnumHandler.isStatistic(previousArg)) {
|
||||||
|
tabSuggestions = switch (EnumHandler.getStatType(previousArg)) {
|
||||||
case UNTYPED -> commandOptions;
|
case UNTYPED -> commandOptions;
|
||||||
case BLOCK -> EnumHandler.getBlockNames().stream().filter(block ->
|
case BLOCK -> getTabSuggestions(EnumHandler.getBlockNames(), currentArg);
|
||||||
block.contains(args[args.length - 1])).collect(Collectors.toList());
|
case ITEM -> getTabSuggestions(EnumHandler.getItemNames(), currentArg);
|
||||||
case ITEM -> EnumHandler.getItemNames().stream().filter(item ->
|
case ENTITY -> getTabSuggestions(EnumHandler.getEntityNames(), currentArg);
|
||||||
item.contains(args[args.length - 1])).collect(Collectors.toList());
|
|
||||||
case ENTITY -> EnumHandler.getEntityNames().stream().filter(entity ->
|
|
||||||
entity.contains(args[args.length - 1])).collect(Collectors.toList());
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
//if previous arg = "player", suggest playerNames
|
//if previous arg = "player", suggest playerNames
|
||||||
else if (args[args.length-2].equalsIgnoreCase("player")) {
|
else if (previousArg.equalsIgnoreCase("player")) {
|
||||||
if (args.length >= 3 && EnumHandler.getEntitySubStatNames().contains(args[args.length-3].toLowerCase())) {
|
if (args.length >= 3 && EnumHandler.getEntitySubStatNames().contains(args[args.length-3].toLowerCase())) {
|
||||||
tabSuggestions = commandOptions;
|
tabSuggestions = commandOptions;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
tabSuggestions = OfflinePlayerHandler.getOfflinePlayerNames().stream().filter(player ->
|
tabSuggestions = getTabSuggestions(OfflinePlayerHandler.getOfflinePlayerNames(), currentArg);
|
||||||
player.toLowerCase().contains(args[args.length-1].toLowerCase())).collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//after a substatistic, suggest commandOptions
|
//after a substatistic, suggest commandOptions
|
||||||
else if (EnumHandler.isSubStatEntry(args[args.length-2])) {
|
else if (EnumHandler.isSubStatEntry(previousArg)) {
|
||||||
tabSuggestions = commandOptions;
|
tabSuggestions = commandOptions;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tabSuggestions;
|
return tabSuggestions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<String> getTabSuggestions(List<String> completeList, String currentArg) {
|
||||||
|
return completeList.stream()
|
||||||
|
.filter(item -> item.toLowerCase().contains(currentArg.toLowerCase()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,6 @@ public class StatRequest {
|
|||||||
private boolean playerFlag;
|
private boolean playerFlag;
|
||||||
|
|
||||||
private Statistic statEnum;
|
private Statistic statEnum;
|
||||||
private Statistic.Type statType;
|
|
||||||
private EntityType entity;
|
private EntityType entity;
|
||||||
private Material block;
|
private Material block;
|
||||||
private Material item;
|
private Material item;
|
||||||
@ -35,17 +34,16 @@ public class StatRequest {
|
|||||||
public void setStatName(String statName) {
|
public void setStatName(String statName) {
|
||||||
this.statName = statName;
|
this.statName = statName;
|
||||||
if (statName != null) {
|
if (statName != null) {
|
||||||
setStatEnumAndType();
|
setStatEnum();
|
||||||
if (subStatEntry != null) {
|
if (subStatEntry != null) {
|
||||||
extractSubStat();
|
extractSubStat();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setStatEnumAndType() throws IllegalArgumentException {
|
private void setStatEnum() throws IllegalArgumentException {
|
||||||
try {
|
try {
|
||||||
statEnum = EnumHandler.getStatEnum(statName);
|
statEnum = EnumHandler.getStatEnum(statName);
|
||||||
statType = statEnum.getType();
|
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
Bukkit.getLogger().warning(e.toString());
|
Bukkit.getLogger().warning(e.toString());
|
||||||
}
|
}
|
||||||
@ -55,7 +53,7 @@ public class StatRequest {
|
|||||||
If the subStatEntry is set to null, any present item/block/entity is set to null again. */
|
If the subStatEntry is set to null, any present item/block/entity is set to null again. */
|
||||||
public void setSubStatEntry(String subStatEntry) {
|
public void setSubStatEntry(String subStatEntry) {
|
||||||
this.subStatEntry = subStatEntry;
|
this.subStatEntry = subStatEntry;
|
||||||
if (subStatEntry != null && statType != null) {
|
if (subStatEntry != null && statEnum != null) {
|
||||||
extractSubStat();
|
extractSubStat();
|
||||||
}
|
}
|
||||||
else if (subStatEntry == null) {
|
else if (subStatEntry == null) {
|
||||||
@ -66,7 +64,7 @@ public class StatRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void extractSubStat() {
|
private void extractSubStat() {
|
||||||
switch (statType) {
|
switch (statEnum.getType()) {
|
||||||
case ENTITY -> {
|
case ENTITY -> {
|
||||||
try {
|
try {
|
||||||
if (EnumHandler.isEntity(subStatEntry)) {
|
if (EnumHandler.isEntity(subStatEntry)) {
|
||||||
@ -120,7 +118,7 @@ public class StatRequest {
|
|||||||
|
|
||||||
/** Returns the type of the stored statistic, or null if no statName has been set. */
|
/** Returns the type of the stored statistic, or null if no statName has been set. */
|
||||||
public Statistic.Type getStatType() {
|
public Statistic.Type getStatType() {
|
||||||
return statType;
|
return statEnum.getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Statistic getStatEnum() {
|
public Statistic getStatEnum() {
|
||||||
|
@ -128,7 +128,7 @@ public class StatThread extends Thread {
|
|||||||
|
|
||||||
private long getServerTotal() {
|
private long getServerTotal() {
|
||||||
List<Integer> numbers = getAllStats().values().stream().toList();
|
List<Integer> numbers = getAllStats().values().stream().toList();
|
||||||
return numbers.parallelStream().mapToInt(Integer::intValue).sum();
|
return numbers.parallelStream().mapToLong(Integer::longValue).sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
//invokes a bunch of worker pool threads to divide and conquer (get the statistics for all players in the list)
|
//invokes a bunch of worker pool threads to divide and conquer (get the statistics for all players in the list)
|
||||||
|
Loading…
Reference in New Issue
Block a user