Solved Random Teleport (nether roof)

Discussion in 'Plugin Development' started by Joshuak52, Jan 21, 2024.

  1. Offline

    Joshuak52

    I was trying to make a simple lightweight random teleport plugin for my SMP and it all works fine other than Nether world. For some reason I keep teleporting to the nether roof. I tried limiting the Highest Y coordinate but that did not help. Any advise?

    Code:
    private Location getRandomLocation(World world) {
            WorldBorder border = world.getWorldBorder();
            Location center = border.getCenter();
            double size = border.getSize();
            Random random = new Random();
            Location location;
    
            do {
                double x = center.getX() + random.nextDouble() * size - size / 2;
                double z = center.getZ() + random.nextDouble() * size - size / 2;
                double highestY = world.getHighestBlockYAt((int) x, (int) z);
                double y = Math.min(highestY, 100); // Ensure y is no greater than 100
    
                location = new Location(world, x, y, z);
            } while (isLocationInWater(location) || !isSolidBlock(location));
    
            return location;
        }
    
        private boolean isLocationInWater(Location location) {
            Block block = location.getBlock();
            return block.getType() == Material.WATER;
        }
    
        private boolean isSolidBlock(Location location) {
            Block block = location.getBlock();
            return block.getType().isSolid();
        }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Joshuak52 Have you considered checking for bedrock?
     
  3. Offline

    Joshuak52

    I've gotten it fixed now

    Code:
        private Location getRandomLocation(World world) {
            WorldBorder border = world.getWorldBorder();
            Location center = border.getCenter();
            double size = border.getSize();
            Random random = new Random();
            Location location;
    
            if (world.getEnvironment() == World.Environment.NETHER) {
                // Nether-specific location finding
                do {
                    double x = center.getX() + random.nextDouble() * size - size / 2;
                    double z = center.getZ() + random.nextDouble() * size - size / 2;
    
                    location = findSuitableLocationNether(world, x, z);
                } while (location == null);
            } else {
                // Other worlds
                do {
                    double x = center.getX() + random.nextDouble() * size - size / 2;
                    double z = center.getZ() + random.nextDouble() * size - size / 2;
                    double highestY = world.getHighestBlockYAt((int) x, (int) z);
                    double y = Math.min(highestY, 100); // Ensure y is no greater than 100
    
                    location = new Location(world, x, y, z);
                } while (isLocationInWater(location) || !isSolidBlock(location));
            }
    
            return location;
        }
    
        private Location findSuitableLocationNether(World world, double x, double z) {
            for (int y = 32; y <= 100; y++) {
                Location location = new Location(world, x, y, z);
                if (isSuitableLocationNether(location)) {
                    return location;
                }
            }
            return null;
        }
    
        private boolean isSuitableLocationNether(Location location) {
            Block standingBlock = location.getBlock();
            Block aboveBlock = standingBlock.getRelative(BlockFace.UP);
            Block twoAboveBlock = aboveBlock.getRelative(BlockFace.UP);
    
            return standingBlock.getType().isSolid() && standingBlock.getType() != Material.MAGMA_BLOCK
                   && aboveBlock.getType() == Material.AIR && twoAboveBlock.getType() == Material.AIR;
        }
    
        private boolean isLocationInWater(Location location) {
            Block block = location.getBlock();
            return block.getType() == Material.WATER;
        }
    
        private boolean isSolidBlock(Location location) {
            Block block = location.getBlock();
            return block.getType().isSolid();
        }
     

Share This Page