When a player runs a command I want it to set a block in a 5-15 block radius from the player, how would I be able to do this?
you can use mtrand to create a random 5 -15 blocks you can + all cords the random number THEN - all cords of the random number to create a box... or the possibilities of where it would spawn with these two vectors which one is min and other is max, you mtrand the min of each axises and the max of the relative axises that shall give you a coordinate and that shall be the coordinate you want to spawn the block at now just do the normal setblock routine and you shall be done
By code: PHP: $x = $player->x + mt_rand(5, 15);$z = $player->z + mt_rand(5, 15);$y = $player->getLevel()->getHighestBlockAt($x, $z);$player->getLevel()->setBlock(Block::get($blockid, $blockdamage), new Vector3($x, $y, $z));
No, just no. Please read before you're showing the code first. He's asking for a distance of 5 - 15 blocks away from the player. This could be - and +, not only plus. What you could do, is something like this: PHP: public function spawnRandomBlock(Player $player): bool { $x = $player->x + mt_rand(-15, 15); $z = $player->z + mt_rand(-15, 15); if($player->distance(new Vector3($x, $player->y, $z)) < 5) { return $this->spawnRandomBlock($player); } $player->level->setBlock(new Vector3($x, $player->y, $z), Block::get(Block::STONE)); return true;} This code however works with a rectangle shape. The maximum radius is not actually a sphere, but a rectangle.
also in the recursive part you are suppose to return $this->spawnRandomBlock($player); not false also using a Position would be enough as there's no need to use player
He wants to do it when a player executes a command though, so the second point doesn't really matter anyway.
Suggested improvement for better performance: PHP: public function spawnRandomBlock(Player $player): bool { $dist = 5 + lcg_value() * 10; $angle = lcg_value() * M_PI * 2; // M_PI*2 radians = 360 degrees $x = cos($angle) * $dist; $z = sin($angle) * $dist; // sin and cos don't exactly map to x and z in practice, but // positive/negative and x/z can be swapped in this case, // so I'm too lazy to check $player->level->setBlock(new Vector3($player->x + $x, $player->y, $player->z + $z), Block::get(Block::STONE)); return true;} Also note that if $x==15 and $z==15 in your original code, it still violates the requirements.