101 lines
3.1 KiB
Java
101 lines
3.1 KiB
Java
package com.alttd.hunger_games.services;
|
|
|
|
import com.alttd.hunger_games.Main;
|
|
import com.alttd.hunger_games.data_objects.PLAYER_STATE;
|
|
import com.alttd.hunger_games.data_objects.ROUND_STATE;
|
|
import com.alttd.hunger_games.event_listeners.PlayerMovementListener;
|
|
import lombok.Getter;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.HandlerList;
|
|
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class RoundService implements RoundListener {
|
|
|
|
private static RoundService instance = null;
|
|
private PlayerMovementListener playerMovementListener;
|
|
private final Main main;
|
|
private final LootService lootService;
|
|
|
|
@Getter
|
|
private ROUND_STATE roundState;
|
|
private final HashMap<UUID, PLAYER_STATE> players = new HashMap<>();
|
|
|
|
public static RoundService createSingletonInstance(Round round, Main main, LootService lootService) {
|
|
if (instance != null) {
|
|
throw new IllegalStateException("RoundService is already initialized.");
|
|
}
|
|
instance = new RoundService(round, main, lootService);
|
|
return instance;
|
|
}
|
|
|
|
private RoundService(Round round, Main main, LootService lootService) {
|
|
this.roundState = round.register(this);
|
|
this.main = main;
|
|
this.lootService = lootService;
|
|
}
|
|
|
|
public Set<UUID> getPlayers(PLAYER_STATE playerState) {
|
|
return players.entrySet().stream()
|
|
.filter(entry -> entry.getValue().equals(playerState))
|
|
.map(Map.Entry::getKey)
|
|
.collect(Collectors.toSet());
|
|
}
|
|
|
|
protected void setPlayerState(UUID uuid, PLAYER_STATE playerState) {
|
|
players.put(uuid, playerState);
|
|
if (!roundState.equals(ROUND_STATE.KILL_PHASE) && !roundState.equals(ROUND_STATE.FINALE)) {
|
|
return;
|
|
}
|
|
Set<UUID> uuidSet = getPlayers(PLAYER_STATE.REGISTERED);
|
|
if (uuidSet.size() != 1) {
|
|
return;
|
|
}
|
|
declareWinner(uuidSet.iterator().next());
|
|
}
|
|
|
|
private void declareWinner(UUID winnerUUID) {
|
|
Player player = Bukkit.getPlayer(winnerUUID);
|
|
//TODO message
|
|
//TODO reset round
|
|
}
|
|
|
|
protected void clear() {
|
|
players.clear();
|
|
}
|
|
|
|
public Optional<PLAYER_STATE> getPlayerState(UUID uuid) {
|
|
return Optional.ofNullable(players.get(uuid));
|
|
}
|
|
|
|
@Override
|
|
public void stateChange(ROUND_STATE roundState) {
|
|
this.roundState = roundState;
|
|
stopFreeze();
|
|
if (roundState.equals(ROUND_STATE.PLAYER_REGISTRATION)) {
|
|
clear();
|
|
lootService.clear();
|
|
}
|
|
if (roundState.equals(ROUND_STATE.COUNTDOWN)) {
|
|
startFreeze();
|
|
}
|
|
}
|
|
|
|
private void stopFreeze() {
|
|
if (playerMovementListener == null) {
|
|
return;
|
|
}
|
|
HandlerList.unregisterAll(playerMovementListener);
|
|
playerMovementListener = null;
|
|
}
|
|
|
|
private void startFreeze() {
|
|
stopFreeze();
|
|
Set<UUID> uuidSet = getPlayers(PLAYER_STATE.REGISTERED);
|
|
playerMovementListener = new PlayerMovementListener(uuidSet);
|
|
main.getServer().getPluginManager().registerEvents(playerMovementListener, main);
|
|
}
|
|
}
|