package com.github.alathra.alathranwars.commands; import com.github.alathra.alathranwars.AlathranWars; import com.github.alathra.alathranwars.conflict.battle.siege.Siege; import com.github.alathra.alathranwars.conflict.war.War; import com.github.alathra.alathranwars.conflict.war.WarController; import com.github.alathra.alathranwars.conflict.war.side.Side; import com.github.alathra.alathranwars.enums.CommandArgsSiege; import com.github.alathra.alathranwars.enums.CommandArgsWar; import com.github.alathra.alathranwars.items.WarItemRegistry; import com.github.alathra.alathranwars.utility.UtilsChat; import com.github.milkdrinkers.colorparser.ColorParser; import com.palmergames.bukkit.towny.TownyAPI; import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.TownyObject; import dev.jorel.commandapi.SuggestionInfo; import dev.jorel.commandapi.arguments.Argument; import dev.jorel.commandapi.arguments.ArgumentSuggestions; import dev.jorel.commandapi.arguments.CustomArgument; import dev.jorel.commandapi.arguments.StringArgument; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.Statistic; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.time.Duration; import java.time.Instant; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class CommandUtil { /*public Argument customWorldArgument(String nodeName) { // Construct our CustomArgument that takes in a String input and returns a World object return new CustomArgument(new StringArgument(nodeName), info -> { // Parse the world from our input World world = Bukkit.getWorld(info.input()); if (world == null) { throw CustomArgumentException.fromMessageBuilder(new MessageBuilder("Unknown world: ").appendArgInput()); } else { return world; } }).replaceSuggestions(ArgumentSuggestions.strings(info -> // List of world names on the server Bukkit.getWorlds().stream().map(World::getName).toArray(String[]::new)) ); }*/ static public Argument customNationArgument(String nodeName) { return new CustomArgument<>(new StringArgument(nodeName), info -> { final String argNationName = info.input(); if (TownyAPI.getInstance().getNation(argNationName) == null) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The nation does not exist!").parseLegacy().build()); return TownyAPI.getInstance().getNation(argNationName); }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> { final @NotNull List nationNames = TownyAPI.getInstance().getNations() .stream() .map(Nation::getName) .sorted() .collect(Collectors.toList()); return nationNames; })); } static public Argument customTownArgument(String nodeName) { return new CustomArgument<>(new StringArgument(nodeName), info -> { final String argTownName = info.input(); if (TownyAPI.getInstance().getTown(argTownName) == null) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The town does not exist!").parseLegacy().build()); return TownyAPI.getInstance().getTown(argTownName); }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> { final @NotNull List townNames = TownyAPI.getInstance().getTowns() .stream() .map(Town::getName) .sorted() .collect(Collectors.toList()); return townNames; })); } static public Argument customNationNotInWarArgument(String nodeName, String warNodeName, boolean hideNationsInWar) { return new CustomArgument<>(new StringArgument(nodeName), info -> { final String argNationName = info.input(); if (TownyAPI.getInstance().getNation(argNationName) == null) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The nation does not exist!").parseLegacy().build()); if (!hideNationsInWar || !(info.previousArgs().get(warNodeName) instanceof War war)) return TownyAPI.getInstance().getNation(argNationName); @Nullable Nation nation = TownyAPI.getInstance().getNation(argNationName); if (war.getNations().contains(nation) || war.getNationsSurrendered().contains(nation)) return nation; throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The nation is already at war!").parseLegacy().build()); }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> { final @NotNull List nationNames = TownyAPI.getInstance().getNations() .stream() .map(Nation::getName) .sorted() .collect(Collectors.toList()); final @NotNull List townNames = TownyAPI.getInstance().getTowns() .stream() .map(Town::getName) .sorted() .toList(); nationNames.addAll(townNames); // Only proceed after this if we want to remove town already in the specified war if (!hideNationsInWar || !(info.previousArgs().get(warNodeName) instanceof War war)) return nationNames; final @NotNull List removeNames = Stream.concat( // Create a list of all nations in war war.getNations().stream().map(Nation::getName), war.getNationsSurrendered().stream().map(Nation::getName) ).toList(); nationNames.removeAll(removeNames); return nationNames; })); } static public Argument customTownNotInWarArgument(String nodeName, String warNodeName, boolean hideTownsInWar) { return new CustomArgument<>(new StringArgument(nodeName), info -> { final String argTownName = info.input(); if (TownyAPI.getInstance().getTown(argTownName) == null) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The town does not exist!").parseLegacy().build()); if (!hideTownsInWar || !(info.previousArgs().get(warNodeName) instanceof War war)) return TownyAPI.getInstance().getTown(argTownName); @Nullable Town town = TownyAPI.getInstance().getTown(argTownName); if (war.getTowns().contains(town) || war.getTownsSurrendered().contains(town)) return town; throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The town is already at war!").parseLegacy().build()); }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> { final @NotNull List townNames = TownyAPI.getInstance().getTowns() .stream() .map(Town::getName) .sorted() .collect(Collectors.toList()); // Only proceed after this if we want to remove town already in the specified war if (!hideTownsInWar || !(info.previousArgs().get(warNodeName) instanceof War war)) return townNames; final @NotNull List removeNames = Stream.concat( // Create a list of all nations & towns in war war.getTowns().stream().map(Town::getName), war.getTownsSurrendered().stream().map(Town::getName) ).toList(); townNames.removeAll(removeNames); return townNames; })); } static public Argument customNationInWarArgument(String nodeName, String warNodeName) { return new CustomArgument<>(new StringArgument(nodeName), info -> { if (!(info.previousArgs().get(warNodeName) instanceof War war)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The war does not exist!").parseLegacy().build()); @Nullable Nation nation = TownyAPI.getInstance().getNation(info.input()); if (nation == null) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The nation does not exist!").parseLegacy().build()); if (!war.isInWar(nation)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The nation is not in that war!").parseLegacy().build()); return nation; }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> { if (!(info.previousArgs().get(warNodeName) instanceof War war)) return Collections.emptyList(); return Stream.concat( // Create a list of all nations & towns in war war.getNations().stream().map(Nation::getName), war.getNationsSurrendered().stream().map(Nation::getName) ).sorted().toList(); })); } static public Argument customTownInWarArgument(String nodeName, String warNodeName) { return new CustomArgument<>(new StringArgument(nodeName), info -> { if (!(info.previousArgs().get(warNodeName) instanceof War war)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The war does not exist!").parseLegacy().build()); @Nullable Town town = TownyAPI.getInstance().getTown(info.input()); if (town == null) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The town does not exist!").parseLegacy().build()); if (!war.isInWar(town)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The town is not in that war!").parseLegacy().build()); return town; }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> { if (!(info.previousArgs().get(warNodeName) instanceof War war)) return Collections.emptyList(); return Stream.concat( // Create a list of all nations & towns in war war.getTowns().stream().map(Town::getName), war.getTownsSurrendered().stream().map(Town::getName) ).sorted().toList(); })); } /** * Accepts all towns and nations as argument. Returns a string. * * @param nodeName the node name * @param warNodeName the war node name * @param hideTownsInWar the hide towns in war * @return the town or nation UUID */ static public Argument customTownAndNationArgument(String nodeName, String warNodeName, boolean hideTownsInWar) { return new CustomArgument<>(new StringArgument(nodeName), info -> { final String argTownOrNationName = info.input(); @Nullable Town town = TownyAPI.getInstance().getTown(argTownOrNationName); if (town != null) return town.getUUID(); @Nullable Nation nation = TownyAPI.getInstance().getNation(argTownOrNationName); if (nation != null) return nation.getUUID(); throw CustomArgument.CustomArgumentException.fromAdventureComponent( ColorParser.of(UtilsChat.getPrefix() + "The town or nation does not exist!") .parseMinimessagePlaceholder("name", argTownOrNationName) .build() ); }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> { final @NotNull List nationNames = TownyAPI.getInstance().getNations() .stream() .map(Nation::getName) .sorted() .collect(Collectors.toList()); final @NotNull List townNames = TownyAPI.getInstance().getTowns() .stream() .map(Town::getName) .sorted() .toList(); nationNames.addAll(townNames); // Only proceed after this if we want to remove town already in the specified war if (!hideTownsInWar || !(info.previousArgs().get(warNodeName) instanceof War war)) return nationNames; final @NotNull List removeNames = Stream.concat( // Create a list of all nations & towns in war war.getNations().stream().map(Nation::getName), Stream.concat( war.getNationsSurrendered().stream().map(Nation::getName), Stream.concat( war.getTowns().stream().map(Town::getName), war.getTownsSurrendered().stream().map(Town::getName) ) ) ).toList(); nationNames.removeAll(removeNames); return nationNames; })); } static public Argument playerOrTownOrNationInWar(String nodeName, String warNodeName) { return new CustomArgument<>(new StringArgument(nodeName), info -> { final String argPlayerOrTownOrNationName = info.input(); if (!(info.previousArgs().get(warNodeName) instanceof War war)) { throw CustomArgument.CustomArgumentException.fromAdventureComponent( ColorParser.of(UtilsChat.getPrefix() + "The war does not exist!") .build() ); } final List townNames = war.getTownsAll().stream().map(Town::getName).sorted().toList(); final List nationNames = war.getNationsAll().stream().map(Nation::getName).sorted().toList(); final List playerNames = war.getPlayersAll().stream() .filter(offlinePlayer -> Instant.ofEpochMilli(offlinePlayer.getLastSeen()).isAfter(Instant.now().minus(Duration.ofDays(60)))) // Remove players that haven't been on in 60 days .filter(p -> p.getName() != null) .map(OfflinePlayer::getName) .sorted() .toList(); if (townNames.contains(argPlayerOrTownOrNationName) && TownyAPI.getInstance().getTown(argPlayerOrTownOrNationName) != null) return TownyAPI.getInstance().getTown(argPlayerOrTownOrNationName).getUUID(); if (nationNames.contains(argPlayerOrTownOrNationName) && TownyAPI.getInstance().getNation(argPlayerOrTownOrNationName) != null) return TownyAPI.getInstance().getNation(argPlayerOrTownOrNationName).getUUID(); if (playerNames.contains(argPlayerOrTownOrNationName)) return Bukkit.getOfflinePlayer(argPlayerOrTownOrNationName).getUniqueId(); throw CustomArgument.CustomArgumentException.fromAdventureComponent( ColorParser.of(UtilsChat.getPrefix() + "The player, town or nation is not in the war!") .parseMinimessagePlaceholder("name", argPlayerOrTownOrNationName) .build() ); }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> { if (!(info.previousArgs().get(warNodeName) instanceof War war)) { return Collections.emptyList(); } final List nationNames = war.getNationsAll().stream().map(Nation::getName).sorted().toList(); final List townNames = war.getTownsAll().stream().map(Town::getName).sorted().toList(); final List playerNames = war.getPlayersAll().stream() .filter(offlinePlayer -> Instant.ofEpochMilli(offlinePlayer.getLastSeen()).isAfter(Instant.now().minus(Duration.ofDays(60)))) // Remove players that haven't been on in 60 days .map(OfflinePlayer::getName) .filter(Objects::nonNull) .sorted() .toList(); final List recommendations = new ArrayList<>(); recommendations.addAll(nationNames); recommendations.addAll(townNames); recommendations.addAll(playerNames); return recommendations; })); } public static Argument customSiegeAttackableTownArgument(String nodeName, String warNodeName, final boolean checkIfIn, final boolean checkIfSieged) { return new CustomArgument<>(new StringArgument(nodeName), info -> { if (!(info.previousArgs().get(warNodeName) instanceof War war)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The war does not exist!").parseLegacy().build()); @Nullable Town town = TownyAPI.getInstance().getTown(info.input()); if (town == null) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The town does not exist!").parseLegacy().build()); if (!war.isInWar(town)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The town is not in that war!").parseLegacy().build()); if (checkIfSieged) { for (@NotNull Siege siege : WarController.getInstance().getSieges()) { if (siege.getTown().getUUID() == town.getUUID()) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The town is already under siege!").parseLegacy().build()); } } return town; }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> { if (!(info.previousArgs().get(warNodeName) instanceof War war)) return Collections.emptyList(); final @NotNull List townNames = new ArrayList<>(); if (info.sender() instanceof Player p) { if (war.getSide1().isOnSide(p)) { townNames.addAll(war.getSide2().getTowns() .stream() .map(TownyObject::getName) .sorted() .toList() ); } else if (war.getSide2().isOnSide(p)) { townNames.addAll(war.getSide1().getTowns() .stream() .map(TownyObject::getName) .sorted() .toList() ); } } return townNames; })); } public static Argument siegeSiegeArgument(String nodeName, final boolean isAdmin, final CommandArgsSiege checkSiege, final String playerNodeName) { return new CustomArgument<>(new StringArgument(nodeName), info -> { final String argSiegeName = info.input(); @Nullable UUID siegeUUID = null; for (Siege siege : WarController.getInstance().getSieges()) { if (siege.getName().equals(argSiegeName)) { siegeUUID = siege.getUUID(); } } final @Nullable Siege siege = WarController.getInstance().getSiege(siegeUUID); if (siege == null) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The siege does not exist!").parseMinimessagePlaceholder("siege", argSiegeName).build()); switch (checkSiege) { case IN_SIEGE -> { if (isAdmin) { if (!(info.previousArgs().get(playerNodeName) instanceof Player player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The player does not exist!").build()); if (!siege.isPlayerParticipating(player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The player is already in that war.").build()); } else { if (!(info.sender() instanceof Player player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "Only players can execute this command!").build()); if (!siege.isPlayerParticipating(player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "You are already in this war.").build()); } } case OUT_SIEGE -> { if (isAdmin) { if (!(info.previousArgs().get(playerNodeName) instanceof Player player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The player does not exist!").build()); if (siege.isPlayerParticipating(player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The player is not in that war.").build()); } else { if (!(info.sender() instanceof Player player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "Only players can execute this command!").build()); if (siege.isPlayerParticipating(player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "You are not in that war.").build()); } } } return siege; }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> switch (checkSiege) { case ALL_SIEGES -> siegeAll(info, isAdmin); case IN_SIEGE -> siegeInside(info, isAdmin, playerNodeName); case OUT_SIEGE -> siegeOutside(info, isAdmin, playerNodeName); })); } private static List siegeAll(SuggestionInfo info, final boolean isAdmin) { return WarController.getInstance().getSieges().stream().map(Siege::getName).toList(); } private static List siegeOutside(SuggestionInfo info, final boolean isAdmin, final String playerNodeName) { List siegeNames = new ArrayList<>(); if (isAdmin) { if (info.previousArgs().get(playerNodeName) instanceof Player player) { siegeNames = WarController.getInstance().getSieges().stream().filter(siege -> !siege.isPlayerParticipating(player)).map(Siege::getName).toList(); } } else { if (info.sender() instanceof Player player) { siegeNames = WarController.getInstance().getSieges().stream().filter(siege -> !siege.isPlayerParticipating(player)).map(Siege::getName).toList(); } } return siegeNames; } private static List siegeInside(SuggestionInfo info, final boolean isAdmin, final String playerNodeName) { List siegeNames = new ArrayList<>(); if (isAdmin) { if (info.previousArgs().get(playerNodeName) instanceof Player player) { siegeNames = WarController.getInstance().getSieges().stream().filter(siege -> siege.isPlayerParticipating(player)).map(Siege::getName).toList(); } } else { if (info.sender() instanceof Player player) { siegeNames = WarController.getInstance().getSieges().stream().filter(siege -> siege.isPlayerParticipating(player)).map(Siege::getName).toList(); } } return siegeNames; } public static Argument warWarArgument(String nodeName, final boolean isAdmin, final CommandArgsWar checkWar, final String playerNodeName) { return new CustomArgument<>(new StringArgument(nodeName), info -> { final String argWarName = info.input(); final @Nullable War war = WarController.getInstance().getWar(argWarName); if (war == null) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The war does not exist!").parseMinimessagePlaceholder("war", argWarName).build()); switch (checkWar) { case IN_WAR -> { if (isAdmin) { if (!(info.previousArgs().get(playerNodeName) instanceof Player player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The player does not exist!").parseLegacy().build()); if (!war.isInWar(player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The player is already in that war.").parseLegacy().build()); } else { if (!(info.sender() instanceof Player player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "Only players can execute this command!").parseLegacy().build()); if (!war.isInWar(player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "You are already in this war.").parseLegacy().build()); } } case OUT_WAR -> { if (isAdmin) { if (!(info.previousArgs().get(playerNodeName) instanceof Player player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The player does not exist!").parseLegacy().build()); if (war.isInWar(player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The player is not in that war.").parseLegacy().build()); } else { if (!(info.sender() instanceof Player player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "Only players can execute this command!").parseLegacy().build()); if (war.isInWar(player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "You are not in that war.").parseLegacy().build()); } } } return war; }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> switch (checkWar) { case ALL_WARS -> warsAll(info, isAdmin); case IN_WAR -> warsInside(info, isAdmin, playerNodeName); case OUT_WAR -> warsOutside(info, isAdmin, playerNodeName); })); } private static List warsAll(SuggestionInfo info, final boolean isAdmin) { return WarController.getInstance().getWarNames(); } private static List warsOutside(SuggestionInfo info, final boolean isAdmin, final String playerNodeName) { List warNames = new ArrayList<>(); if (isAdmin) { if (info.previousArgs().get(playerNodeName) instanceof Player player) { warNames = WarController.getInstance().getWars().stream().filter(war -> !war.isInWar(player)).map(War::getName).toList(); } } else { if (info.sender() instanceof Player player) { warNames = WarController.getInstance().getWars().stream().filter(war -> !war.isInWar(player)).map(War::getName).toList(); } } return warNames; } private static List warsInside(SuggestionInfo info, final boolean isAdmin, final String playerNodeName) { List warNames = new ArrayList<>(); if (isAdmin) { if (info.previousArgs().get(playerNodeName) instanceof Player player) { warNames = WarController.getInstance().getWars().stream().filter(war -> war.isInWar(player)).map(War::getName).toList(); } } else { if (info.sender() instanceof Player player) { warNames = WarController.getInstance().getWars().stream().filter(war -> war.isInWar(player)).map(War::getName).toList(); } } return warNames; } public static Argument warSideCreateArgument(final String nodeName, final String warNodeName, final boolean isAdmin, final boolean checkIfIn, final String playerNodeName) { return new CustomArgument<>(new StringArgument(nodeName), info -> { if (!(info.previousArgs().get(warNodeName) instanceof War war)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "You need to specify a war.").parseLegacy().build()); final String argSideName = info.input(); if (!war.isSideValid(argSideName)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The side does not exist.").parseLegacy().build()); final @Nullable Side side = war.getSide(argSideName); if (side == null) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The side is invalid.").parseLegacy().build()); if (checkIfIn) { if (isAdmin) { if (!(info.previousArgs().get(playerNodeName) instanceof Player player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The player does not exist!").parseLegacy().build()); if (side.getPlayersOnline().contains(player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "The player is already on that side.").parseLegacy().build()); } else { if (!(info.sender() instanceof Player player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "Only players can execute this command!").parseLegacy().build()); if (side.getPlayersOnline().contains(player)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "You are already on that side.").parseLegacy().build()); } } return side; }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> { if (!(info.previousArgs().get(warNodeName) instanceof War war)) return Collections.emptyList(); final @NotNull List sideNames = new ArrayList<>(); if (checkIfIn) { if (isAdmin) { if (info.previousArgs().get(playerNodeName) instanceof Player player) { for (@NotNull Side side : war.getSides()) { if (!side.getPlayersOnline().contains(player)) sideNames.add(side.getName()); } } } else { if (info.sender() instanceof Player player) { for (@NotNull Side side : war.getSides()) { if (!side.getPlayersOnline().contains(player)) sideNames.add(side.getName()); } } } } else { sideNames.add(war.getSide1().getName()); sideNames.add(war.getSide2().getName()); } return sideNames; } )); } public static Argument warTargetCreateArgument(final String nodeName, final String warNodeName, final boolean isAdmin) { return new CustomArgument<>(new StringArgument(nodeName), info -> { final String argTargetName = info.input(); if (!(info.previousArgs().get(warNodeName) instanceof War war)) throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "You need to specify a war.").parseLegacy().build()); final boolean isNation = TownyAPI.getInstance().getNation(argTargetName) != null; final boolean isTown = TownyAPI.getInstance().getTown(argTargetName) != null; final boolean isPlayer = Bukkit.getPlayer(argTargetName) != null; if (isNation && !war.getNations().stream().map(Nation::getName).toList().contains(argTargetName)) return argTargetName; if (isTown && !war.getTowns().stream().map(Town::getName).toList().contains(argTargetName)) return argTargetName; if (isPlayer && !war.getPlayersOnline().stream().map(Player::getName).toList().contains(argTargetName)) return argTargetName; throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "Invalid target.").build()); }).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> { // Returns list of every nation, town and player not in the war if (!(info.previousArgs().get(warNodeName) instanceof War war)) return Collections.emptyList(); // Get all participants in war into list final List inWar = Stream.concat( war.getPlayersOnline().stream().map(Player::getName), Stream.concat( war.getNations().stream().map(Nation::getName), war.getTowns().stream().map(Town::getName) ) ).toList(); // Make list of all nations, towns and player final List players = Bukkit.getOnlinePlayers().stream().map(Player::getName).toList(); final List towns = TownyAPI.getInstance().getTowns().stream().map(Town::getName).toList(); final List nations = TownyAPI.getInstance().getNations().stream().map(Nation::getName).toList(); return Stream.concat( nations.stream(), Stream.concat( towns.stream(), players.stream() ) ) .filter(string -> !inWar.contains(string)) // Remove participants in te war .toList(); } )); } /** * @param player player to check * @return 0, 1, 2 (good to go, insufficient join, insufficient playtime) */ public static int isPlayerMinuteman(String player) { @Nullable Resident res = TownyAPI.getInstance().getResident(player); if (res != null) { //getInstance registration time long regTime = res.getRegistered(); int playTicks = Bukkit.getPlayer(res.getUUID()).getStatistic(Statistic.PLAY_ONE_MINUTE); int out = 0; if (AlathranWars.getInstance().getConfig().getInt("minimumPlayerAge") > 0) { //compare the join date, if they joined less that min age ago it is true if ((System.currentTimeMillis() - regTime) < 86400000L * AlathranWars.getInstance().getConfig().getInt("minimumPlayerAge")) out = 1; } if (AlathranWars.getInstance().getConfig().getInt("minimumPlayTime") > 0) { //check playtime, if its less than min its true if (playTicks < (AlathranWars.getInstance().getConfig().getInt("minimumPlayTime") * (60 * 60 * 20))) out = 2; } return out; } //if the player doesnt exist just return true return 0; } /** * return list of all alathranwars items with namespace * * @return list of all alathranwars items namespaced */ public static @NotNull List getWarItems() { return new ArrayList<>(WarItemRegistry.getInstance().getItemRegistry().keySet()); } }