package me.otisdiver.otisarena.task;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.entity.Player;

import me.otisdiver.otisarena.ConfigUtils;
import me.otisdiver.otisarena.OtisArena;
import me.otisdiver.otisarena.game.Game;
import me.otisdiver.otisarena.game.GameState;

public class StartGame extends Task {
    
    private final String countdownMessage = ChatColor.AQUA + " Game starting in " + ChatColor.YELLOW + "%s seconds!";
    
    private final int gameLengthTicks = 360 * 20; // average 20 ticks/second
    
    private final int warningSeconds = 30;
    
    private final String warningMessage = " " + ChatColor.RED + warningSeconds + " seconds remaining!";

    private final String worldName;
    
    private final Game game;
    
    public StartGame(OtisArena main) {
        
        super(main);
        
        game = main.getGame();
        
        worldName = ConfigUtils.getRandomWorld();
    }
    
    @Override
    public void run() {
        
        // go to next state - LOADING
        GameState.advance();
        
        // assign teams
        game.distributePlayersTeams(game.getActivePlayers());
        
        // load the (random) arena
        World arena = Bukkit.createWorld(new WorldCreator(worldName));
        game.setActiveWorld(arena);
        
        // send players into game world
        for (Player player : game.getActivePlayers()) {
            
            Location spawn = ConfigUtils.getRandomSpawn(arena);
            player.teleport(spawn);
            
        }
        
        // unload the lobby, don't save changes
        Bukkit.unloadWorld(game.getLobby(), false);
        
        // run a countdown (after 1 second)
        new Countdown(main, 10, 1, countdownMessage).runFuture(10);
        
        // advance state after countdown (after 11 seconds)
        new AdvanceState(main).runFuture(221);
        
        // schedule warning <warningSeconds> seconds before game ends
        new FutureBroadcast(main, warningMessage).runFuture(gameLengthTicks - (warningSeconds * 20));
        
        // end game after gameLengthTicks
        new EndGame(main, true).runFuture(gameLengthTicks);
        
        // go to next state - STARTING
        GameState.advance();
        
    }

}

@@ -31,16 +31,71 @@
 .task;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.HashMap;%0A%0A
 import o
@@ -430,16 +430,57 @@
 meState;
+%0Aimport me.otisdiver.otisarena.game.Team;
 %0A%0Apublic
@@ -1549,181 +1549,787 @@
     
-for (Player player : game.getActivePlayers()) %7B%0A            %0A            Location spawn = ConfigUtils.getRandomSpawn(arena);%0A            player.teleport(spawn);%0A            
+HashMap%3CLocation, Team%3E usedLocations = new HashMap%3CLocation, Team%3E();%0A        %0A        for (Player player : game.getActivePlayers()) %7B%0A            %0A            Team playerTeam = game.getPlayerTeam(player);%0A            %0A            boolean spawned = false;%0A            while (!spawned) %7B%0A                Location spawn = ConfigUtils.getRandomSpawn(arena);%0A                Team spawnTeam = usedLocations.get(spawn);%0A                %0A                // if the spawn belongs to that player's team or no team, teleport to it%0A                if (spawnTeam == null %7C%7C spawnTeam.equals(playerTeam)) %7B%0A                    player.teleport(spawn);%0A                    usedLocations.put(spawn, game.getPlayerTeam(player));%0A                    spawned = true;%0A                %7D%0A            %7D
 %0A   

update spawning - players of different teams no longer spawn at same location