1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Setting a block in a radius from a player

Discussion in 'Development' started by RoyalMCPE, Apr 24, 2017.

  1. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    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?
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    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
     
  3. Aviv

    Aviv Baby Zombie

    Messages:
    156
    By code:
    PHP:
    $x $player->mt_rand(515);
    $z $player->mt_rand(515);
    $y $player->getLevel()->getHighestBlockAt($x$z);
    $player->getLevel()->setBlock(Block::get($blockid$blockdamage), new Vector3($x$y$z));
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    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->mt_rand(-1515);
      
    $z $player->mt_rand(-1515);
      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.
     
    Last edited: Apr 24, 2017
    Primus and corytortoise like this.
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    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
     
  6. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    He wants to do it when a player executes a command though, so the second point doesn't really matter anyway.
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    grammar check? a random one? or all of them?
     
  8. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Recursive functions aren't always the greatest in PocketMine.
     
    SOFe likes this.
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Suggested improvement for better performance:
    PHP:
    public function spawnRandomBlock(Player $player): bool {
      
    $dist 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$player->y$player->$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.
     
    rektpixel and Sandertv like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.