package cx.sfy.TheBridge.game; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.Random; import org.bukkit.ChatColor; import org.bukkit.Color; import org.bukkit.FireworkEffect; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.entity.Firework; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.FireworkMeta; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.LeatherArmorMeta; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; import cx.sfy.TheBridge.Main; import cx.sfy.TheBridge.database.PlayerStat; import cx.sfy.TheBridge.hologram.TruenoHologram; import cx.sfy.TheBridge.hologram.TruenoHologramAPI; import cx.sfy.TheBridge.team.Team; import cx.sfy.TheBridge.utils.CenterMessage; import cx.sfy.TheBridge.utils.ItemBuilder; import cx.sfy.TheBridge.utils.Utils; public class Game { private final Main plugin; private final HashMap teams; private final HashMap teamPlayer; private final ArrayList players; private final HashMap pd = new HashMap<>(); private final HashMap goals = new HashMap<>(); private final HashMap kills = new HashMap<>(); private final ArrayList holograms = new ArrayList<>(); private final ArrayList build = new ArrayList<>(); private final ArrayList placed = new ArrayList<>(); private final String name; private final String mode; private final int min; private final int max; private final int teamSize; private int starting; private int prestart; private int restart; private final Location lobby; private final Location spect; private BukkitTask start; private BukkitTask prestarts; private State state; int time = 0; int tick = 0; public Game(Main plugin, String name, int min, int max, int teamSize, int starting, int prestart, int restart, Location lobby, Location spect, Location buildMin, Location buildMax, Location spawnsRed, Location spawnsBlue, ArrayList portalRed, ArrayList portalBlue, Location respawnsRed, Location respawnsBlue, Location hRed, Location hBlue) { this.plugin = plugin; this.starting = starting; this.prestart = prestart; this.restart = restart; this.max = max; this.min = min; this.teamSize = teamSize; this.mode = teamSize + "v" + teamSize; this.name = name; this.lobby = lobby; this.spect = spect; this.teamPlayer = new HashMap<>(); this.teams = new HashMap<>(); this.players = new ArrayList<>(); final int gn = Math.min(buildMin.getBlockX(), buildMax.getBlockX()); final int gn2 = Math.max(buildMin.getBlockX(), buildMax.getBlockX()); final int gn3 = Math.min(buildMin.getBlockZ(), buildMax.getBlockZ()); final int gn4 = Math.max(buildMin.getBlockZ(), buildMax.getBlockZ()); final int gn5 = Math.min(buildMin.getBlockY(), buildMax.getBlockY()); final int gn6 = Math.max(buildMin.getBlockY(), buildMax.getBlockY()); for (int i = gn; i <= gn2; ++i) for (int j = gn5; j <= gn6; ++j) for (int k = gn3; k <= gn4; ++k) { final Location location3 = new Location(buildMin.getWorld(), i, j, k); build.add(location3); } this.teams.put(ChatColor.BLUE, new Team(plugin.getConfig().getString("names.blue"), ChatColor.BLUE, Color.BLUE, respawnsBlue, spawnsBlue, portalBlue, hBlue)); this.teams.put(ChatColor.RED, new Team(plugin.getConfig().getString("names.red"), ChatColor.RED, Color.RED, respawnsRed, spawnsRed, portalRed, hRed)); setState(State.WAITING); updateSign(); } public void checkCancel() { if (isState(State.STARTING) && players.size() <= getMin()) { if (start != null) start.cancel(); if (prestarts != null) prestarts.cancel(); setState(State.WAITING); starting = 30; updateSign(); for (final Player on : players) updateSB(on); } } public void updateSB(Player p) { plugin.getSB().update(p); } public void updateSign() { plugin.getSIM().updateGameSign(this); } public void addPlayer(Player p) { pd.put(p, new PlayerData(p)); Utils.setCleanPlayer(p); players.add(p); kills.put(p, 0); goals.put(p, 0); new BukkitRunnable() { @Override public void run() { givePlayerItems(p); } }.runTaskLater(plugin, 20); p.teleport(getLobby()); updateSign(); if (isState(State.WAITING) || isState(State.STARTING)) for (final Player on : players) { on.sendMessage(plugin.getLang().get("messages.join").replaceAll("", p.getName()) .replaceAll("", String.valueOf(players.size())) .replaceAll("", String.valueOf(getMax()))); on.playSound(on.getLocation(), Sound.valueOf(plugin.getSounds().get("sounds.game.join")), 1.0f, 1.0f); updateSB(on); } } public void removePlayer(Player p) { Utils.setCleanPlayer(p); pd.get(p).restore(); removeAllPlayerTeam(p); players.remove(p); if (isState(State.WAITING) || isState(State.STARTING)) for (final Player on : players) { on.sendMessage(plugin.getLang().get("messages.quit").replaceAll("", p.getName()) .replaceAll("", String.valueOf(players.size())) .replaceAll("", String.valueOf(getMax()))); on.playSound(on.getLocation(), Sound.valueOf(plugin.getSounds().get("sounds.game.quit")), 1.0f, 1.0f); updateSB(on); } else if (getTeamsAlive() == 1 && players.size() >= 1) checkWin(p, getLastTeam(), getLastTeam().getGoals()); updateSign(); checkCancel(); } // FIXME: Not properly giving coins public boolean checkWin(Player w, Team team, int goals) { if (isState(State.FINISH)) return false; final DecimalFormat df = new DecimalFormat("#.##"); if (getTeamsAlive() <= 1 && players.size() >= 1) { final Team win = getLastTeam(); setState(State.FINISH); firework(getSpect(), win.getFColor()); for (final Player r : win.getTeamPlayers()) PlayerStat.getPlayerStat(r).addNormalWins(); for (final Player p : players) { p.teleport(lobby); updateSB(p); plugin.getNMS().sendTitle(p, 0, 60, 0, plugin.getLang().get("titles.win.title") .replaceAll("", String.valueOf(teams.get(ChatColor.BLUE).getGoals())) .replaceAll("", String.valueOf(teams.get(ChatColor.RED).getGoals())) .replaceAll("", team.getTeamName()).replaceAll("", team.getColor() + ""), plugin.getLang().get("titles.win.subtitle") .replaceAll("", String.valueOf(teams.get(ChatColor.BLUE).getGoals())) .replaceAll("", String.valueOf(teams.get(ChatColor.RED).getGoals())) .replaceAll("", team.getTeamName()).replaceAll("", team.getColor() + "")); for (final String msg : plugin.getLang().getList("messages.win")) CenterMessage.sendCenteredMessage(p, msg.replaceAll("&", "§").replaceAll("", w.getName()) .replaceAll("", String.valueOf(teams.get(ChatColor.BLUE).getGoals())) .replaceAll("", String.valueOf(teams.get(ChatColor.RED).getGoals())) .replaceAll("", getGoals(w) > 1 ? "es" : "") .replaceAll("", String.valueOf(getGoals(w))) .replaceAll("", df.format(w.getHealth())).replaceAll("", "❤") .replaceAll("", "" + team.getColor()) .replaceAll("", win.getTeamName().toUpperCase())); } new BukkitRunnable() { @Override public void run() { for (final TruenoHologram th : holograms) th.delete(); for (final Player p : spect.getWorld().getPlayers()) if (pd.containsKey(p)) plugin.getGM().removePlayerAllGames(p); else p.teleport(plugin.getMainLobby()); } }.runTaskLater(plugin, 20 * 10); new BukkitRunnable() { @Override public void run() { plugin.getGM().resetNormalGame(getName()); } }.runTaskLater(plugin, 20 * 12); updateSign(); return true; } if (team.getGoals() >= goals) { setState(State.FINISH); final Team win = getLastTeam(); firework(getSpect(), team.getFColor()); for (final Player r : win.getTeamPlayers()) PlayerStat.getPlayerStat(r).addNormalWins(); for (final Player p : players) { p.teleport(lobby); plugin.getNMS().sendTitle(p, 0, 60, 0, plugin.getLang().get("titles.win.title") .replaceAll("", String.valueOf(teams.get(ChatColor.BLUE).getGoals())) .replaceAll("", String.valueOf(teams.get(ChatColor.RED).getGoals())) .replaceAll("", team.getTeamName()).replaceAll("", team.getColor() + ""), plugin.getLang().get("titles.win.subtitle") .replaceAll("", String.valueOf(teams.get(ChatColor.BLUE).getGoals())) .replaceAll("", String.valueOf(teams.get(ChatColor.RED).getGoals())) .replaceAll("", team.getTeamName()).replaceAll("", team.getColor() + "")); for (final String msg : plugin.getLang().getList("messages.win")) CenterMessage.sendCenteredMessage(p, msg.replaceAll("&", "§").replaceAll("", w.getName()) .replaceAll("", String.valueOf(teams.get(ChatColor.BLUE).getGoals())) .replaceAll("", String.valueOf(teams.get(ChatColor.RED).getGoals())) .replaceAll("", getGoals(w) > 1 ? "es" : "") .replaceAll("", String.valueOf(getGoals(w))) .replaceAll("", df.format(w.getHealth())).replaceAll("", "❤") .replaceAll("", "" + team.getColor()) .replaceAll("", team.getTeamName().toUpperCase())); } new BukkitRunnable() { @Override public void run() { for (final Player p : spect.getWorld().getPlayers()) if (pd.containsKey(p)) plugin.getGM().removePlayerAllGames(p); else p.teleport(plugin.getMainLobby()); } }.runTaskLater(plugin, 20 * 10); new BukkitRunnable() { @Override public void run() { plugin.getGM().resetNormalGame(getName()); } }.runTaskLater(plugin, 20 * 12); updateSign(); return true; } return false; } public void celebrateGoal(Team team, Player e) { restart = 5; team.addGoal(); addGoal(e); PlayerStat.getPlayerStat(e).addNormalGoals(); if (checkWin(e, team, 5)) return; firework(getSpect(), team.getFColor()); if (plugin.isCage()) { for (final Team te : teams.values()) if (!te.isCage()) if (te.getTeamPlayers().get(0) != null) { te.createCage(plugin.getCM() .getCageByName(PlayerStat.getPlayerStat(te.getTeamPlayers().get(0)).getCage())); te.setCage(true); } else { te.createCage(plugin.getCM().getCageByName(plugin.getConfig().getString("defaultCage"))); te.setCage(true); } } else for (final ChatColor color : teams.keySet()) plugin.getGLM().createCage(teams.get(color).getTeamSpawn(), color); for (final Player p : players) { p.teleport(getTeamPlayer(p).getTeamSpawn().clone().add(0, 1, 0)); updateSB(p); } checkRestart(team, team.getColor(), e); } public void firework(Location loc, Color c1) { time = 0; new BukkitRunnable() { @Override public void run() { final Firework fa = loc.getWorld().spawn(loc.clone().add(new Random().nextInt(5) + 1, new Random().nextInt(1) + 1, new Random().nextInt(5) + 1), Firework.class); final FireworkMeta fam = fa.getFireworkMeta(); final FireworkEffect.Type tipo = FireworkEffect.Type.STAR; final Color c2 = Color.WHITE; final FireworkEffect ef = FireworkEffect.builder().withColor(c1).withFade(c2).with(tipo).build(); fam.addEffect(ef); fam.setPower(0); fa.setFireworkMeta(fam); time++; if (time == 10) cancel(); } }.runTaskTimer(plugin, 0, 10); } public void checkRestart(Team team, ChatColor color, Player e) { final DecimalFormat df = new DecimalFormat("#.##"); for (final Player p : players) { Utils.setCleanPlayer(p); p.setGameMode(GameMode.ADVENTURE); giveKit(p, getTeamPlayer(p)); for (final String msg : plugin.getLang().getList("messages.goal")) CenterMessage.sendCenteredMessage(p, msg.replaceAll("&", "§").replaceAll("", e.getName()) .replaceAll("", String.valueOf(teams.get(ChatColor.BLUE).getGoals())) .replaceAll("", String.valueOf(teams.get(ChatColor.RED).getGoals())) .replaceAll("", getGoals(e) > 1 ? "es" : "") .replaceAll("", String.valueOf(getGoals(e))) .replaceAll("", df.format(e.getHealth())).replaceAll("", "❤") .replaceAll("", "" + team.getColor()) .replaceAll("", team.getTeamName().toUpperCase())); } new BukkitRunnable() { @Override public void run() { if (restart == 10 || restart == 5 || restart == 4 || restart == 3 || restart == 2) for (final Player p : players) { p.sendMessage(plugin.getLang().get(p, "messages.restart").replaceAll("", "" + color) .replaceAll("", e.getName()).replaceAll("