Introduced JSON-based configuration handling for teams using Jackson and validation utilities. Added commands for reloading configurations and creating teams with support for saving and loading team data. Refactored related classes to integrate with the new system.
82 lines
2.9 KiB
Java
82 lines
2.9 KiB
Java
package com.alttd.ctf.game;
|
|
|
|
import com.alttd.ctf.game.phases.ClassSelectionPhase;
|
|
import com.alttd.ctf.game_class.implementations.Fighter;
|
|
import com.alttd.ctf.team.Team;
|
|
import com.alttd.ctf.team.TeamPlayer;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.time.Duration;
|
|
import java.util.*;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
public class GameManager {
|
|
|
|
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
|
private final HashMap<GamePhase, GamePhaseExecutor> phases;
|
|
private RunningGame runningGame;
|
|
private final HashMap<Integer, Team> teams = new HashMap<>();
|
|
|
|
public GameManager() {
|
|
phases = new HashMap<>();
|
|
//TODO initialize this somewhere else (maybe load it from a json file?)
|
|
phases.put(GamePhase.CLASS_SELECTION, new ClassSelectionPhase(this, new Fighter(List.of(Material.LEATHER_CHESTPLATE), List.of(), new ItemStack(Material.STONE), 15, 3, 5)));
|
|
}
|
|
|
|
public Optional<GamePhase> getGamePhase() {
|
|
return runningGame == null ? Optional.empty() : Optional.of(runningGame.getCurrentPhase());
|
|
}
|
|
|
|
public void registerPlayer(Team team, Player player) {
|
|
unregisterPlayer(player);
|
|
teams.get(team.getId()).addPlayer(player.getUniqueId());
|
|
}
|
|
|
|
public void unregisterPlayer(Player player) {
|
|
teams.values().forEach(team -> team.removePlayer(player.getUniqueId()));
|
|
}
|
|
|
|
public void registerTeam(Team team) {
|
|
teams.put(team.getId(), team);
|
|
}
|
|
|
|
public Collection<Team> getTeams() {
|
|
return teams.values();
|
|
}
|
|
|
|
public Optional<Team> getTeam(@NotNull UUID uuid) {
|
|
return getTeams().stream().filter(filterTeam -> filterTeam.getPlayer(uuid).isPresent()).findAny();
|
|
}
|
|
|
|
public Optional<TeamPlayer> getTeamPlayer(@NotNull UUID uuid) {
|
|
return getTeams().stream()
|
|
.map(team -> team.getPlayer(uuid))
|
|
.filter(Optional::isPresent)
|
|
.findFirst()
|
|
.orElseGet(Optional::empty);
|
|
}
|
|
|
|
public void start(Duration duration) {
|
|
if (runningGame != null) {
|
|
runningGame.end();
|
|
executorService.shutdown();
|
|
executorService = Executors.newSingleThreadScheduledExecutor();
|
|
}
|
|
runningGame = new RunningGame(this, duration);
|
|
executorService.scheduleAtFixedRate(runningGame, 0, 1, TimeUnit.SECONDS);
|
|
}
|
|
|
|
protected GamePhaseExecutor getPhaseExecutor(GamePhase gamePhase) {
|
|
GamePhaseExecutor gamePhaseExecutor = phases.get(gamePhase);
|
|
if (gamePhaseExecutor == null) {
|
|
throw new IllegalArgumentException("No phase executor found for phase " + gamePhase);
|
|
}
|
|
return gamePhaseExecutor;
|
|
}
|
|
}
|