package me.stefvanschie.buildinggame.timers;

import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import me.stefvanschie.buildinggame.managers.files.SettingsManager;
import me.stefvanschie.buildinggame.managers.messages.MessageManager;
import me.stefvanschie.buildinggame.utils.Arena;
import me.stefvanschie.buildinggame.utils.Plot;

public class VoteTimer extends BukkitRunnable {

	private int seconds;
	private int originalSeconds;
	private Arena arena;
	private Plot plot;
	
	public VoteTimer(int seconds, Arena arena) {
		this.seconds = seconds;
		originalSeconds = seconds;
		this.arena = arena;
	}

	@Override
	public void run() {
		YamlConfiguration messages = SettingsManager.getInstance().getMessages();
		
		if (seconds == originalSeconds) {
			plot = getNextPlot();
			if (plot == null) {
				arena.stop();
				this.cancel();
				return;
			}
			
			arena.setVotingPlot(plot);
			for (Plot plot : arena.getPlots()) {
				if (plot.getPlayerData() != null) {
					Player player = plot.getPlayerData().getPlayer();
					MessageManager.getInstance().send(player, messages.getString("voting.message")
							.replace("%playerplot%", player.getName()
							.replaceAll("&", "§")));
					arena.getScoreboard().show(player);
					
				}
			}
		}
		seconds--;
		if (seconds <= 0) {
			YamlConfiguration config = SettingsManager.getInstance().getConfig();
			
			seconds = config.getInt("votetimer");
			originalSeconds = seconds;
		}
	}
	public Plot getNextPlot() {
		for (Plot plot : arena.getPlots()) {
			if (!arena.getVotedPlots().contains(plot)) {
				if (plot.getPlayerData() != null) {
					return plot;
				}
			}
		}
		return null;
	}
}

@@ -39,16 +39,47 @@
 imers;%0A%0A
+import org.bukkit.WeatherType;%0A
 import o
@@ -433,16 +433,21 @@
 e.utils.
+plot.
 Plot;%0A%0Ap
@@ -1251,22 +1251,53 @@
 plot%25%22, 
-player
+this.plot.getPlayerData().getPlayer()
 .getName
@@ -1340,49 +1340,382 @@
 %09%09%09%09
-arena.getScoreboard().show(player);%0A%09%09%09%09%09
+plot.getPlayerData().sendTitle(messages.getString(%22global.title%22)%0A%09%09%09%09%09%09%09.replace(%22%25playerplot%25%22, this.plot.getPlayerData().getPlayer().getName()));%0A%09%09%09%09%09%0A%09%09%09%09%09arena.getScoreboard().show(player);%0A%09%09%09%09%09player.setPlayerTime(this.plot.getTime().decode(this.plot.getTime()), false);%0A%09%09%09%09%09player.setPlayerWeather(this.plot.isRaining() ? WeatherType.DOWNFALL : WeatherType.CLEAR);
 %0A%09%09%09

Update VoteTimer.java