ctf/src/main/java/com/alttd/ctf/game/phases/EndedPhase.java
Teriuihi be7b508667 Integrate WorldBorderAPI and enhance game phase functionality
Added WorldBorderAPI dependency to manage player boundaries dynamically during game phases. Updated game phases, respawn mechanics, and class selection to utilize the WorldBorderAPI. Reworked related components to improve boundary handling, ensuring consistent gameplay flow and preparation for future enhancements.
2025-02-08 20:45:06 +01:00

109 lines
4.6 KiB
Java

package com.alttd.ctf.game.phases;
import com.alttd.ctf.config.Config;
import com.alttd.ctf.flag.Flag;
import com.alttd.ctf.game.GamePhase;
import com.alttd.ctf.game.GamePhaseExecutor;
import com.alttd.ctf.team.Team;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class EndedPhase implements GamePhaseExecutor {
private final MiniMessage miniMessage = MiniMessage.miniMessage();
@Override
public void start(Flag flag) {
Bukkit.broadcast(MiniMessage.miniMessage().deserialize("<green>Capture the flag has ended!</green>"));
HashMap<Team, Integer> wins = flag.getWins();
Bukkit.broadcast(Component.join(JoinConfiguration.separator(Component.newline()), getWinnerMessages(wins)));
new Thread(() -> {
World world = Bukkit.getWorld(Config.FLAG.world);
if (world == null) {
log.error("Invalid flag world defined");
return;
}
Location spawnLocation = world.getSpawnLocation();
Bukkit.getOnlinePlayers().forEach(player -> {
player.getInventory().clear();
player.updateInventory();
player.teleportAsync(spawnLocation);
AttributeInstance maxHealthAttribute = player.getAttribute(Attribute.GENERIC_MAX_HEALTH);
if (maxHealthAttribute == null) {
log.error("Player does not have max health attribute");
return;
}
maxHealthAttribute.setBaseValue(20);
player.setHealth(20);
});
flag.reset();
}).start();
// TODO reset world (coreprotect) to prep for next round
}
private List<Component> getWinnerMessages(HashMap<Team, Integer> wins) {
List<Component> messages = new ArrayList<>();
int highestScore = wins.values().stream()
.max(Integer::compareTo)
.orElse(0);
List<Team> topTeams = wins.entrySet().stream()
.filter(entry -> entry.getValue() == highestScore)
.map(Map.Entry::getKey)
.toList();
if (highestScore <= 0) { // No one captured the flag, no winners
messages.add(miniMessage.deserialize("<red>No one captured the flag, it's a draw!</red>"));
return messages;
} else if (topTeams.size() > 1) { // Draw scenario, multiple teams have the same top score
messages.add(miniMessage.deserialize("<yellow>It's a draw! Top teams:</yellow>"));
topTeams.forEach(team -> {
messages.add(miniMessage.deserialize("<team> had <score> captures.",
Placeholder.component("team", team.getName()),
Placeholder.parsed("score", String.valueOf(highestScore))));
});
addOtherTeamsScore(wins, highestScore, messages);
return messages;
} else { // Single winner
Team winner = topTeams.getFirst();
messages.add(miniMessage.deserialize("<green><team> has won with <score> captures!</green>",
Placeholder.component("team", winner.getName()),
Placeholder.parsed("score", String.valueOf(highestScore))));
if (wins.size() <= 1) {
return messages;
}
messages.add(miniMessage.deserialize("<yellow>Other teams:</yellow>"));
addOtherTeamsScore(wins, highestScore, messages);
return messages;
}
}
private void addOtherTeamsScore(HashMap<Team, Integer> wins, int winningScore, List<Component> messages) {
messages.add(miniMessage.deserialize("<yellow>Other teams:</yellow>"));
wins.entrySet().stream()
.filter(entry -> entry.getValue() < winningScore)
.forEach(entry -> messages.add(miniMessage.deserialize("<yellow><team> had <score> captures.</yellow>",
Placeholder.component("team", entry.getKey().getName()),
Placeholder.parsed("score", String.valueOf(entry.getValue())))));
}
@Override
public void end(GamePhase ignored) {
}
}