package me.otisdiver.otisarena;

import java.util.Random;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;

public class ConfigUtils {
    
    private static OtisArena main;
    
    private static Random randomSet;
    
    private static boolean initiated = false;
    
    static void initiate(OtisArena mainClass) {
        
        if (initiated) return;
        
        // save the main class
        main = mainClass;
        
        // create a config if needed
        main.saveDefaultConfig();
        
        // make a random
        randomSet = new Random();
        
        // flag class as initiated
        initiated = true;
    }
    
    public static void reload() {
        main.reloadConfig();
    }
    
    public static FileConfiguration getConfig() {
        return main.getConfig();
    }
    
    private static String randomKey(Set<String> set) {
        
        // choose a random index, then find it from the set
        int random = randomSet.nextInt(set.size());
        int i = 0;
        for(String item : set) {
            if (i == random) return item;
            i++;
        }
        
        // dead code (unless I did something stupid)
        return null;
    }
    
    public static String getRandomWorld() {
        
        // get all items under the section worlds
        Set<String> worlds = getConfig().getConfigurationSection("worlds").getKeys(false);
        
        return randomKey(worlds);
    }
    
    public static Location getRandomSpawn(World world) {
        
        // get the config section worlds.[world].spawns
        StringBuilder path = new StringBuilder();
        path.append("worlds.").append(world.getName()).append(".spawns");
        ConfigurationSection locations = getConfig().getConfigurationSection(path.toString());
        
        // find a set of coords from the config
        path.append(".").append(randomKey(locations.getKeys(false)));
        String pathCoords = path.toString();
        
        Bukkit.getLogger().info("Path: " + pathCoords);
        
        // build a Location object
        Double x = locations.getDouble(pathCoords + ".x");
        Double y = locations.getDouble(pathCoords + ".y");
        Double z = locations.getDouble(pathCoords + ".z");
        
        Bukkit.getLogger().info(world.getName() + x + y + z);
        return new Location(world, x, y, z);
    }
    
}

@@ -2029,32 +2029,23 @@
     
-path.append(%22.%22).append(
+String spawn = 
 rand
@@ -2079,119 +2079,8 @@
 se))
-);%0A        String pathCoords = path.toString();%0A        %0A        Bukkit.getLogger().info(%22Path: %22 + pathCoords)
 ;%0A  
@@ -2156,34 +2156,29 @@
 s.getDouble(
+s
 pa
-thCoords
+wn
  + %22.x%22);%0A  
@@ -2210,34 +2210,29 @@
 s.getDouble(
+s
 pa
-thCoords
+wn
  + %22.y%22);%0A  
@@ -2272,18 +2272,13 @@
 ble(
+s
 pa
-thCoords
+wn
  + %22

fix spawn points not loading properly locations.get(locations path + full spawn path + x/y/z) was causing this issue. electronicboy in #spigot solved this one.