World::getHighestYAt(x,z) doesnt works properly?

I want to do “random teleports”.
Writed this code:

} else if (plain.equalsIgnoreCase("rtp")) {
            World world = Sponge.getServer().getWorld(Sponge.getServer().getDefaultWorldName()).get();
            int x = NevendaarUtils.RANDOM.nextInt(20000) - 10000;
            int z = NevendaarUtils.RANDOM.nextInt(20000) - 10000;
            int y = world.getHighestYAt(x, z);
            Sponge.getServer().getBroadcastChannel().send(Text.of("highest = " + y));
            event.getCause().first(Player.class).get().setLocation(new Location<World>(world, x, y, z));
            Sponge.getServer().getBroadcastChannel().send(Text.of("Successfully!"));
        }

But Y is always 0…

What Im doing wrong?

my guess is that the chunk has not loaded and so the default height of that XZ location has not spawned, so it would be 0. Try doing this before you get the Y height.

Location<World> blockChunk = new Location<>(world, X, 32, Z);
world.loadChunk(blockChunk.getChunkPosition(), true);

I use 32 because the default water height is 32 in overworld, so the block height will be around there and due to the fact chunks now have a Y, it should get roughly the correct height.

2 Likes

Thank you! Exactly what I want)

1 Like