mirror of
https://github.com/darksaid98/AlathranWars.git
synced 2026-12-02 06:28:52 +00:00
refactor: rename main package
This commit is contained in:
87 files changed
No files matched your search
@@ -0,0 +1,127 @@
|
||||
package com.github.alathra.alathranwars.commands;
|
||||
|
||||
import com.github.alathra.alathranwars.AlathranWars;
|
||||
import com.github.alathra.alathranwars.hooks.NameColorHandler;
|
||||
import com.github.alathra.alathranwars.items.WarItemRegistry;
|
||||
import com.github.alathra.alathranwars.utility.UtilsChat;
|
||||
import com.github.milkdrinkers.colorparser.ColorParser;
|
||||
import dev.jorel.commandapi.CommandAPIBukkit;
|
||||
import dev.jorel.commandapi.CommandAPICommand;
|
||||
import dev.jorel.commandapi.arguments.ArgumentSuggestions;
|
||||
import dev.jorel.commandapi.arguments.IntegerArgument;
|
||||
import dev.jorel.commandapi.arguments.PlayerArgument;
|
||||
import dev.jorel.commandapi.arguments.StringArgument;
|
||||
import dev.jorel.commandapi.executors.CommandArguments;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class AdminCommands {
|
||||
public AdminCommands() {
|
||||
new CommandAPICommand("alathranwarsadmin")
|
||||
.withAliases("awa", "wa", "waradmin")
|
||||
.withPermission("AlathranWars.admin")
|
||||
.withSubcommands(
|
||||
commandItem(),
|
||||
commandWar(),
|
||||
commandSiege(),
|
||||
commandNames()
|
||||
)
|
||||
.executes((sender, args) -> {
|
||||
if (args.count() == 0)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "Invalid Arguments.").build());
|
||||
})
|
||||
.register();
|
||||
}
|
||||
|
||||
private CommandAPICommand commandItem() {
|
||||
return new CommandAPICommand("item")
|
||||
.withArguments(
|
||||
new StringArgument("item")
|
||||
.replaceSuggestions(
|
||||
ArgumentSuggestions.strings(
|
||||
CommandUtil.getWarItems()
|
||||
)
|
||||
),
|
||||
new IntegerArgument("amount").setOptional(true),
|
||||
new PlayerArgument("player").setOptional(true)
|
||||
)
|
||||
.executesPlayer((Player sender, CommandArguments args) -> {
|
||||
// Parse item
|
||||
if (!(args.get("item") instanceof String argItem))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>Invalid item argument.").build());
|
||||
|
||||
@Nullable ItemStack stack;
|
||||
if (argItem.contains(AlathranWars.getInstance().getName().toLowerCase())) {
|
||||
stack = WarItemRegistry.getInstance().getOrNullNamespace(argItem);
|
||||
} else {
|
||||
stack = WarItemRegistry.getInstance().getOrNull(argItem);
|
||||
}
|
||||
|
||||
if (stack == null)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>The item is not an AlathranWars item.").build());
|
||||
|
||||
// Parse amount
|
||||
int argAmount = (int) args.getOptional("amount").orElse(1);
|
||||
|
||||
if (argAmount > 64)
|
||||
argAmount = 64;
|
||||
else if (argAmount < 1) {
|
||||
argAmount = 1;
|
||||
}
|
||||
stack.setAmount(argAmount);
|
||||
|
||||
// Determine who gets the item
|
||||
Player argPlayer = (Player) args.getOptional("player").orElse(sender);
|
||||
|
||||
// Give
|
||||
argPlayer.getInventory().addItem(stack);
|
||||
|
||||
final @Nullable Component itemName = stack.getItemMeta() == null ? ColorParser.of(stack.toString()).build() : stack.getItemMeta().displayName();
|
||||
sender.sendMessage(
|
||||
ColorParser.of(
|
||||
"<prefix>Gave <amount>x <item> to <player>."
|
||||
)
|
||||
.parseMinimessagePlaceholder("prefix", UtilsChat.getPrefix())
|
||||
.parseMinimessagePlaceholder("amount", String.valueOf(stack.getAmount()))
|
||||
.parseMinimessagePlaceholder("item", itemName)
|
||||
.parseMinimessagePlaceholder("player", argPlayer.getName())
|
||||
.build()
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private CommandAPICommand commandWar() {
|
||||
return new CommandAPICommand("war")
|
||||
.withSubcommands(
|
||||
WarCommands.commandCreate(true),
|
||||
WarCommands.commandDelete(true),
|
||||
WarCommands.commandJoin(true),
|
||||
WarCommands.commandJoinNear(),
|
||||
WarCommands.commandSurrender(true),
|
||||
WarCommands.commandList(),
|
||||
WarCommands.commandInfo(),
|
||||
WarCommands.commandKick()
|
||||
);
|
||||
}
|
||||
|
||||
private CommandAPICommand commandSiege() {
|
||||
return new CommandAPICommand("siege")
|
||||
.withSubcommands(
|
||||
SiegeCommands.commandStart(true),
|
||||
SiegeCommands.commandStop(true),
|
||||
SiegeCommands.commandAbandon(true),
|
||||
SiegeCommands.commandSurrender(true),
|
||||
SiegeCommands.commandList()
|
||||
);
|
||||
}
|
||||
|
||||
private CommandAPICommand commandNames() {
|
||||
return new CommandAPICommand("updatenames")
|
||||
.executesPlayer((player, commandArguments) -> {
|
||||
Bukkit.getOnlinePlayers().forEach(p -> NameColorHandler.getInstance().calculatePlayerColors(p));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.github.alathra.alathranwars.commands;
|
||||
|
||||
import com.github.alathra.alathranwars.AlathranWars;
|
||||
import dev.jorel.commandapi.CommandAPI;
|
||||
import dev.jorel.commandapi.CommandAPIBukkitConfig;
|
||||
|
||||
public class CommandManager {
|
||||
private final AlathranWars instance;
|
||||
|
||||
public CommandManager(AlathranWars instance) {
|
||||
this.instance = instance;
|
||||
}
|
||||
|
||||
public void onLoad() {
|
||||
CommandAPI.onLoad(new CommandAPIBukkitConfig(instance).shouldHookPaperReload(true).silentLogs(true));
|
||||
}
|
||||
|
||||
public void onEnable() {
|
||||
CommandAPI.onEnable();
|
||||
|
||||
// Register commands
|
||||
new WarCommands();
|
||||
new SiegeCommands();
|
||||
new AdminCommands();
|
||||
}
|
||||
|
||||
public void onDisable() {
|
||||
CommandAPI.onDisable();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,669 @@
|
||||
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.hooks.TownyHook;
|
||||
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.Statistic;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class CommandUtil {
|
||||
/*public Argument<World> customWorldArgument(String nodeName) {
|
||||
|
||||
// Construct our CustomArgument that takes in a String input and returns a World object
|
||||
return new CustomArgument<World, String>(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<Nation> 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() + "<red>The nation does not exist!").parseLegacy().build());
|
||||
|
||||
return TownyAPI.getInstance().getNation(argNationName);
|
||||
}).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> {
|
||||
final @NotNull List<String> nationNames = TownyHook.getNations()
|
||||
.stream()
|
||||
.map(Nation::getName)
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return nationNames;
|
||||
}));
|
||||
}
|
||||
|
||||
static public Argument<Town> 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() + "<red>The town does not exist!").parseLegacy().build());
|
||||
|
||||
return TownyAPI.getInstance().getTown(argTownName);
|
||||
}).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> {
|
||||
final @NotNull List<String> townNames = TownyHook.getTowns()
|
||||
.stream()
|
||||
.map(Town::getName)
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return townNames;
|
||||
}));
|
||||
}
|
||||
|
||||
static public Argument<Nation> 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() + "<red>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.getSurrenderedNations().contains(nation))
|
||||
return nation;
|
||||
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>The nation is already at war!").parseLegacy().build());
|
||||
}).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> {
|
||||
final @NotNull List<String> nationNames = TownyHook.getNations()
|
||||
.stream()
|
||||
.map(Nation::getName)
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
final @NotNull List<String> townNames = TownyHook.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<String> removeNames = Stream.concat( // Create a list of all nations in war
|
||||
war.getNations().stream().map(Nation::getName),
|
||||
war.getSurrenderedNations().stream().map(Nation::getName)
|
||||
).toList();
|
||||
|
||||
nationNames.removeAll(removeNames);
|
||||
|
||||
return nationNames;
|
||||
}));
|
||||
}
|
||||
|
||||
static public Argument<Town> 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() + "<red>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.getSurrenderedTowns().contains(town))
|
||||
return town;
|
||||
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>The town is already at war!").parseLegacy().build());
|
||||
}).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> {
|
||||
final @NotNull List<String> townNames = TownyHook.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<String> removeNames = Stream.concat( // Create a list of all nations & towns in war
|
||||
war.getTowns().stream().map(Town::getName),
|
||||
war.getSurrenderedTowns().stream().map(Town::getName)
|
||||
).toList();
|
||||
|
||||
townNames.removeAll(removeNames);
|
||||
|
||||
return townNames;
|
||||
}));
|
||||
}
|
||||
|
||||
static public Argument<Nation> 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() + "<red>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() + "<red>The nation does not exist!").parseLegacy().build());
|
||||
|
||||
if (!war.isNationInWar(nation))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>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.getSurrenderedNations().stream().map(Nation::getName)
|
||||
).sorted().toList();
|
||||
}));
|
||||
}
|
||||
|
||||
static public Argument<Town> 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() + "<red>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() + "<red>The town does not exist!").parseLegacy().build());
|
||||
|
||||
if (!war.isTownInWar(town))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>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.getSurrenderedTowns().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<UUID> 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() + "<red>The town or nation <name> does not exist!")
|
||||
.parseMinimessagePlaceholder("name", argTownOrNationName)
|
||||
.build()
|
||||
);
|
||||
}).replaceSuggestions(ArgumentSuggestions.stringCollection(info -> {
|
||||
final @NotNull List<String> nationNames = TownyHook.getNations()
|
||||
.stream()
|
||||
.map(Nation::getName)
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
final @NotNull List<String> townNames = TownyHook.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<String> removeNames = Stream.concat( // Create a list of all nations & towns in war
|
||||
war.getNations().stream().map(Nation::getName),
|
||||
Stream.concat(
|
||||
war.getSurrenderedNations().stream().map(Nation::getName),
|
||||
Stream.concat(
|
||||
war.getTowns().stream().map(Town::getName),
|
||||
war.getSurrenderedTowns().stream().map(Town::getName)
|
||||
)
|
||||
)
|
||||
).toList();
|
||||
|
||||
nationNames.removeAll(removeNames);
|
||||
|
||||
return nationNames;
|
||||
}));
|
||||
}
|
||||
|
||||
public static Argument<Town> 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() + "<red>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() + "<red>The town does not exist!").parseLegacy().build());
|
||||
|
||||
if (!war.isTownInWar(town))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>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() + "<red>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<String> townNames = new ArrayList<>();
|
||||
|
||||
if (info.sender() instanceof Player p) {
|
||||
if (war.getSide1().isPlayerOnSide(p)) {
|
||||
townNames.addAll(war.getSide2().getTowns()
|
||||
.stream()
|
||||
.map(TownyObject::getName)
|
||||
.sorted()
|
||||
.toList()
|
||||
);
|
||||
} else if (war.getSide2().isPlayerOnSide(p)) {
|
||||
townNames.addAll(war.getSide1().getTowns()
|
||||
.stream()
|
||||
.map(TownyObject::getName)
|
||||
.sorted()
|
||||
.toList()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return townNames;
|
||||
}));
|
||||
}
|
||||
|
||||
public static Argument<Siege> 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() + "<red>The siege <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() + "<red>The player does not exist!").build());
|
||||
|
||||
if (!siege.isPlayerInSiege(player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>The player is already in that war.").build());
|
||||
} else {
|
||||
if (!(info.sender() instanceof Player player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>Only players can execute this command!").build());
|
||||
|
||||
if (!siege.isPlayerInSiege(player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>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() + "<red>The player does not exist!").build());
|
||||
|
||||
if (siege.isPlayerInSiege(player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>The player is not in that war.").build());
|
||||
} else {
|
||||
if (!(info.sender() instanceof Player player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>Only players can execute this command!").build());
|
||||
|
||||
if (siege.isPlayerInSiege(player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>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<String> siegeAll(SuggestionInfo<CommandSender> info, final boolean isAdmin) {
|
||||
return WarController.getInstance().getSieges().stream().map(Siege::getName).toList();
|
||||
}
|
||||
|
||||
private static List<String> siegeOutside(SuggestionInfo<CommandSender> info, final boolean isAdmin, final String playerNodeName) {
|
||||
List<String> siegeNames = new ArrayList<>();
|
||||
|
||||
if (isAdmin) {
|
||||
if (info.previousArgs().get(playerNodeName) instanceof Player player) {
|
||||
siegeNames = WarController.getInstance().getSieges().stream().filter(siege -> !siege.isPlayerInSiege(player)).map(Siege::getName).toList();
|
||||
}
|
||||
} else {
|
||||
if (info.sender() instanceof Player player) {
|
||||
siegeNames = WarController.getInstance().getSieges().stream().filter(siege -> !siege.isPlayerInSiege(player)).map(Siege::getName).toList();
|
||||
}
|
||||
}
|
||||
|
||||
return siegeNames;
|
||||
}
|
||||
|
||||
private static List<String> siegeInside(SuggestionInfo<CommandSender> info, final boolean isAdmin, final String playerNodeName) {
|
||||
List<String> siegeNames = new ArrayList<>();
|
||||
|
||||
if (isAdmin) {
|
||||
if (info.previousArgs().get(playerNodeName) instanceof Player player) {
|
||||
siegeNames = WarController.getInstance().getSieges().stream().filter(siege -> siege.isPlayerInSiege(player)).map(Siege::getName).toList();
|
||||
}
|
||||
} else {
|
||||
if (info.sender() instanceof Player player) {
|
||||
siegeNames = WarController.getInstance().getSieges().stream().filter(siege -> siege.isPlayerInSiege(player)).map(Siege::getName).toList();
|
||||
}
|
||||
}
|
||||
|
||||
return siegeNames;
|
||||
}
|
||||
|
||||
public static Argument<War> 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() + "<red>The war <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() + "<red>The player does not exist!").parseLegacy().build());
|
||||
|
||||
if (!war.isPlayerInWar(player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>The player is already in that war.").parseLegacy().build());
|
||||
} else {
|
||||
if (!(info.sender() instanceof Player player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>Only players can execute this command!").parseLegacy().build());
|
||||
|
||||
if (!war.isPlayerInWar(player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>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() + "<red>The player does not exist!").parseLegacy().build());
|
||||
|
||||
if (war.isPlayerInWar(player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>The player is not in that war.").parseLegacy().build());
|
||||
} else {
|
||||
if (!(info.sender() instanceof Player player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>Only players can execute this command!").parseLegacy().build());
|
||||
|
||||
if (war.isPlayerInWar(player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>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<String> warsAll(SuggestionInfo<CommandSender> info, final boolean isAdmin) {
|
||||
return WarController.getInstance().getWarNames();
|
||||
}
|
||||
|
||||
private static List<String> warsOutside(SuggestionInfo<CommandSender> info, final boolean isAdmin, final String playerNodeName) {
|
||||
List<String> warNames = new ArrayList<>();
|
||||
|
||||
if (isAdmin) {
|
||||
if (info.previousArgs().get(playerNodeName) instanceof Player player) {
|
||||
warNames = WarController.getInstance().getWars().stream().filter(war -> !war.isPlayerInWar(player)).map(War::getName).toList();
|
||||
}
|
||||
} else {
|
||||
if (info.sender() instanceof Player player) {
|
||||
warNames = WarController.getInstance().getWars().stream().filter(war -> !war.isPlayerInWar(player)).map(War::getName).toList();
|
||||
}
|
||||
}
|
||||
|
||||
return warNames;
|
||||
}
|
||||
|
||||
private static List<String> warsInside(SuggestionInfo<CommandSender> info, final boolean isAdmin, final String playerNodeName) {
|
||||
List<String> warNames = new ArrayList<>();
|
||||
|
||||
if (isAdmin) {
|
||||
if (info.previousArgs().get(playerNodeName) instanceof Player player) {
|
||||
warNames = WarController.getInstance().getWars().stream().filter(war -> war.isPlayerInWar(player)).map(War::getName).toList();
|
||||
}
|
||||
} else {
|
||||
if (info.sender() instanceof Player player) {
|
||||
warNames = WarController.getInstance().getWars().stream().filter(war -> war.isPlayerInWar(player)).map(War::getName).toList();
|
||||
}
|
||||
}
|
||||
|
||||
return warNames;
|
||||
}
|
||||
|
||||
public static Argument<Side> 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() + "<red>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() + "<red>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() + "<red>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() + "<red>The player does not exist!").parseLegacy().build());
|
||||
|
||||
if (side.getPlayers().contains(player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>The player is already on that side.").parseLegacy().build());
|
||||
} else {
|
||||
if (!(info.sender() instanceof Player player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>Only players can execute this command!").parseLegacy().build());
|
||||
|
||||
if (side.getPlayers().contains(player))
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>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<String> sideNames = new ArrayList<>();
|
||||
|
||||
if (checkIfIn) {
|
||||
if (isAdmin) {
|
||||
if (info.previousArgs().get(playerNodeName) instanceof Player player) {
|
||||
for (@NotNull Side side : war.getSides()) {
|
||||
if (!side.getPlayers().contains(player))
|
||||
sideNames.add(side.getName());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (info.sender() instanceof Player player) {
|
||||
for (@NotNull Side side : war.getSides()) {
|
||||
if (!side.getPlayers().contains(player))
|
||||
sideNames.add(side.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sideNames.add(war.getSide1().getName());
|
||||
sideNames.add(war.getSide2().getName());
|
||||
}
|
||||
|
||||
return sideNames;
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
public static Argument<String> 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() + "<red>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.getPlayers().stream().map(Player::getName).toList().contains(argTargetName))
|
||||
return argTargetName;
|
||||
|
||||
throw CustomArgument.CustomArgumentException.fromAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>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<String> inWar = Stream.concat(
|
||||
war.getPlayers().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<String> players = Bukkit.getOnlinePlayers().stream().map(Player::getName).toList();
|
||||
final List<String> towns = TownyAPI.getInstance().getTowns().stream().map(Town::getName).toList();
|
||||
final List<String> 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<String> getWarItems() {
|
||||
return new ArrayList<>(WarItemRegistry.getInstance().getItemRegistry().keySet());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
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.battle.BattleVictoryReason;
|
||||
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.Resident;
|
||||
import com.palmergames.bukkit.towny.object.Town;
|
||||
import dev.jorel.commandapi.CommandAPIBukkit;
|
||||
import dev.jorel.commandapi.CommandAPICommand;
|
||||
import dev.jorel.commandapi.arguments.PlayerArgument;
|
||||
import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException;
|
||||
import dev.jorel.commandapi.executors.CommandArguments;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
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.Set;
|
||||
|
||||
import static com.github.alathra.alathranwars.enums.CommandArgsSiege.ALL_SIEGES;
|
||||
import static com.github.alathra.alathranwars.enums.CommandArgsSiege.IN_SIEGE;
|
||||
import static com.github.alathra.alathranwars.enums.CommandArgsWar.ALL_WARS;
|
||||
import static com.github.alathra.alathranwars.enums.CommandArgsWar.IN_WAR;
|
||||
|
||||
public class SiegeCommands {
|
||||
public SiegeCommands() {
|
||||
new CommandAPICommand("siege")
|
||||
.withSubcommands(
|
||||
commandStart(false),
|
||||
commandStop(false),
|
||||
commandAbandon(false),
|
||||
commandSurrender(false),
|
||||
commandList()
|
||||
)
|
||||
.executesPlayer((sender, args) -> {
|
||||
if (args.count() == 0)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "Invalid Arguments. /siege help").build());
|
||||
})
|
||||
.register();
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandStart(boolean asAdmin) {
|
||||
return new CommandAPICommand("start")
|
||||
// .withPermission("AlathranWars.admin")
|
||||
.withArguments(
|
||||
CommandUtil.warWarArgument(
|
||||
"war",
|
||||
asAdmin,
|
||||
asAdmin ? ALL_WARS : IN_WAR,
|
||||
""
|
||||
),
|
||||
CommandUtil.customSiegeAttackableTownArgument(
|
||||
"town",
|
||||
"war",
|
||||
false,
|
||||
true
|
||||
),
|
||||
new PlayerArgument("leader")
|
||||
.setOptional(!asAdmin)
|
||||
.withPermission("AlathranWars.admin")/*,
|
||||
new BooleanArgument("minutemen")
|
||||
.setOptional(true)
|
||||
.withPermission("AlathranWars.admin")*/
|
||||
)
|
||||
.executesPlayer((Player p, CommandArguments args) -> siegeStart(p, args, asAdmin));
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandStop(boolean asAdmin) {
|
||||
return new CommandAPICommand("stop")
|
||||
.withPermission("AlathranWars.admin")
|
||||
.withArguments(
|
||||
CommandUtil.siegeSiegeArgument("siege", asAdmin, asAdmin ? ALL_SIEGES : IN_SIEGE, "player")
|
||||
)
|
||||
.executesPlayer((Player p, CommandArguments args) -> siegeStop(p, args, asAdmin));
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandAbandon(boolean asAdmin) {
|
||||
return new CommandAPICommand("abandon")
|
||||
.withArguments(
|
||||
CommandUtil.siegeSiegeArgument("siege", asAdmin, asAdmin ? ALL_SIEGES : IN_SIEGE, "player")
|
||||
)
|
||||
.executesPlayer((Player p, CommandArguments args) -> siegeAbandon(p, args, asAdmin));
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandSurrender(boolean asAdmin) {
|
||||
return new CommandAPICommand("surrender")
|
||||
.withArguments(
|
||||
CommandUtil.siegeSiegeArgument("siege", asAdmin, asAdmin ? ALL_SIEGES : IN_SIEGE, "player")
|
||||
)
|
||||
.executesPlayer((Player p, CommandArguments args) -> siegeSurrender(p, args, asAdmin));
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandList() {
|
||||
return new CommandAPICommand("list")
|
||||
.executesPlayer(SiegeCommands::siegeList);
|
||||
}
|
||||
|
||||
protected static void siegeStart(@NotNull Player sender, @NotNull CommandArguments args, boolean asAdmin) throws WrapperCommandSyntaxException {
|
||||
if (!(args.get("war") instanceof final @NotNull War war))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You need to specify a war.").build());
|
||||
|
||||
if (!(args.get("town") instanceof final @NotNull Town town))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You need to specify a town.").build());
|
||||
|
||||
final @NotNull Player siegeLeader = (Player) args.getOptional("leader").orElse(sender);
|
||||
|
||||
// Player participance check
|
||||
@Nullable Side side = war.getPlayerSide(siegeLeader);
|
||||
if (side == null)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You are not in this war.").build());
|
||||
|
||||
if (side.isPlayerSurrendered(siegeLeader))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You have surrendered and cannot participate in this war.").build());
|
||||
|
||||
if (side.isSiegeGraceActive() && !asAdmin)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(
|
||||
ColorParser.of("<red>Your side needs to wait <time> minutes before starting another siege.")
|
||||
.parseMinimessagePlaceholder("time", String.valueOf(side.getSiegeGraceCooldown().toMinutes()))
|
||||
.build()
|
||||
);
|
||||
|
||||
// Attacking own side
|
||||
if (side.isTownOnSide(town) && !side.isTownSurrendered(town)) {
|
||||
if (asAdmin)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You cannot attack your own side.").build());
|
||||
else
|
||||
siegeLeader.sendMessage(ColorParser.of("<red>You cannot attack your own towns.").build());
|
||||
return;
|
||||
}
|
||||
|
||||
final Location location = siegeLeader.getLocation();
|
||||
final Location townLocation = town.getSpawnOrNull();
|
||||
if (townLocation == null)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>The town you are attacking doesn't have a spawn point set. Contact admin.").build());
|
||||
|
||||
if (!location.getWorld().equals(townLocation.getWorld()))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You need to be in the same world as the town you're attacking!").build());
|
||||
|
||||
if (location.distance(townLocation) >= Siege.BATTLEFIELD_START_MAX_RANGE && !asAdmin)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You need to be within <range> blocks of the town to start a siege!").parseMinimessagePlaceholder("range", String.valueOf(Siege.BATTLEFIELD_START_MAX_RANGE)).build());
|
||||
|
||||
if (location.distance(townLocation) <= Siege.BATTLEFIELD_START_MIN_RANGE && !asAdmin)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You need to be further than <range> blocks away from the town to start a siege!").parseMinimessagePlaceholder("range", String.valueOf(Siege.BATTLEFIELD_START_MIN_RANGE)).build());
|
||||
|
||||
if ((AlathranWars.econ.getBalance(siegeLeader) < Siege.SIEGE_VICTORY_MONEY) && !asAdmin)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You need to have $<amount> to start a siege!").parseMinimessagePlaceholder("amount", String.valueOf(Siege.SIEGE_VICTORY_MONEY)).build());
|
||||
|
||||
side.setSiegeGrace();
|
||||
Siege siege = new Siege(war, town, siegeLeader);
|
||||
|
||||
war.addSiege(siege);
|
||||
|
||||
Bukkit.broadcast(
|
||||
ColorParser.of(
|
||||
"<prefix>The town of <town> has been put to siege by <side>!"
|
||||
)
|
||||
.parseMinimessagePlaceholder("prefix", UtilsChat.getPrefix())
|
||||
.parseMinimessagePlaceholder("town", town.getName())
|
||||
.parseMinimessagePlaceholder("side", siege.getAttackerSide().getName())
|
||||
.build()
|
||||
);
|
||||
|
||||
siege.start();
|
||||
}
|
||||
|
||||
private static void siegeStop(@NotNull Player sender, @NotNull CommandArguments args, boolean asAdmin) throws WrapperCommandSyntaxException {
|
||||
if (!(args.get("siege") instanceof final @NotNull Siege siege))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Invalid siege.").build());
|
||||
|
||||
siege.equalWin(BattleVictoryReason.ADMIN_COMMAND);
|
||||
}
|
||||
|
||||
private static void siegeAbandon(Player p, @NotNull CommandArguments args, boolean asAdmin) throws WrapperCommandSyntaxException {
|
||||
if (!(args.get("siege") instanceof final @NotNull Siege siege))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Invalid siege.").build());
|
||||
|
||||
@NotNull OfflinePlayer siegeLeader = siege.getSiegeLeader();
|
||||
if (siegeLeader != p && !asAdmin)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You are not the leader of this siege.").build());
|
||||
|
||||
siege.defendersWin(BattleVictoryReason.OPPONENT_RETREAT);
|
||||
}
|
||||
|
||||
private static void siegeSurrender(Player p, @NotNull CommandArguments args, boolean asAdmin) throws WrapperCommandSyntaxException {
|
||||
if (!(args.get("siege") instanceof final @NotNull Siege siege))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Invalid siege.").build());
|
||||
|
||||
Town town = siege.getTown();
|
||||
|
||||
@Nullable Resident res = TownyAPI.getInstance().getResident(p);
|
||||
if (res == null)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Resident invalid.").build());
|
||||
|
||||
final boolean canKingSurrender = (res.hasNation() && town.hasNation() && res.getNationOrNull().equals(town.getNationOrNull()) && res.isKing());
|
||||
final boolean canMayorSurrender = (res.hasTown() && res.getTownOrNull().equals(town) && res.isMayor());
|
||||
|
||||
if (!asAdmin && !canKingSurrender && !canMayorSurrender)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You cannot surrender this town.").build());
|
||||
|
||||
siege.attackersWin(BattleVictoryReason.OPPONENT_RETREAT);
|
||||
}
|
||||
|
||||
private static void siegeList(@NotNull Player sender, CommandArguments args) throws WrapperCommandSyntaxException {
|
||||
Set<Siege> sieges = WarController.getInstance().getSieges();
|
||||
|
||||
if (sieges.isEmpty()) {
|
||||
sender.sendMessage(ColorParser.of("<red>There are no sieges at the moment.").build());
|
||||
return;
|
||||
}
|
||||
|
||||
final StringBuilder msg = new StringBuilder();
|
||||
|
||||
for (Siege siege : sieges) {
|
||||
msg.append("\n<white><bold>%s<reset>".formatted(siege.getName()));
|
||||
msg.append("\n <grey>Progress: <white>%.0f%%".formatted(siege.getSiegeProgressPercentage() * 100));
|
||||
msg.append("\n <grey>Remaining: <white>%s minutes".formatted(String.valueOf(Duration.between(Instant.now(), siege.getEndTime()).toMinutesPart())));
|
||||
msg.append("\n");
|
||||
}
|
||||
|
||||
sender.sendMessage(
|
||||
ColorParser.of(
|
||||
msg.toString()
|
||||
).build()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,500 @@
|
||||
package com.github.alathra.alathranwars.commands;
|
||||
|
||||
import com.github.alathra.alathranwars.conflict.war.War;
|
||||
import com.github.alathra.alathranwars.conflict.war.WarBuilder;
|
||||
import com.github.alathra.alathranwars.conflict.war.WarController;
|
||||
import com.github.alathra.alathranwars.conflict.war.side.Side;
|
||||
import com.github.alathra.alathranwars.conflict.war.side.SideCreationException;
|
||||
import com.github.alathra.alathranwars.hooks.NameColorHandler;
|
||||
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 dev.jorel.commandapi.CommandAPIBukkit;
|
||||
import dev.jorel.commandapi.CommandAPICommand;
|
||||
import dev.jorel.commandapi.arguments.ArgumentSuggestions;
|
||||
import dev.jorel.commandapi.arguments.BooleanArgument;
|
||||
import dev.jorel.commandapi.arguments.GreedyStringArgument;
|
||||
import dev.jorel.commandapi.arguments.PlayerArgument;
|
||||
import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException;
|
||||
import dev.jorel.commandapi.executors.CommandArguments;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
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.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.github.alathra.alathranwars.enums.CommandArgsWar.*;
|
||||
|
||||
public class WarCommands {
|
||||
public WarCommands() {
|
||||
new CommandAPICommand("war")
|
||||
.withSubcommands(
|
||||
commandCreate(false),
|
||||
commandDelete(false),
|
||||
commandJoin(false),
|
||||
commandSurrender(false),
|
||||
commandList(),
|
||||
commandInfo()
|
||||
)
|
||||
.executesPlayer((sender, args) -> {
|
||||
if (args.count() == 0)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "Invalid Arguments. /war help").build());
|
||||
})
|
||||
.register();
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandCreate(boolean asAdmin) {
|
||||
return new CommandAPICommand("create")
|
||||
.withPermission("AlathranWars.admin")
|
||||
.withArguments(
|
||||
CommandUtil.customTownAndNationArgument("side1", "war", false),
|
||||
CommandUtil.customTownAndNationArgument("side2", "war", false),
|
||||
new BooleanArgument("event"),
|
||||
new GreedyStringArgument("warlabel")
|
||||
.replaceSuggestions(
|
||||
ArgumentSuggestions.stringCollection(info -> List.of(
|
||||
"Nations War of Conquest",
|
||||
"Nations War for Survival",
|
||||
"Nations War of Liberation"
|
||||
))
|
||||
)
|
||||
)
|
||||
.executesPlayer(WarCommands::warCreate);
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandDelete(boolean asAdmin) {
|
||||
return new CommandAPICommand("delete")
|
||||
.withPermission("AlathranWars.admin")
|
||||
.withArguments(
|
||||
CommandUtil.warWarArgument("war", asAdmin, ALL_WARS, "")
|
||||
)
|
||||
.executesPlayer(WarCommands::warDelete);
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandJoin(boolean asAdmin) {
|
||||
return new CommandAPICommand("join")
|
||||
.withArguments(
|
||||
CommandUtil.warWarArgument("war", asAdmin, asAdmin ? ALL_WARS : OUT_WAR, ""),
|
||||
CommandUtil.warSideCreateArgument("side", "war", asAdmin, !asAdmin, ""),
|
||||
CommandUtil.warTargetCreateArgument("target", "war", asAdmin).setOptional(!asAdmin)
|
||||
// new PlayerArgument("player").setOptional(true).withPermission("AlathranWars.admin.join"),
|
||||
// new PlayerArgument("nation").setOptional(true).withPermission("AlathranWars.admin.join"),
|
||||
// new PlayerArgument("town").setOptional(true).withPermission("AlathranWars.admin.join")
|
||||
)
|
||||
.executesPlayer((Player p, CommandArguments args) -> warJoin(p, args, asAdmin));
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandJoinNear() {
|
||||
return new CommandAPICommand("joinnear")
|
||||
.withPermission("AlathranWars.admin")
|
||||
.withArguments(
|
||||
CommandUtil.warWarArgument("war", true, ALL_WARS, ""),
|
||||
CommandUtil.warSideCreateArgument("side", "war", true, false, "")
|
||||
)
|
||||
.executesPlayer((Player p, CommandArguments args) -> warJoinNear(p, args, true));
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandSurrender(boolean asAdmin) {
|
||||
return new CommandAPICommand("surrender")
|
||||
.withArguments(
|
||||
CommandUtil.warWarArgument("war", asAdmin, asAdmin ? ALL_WARS : IN_WAR, "player"),
|
||||
new PlayerArgument("player")
|
||||
.setOptional(true)
|
||||
.withPermission("AlathranWars.admin")
|
||||
)
|
||||
.executesPlayer((Player p, CommandArguments args) -> warSurrender(p, args, false));
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandList() {
|
||||
return new CommandAPICommand("list")
|
||||
.executesPlayer(WarCommands::warList);
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandInfo() {
|
||||
return new CommandAPICommand("info")
|
||||
.withArguments(
|
||||
CommandUtil.warWarArgument("war", false, ALL_WARS, "player")
|
||||
)
|
||||
.executesPlayer(WarCommands::warInfo);
|
||||
}
|
||||
|
||||
public static CommandAPICommand commandKick() {
|
||||
return new CommandAPICommand("kick")
|
||||
.withArguments(
|
||||
CommandUtil.warWarArgument("war", true, ALL_WARS, "player"),
|
||||
new PlayerArgument("player")
|
||||
.setOptional(true)
|
||||
.withPermission("AlathranWars.admin")
|
||||
)
|
||||
.executesPlayer(WarCommands::warKick);
|
||||
}
|
||||
|
||||
protected static void warCreate(CommandSender p, @NotNull CommandArguments args) throws WrapperCommandSyntaxException {
|
||||
if (!(args.get("warlabel") instanceof final String argLabel))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>You need to specify a war label.").build());
|
||||
|
||||
if (Objects.equals(args.get("side1"), args.get("side2")))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>Cannot declare war on oneself.").build());
|
||||
|
||||
final boolean event = (boolean) args.getOrDefault("event", false);
|
||||
|
||||
final UUID uuid1 = (UUID) args.get("side1");
|
||||
final UUID uuid2 = (UUID) args.get("side2");
|
||||
|
||||
// Create new war depending on what argument was specified
|
||||
@Nullable Town town1 = TownyAPI.getInstance().getTown(uuid1);
|
||||
@Nullable Town town2 = TownyAPI.getInstance().getTown(uuid2);
|
||||
|
||||
// Automatically escalate to nation level if possible
|
||||
@Nullable Nation nation1 = town1 == null ? TownyAPI.getInstance().getNation(uuid1) : town1.getNationOrNull();
|
||||
@Nullable Nation nation2 = town2 == null ? TownyAPI.getInstance().getNation(uuid2) : town2.getNationOrNull();
|
||||
|
||||
// Implement permission checking here if this is ever going to be exposed to players
|
||||
|
||||
// Create war depending on what args have been passed, attempt to create nation wars first
|
||||
WarBuilder builder = new WarBuilder()
|
||||
.setUuid(UUID.randomUUID())
|
||||
.setLabel(argLabel)
|
||||
.setEvent(event);
|
||||
if (nation1 != null) {
|
||||
if (nation2 != null) { // Nation vs Nation
|
||||
// Allow civil wars
|
||||
if (nation2.getUUID() != nation1.getUUID()) {
|
||||
builder
|
||||
.setAggressor(nation1)
|
||||
.setVictim(nation2);
|
||||
} else {
|
||||
builder
|
||||
.setAggressor(town1)
|
||||
.setVictim(town2);
|
||||
}
|
||||
} else if (town2 != null) { // Nation vs Town
|
||||
builder
|
||||
.setAggressor(nation1)
|
||||
.setVictim(town2);
|
||||
}
|
||||
} else if (town1 != null) {
|
||||
if (town2 != null) { // Town vs Town
|
||||
builder
|
||||
.setAggressor(town1)
|
||||
.setVictim(town2);
|
||||
} else if (nation2 != null) { // Nation vs Town
|
||||
builder
|
||||
.setAggressor(town1)
|
||||
.setVictim(nation2);
|
||||
}
|
||||
}
|
||||
try {
|
||||
builder.create(); // TODO Add war to list
|
||||
} catch (SideCreationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void warDelete(Player p, @NotNull CommandArguments args) throws WrapperCommandSyntaxException {
|
||||
War war = (War) args.get("war");
|
||||
if (war == null) return;
|
||||
|
||||
war.draw();
|
||||
}
|
||||
|
||||
protected static void warJoin(Player p, @NotNull CommandArguments args, boolean asAdmin) throws WrapperCommandSyntaxException {
|
||||
War war = (War) args.get("war");
|
||||
if (war == null) return;
|
||||
|
||||
if (!(args.get("side") instanceof final Side side))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>You need to specify a side.").build());
|
||||
|
||||
// The target or empty string
|
||||
if (!(args.getOptional("target").orElse("") instanceof String targetString))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>Invalid target name.").build());
|
||||
|
||||
// The target player or sender of the command
|
||||
Player targetPlayer = args.getOptional("target").map(o -> Bukkit.getPlayer((String) o)).orElse(p);
|
||||
|
||||
// Get the player resident
|
||||
@Nullable Resident res = TownyAPI.getInstance().getResident(targetPlayer);
|
||||
if (res == null)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Resident invalid.").build());
|
||||
|
||||
// Get nations and towns
|
||||
@Nullable Nation nation = res.hasNation() ? res.getNationOrNull() : TownyAPI.getInstance().getNation(targetString);
|
||||
@Nullable Town town = res.hasTown() ? res.getTownOrNull() : TownyAPI.getInstance().getTown(targetString);
|
||||
|
||||
final boolean isArgNation = nation != null;
|
||||
final boolean isArgTown = town != null;
|
||||
final boolean isArgPlayer = targetPlayer != null;
|
||||
|
||||
// If no valid targets
|
||||
if ((!isArgNation && !isArgTown && !isArgPlayer))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Invalid target.").build());
|
||||
|
||||
// Check if player can validly join or not
|
||||
final boolean canKingJoin = (res.hasNation() && res.getNationOrNull().equals(nation) && res.isKing() && nation.isKing(res));
|
||||
final boolean canMayorJoin = (res.hasTown() && res.getTownOrNull().equals(town) && res.isMayor() && town.isMayor(res));
|
||||
|
||||
if (!war.isEventWar()) {
|
||||
// Join nation into war
|
||||
if (isArgNation && nation != null && !war.isNationInWar(nation) && (asAdmin || canKingJoin)) {
|
||||
side.addNation(nation);
|
||||
nation.getResidents().stream().filter(Resident::isOnline).map(Resident::getPlayer).toList().forEach(player -> NameColorHandler.getInstance().calculatePlayerColors(player));
|
||||
Bukkit.broadcast(
|
||||
ColorParser.of(
|
||||
"<prefix>The nation of <nation> joined the war of <war> on the side of <side>."
|
||||
)
|
||||
.parseMinimessagePlaceholder("prefix", UtilsChat.getPrefix())
|
||||
.parseMinimessagePlaceholder("nation", nation.getName())
|
||||
.parseMinimessagePlaceholder("war", war.getLabel())
|
||||
.parseMinimessagePlaceholder("side", side.getName())
|
||||
.build()
|
||||
);
|
||||
final Title warTitle = Title.title(
|
||||
ColorParser.of("<gradient:#D72A09:#B01F03><u><b>War")
|
||||
.build(),
|
||||
ColorParser.of("<gray><i>You entered the war of <war>!")
|
||||
.parseMinimessagePlaceholder("war", war.getLabel())
|
||||
.build(),
|
||||
Title.Times.times(Duration.ofMillis(500), Duration.ofMillis(3500), Duration.ofMillis(500))
|
||||
);
|
||||
final Sound warSound = Sound.sound(Key.key("entity.wither.spawn"), Sound.Source.VOICE, 0.5f, 1.0F);
|
||||
|
||||
nation.getResidents().stream().filter(Resident::isOnline).map(Resident::getPlayer).toList().forEach(player -> {
|
||||
player.showTitle(warTitle);
|
||||
player.playSound(warSound);
|
||||
});
|
||||
return;
|
||||
} else if (town == null && nation != null && (!canKingJoin || !asAdmin)) {
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Nation is already in that war.").build());
|
||||
}
|
||||
|
||||
// Join town into war
|
||||
if (isArgTown && town != null && !war.isTownInWar(town) && (asAdmin || canMayorJoin)) {
|
||||
side.addTown(town);
|
||||
town.getResidents().stream().filter(Resident::isOnline).map(Resident::getPlayer).toList().forEach(player -> NameColorHandler.getInstance().getPlayerNameColor(player));
|
||||
Bukkit.broadcast(
|
||||
ColorParser.of(
|
||||
"<prefix>The town of <town> joined the war of <war> on the side of <side>."
|
||||
)
|
||||
.parseMinimessagePlaceholder("prefix", UtilsChat.getPrefix())
|
||||
.parseMinimessagePlaceholder("town", town.getName())
|
||||
.parseMinimessagePlaceholder("war", war.getLabel())
|
||||
.parseMinimessagePlaceholder("side", side.getName())
|
||||
.build()
|
||||
);
|
||||
final Title warTitle = Title.title(
|
||||
ColorParser.of("<gradient:#D72A09:#B01F03><u><b>War")
|
||||
.build(),
|
||||
ColorParser.of("<gray><i>You entered the war of <war>!")
|
||||
.parseMinimessagePlaceholder("war", war.getLabel())
|
||||
.build(),
|
||||
Title.Times.times(Duration.ofMillis(500), Duration.ofMillis(3500), Duration.ofMillis(500))
|
||||
);
|
||||
final Sound warSound = Sound.sound(Key.key("entity.wither.spawn"), Sound.Source.VOICE, 0.5f, 1.0F);
|
||||
|
||||
town.getResidents().stream().filter(Resident::isOnline).map(Resident::getPlayer).toList().forEach(player -> {
|
||||
player.showTitle(warTitle);
|
||||
player.playSound(warSound);
|
||||
});
|
||||
return;
|
||||
} else if (targetPlayer == null && town != null && !canMayorJoin) {
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Town is already in that war.").build());
|
||||
}
|
||||
}
|
||||
|
||||
// Join player into war
|
||||
if (isArgPlayer && targetPlayer != null && !war.isPlayerInWar(targetPlayer) && asAdmin) {
|
||||
targetPlayer.sendMessage(ColorParser.of(UtilsChat.getPrefix() + "You have joined the war.").build());
|
||||
side.addPlayer(targetPlayer);
|
||||
NameColorHandler.getInstance().calculatePlayerColors(targetPlayer);
|
||||
final Title warTitle = Title.title(
|
||||
ColorParser.of("<gradient:#D72A09:#B01F03><u><b>War")
|
||||
.build(),
|
||||
ColorParser.of("<gray><i>You entered the war of <war>!")
|
||||
.parseMinimessagePlaceholder("war", war.getLabel())
|
||||
.build(),
|
||||
Title.Times.times(Duration.ofMillis(500), Duration.ofMillis(3500), Duration.ofMillis(500))
|
||||
);
|
||||
final Sound warSound = Sound.sound(Key.key("entity.wither.spawn"), Sound.Source.VOICE, 0.5f, 1.0F);
|
||||
|
||||
targetPlayer.showTitle(warTitle);
|
||||
targetPlayer.playSound(warSound);
|
||||
return;
|
||||
} else if (!asAdmin) {
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Players cannot individually join wars.").build());
|
||||
} else {
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Player is already in that war.").build());
|
||||
}
|
||||
|
||||
// Join player into war
|
||||
// if (war.isPlayerInWar(player))
|
||||
// throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You are already in this war.").build());
|
||||
|
||||
|
||||
/*for (@NotNull Siege siege : war.getSieges()) {
|
||||
if (siege.getAttackerSide().equals(side)) {
|
||||
siege.addPlayer(argPlayer, BattleSide.ATTACKER);
|
||||
} else {
|
||||
siege.addPlayer(argPlayer, BattleSide.DEFENDER);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
protected static void warJoinNear(@NotNull Player p, @NotNull CommandArguments args, boolean asAdmin) throws WrapperCommandSyntaxException {
|
||||
War war = (War) args.get("war");
|
||||
if (war == null) return;
|
||||
|
||||
if (!(args.get("side") instanceof final Side side))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of(UtilsChat.getPrefix() + "<red>You need to specify a side.").build());
|
||||
|
||||
final Location location = p.getLocation();
|
||||
|
||||
for (Player targetPlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (p.equals(targetPlayer)) continue;
|
||||
final Location targetLocation = targetPlayer.getLocation();
|
||||
if (!location.getWorld().equals(targetLocation.getWorld())) continue;
|
||||
if (location.distance(targetLocation) > 25D) continue;
|
||||
if (war.isPlayerInWar(targetPlayer)) continue;
|
||||
|
||||
targetPlayer.sendMessage(ColorParser.of(UtilsChat.getPrefix() + "You have joined the war.").build());
|
||||
side.addPlayer(targetPlayer);
|
||||
NameColorHandler.getInstance().calculatePlayerColors(targetPlayer);
|
||||
final Title warTitle = Title.title(
|
||||
ColorParser.of("<gradient:#D72A09:#B01F03><u><b>War")
|
||||
.build(),
|
||||
ColorParser.of("<gray><i>You entered the war of <war>!")
|
||||
.parseMinimessagePlaceholder("war", war.getLabel())
|
||||
.build(),
|
||||
Title.Times.times(Duration.ofMillis(500), Duration.ofMillis(3500), Duration.ofMillis(500))
|
||||
);
|
||||
final Sound warSound = Sound.sound(Key.key("entity.wither.spawn"), Sound.Source.VOICE, 0.5f, 1.0F);
|
||||
|
||||
targetPlayer.showTitle(warTitle);
|
||||
targetPlayer.playSound(warSound);
|
||||
}
|
||||
}
|
||||
|
||||
protected static void warSurrender(@NotNull Player p, @NotNull CommandArguments args, boolean asAdmin) throws WrapperCommandSyntaxException {
|
||||
War war = (War) args.get("war");
|
||||
if (war == null) return;
|
||||
|
||||
if (!(args.getOptional("player").orElse(p) instanceof Player argPlayer))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Player not found.").build());
|
||||
|
||||
// Towny Resident Object
|
||||
@Nullable Resident res = TownyAPI.getInstance().getResident(argPlayer);
|
||||
if (res == null)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Resident invalid.").build());
|
||||
|
||||
// Town check
|
||||
@Nullable Town town = res.getTownOrNull();
|
||||
if (town == null)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You are not in a town.").build());
|
||||
|
||||
@Nullable Side side = war.getTownSide(town);
|
||||
if (side == null)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You are not in that war.").build());
|
||||
|
||||
final @Nullable Nation resNation = res.getNationOrNull();
|
||||
final @Nullable Town resTown = res.getTownOrNull();
|
||||
|
||||
if (resNation != null && (p.hasPermission("AlathranWars.nationsurrender") || res.isKing() || asAdmin)) {
|
||||
// Has nation surrender permission
|
||||
argPlayer.sendMessage(ColorParser.of(UtilsChat.getPrefix() + "You have surrendered the war for " + resNation.getName() + ".").build());
|
||||
Bukkit.broadcast(ColorParser.of(UtilsChat.getPrefix() + "The nation of " + resNation.getName() + " has surrendered!").build());
|
||||
war.surrenderNation(resNation);
|
||||
} else if (resTown == null) {
|
||||
// Cannot surrender nation involvement
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You cannot surrender for your nation.").build());
|
||||
}
|
||||
|
||||
if (resTown != null && (p.hasPermission("AlathranWars.townsurrender") || res.isMayor() || asAdmin)) {
|
||||
// Is in indepdenent town & has surrender perms
|
||||
argPlayer.sendMessage(ColorParser.of(UtilsChat.getPrefix() + "You have surrendered the war for " + resTown.getName() + ".").build());
|
||||
Bukkit.broadcast(ColorParser.of(UtilsChat.getPrefix() + "The town of " + resTown.getName() + " has surrendered!").build());
|
||||
war.cancelSieges(resTown);
|
||||
war.surrenderTown(resTown);
|
||||
} else {
|
||||
// No perms
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>You cannot surrender war for your town.").build());
|
||||
}
|
||||
}
|
||||
|
||||
protected static void warKick(@NotNull Player p, @NotNull CommandArguments args) throws WrapperCommandSyntaxException {
|
||||
War war = (War) args.get("war");
|
||||
if (war == null) return;
|
||||
|
||||
if (!(args.getOptional("player").orElse(p) instanceof Player argPlayer))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Player not found.").build());
|
||||
|
||||
if (!war.isPlayerInWar(argPlayer))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>The player is not in that war.").build());
|
||||
|
||||
@Nullable Side side = war.getPlayerSide(argPlayer);
|
||||
if (side == null)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>The player is not in that war.").build());
|
||||
|
||||
p.sendMessage(ColorParser.of("Target yeeted.").build());
|
||||
side.removePlayer(argPlayer);
|
||||
|
||||
NameColorHandler.getInstance().calculatePlayerColors(argPlayer);
|
||||
}
|
||||
|
||||
private static void warList(@NotNull Player p, CommandArguments args) throws WrapperCommandSyntaxException {
|
||||
Set<War> wars = WarController.getInstance().getWars();
|
||||
|
||||
if (wars.isEmpty()) {
|
||||
p.sendMessage(ColorParser.of("<red>There are no wars at the moment, the world is at peace.").build());
|
||||
return;
|
||||
}
|
||||
|
||||
final StringBuilder msg = new StringBuilder();
|
||||
|
||||
for (War war : wars) {
|
||||
msg.append("\n<white><bold>%s<reset>".formatted(war.getLabel()));
|
||||
msg.append("\n <grey>Sides: <white>%s <grey>vs <white>%s".formatted(war.getSide1().getName(), war.getSide2().getName()));
|
||||
msg.append("\n <grey>Score: <white>%s <grey>vs <white>%s".formatted(war.getSide1().getScore(), war.getSide2().getScore()));
|
||||
msg.append("\n");
|
||||
}
|
||||
|
||||
p.sendMessage(
|
||||
ColorParser.of(
|
||||
msg.toString()
|
||||
).build()
|
||||
);
|
||||
}
|
||||
|
||||
private static void warInfo(Player p, CommandArguments args) throws WrapperCommandSyntaxException {
|
||||
War war = (War) args.get("war");
|
||||
if (war == null) return;
|
||||
|
||||
final StringBuilder msg = new StringBuilder();
|
||||
|
||||
for (Side side : war.getSides()) {
|
||||
msg.append("\n<white><bold>%s<reset>".formatted(side.getName()));
|
||||
msg.append("\n <grey>Score: <green>%s".formatted(side.getScore()));
|
||||
msg.append("\n <grey>Nations: <green>%s<grey>/<red>%s".formatted(side.getNations().size(), side.getNations().size() + side.getSurrenderedNations().size()));
|
||||
msg.append("\n <grey>Towns: <green>%s<grey>/<red>%s".formatted(side.getTowns().size(), side.getTowns().size() + side.getSurrenderedTowns().size()));
|
||||
msg.append("\n <grey>Players: <green>%s<grey>/<red>%s".formatted(side.getPlayersIncludingOffline().size(), side.getPlayersIncludingOffline().size() + side.getSurrenderedPlayersIncludingOffline().size()));
|
||||
msg.append("\n");
|
||||
}
|
||||
|
||||
p.sendMessage(
|
||||
ColorParser.of(
|
||||
msg.toString()
|
||||
).build()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user