mirror of
https://github.com/darksaid98/AlathranWars.git
synced 2026-12-01 14:38:55 +00:00
feat: player, town, nation kicking
This commit is contained in:
3 files changed
+114
-13
No files matched your search
@@ -22,12 +22,15 @@ 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.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -225,7 +228,6 @@ public class CommandUtil {
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Accepts all towns and nations as argument. Returns a string.
|
||||
*
|
||||
@@ -288,6 +290,64 @@ public class CommandUtil {
|
||||
}));
|
||||
}
|
||||
|
||||
static public Argument<UUID> 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() + "<red>The war does not exist!")
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
final List<String> townNames = war.getTowns().stream().map(Town::getName).sorted().toList();
|
||||
final List<String> nationNames = war.getNations().stream().map(Nation::getName).sorted().toList();
|
||||
final List<String> playerNames = war.getPlayersIncludingOffline().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() + "<red>The player, town or nation <name> 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<String> nationNames = war.getNations().stream().map(Nation::getName).sorted().toList();
|
||||
final List<String> townNames = war.getTowns().stream().map(Town::getName).sorted().toList();
|
||||
final List<String> playerNames = war.getPlayersIncludingOffline().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();
|
||||
|
||||
final List<String> recommendations = new ArrayList<>();
|
||||
recommendations.addAll(nationNames);
|
||||
recommendations.addAll(townNames);
|
||||
recommendations.addAll(playerNames);
|
||||
|
||||
return recommendations;
|
||||
}));
|
||||
}
|
||||
|
||||
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))
|
||||
|
||||
@@ -136,8 +136,7 @@ public class WarCommands {
|
||||
return new CommandAPICommand("kick")
|
||||
.withArguments(
|
||||
CommandUtil.warWarArgument("war", true, ALL_WARS, "player"),
|
||||
new PlayerArgument("player")
|
||||
.setOptional(true)
|
||||
CommandUtil.playerOrTownOrNationInWar("target", "war")
|
||||
.withPermission("AlathranWars.admin")
|
||||
)
|
||||
.executesPlayer(WarCommands::warKick);
|
||||
@@ -437,20 +436,43 @@ public class WarCommands {
|
||||
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());
|
||||
UUID uuid = (UUID) args.get("target");
|
||||
|
||||
if (!war.isPlayerInWar(argPlayer))
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>The player is not in that war.").build());
|
||||
boolean isNation = TownyAPI.getInstance().getNation(uuid) != null;
|
||||
boolean isTown = TownyAPI.getInstance().getTown(uuid) != null;
|
||||
boolean isPlayer = Bukkit.getOfflinePlayer(uuid) != null;
|
||||
|
||||
@Nullable Side side = war.getPlayerSide(argPlayer);
|
||||
if (side == null)
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>The player is not in that war.").build());
|
||||
Nation nation = TownyAPI.getInstance().getNation(uuid);
|
||||
Town town = TownyAPI.getInstance().getTown(uuid);
|
||||
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
|
||||
|
||||
p.sendMessage(ColorParser.of("Target yeeted.").build());
|
||||
side.removePlayer(argPlayer);
|
||||
Side sideNation = war.getNationSide(nation);
|
||||
Side sideTown = war.getTownSide(town);
|
||||
Side sidePlayer = war.getPlayerSide(uuid);
|
||||
|
||||
NameColorHandler.getInstance().calculatePlayerColors(argPlayer);
|
||||
if (isNation && sideNation != null) {
|
||||
sideNation.kickNation(nation);
|
||||
p.sendMessage(ColorParser.of("Target yeeted.").build());
|
||||
nation.getResidents().stream().filter(Resident::isOnline).map(Resident::getPlayer).forEach(player1 -> NameColorHandler.getInstance().calculatePlayerColors(player1));
|
||||
return;
|
||||
}
|
||||
|
||||
if (isTown && sideTown != null) {
|
||||
sideTown.kickTown(town);
|
||||
p.sendMessage(ColorParser.of("Target yeeted.").build());
|
||||
town.getResidents().stream().filter(Resident::isOnline).map(Resident::getPlayer).forEach(player1 -> NameColorHandler.getInstance().calculatePlayerColors(player1));
|
||||
return;
|
||||
}
|
||||
|
||||
if (isPlayer && sidePlayer != null) {
|
||||
sidePlayer.kickPlayer(player.getUniqueId());
|
||||
p.sendMessage(ColorParser.of("Target yeeted.").build());
|
||||
if (player.getPlayer() != null)
|
||||
NameColorHandler.getInstance().calculatePlayerColors(player.getPlayer());
|
||||
return;
|
||||
}
|
||||
|
||||
throw CommandAPIBukkit.failWithAdventureComponent(ColorParser.of("<red>Failed to yeet from the war because the target is not in it.").build());
|
||||
}
|
||||
|
||||
private static void warList(@NotNull Player p, CommandArguments args) throws WrapperCommandSyntaxException {
|
||||
|
||||
@@ -251,6 +251,20 @@ public class Side {
|
||||
nation.getTowns().forEach(this::unsurrenderTown);
|
||||
}
|
||||
|
||||
public void kickTown(Town town) {
|
||||
towns.remove(town);
|
||||
surrenderedTowns.remove(town);
|
||||
|
||||
town.getResidents().forEach(resident -> kickPlayer(resident.getUUID()));
|
||||
}
|
||||
|
||||
public void kickNation(Nation nation) {
|
||||
nations.remove(nation);
|
||||
surrenderedNations.remove(nation);
|
||||
|
||||
nation.getTowns().forEach(this::kickTown);
|
||||
}
|
||||
|
||||
public boolean isPlayerOnSide(Player p) {
|
||||
return isPlayerOnSide(p.getUniqueId());
|
||||
}
|
||||
@@ -329,6 +343,11 @@ public class Side {
|
||||
}
|
||||
}
|
||||
|
||||
public void kickPlayer(UUID uuid) {
|
||||
removePlayer(uuid);
|
||||
surrenderedPlayersIncludingOffline.remove(uuid);
|
||||
}
|
||||
|
||||
public void removeOnlinePlayer(Player p) {
|
||||
players.remove(p);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user