Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.World;
- import org.bukkit.entity.Player;
- import org.bukkit.event.player.PlayerTeleportEvent;
- public class RandomLocation
- {
- private static class Location2D
- {
- public int highestY(World world)
- {
- Location l = new Location(world, x, 256D, z);
- do
- {
- if(l.getY() <= 0)
- return 257;
- l.add(0, -1, 0);
- } while(l.getBlock().getType() == Material.AIR);
- return l.getBlockY() + 1;
- }
- public boolean canSpawn(World world)
- {
- Location l = new Location(world, x, 256D, z);
- Material m;
- do
- {
- if(l.getY() <= 0)
- return false;
- l.add(0, -1, 0);
- m = l.getBlock().getType();
- } while(m == Material.AIR);
- return !l.getBlock().isLiquid() && m != Material.FIRE;
- }
- public void random(Random random, double x0, double z0, double x1, double z1)
- {
- x = random(random, x0, x1);
- z = random(random, z0, z1);
- }
- public static double random(Random random, double min, double max)
- {
- if(min >= max)
- return min;
- else
- return random.nextDouble() * (max - min) + min;
- }
- double x;
- double z;
- Location2D(double x, double z)
- {
- this.x = x;
- this.z = z;
- }
- }
- public void execute(World w, Player p)
- {
- final double x = w.getSpawnLocation().getX();
- final double z = w.getSpawnLocation().getZ();
- final double range = 1000D;
- Location2D loc = new Location2D(x, z);
- Random random = new Random();
- final double x0 = loc.x - range;
- final double z0 = loc.z - range;
- final double x1 = loc.x + range;
- final double z1 = loc.z + range;
- loc.random(random, x0, z0, x1, z1);
- boolean f = true;
- int i;
- for(i = 0; i < 10000 && f; i++)
- {
- f = false;
- if(!loc.canSpawn(w))
- {
- loc.random(random, x0, z0, x1, z1);
- f = true;
- }
- }
- p.teleport(new Location(w, Math.floor(loc.x) + 0.5D, loc.highestY(w), Math.floor(loc.z) + 0.5D), PlayerTeleportEvent.TeleportCause.END_PORTAL);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement