I imagine you would begin by getting the coordinates of the highest block, then you could use a loop to find the next block below it, using the coordinates from the highest block as a starting place.
Well you can write your own function to do that PHP: public function getSecondHighestBlockAt(Level $level,int $x,$int z): int{ $chunk = $level->getChunk($x >> 4,$z >> 4,true); $first = true; for($y = $level->getWorldHeight();$y >= 0;$y--) if($chunk->getBlockId($x & 0x0f,$y,$z & 0x0f) !== 0){ if($first) $first = false; else return $y; } return -1; }