diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java index 1698fda..a5fc856 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java @@ -12,7 +12,7 @@ public class Main extends JavaPlugin { ConfigHandler config = new ConfigHandler(this); 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").setTabCompleter(new TabCompleter(enumHandler, statManager,this)); this.getLogger().info("Enabled PlayerStats!"); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/StatManager.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/StatManager.java index 38e7261..ddd6007 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/StatManager.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/StatManager.java @@ -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.OfflinePlayerHandler; +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; import org.bukkit.Statistic; +import org.bukkit.entity.EntityType; import java.util.ArrayList; import java.util.Arrays; @@ -11,12 +14,14 @@ import java.util.stream.Collectors; public class StatManager { + private final Main plugin; private final EnumHandler enumHandler; private final List statNames; private final List entityStatNames; private final List subStatEntryNames; - public StatManager(EnumHandler e) { + public StatManager(EnumHandler e, Main p) { + plugin = p; enumHandler = e; 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) - public int getStatistic(Statistic stat, String playerName) { - return OfflinePlayerHandler.getOfflinePlayer(playerName).getStatistic(stat); + public int getStatistic(String statName, String playerName) throws IllegalArgumentException, NullPointerException { + 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) { try { - return Statistic.valueOf(statName).getType(); + return Statistic.valueOf(statName.toUpperCase()).getType(); } catch (IllegalArgumentException | NullPointerException exception) { - exception.printStackTrace(); + logWarnings(exception); return null; } } @@ -72,4 +117,13 @@ public class StatManager { public List getSubStatEntryNames() { 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"); + } + } } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java index a140787..e57c2a6 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java @@ -27,39 +27,24 @@ public class StatCommand implements CommandExecutor { if (args.length >= 2) { String statName = null; - String blockName = null; - String itemName = null; - String entityName = null; - Material block = null; - Material item = null; - EntityType entity = null; + String subStatEntry = null; String playerName = null; boolean playerFlag = false; //all args are in lowercase for (String arg : args) { if (statManager.isStatistic(arg)) { - statName = arg; + statName = (statName == null) ? arg : statName; } - else if (enumHandler.isBlock(arg)) { - blockName = arg; - } - else if (enumHandler.isItem(arg)) { - itemName = arg; - } - - else if (enumHandler.isEntityType(arg)) { + else if (statManager.isSubStatistic(arg)) { if (arg.equalsIgnoreCase("player")) { if (!playerFlag) { - entityName = (entityName == null) ? arg : entityName; + subStatEntry = (subStatEntry == null) ? arg : subStatEntry; playerFlag = true; } - else { - entityName = arg; - } } else { - entityName = arg; + subStatEntry = (subStatEntry == null || playerFlag) ? arg : subStatEntry; } } @@ -67,29 +52,18 @@ public class StatCommand implements CommandExecutor { playerName = sender.getName(); } else if (OfflinePlayerHandler.isOfflinePlayer(arg)) { - playerName = arg; + playerName = (playerName == null) ? arg : playerName; } } if (playerName != null && statName != null) { - switch (statManager.getStatType(statName)) { - case UNTYPED: - sender.sendMessage(OutputFormatter.formatPlayerStat(playerName, statName, OfflinePlayerHandler.getOfflinePlayer(playerName).getStatistic(stat))); - 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)); - } - + try { + sender.sendMessage(OutputFormatter.formatPlayerStat(playerName, statName, subStatEntry, + statManager.getStatistic(statName, subStatEntry, playerName))); } + catch (Exception e) { + sender.sendMessage(e.toString()); + } + } } return true; diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java index 2849c42..2074569 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java @@ -40,11 +40,11 @@ public class EnumHandler { 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) { EntityType entityType = null; try { - entityType = EntityType.valueOf(entityName); + entityType = EntityType.valueOf(entityName.toUpperCase()); } catch (IllegalArgumentException | NullPointerException exception) { exception.printStackTrace(); @@ -70,4 +70,5 @@ public class EnumHandler { public List getBlockNames() { return blockNames; } + } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OutputFormatter.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OutputFormatter.java index 5d56e9e..e62bf1b 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OutputFormatter.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OutputFormatter.java @@ -11,14 +11,13 @@ public class OutputFormatter { } public static String formatPlayerStat(String playerName, String statName, int stat) { - return ChatColor.GOLD + playerName + ChatColor.WHITE + ": " + stat + " " + - ChatColor.AQUA + statName.toLowerCase().replace("_", " "); + return formatPlayerStat(playerName, statName, null, 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 + " " + - ChatColor.AQUA + statName.toLowerCase().replace("_", " ") + - ChatColor.BLUE + " (" + subStatEntryName + ")"; + ChatColor.AQUA + statName.toLowerCase().replace("_", " ") + subStat; } }