Refactor world border handling for game phases.

Removed world border size from `GamePhase` and centralized configuration in `GameConfig`. Updated logic to use `WorldBorderSettings` dynamically, ensuring all phases define valid world border behavior. Adjusted related methods to improve maintainability and clarity.
This commit is contained in:
Teriuihi 2025-02-08 23:06:10 +01:00
parent 728e8b7486
commit 8718ca0918
4 changed files with 24 additions and 10 deletions

View File

@ -49,6 +49,7 @@ public class GameConfig extends AbstractConfig {
public static class WORLD_BORDER {
private static final String prefix = "world-border.";
public static final double DEFAULT_SIZE = 140;
private static final HashMap<GamePhase, WorldBorderSettings> GAME_PHASE_WORLD_BORDER = new HashMap<>();
public static HashMap<GamePhase, WorldBorderSettings> getGAME_PHASE_WORLD_BORDER() {

View File

@ -6,16 +6,14 @@ import net.kyori.adventure.text.minimessage.MiniMessage;
@Getter
public enum GamePhase {
CLASS_SELECTION(MiniMessage.miniMessage().deserialize("<green>Class selection phase</green>"), 25),
GATHERING(MiniMessage.miniMessage().deserialize("<green>Gathering phase</green>"), 50),
COMBAT(MiniMessage.miniMessage().deserialize("<green>Combat phase</green>"), 0),
ENDED(MiniMessage.miniMessage().deserialize("<green>Game end phase</green>"), 0);
CLASS_SELECTION(MiniMessage.miniMessage().deserialize("<green>Class selection phase</green>")),
GATHERING(MiniMessage.miniMessage().deserialize("<green>Gathering phase</green>")),
COMBAT(MiniMessage.miniMessage().deserialize("<green>Combat phase</green>")),
ENDED(MiniMessage.miniMessage().deserialize("<green>Game end phase</green>"));
private final Component displayName;
private final double worldBorderSize;
GamePhase(Component displayName, double worldBorderSize) {
GamePhase(Component displayName) {
this.displayName = displayName;
this.worldBorderSize = worldBorderSize;
}
}

View File

@ -1,5 +1,6 @@
package com.alttd.ctf.game.phases;
import com.alttd.ctf.config.GameConfig;
import com.alttd.ctf.flag.Flag;
import com.alttd.ctf.game.GameManager;
import com.alttd.ctf.game.GamePhase;
@ -9,11 +10,14 @@ import com.alttd.ctf.team.Team;
import com.alttd.ctf.team.TeamColor;
import com.alttd.ctf.team.TeamPlayer;
import com.alttd.ctf.util.CircularIterator;
import com.github.yannicklamprecht.worldborder.api.IWorldBorder;
import com.github.yannicklamprecht.worldborder.api.Position;
import com.github.yannicklamprecht.worldborder.api.WorldBorderApi;
import lombok.extern.slf4j.Slf4j;
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.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -40,6 +44,11 @@ public class ClassSelectionPhase implements GamePhaseExecutor {
@Override
public void start(Flag flag) {
Location flagLocation = flag.getFlagLocation();
IWorldBorder worldBorder = worldBorderApi.getWorldBorder(flagLocation.getWorld());
worldBorder.setCenter(Position.of(flagLocation));
worldBorder.setSize(GameConfig.WORLD_BORDER.DEFAULT_SIZE);
Bukkit.broadcast(MiniMessage.miniMessage().deserialize("<green>Select your class with <gold>/ctf selectclass</gold></green>"));
CircularIterator<Team> teamCircularIterator = new CircularIterator<>(gameManager.getTeams());
if (teamCircularIterator.hasNext()) {

View File

@ -1,5 +1,8 @@
package com.alttd.ctf.team;
import com.alttd.ctf.config.GameConfig;
import com.alttd.ctf.config.WorldBorderSettings;
import com.alttd.ctf.config.WorldBorderType;
import com.alttd.ctf.game.GamePhase;
import com.alttd.ctf.game_class.GameClass;
import com.alttd.ctf.game_class.GameClassRetrieval;
@ -48,12 +51,15 @@ public class TeamPlayer {
}
public void resetWorldBorder(Player player, WorldBorderApi worldBorderApi, GamePhase gamePhase, Location worldBorderCenter) {
double worldBorderSize = gamePhase.getWorldBorderSize();
if (worldBorderSize <= 0) {
WorldBorderSettings worldBorderSettings = GameConfig.WORLD_BORDER.getGAME_PHASE_WORLD_BORDER().get(gamePhase);
if (worldBorderSettings == null) {
throw new IllegalStateException("All phases need to have world border settings");
}
if (worldBorderSettings.type().equals(WorldBorderType.FLAG)) {
worldBorderApi.resetWorldBorderToGlobal(player);
return;
}
worldBorderApi.setBorder(player, worldBorderSize, worldBorderCenter);
worldBorderApi.setBorder(player, worldBorderSettings.size(), worldBorderCenter);
}
@Override