Finished tabComplete and started implementing statCommand

This commit is contained in:
Artemis-the-gr8 2022-05-05 22:40:56 +02:00
parent bf1914119b
commit b28a1ffd40
4 changed files with 134 additions and 27 deletions

View File

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

View File

@ -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<String> itemNames;
private final List<String> statNames;
private final List<String> subStatNames;
private final List<String> 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<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, String label, String[] args) {
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
List<String> 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

View File

@ -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<String> 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<String> 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<String> 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<String> getStatNames() {
return Arrays.stream(Statistic.values()).map(Statistic::toString).map(String::toLowerCase).toList();
}
//returns all substatnames in lowercase
public static List<String> getSubStatNames() {
List<String> subStatNames = new ArrayList<>();
subStatNames.addAll(getBlockNames());
subStatNames.addAll(getEntityNames());
subStatNames.addAll(getItemNames());
return subStatNames;
}
}

View File

@ -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() {
}