[Code] (Maybe) better/more secure getHighestBlock method

Discussion in 'Resources' started by p000ison, Jun 21, 2012.

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

    p000ison

    Heyo Community,
    since I had problems with the World.getHighestBlockY() method I made it myself. Its only a small and really really really easy code, so If somebody knows how to make it faster let me know. But it way really annoying that I spawned below layer 0...

    Code:
        public static int getHighestBlockY(World world, int x, int z)
        {
            loadChunk(x, z, world);
            for (int i = world.getMaxHeight(); i > 0; i--) {
                if (world.getBlockTypeIdAt(x, i - 1, z) != 0) {
                    return i;
                }
            }
            return 0;
        }
     
        public static void loadChunk(int x, int z, World world)
        {
     
            Location loc = new Location(world, (double) x, 0.0, (double) z);
     
            Chunk chunk = world.getChunkAt(loc);
            if (!world.isChunkLoaded(chunk)) {
                world.loadChunk(chunk);
            }
        }
     
  2. Offline

    Ne0nx3r0

    I could be wrong, but I think that's about as fast as it gets without some form of caching. Even then I don't know that it would provide any noticeable improvement.

    *edit* Actually I don't know if you actually need to load the chunk first. You might experiment with that.
     
  3. Offline

    p000ison

    Not sure about caching, need someone whos experienced with that. With the Chunk loading: Its better to do it. Im most cases it works without, but sometimes it can happen that the chunk isnt loaded and you spawn below layer 0. So its possible and with this it should work.
     
Thread Status:
Not open for further replies.

Share This Page