How can I find out the Y coordinate of the highest placing block if I have the X and Z coordinates given? I want to place some chests random! PHP: public function randomChest(Level $level) { $randx = mt_rand(-200, 200); $randz = mt_rand(-200, 200); //$y = Coordinates of the highest placed block $chest = Block::get(Block::CHEST); $level->setBlock(new Vector3($randx, $y+1, $randz), $chest); }
PHP: $catch = [0, 9];$y = 1;while($y <= 128){ if(in_array($level->getBlockIdAt($x, $y, $z), $catch, true)) break;}
While loops should not be used in plugins, because nothing else can run on the thread until the loop is over.
You may not notice it because of how short the loop lasts, but when you use a while loop the thread stops until the loop is over, which would appear as the server froze for a moment on a pocket mine server. I don't think anyone should use while loops in plugins.
If you want to spawn a safe random chest. Basically it runs a loop checking if there are any blocks around that are solid just so it doesn't get stuck inside blocks and when it finds open area around it will place a chest there. PHP: public function safeRandomChest(Level $lvl, $rand = 10){$randVX = (mt_rand() % ($rand*20)-($rand*20) + $rand*20);//random depending on randomness you choose (default = 10)$randVZ = (mt_rand() % ($rand*20)-($rand*20) + $rand*20);//random depending on randomness you choose (default = 10)for($y = 0; $y <= 128; ++$y){//run loop for checking$b1_low = $level->getBlock(new Vector3($randVX-1, $y, $randVZ));//block low left$b2_low = $level->getBlock(new Vector3($randVX, $y, $randVZ+1));//block low right$b1_high = $level->getBlock(new Vector3($randVX+1, $y, $randVZ));//block high left$b2_high = $level->getBlock(new Vector3($randVX, $y+1, $randVZ));//block high top$b3_high = $level->getBlock(new Vector3($randVX, $y, $randVZ+1));//block high rightif(($b1_low instanceof Solid) and ($b2_low instanceof Solid)and ($b1_high instanceof Solid) and (b2_high instanceof Solid)and (b3_high instanceof Solid)) continue;//if all blocks around are solid, continue checking$level->setBlock(new Vector3($randVX, $y, $randVY), Block::get(Block::CHEST));//if around is empty, set blockbreak;}}
You are just setting the block ID. There isn't a chest tile associated with the block. You must add it too.