getHighestBlockYAt returning 0 even though chunk is loaded [818]

Discussion in 'Plugin Development' started by 4LT, Jun 7, 2011.

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

    4LT

    I call this with a random x and z:

    Code:
        public void loadSpawn(int x, int z)
        {
            log.info("Chunk loaded successfully: " + world.loadChunk(x/16, z/16, true));
            int spawnY = world.getHighestBlockYAt(x, z);
            log.info("Setting spawn at [" + x + ", " + spawnY + ", " + z + "]");
            world.getBlockAt(x, spawnY, z).setType(Material.SPONGE);
            world.setSpawnLocation(x, spawnY, z);
        }
    
    and get this output:
    Code:
    13:22:09 [INFO] [Siege] Initializing...
    13:22:09 [INFO] [Siege] Chunk loaded successfully: true
    13:22:09 [INFO] [Siege] Setting spawn at [8273, 0, -525]
    
    However, if I use a chunk loaded by the game (not the loadChunk method) I get the correct y.
     
  2. Offline

    simphax

    I have the same problem. Solution?
     
  3. Offline

    NuclearW

    I watched this thread long ago when I had this problem.

    It isn't pretty, but this is how I solved it:

    Code:
    public int getHighestBlockYAt(int x, int z, World world) {
            ensureChunkLoaded(x, z, world);
            return world.getHighestBlockYAt(x, z);
        }
        
        //Make sure the target is loaded, if not, load it
        public void ensureChunkLoaded(int x, int z, World world) {
            //Apparently all the chunk functions need to deal with chunk ints, or with a Chunk type variable, so make a location
            Location randlocation = new Location(world, Double.parseDouble(Integer.toString(x)), 0.0, Double.parseDouble(Integer.toString(z)));
            //To get a Chunk
            Chunk chunk = world.getChunkAt(randlocation);
            //To check if loaded and if not load
            if(!world.isChunkLoaded(chunk)) world.loadChunk(chunk);
        }
    Hope this helps, although looking at this old code makes me cringe, it does work.
     
  4. Offline

    xpansive

    Cool, I was having this problem with my world generator. Finally a solution!
     
Thread Status:
Not open for further replies.

Share This Page