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

Setting blocks in a radius of another

Discussion in 'Development' started by iCirgio, Jan 29, 2018.

  1. iCirgio

    iCirgio Slime

    Messages:
    92
    GitHub:
    lolnova
    As the title said I would like to know how to set a block in a radius of another blick. I tried $block->x + 1 but i want it to form like a square around the block
     
  2. DaPigGuy

    DaPigGuy Slime

    Messages:
    86
    GitHub:
    DaPigGuy
    PHP:
    $radius 1;
    for (
    $x = -$radius$x <= $radius$x++) {
           for (
    $y = -$radius$y <= $radius$y++) {
                  for (
    $z = -$radius$z <= $radius$z++) {
                         
    $block->getLevel()->setBlock($block->add($x$y$z), Block::get($id$meta);
                  }
           }
    }
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    /** @var Block $block */
    foreach($block->getAllSides() as $pos){
        
    $block->getLevel()->setBlock($posBlock::get(intint));
    }
    NOTE: $pos is actually a Block instance whichwhich indir extends Vector3. Also, you may want to set the fourth parameter of Level::setBlock() as false to disallow any block updates from happening. Level::setBlock($pos, $block, false, false);
     
    xXNiceAssassinlo YT and DaPigGuy like this.
  4. iCirgio

    iCirgio Slime

    Messages:
    92
    GitHub:
    lolnova
    Hey it worked, but it spawns like this

    x
    xox
    x

    But I want it to spawn like

    XXX
    XoX
    XXX
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Oh I see, try:
    PHP:
    foreach(array_merge(
        [
            
    $block->add(101),
            
    $block->add(-10, -1),
            
    $block->add(10, -1),
            
    $block->add(-101)
        ],
        
    $block->getAllSides()
    ) as 
    $pos){
    ]
    But in that case, its better to use @DaPigGuy's with an exception to not loop through the main (center) block.
     
  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.