package in.twizmwaz.cardinal;

import in.twizmwaz.cardinal.cycle.Cycle;
import in.twizmwaz.cardinal.cycle.CycleTimer;
import in.twizmwaz.cardinal.event.CycleCompleteEvent;
import in.twizmwaz.cardinal.match.Match;
import in.twizmwaz.cardinal.match.MatchState;
import in.twizmwaz.cardinal.rotation.Rotation;
import in.twizmwaz.cardinal.rotation.exception.RotationLoadException;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.UUID;

public class GameHandler {

    private static GameHandler handler;
    private final JavaPlugin plugin;
    private Rotation rotation;
    private UUID matchUUID;
    private World matchWorld;
    private Match match;
    private Cycle cycle;
    private CycleTimer cycleTimer;

    public GameHandler(JavaPlugin plugin) throws RotationLoadException {
        this.plugin = plugin;
        handler = this;
        rotation = new Rotation(plugin);
        cycle = new Cycle(rotation.getNext(), UUID.randomUUID(), this);
        cycleAndMakeMatch();
    }

    public static GameHandler getGameHandler() {
        return handler;
    }

    public void cycleAndMakeMatch() {
        matchUUID = UUID.randomUUID();
        rotation.move();
        World oldMatchWorld = matchWorld;
        cycle.run();
        this.matchUUID = cycle.getUuid();
        try {
            match.unregisterModules();
        } catch (NullPointerException e) {
        }
        this.match = new Match(this, matchUUID);
        this.match.registerModules();
        Bukkit.getLogger().info("[CardinalPGM] " + this.match.getModules().size() + " modules loaded.");
        Bukkit.getServer().getPluginManager().callEvent(new CycleCompleteEvent(match));
        rotation.move();
        cycle = new Cycle(rotation.getNext(), UUID.randomUUID(), this);
        Bukkit.unloadWorld(oldMatchWorld, true);
    }

    public Rotation getRotation() {
        return rotation;
    }

    public World getMatchWorld() {
        return matchWorld;
    }

    public void setMatchWorld(World world) {
        this.matchWorld = world;
    }

    public Match getMatch() {
        return match;
    }

    public void setMatch(Match match) {
        this.match = match;
    }

    public Cycle getCycle() {
        return cycle;
    }

    public CycleTimer getCycleTimer() {
        return cycleTimer;
    }

    public JavaPlugin getPlugin() {
        return plugin;
    }

    public boolean startCycleTimer(int seconds) {
        if (this.getMatch().getState() != MatchState.PLAYING) {
            this.cycleTimer = new CycleTimer(this.getCycle(), seconds);
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this.getPlugin(), cycleTimer);
            return true;
        } else return false;
    }
}

@@ -627,36 +627,8 @@
 on;%0A
-    private UUID matchUUID;%0A
     
@@ -649,24 +649,24 @@
 matchWorld;%0A
+
     private 
@@ -1137,47 +1137,8 @@
 ) %7B%0A
-        matchUUID = UUID.randomUUID();%0A
     
@@ -1225,50 +1225,8 @@
 ();%0A
-        this.matchUUID = cycle.getUuid();%0A
     
@@ -1368,17 +1368,23 @@
 is, 
-matchUUID
+cycle.getUuid()
 );%0A 
@@ -1525,16 +1525,16 @@
 ded.%22);%0A
+
         
@@ -1617,33 +1617,8 @@
 ));%0A
-        rotation.move();%0A
     

Correct match UUID issues and double rotation moving