Advertisement
Guest User

RandomLocation

a guest
Sep 8th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.util.*;
  2. import org.bukkit.Location;
  3. import org.bukkit.Material;
  4. import org.bukkit.World;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.event.player.PlayerTeleportEvent;
  7.  
  8. public class RandomLocation
  9. {
  10.     private static class Location2D
  11.     {
  12.         public int highestY(World world)
  13.         {
  14.             Location l = new Location(world, x, 256D, z);
  15.             do
  16.             {
  17.                 if(l.getY() <= 0)
  18.                     return 257;
  19.                 l.add(0, -1, 0);
  20.             } while(l.getBlock().getType() == Material.AIR);
  21.             return l.getBlockY() + 1;
  22.         }
  23.  
  24.         public boolean canSpawn(World world)
  25.         {
  26.             Location l = new Location(world, x, 256D, z);
  27.             Material m;
  28.             do
  29.             {
  30.                 if(l.getY() <= 0)
  31.                     return false;
  32.                 l.add(0, -1, 0);
  33.                 m = l.getBlock().getType();
  34.             } while(m == Material.AIR);
  35.             return !l.getBlock().isLiquid() && m != Material.FIRE;
  36.         }
  37.  
  38.         public void random(Random random, double x0, double z0, double x1, double z1)
  39.         {
  40.             x = random(random, x0, x1);
  41.             z = random(random, z0, z1);
  42.         }
  43.         public static double random(Random random, double min, double max)
  44.         {
  45.             if(min >= max)
  46.                 return min;
  47.             else
  48.                 return random.nextDouble() * (max - min) + min;
  49.         }
  50.  
  51.         double x;
  52.         double z;
  53.  
  54.         Location2D(double x, double z)
  55.         {
  56.             this.x = x;
  57.             this.z = z;
  58.         }
  59.     }
  60.     public void execute(World w, Player p)
  61.     {
  62.         final double x = w.getSpawnLocation().getX();
  63.         final double z = w.getSpawnLocation().getZ();
  64.         final double range = 1000D;
  65.        
  66.         Location2D loc = new Location2D(x, z);
  67.         Random random = new Random();
  68.         final double x0 = loc.x - range;
  69.         final double z0 = loc.z - range;
  70.         final double x1 = loc.x + range;
  71.         final double z1 = loc.z + range;
  72.        
  73.         loc.random(random, x0, z0, x1, z1);
  74.        
  75.         boolean f = true;
  76.         int i;
  77.         for(i = 0; i < 10000 && f; i++)
  78.         {
  79.             f = false;
  80.             if(!loc.canSpawn(w))
  81.             {
  82.                 loc.random(random, x0, z0, x1, z1);
  83.                 f = true;
  84.             }
  85.         }
  86.         p.teleport(new Location(w, Math.floor(loc.x) + 0.5D, loc.highestY(w), Math.floor(loc.z) + 0.5D), PlayerTeleportEvent.TeleportCause.END_PORTAL);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement