198 lines
4.9 KiB
Java

package gg.malloc.defense.engine;
import org.bukkit.entity.Player;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.attribute.Attribute;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.scoreboard.ScoreboardManager;
import gg.malloc.defense.ui.Sidebar;
import gg.malloc.defense.model.Progress;
import java.util.HashMap;
import java.util.Collection;
public class PlayerManager {
HashMap<Player, State> m_playerStates = new HashMap<>();
HashMap<Player, Boolean> m_playerReadyStates = new HashMap<>();
HashMap<Player, Integer> m_playerExpenses = new HashMap<>();
HashMap<Player, Integer> m_respawnCounters = new HashMap<>();
public enum State {
Idle,
Playing,
Dead
}
public boolean requestTransitionForAll(State toState) {
boolean allSuccess = true;
for (Player p : getPlayers()) {
allSuccess = allSuccess && requestTransition(p, toState);
}
return allSuccess;
}
public boolean contains(Player player) {
return m_playerStates.containsKey(player);
}
public boolean requestTransition(Player player, State toState) {
State currentState = m_playerStates.get(player);
if (currentState == toState) {
return false;
}
boolean ret = false;
switch(toState) {
case Idle:
ret = enterIdle(player);break;
case Playing:
ret = enterPlaying(player);break;
case Dead:
ret = enterDead(player);break;
}
if (ret) {
m_playerStates.put(player, toState);
}
return ret;
}
public boolean enterIdle(Player player) {
// Reset player to non-game state, return control back to Minecraft
player.setGameMode(Bukkit.getDefaultGameMode());
return true;
}
public void healPlayer(Player player) {
player.setHealth(player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
player.setFoodLevel(40);
}
// Respawn player
public boolean enterPlaying(Player player) {
player.setGameMode(Bukkit.getDefaultGameMode());
healPlayer(player);
return true;
}
public boolean enterDead(Player player) {
player.setGameMode(GameMode.SPECTATOR);
m_respawnCounters.put(player, 15);
return true;
}
public void addPlayer(Player player) {
m_playerStates.put(player, State.Idle);
m_playerReadyStates.put(player, false);
m_playerExpenses.put(player, 0);
m_respawnCounters.put(player, 0);
}
public boolean isReady(Player player) {
return m_playerReadyStates.get(player);
}
public void setReady(Player player, boolean ready) {
m_playerReadyStates.put(player, ready);
}
public Progress readyProgress() {
int readyNum = 0;
for(boolean b : m_playerReadyStates.values()) {
if (b) {
readyNum += 1;
}
}
return new StaticProgress(readyNum, m_playerReadyStates.size());
}
public boolean isEveryoneReady() {
for(boolean b : m_playerReadyStates.values()) {
if (!b) {
return false;
}
}
return true;
}
public Progress getRespawnProgress(Player player) {
return new StaticProgress(m_respawnCounters.get(player), 15);
}
public int getRespawnCounter(Player player) {
if (m_respawnCounters.containsKey(player)) {
return m_respawnCounters.get(player);
} else {
return 0;
}
}
public void tickRespawnCounters() {
for(Player p : m_respawnCounters.keySet()) {
if (m_respawnCounters.get(p) > 0) {
m_respawnCounters.put(p, m_respawnCounters.get(p) - 1);
}
}
}
public boolean readyToRespawn(Player player) {
return m_respawnCounters.get(player) == 0 && m_playerStates.get(player) == State.Dead;
}
public void addPlayerExpenses(Player player, int amount) {
if (m_playerExpenses.containsKey(player)) {
m_playerExpenses.put(player, m_playerExpenses.get(player) + amount);
}
}
public int getPlayerExpenses(Player player) {
if (m_playerExpenses.containsKey(player)) {
return m_playerExpenses.get(player);
}
return 0;
}
public boolean removePlayer(Player player) {
healPlayer(player);
requestTransition(player, State.Idle);
m_playerStates.remove(player);
m_playerReadyStates.remove(player);
m_playerExpenses.remove(player);
m_respawnCounters.remove(player);
return true;
}
public boolean isEmpty() {
return m_playerStates.size() == 0;
}
public boolean isAlive(Player player) {
return m_playerStates.get(player) == State.Playing;
}
public boolean isAnyoneAlive() {
for(Player p : m_playerStates.keySet()) {
if (m_playerStates.get(p) == State.Playing) {
return true;
}
}
return false;
}
public int remainingPlayers() {
int aliveCount = 0;
for(State s : m_playerStates.values()) {
if (s == State.Playing) {
aliveCount += 1;
}
}
return aliveCount;
}
Collection<Player> getPlayers() {
return m_playerStates.keySet();
}
}