package com.matthewhatcher.randomspawn.Utils;

import java.util.Random;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import com.matthewhatcher.randomspawn.RandomSpawn;

public class TeleportUtils {
	public static void teleport(Player p) {
		if(RandomSpawn.getInstance().getConfig().getBoolean("debug"))
			RandomSpawn.logger.info("Teleporting " + p.getName());
		
		p.teleport(randomLocation(p.getWorld()));
		MessageUtils.send(p, "You have been teleported to a random location.");
	}
	
	public static Location randomLocation(World w) {
		FileConfiguration c = RandomSpawn.getInstance().getConfig();
		Random r = new Random();
		
		int x = c.getInt("coods.x.min") + r.nextInt(c.getInt("coords.x.max") - c.getInt("coords.x.min") + 1);
		int z = c.getInt("coods.z.min") + r.nextInt(c.getInt("coords.z.max") - c.getInt("coords.z.min") + 1);
		int y = w.getHighestBlockYAt(x, z);
		
		return new Location(w, x, y, z);
	}
}

@@ -66,24 +66,50 @@
 .Random;%0D%0A%0D%0A
+import org.bukkit.Chunk;%0D%0A
 import org.b
@@ -992,16 +992,174 @@
 int y = 
+0;%0D%0A%09%09%0D%0A%09%09Location loc = new Location(w, x, y, z);%0D%0A%09%09Chunk chunk = loc.getChunk();%0D%0A%09%09%0D%0A%09%09if(!chunk.isLoaded()) %7B%0D%0A%09%09%09chunk.load(true);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09loc.setY(
 w.getHig
@@ -1175,12 +1175,12 @@
 YAt(
-x, z
+loc)
 );%0D%0A
@@ -1196,32 +1196,11 @@
 urn 
-new Location(w, x, y, z)
+loc
 ;%0D%0A%09

Load the chunk before teleporting. I believe this will fix issue #1.