From b28a1ffd40bc70c2be6224941eccfa73ad746d2f Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Thu, 5 May 2022 22:40:56 +0200 Subject: [PATCH] Finished tabComplete and started implementing statCommand --- .../gr8/playerstats/commands/StatCommand.java | 52 +++++++++++-- .../playerstats/commands/TabCompleter.java | 28 +++---- .../gr8/playerstats/utils/EnumHandler.java | 78 +++++++++++++++++++ .../utils/OfflinePlayerHandler.java | 3 +- 4 files changed, 134 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java 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 d3d4396..1f29f26 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 @@ -1,21 +1,61 @@ package com.gmail.artemis.the.gr8.playerstats.commands; +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.Statistic; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; public class StatCommand implements CommandExecutor { @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command,String label, String[] args) { + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + if (args.length >= 2) { - if (label.equalsIgnoreCase("statistic")) { - sender.sendMessage("hello"); + Statistic stat = null; + Material block = null; + Material item = null; + EntityType entity = null; + String playerName = null; + boolean playerFlag = false; + boolean doublePlayerFlag = false; + + for (String arg : args) { + if (EnumHandler.getStatNames().contains(arg)) { + stat = EnumHandler.getStatistic(arg.toUpperCase()); + } + else if (EnumHandler.getBlockNames().contains(arg)) { + block = EnumHandler.getBlock(arg); + } + else if (EnumHandler.getItemNames().contains(arg)) { + item = EnumHandler.getItem(arg); + } + else if (EnumHandler.getEntityNames().contains(arg)) { + entity = EnumHandler.getEntityType(arg); + } + else if (arg.equalsIgnoreCase("me") && sender instanceof Player) { + playerName = sender.getName(); + } + else if (arg.equalsIgnoreCase("player")) { + if (!playerFlag) playerFlag = true; + else doublePlayerFlag = true; + } + else if (OfflinePlayerHandler.getAllOfflinePlayerNames().stream().anyMatch(arg::equalsIgnoreCase)) { + playerName = arg; + } + } + if (playerName != null && stat != null) { + if (stat.getType() == Statistic.Type.UNTYPED) { + sender.sendMessage(stat + " for " + playerName + ": " + OfflinePlayerHandler.getOfflinePlayer(playerName).getStatistic(stat)); + } + } - return true; } - sender.sendMessage("bye"); - return false; + return true; } } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/TabCompleter.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/TabCompleter.java index f92a6f5..713d110 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/TabCompleter.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/TabCompleter.java @@ -1,16 +1,14 @@ package com.gmail.artemis.the.gr8.playerstats.commands; import com.gmail.artemis.the.gr8.playerstats.Main; +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.Statistic; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -23,7 +21,6 @@ public class TabCompleter implements org.bukkit.command.TabCompleter { private final List itemNames; private final List statNames; private final List subStatNames; - private final List playerNames; public TabCompleter(Main p) { plugin = p; @@ -33,17 +30,11 @@ public class TabCompleter implements org.bukkit.command.TabCompleter { commandOptions.add("player"); commandOptions.add("me"); - blockNames = Arrays.stream(Material.values()).filter(Material::isBlock).map(Material::toString).map(String::toLowerCase).toList(); - entityNames = Arrays.stream(EntityType.values()).map(EntityType::toString).map(String::toLowerCase).toList(); - itemNames = Arrays.stream(Material.values()).filter(Material::isItem).map(Material::toString).map(String::toLowerCase).toList(); - statNames = Arrays.stream(Statistic.values()).map(Statistic::toString).map(String::toLowerCase).toList(); - - subStatNames = new ArrayList<>(); - subStatNames.addAll(blockNames); - subStatNames.addAll(entityNames); - subStatNames.addAll(itemNames); - - playerNames = OfflinePlayerHandler.getAllOfflinePlayerNames(); + blockNames = EnumHandler.getBlockNames(); + entityNames = EnumHandler.getEntityNames(); + itemNames = EnumHandler.getItemNames(); + statNames = EnumHandler.getStatNames(); + subStatNames = EnumHandler.getSubStatNames(); } //args[0] = statistic (length = 1) @@ -52,12 +43,11 @@ public class TabCompleter implements org.bukkit.command.TabCompleter { //args[3] = playerName (length = 4) @Override - public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, String label, String[] args) { + public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { List tabSuggestions = new ArrayList<>(); //after typing "stat", suggest a list of viable statistics - if ((label.equalsIgnoreCase("statistic") || command.getAliases().contains(label)) && args.length >= 1) { - + if (args.length >= 1) { if (args.length == 1) { tabSuggestions = statNames.stream().filter(stat -> stat.contains(args[0].toLowerCase())).collect(Collectors.toList()); } @@ -92,7 +82,7 @@ public class TabCompleter implements org.bukkit.command.TabCompleter { //if previous arg = "player", suggest playerNames else if (args[args.length-2].equalsIgnoreCase("player")) { - tabSuggestions = playerNames.stream().filter(player -> player.toLowerCase().contains(args[args.length-1].toLowerCase())).collect(Collectors.toList()); + tabSuggestions = OfflinePlayerHandler.getAllOfflinePlayerNames().stream().filter(player -> player.toLowerCase().contains(args[args.length-1].toLowerCase())).collect(Collectors.toList()); } //after a substatistic, suggest commandOptions 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 new file mode 100644 index 0000000..e5ce90f --- /dev/null +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java @@ -0,0 +1,78 @@ +package com.gmail.artemis.the.gr8.playerstats.utils; + +import org.bukkit.Material; +import org.bukkit.Statistic; +import org.bukkit.entity.EntityType; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class EnumHandler { + + private EnumHandler() { + } + + //returns corresponding item enum constant, otherwise null (param: itemName, can be lowercase) + public static Material getItem(String itemName) { + return Material.matchMaterial(itemName); + } + + //returns all item names in lowercase + public static List getItemNames() { + return Arrays.stream(Material.values()).filter(Material::isItem).map(Material::toString).map(String::toLowerCase).toList(); + } + + //returns EntityType enum constant if the input name is valid, otherwise null (param: entityName in uppercase) + public static EntityType getEntityType(String entityName) { + EntityType entityType = null; + try { + entityType = EntityType.valueOf(entityName); + } + catch (IllegalArgumentException | NullPointerException exception) { + exception.printStackTrace(); + } + return entityType; + } + + //returns all entitytype names in lowercase + public static List getEntityNames() { + return Arrays.stream(EntityType.values()).map(EntityType::toString).map(String::toLowerCase).toList(); + } + + //returns corresponding block enum constant, otherwise null (param: materialName, can be lowercase) + public static Material getBlock(String materialName) { + return Material.matchMaterial(materialName); + } + + //returns all block names in lowercase + public static List getBlockNames() { + return Arrays.stream(Material.values()).filter(Material::isBlock).map(Material::toString).map(String::toLowerCase).toList(); + } + + //returns Statistic enum constant if the input name is valid, otherwise null (param: statName in uppercase) + public static Statistic getStatistic(String statName) { + Statistic stat = null; + try { + stat = Statistic.valueOf(statName); + } + catch (IllegalArgumentException | NullPointerException exception) { + exception.printStackTrace(); + } + return stat; + } + + //returns the names of all general statistics in lowercase + public static List getStatNames() { + return Arrays.stream(Statistic.values()).map(Statistic::toString).map(String::toLowerCase).toList(); + } + + //returns all substatnames in lowercase + public static List getSubStatNames() { + List subStatNames = new ArrayList<>(); + subStatNames.addAll(getBlockNames()); + subStatNames.addAll(getEntityNames()); + subStatNames.addAll(getItemNames()); + return subStatNames; + } +} diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java index d3b8f5d..f1a3254 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java @@ -3,12 +3,11 @@ package com.gmail.artemis.the.gr8.playerstats.utils; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; -public final class OfflinePlayerHandler { +public class OfflinePlayerHandler { private OfflinePlayerHandler() { }