Most efficient way to find a random location in the world

Discussion in 'Plugin Development' started by InsertNameHere, Jul 17, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    InsertNameHere

    Currently, I have a wilderness plugin that generates a random location through a while loop that continues to run if the randomized block is either lava or in a water biome.

    Code:
    private void randomTP(CommandSender sender, Player target) {
    
        int minX = p.getMinX();
        int maxX = p.getMaxX();
        int minZ = p.getMinZ();
        int maxZ = p.getMaxZ();
        if (target == null) {
            if (sender instanceof ConsoleCommandSender) {
                sender.sendMessage(GRAY + "[" + DARK_RED + "Wilderness" + GRAY + "] " + GREEN + "You must be a player to run this command.");
                return;
            }
    
        Player player = (Player) sender;
        Location newLocation;
        Location oldLocation = player.getLocation();
        int x,z;Random r = new Random();
        player.sendMessage(GRAY + "[" + DARK_RED + "Wilderness" + GRAY + "] " + GREEN.toString() + BOLD + "Teleporting... Please be patient!");
    
        do {
            x = r.nextInt(maxX - minX + 1) + minX;
            z = r.nextInt(maxZ - minZ + 1) + minZ;
            newLocation = new Location(player.getWorld(), x, player.getWorld().getHighestBlockYAt(x, z), z);
        } while (newLocation.getBlock().getRelative(BlockFace.DOWN).getType().equals(Material.LAVA) || newLocation.getBlock().getType().equals(Material.AIR) || checkOcean(newLocation));
    
        if (p.getConfig().getBoolean("Display-distance-message"))
            player.sendMessage(GRAY + "[" + DARK_RED + "Wilderness" + GRAY + "] " + GREEN + "You have been teleported " + (int)newLocation.distance(oldLocation) + " blocks away.");
        player.teleport(newLocation);
    } else {
        Location newLocation;Location oldLocation = target.getLocation();
        int x,z;
        Random r = new Random();
        target.sendMessage(GRAY + "[" + DARK_RED + "Wilderness" + GRAY + "] " + GREEN.toString() + BOLD + "Teleporting... Please be patient!");
    
        do {
            x = r.nextInt(maxX - minX + 1) + minX;
            z = r.nextInt(maxZ - minZ + 1) + minZ;
            newLocation = new Location(target.getWorld(), x, target.getWorld().getHighestBlockYAt(x, z), z);
        } while (newLocation.getBlock().getRelative(BlockFace.DOWN).getType().equals(Material.LAVA) || newLocation.getBlock().getType().equals(Material.AIR) || checkOcean(newLocation));
    
        if (p.getConfig().getBoolean("Display-distance-message"))
            target.sendMessage(GRAY + "[" + DARK_RED + "Wilderness" + GRAY + "] " + GREEN + "You have been teleported " + (int)newLocation.distance(oldLocation) + " blocks away.");
        target.teleport(newLocation);sender.sendMessage(GRAY + "[" + DARK_RED + "Wilderness" + GRAY + "] " + GREEN + target.getName() + " has been randomly teleported.");
        }
    }
    
    private boolean checkOcean(Location location) {
    
        return location.getBlock().getBiome().equals(Biome.OCEAN) || location.getBlock().getBiome().equals(Biome.LUKEWARM_OCEAN) || location.getBlock().getBiome().equals(Biome.FROZEN_OCEAN)
    || location.getBlock().getBiome().equals(Biome.COLD_OCEAN) || location.getBlock().getBiome().equals(Biome.WARM_OCEAN) || location.getBlock().getBiome().equals(Biome.DEEP_OCEAN)
    || location.getBlock().getBiome().equals(Biome.DEEP_COLD_OCEAN) || location.getBlock().getBiome().equals(Biome.DEEP_WARM_OCEAN) || location.getBlock().getBiome().equals(Biome.DEEP_LUKEWARM_OCEAN)
    || location.getBlock().getBiome().equals(Biome.DEEP_FROZEN_OCEAN);}
    
    But I feel like this is an inefficient way of getting a location since I have a while loop continuously running until it finds a block. Is there any other way to make the code more efficient?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @InsertNameHere the location.getBlock() could be minimized by making a local variable of type Biome and checking the enum after that.
    Not a lot else that you can do outside that.
     
  3. Offline

    InsertNameHere

    @timtower Alright. I was wondering this since when I ran the plugin on my test server, it crashed. Sorry for "wasting" your time
     
  4. Offline

    timtower Administrator Administrator Moderator

    If it was wasting then I wouldn't be here :p
     
Thread Status:
Not open for further replies.

Share This Page