Finished making stat command modular (#5)

This commit is contained in:
Artemis-the-gr8 2022-05-07 17:07:56 +02:00
parent 7b93153d83
commit 727078f464
5 changed files with 79 additions and 51 deletions

View File

@ -12,7 +12,7 @@ public class Main extends JavaPlugin {
ConfigHandler config = new ConfigHandler(this); ConfigHandler config = new ConfigHandler(this);
EnumHandler enumHandler = new EnumHandler(); EnumHandler enumHandler = new EnumHandler();
StatManager statManager = new StatManager(enumHandler); StatManager statManager = new StatManager(enumHandler, this);
this.getCommand("statistic").setExecutor(new StatCommand(enumHandler, statManager)); this.getCommand("statistic").setExecutor(new StatCommand(enumHandler, statManager));
this.getCommand("statistic").setTabCompleter(new TabCompleter(enumHandler, statManager,this)); this.getCommand("statistic").setTabCompleter(new TabCompleter(enumHandler, statManager,this));
this.getLogger().info("Enabled PlayerStats!"); this.getLogger().info("Enabled PlayerStats!");

View File

@ -2,7 +2,10 @@ package com.gmail.artemis.the.gr8.playerstats;
import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler; import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Statistic; import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -11,12 +14,14 @@ import java.util.stream.Collectors;
public class StatManager { public class StatManager {
private final Main plugin;
private final EnumHandler enumHandler; private final EnumHandler enumHandler;
private final List<String> statNames; private final List<String> statNames;
private final List<String> entityStatNames; private final List<String> entityStatNames;
private final List<String> subStatEntryNames; private final List<String> subStatEntryNames;
public StatManager(EnumHandler e) { public StatManager(EnumHandler e, Main p) {
plugin = p;
enumHandler = e; enumHandler = e;
statNames = Arrays.stream(Statistic.values()).map( statNames = Arrays.stream(Statistic.values()).map(
@ -32,16 +37,56 @@ public class StatManager {
} }
//returns Statistic enum constant (uppercase) if the input name is valid, otherwise null (param: statName in uppercase) //returns Statistic enum constant (uppercase) if the input name is valid, otherwise null (param: statName in uppercase)
public int getStatistic(Statistic stat, String playerName) { public int getStatistic(String statName, String playerName) throws IllegalArgumentException, NullPointerException {
return OfflinePlayerHandler.getOfflinePlayer(playerName).getStatistic(stat); return getStatistic(statName, null, playerName);
}
public int getStatistic(String statName, String subStatEntryName, String playerName) throws IllegalArgumentException, NullPointerException {
OfflinePlayer player = OfflinePlayerHandler.getOfflinePlayer(playerName);
if (player == null) throw new NullPointerException("No player called " + playerName + " was found!");
Statistic stat = getStatistic(statName);
if (stat != null) {
switch (stat.getType()) {
case UNTYPED -> {
return player.getStatistic(stat);
}
case BLOCK -> {
Material block = enumHandler.getBlock(subStatEntryName);
if (block == null) throw new NullPointerException(subStatEntryName + " is not a valid block name!");
return player.getStatistic(stat, block);
}
case ENTITY -> {
EntityType entity = enumHandler.getEntityType(subStatEntryName);
if (entity == null) throw new NullPointerException(subStatEntryName + " is not a valid entity name!");
return player.getStatistic(stat, entity);
}
case ITEM -> {
Material item = enumHandler.getItem(subStatEntryName);
if (item == null) throw new NullPointerException(subStatEntryName + " is not a valid item name!");
return player.getStatistic(stat, item);
}
}
}
throw new NullPointerException(statName + " is not a valid statistic name!");
}
private Statistic getStatistic(String statName) {
try {
return Statistic.valueOf(statName.toUpperCase());
}
catch (IllegalArgumentException | NullPointerException exception) {
logWarnings(exception);
return null;
}
} }
public Statistic.Type getStatType(String statName) { public Statistic.Type getStatType(String statName) {
try { try {
return Statistic.valueOf(statName).getType(); return Statistic.valueOf(statName.toUpperCase()).getType();
} }
catch (IllegalArgumentException | NullPointerException exception) { catch (IllegalArgumentException | NullPointerException exception) {
exception.printStackTrace(); logWarnings(exception);
return null; return null;
} }
} }
@ -72,4 +117,13 @@ public class StatManager {
public List<String> getSubStatEntryNames() { public List<String> getSubStatEntryNames() {
return subStatEntryNames; return subStatEntryNames;
} }
public void logWarnings(Exception exception) {
if (exception instanceof IllegalArgumentException) {
plugin.getLogger().warning("IllegalArgumentException - this is probably not a valid statistic name!");
}
else if (exception instanceof NullPointerException) {
plugin.getLogger().warning("NullPointerException - no statistic name was provided");
}
}
} }

View File

@ -27,39 +27,24 @@ public class StatCommand implements CommandExecutor {
if (args.length >= 2) { if (args.length >= 2) {
String statName = null; String statName = null;
String blockName = null; String subStatEntry = null;
String itemName = null;
String entityName = null;
Material block = null;
Material item = null;
EntityType entity = null;
String playerName = null; String playerName = null;
boolean playerFlag = false; boolean playerFlag = false;
//all args are in lowercase //all args are in lowercase
for (String arg : args) { for (String arg : args) {
if (statManager.isStatistic(arg)) { if (statManager.isStatistic(arg)) {
statName = arg; statName = (statName == null) ? arg : statName;
} }
else if (enumHandler.isBlock(arg)) { else if (statManager.isSubStatistic(arg)) {
blockName = arg;
}
else if (enumHandler.isItem(arg)) {
itemName = arg;
}
else if (enumHandler.isEntityType(arg)) {
if (arg.equalsIgnoreCase("player")) { if (arg.equalsIgnoreCase("player")) {
if (!playerFlag) { if (!playerFlag) {
entityName = (entityName == null) ? arg : entityName; subStatEntry = (subStatEntry == null) ? arg : subStatEntry;
playerFlag = true; playerFlag = true;
} }
else {
entityName = arg;
}
} }
else { else {
entityName = arg; subStatEntry = (subStatEntry == null || playerFlag) ? arg : subStatEntry;
} }
} }
@ -67,29 +52,18 @@ public class StatCommand implements CommandExecutor {
playerName = sender.getName(); playerName = sender.getName();
} }
else if (OfflinePlayerHandler.isOfflinePlayer(arg)) { else if (OfflinePlayerHandler.isOfflinePlayer(arg)) {
playerName = arg; playerName = (playerName == null) ? arg : playerName;
} }
} }
if (playerName != null && statName != null) { if (playerName != null && statName != null) {
switch (statManager.getStatType(statName)) { try {
case UNTYPED: sender.sendMessage(OutputFormatter.formatPlayerStat(playerName, statName, subStatEntry,
sender.sendMessage(OutputFormatter.formatPlayerStat(playerName, statName, OfflinePlayerHandler.getOfflinePlayer(playerName).getStatistic(stat))); statManager.getStatistic(statName, subStatEntry, playerName)));
break;
case BLOCK:
if (block != null) {
sender.sendMessage(statName + " " + block + " for " + playerName + ": " + OfflinePlayerHandler.getOfflinePlayer(playerName).getStatistic(stat, block));
}
break;
case ITEM:
if (item != null) {
sender.sendMessage(statName + " " + item + " for " + playerName + ": " + OfflinePlayerHandler.getOfflinePlayer(playerName).getStatistic(stat, item));
}
case ENTITY:
if (entity != null) {
sender.sendMessage(statName + " " + entity + " for " + playerName + ": " + OfflinePlayerHandler.getOfflinePlayer(playerName).getStatistic(stat, entity));
}
} }
catch (Exception e) {
sender.sendMessage(e.toString());
}
} }
} }
return true; return true;

View File

@ -40,11 +40,11 @@ public class EnumHandler {
return entityTypeNames.contains(entityName.toLowerCase()); return entityTypeNames.contains(entityName.toLowerCase());
} }
//returns EntityType enum constant (uppercase) if the input name is valid, otherwise null (param: entityName in uppercase) //returns EntityType enum constant (uppercase) if the input name is valid, otherwise null (param: entityName, not case sensitive)
public EntityType getEntityType(String entityName) { public EntityType getEntityType(String entityName) {
EntityType entityType = null; EntityType entityType = null;
try { try {
entityType = EntityType.valueOf(entityName); entityType = EntityType.valueOf(entityName.toUpperCase());
} }
catch (IllegalArgumentException | NullPointerException exception) { catch (IllegalArgumentException | NullPointerException exception) {
exception.printStackTrace(); exception.printStackTrace();
@ -70,4 +70,5 @@ public class EnumHandler {
public List<String> getBlockNames() { public List<String> getBlockNames() {
return blockNames; return blockNames;
} }
} }

View File

@ -11,14 +11,13 @@ public class OutputFormatter {
} }
public static String formatPlayerStat(String playerName, String statName, int stat) { public static String formatPlayerStat(String playerName, String statName, int stat) {
return ChatColor.GOLD + playerName + ChatColor.WHITE + ": " + stat + " " + return formatPlayerStat(playerName, statName, null, stat);
ChatColor.AQUA + statName.toLowerCase().replace("_", " ");
} }
public static String formatPlayerStat(String playerName, String statName, String subStatEntryName, int stat) { public static String formatPlayerStat(String playerName, String statName, String subStatEntryName, int stat) {
String subStat = subStatEntryName != null ? ChatColor.BLUE + " (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
return ChatColor.GOLD + playerName + ChatColor.WHITE + ": " + stat + " " + return ChatColor.GOLD + playerName + ChatColor.WHITE + ": " + stat + " " +
ChatColor.AQUA + statName.toLowerCase().replace("_", " ") + ChatColor.AQUA + statName.toLowerCase().replace("_", " ") + subStat;
ChatColor.BLUE + " (" + subStatEntryName + ")";
} }
} }