pocketmine\level\Level::setBlock PHP Method

setBlock() public method

If $direct is true, it'll send changes directly to players. if false, it'll be queued and the best way to send queued changes will be done in the next tick. This way big changes can be sent on a single chunk update packet instead of thousands of packets. If $update is true, it'll get the neighbour blocks (6 sides) and update them. If you are doing big changes, you might want to set this to false, then update manually.
public setBlock ( Vector3 $pos, Block $block, boolean $direct = false, boolean $update = true ) : boolean
$pos pocketmine\math\Vector3
$block pocketmine\block\Block
$direct boolean @deprecated
$update boolean
return boolean Whether the block has been updated or not
    public function setBlock(Vector3 $pos, Block $block, bool $direct = false, bool $update = true) : bool
    {
        $pos = $pos->floor();
        if ($pos->y < 0 or $pos->y >= 128) {
            return false;
        }
        if ($this->getChunk($pos->x >> 4, $pos->z >> 4, true)->setBlock($pos->x & 0xf, $pos->y & 0x7f, $pos->z & 0xf, $block->getId(), $block->getDamage())) {
            if (!$pos instanceof Position) {
                $pos = $this->temporalPosition->setComponents($pos->x, $pos->y, $pos->z);
            }
            $block->position($pos);
            unset($this->blockCache[Level::blockHash($pos->x, $pos->y, $pos->z)]);
            $index = Level::chunkHash($pos->x >> 4, $pos->z >> 4);
            if ($direct === true) {
                $this->sendBlocks($this->getChunkPlayers($pos->x >> 4, $pos->z >> 4), [$block], UpdateBlockPacket::FLAG_ALL_PRIORITY);
                unset($this->chunkCache[$index]);
            } else {
                if (!isset($this->changedBlocks[$index])) {
                    $this->changedBlocks[$index] = [];
                }
                $this->changedBlocks[$index][Level::blockHash($block->x, $block->y, $block->z)] = clone $block;
            }
            foreach ($this->getChunkLoaders($pos->x >> 4, $pos->z >> 4) as $loader) {
                $loader->onBlockChanged($block);
            }
            if ($update === true) {
                $this->updateAllLight($block);
                $this->server->getPluginManager()->callEvent($ev = new BlockUpdateEvent($block));
                if (!$ev->isCancelled()) {
                    foreach ($this->getNearbyEntities(new AxisAlignedBB($block->x - 1, $block->y - 1, $block->z - 1, $block->x + 1, $block->y + 1, $block->z + 1)) as $entity) {
                        $entity->scheduleUpdate();
                    }
                    $ev->getBlock()->onUpdate(self::BLOCK_UPDATE_NORMAL);
                }
                $this->updateAround($pos);
            }
            return true;
        }
        return false;
    }

Usage Example

Esempio n. 1
1
 public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz)
 {
     if ($this->meta === Item::AIR) {
         if ($target instanceof Liquid) {
             $level->setBlock($target, new Air(), true);
             if (($player->gamemode & 0x1) === 0) {
                 $this->meta = $target instanceof Water ? Item::WATER : Item::LAVA;
             }
             return true;
         }
     } elseif ($this->meta === Item::WATER) {
         //Support Make Non-Support Water to Support Water
         if ($block->getID() === self::AIR || $block instanceof Water && ($block->getDamage() & 0x7) != 0x0) {
             $water = new Water();
             $level->setBlock($block, $water, true);
             $water->place(clone $this, $block, $target, $face, $fx, $fy, $fz, $player);
             if (($player->gamemode & 0x1) === 0) {
                 $this->meta = 0;
             }
             return true;
         }
     } elseif ($this->meta === Item::LAVA) {
         if ($block->getID() === self::AIR) {
             $level->setBlock($block, new Lava(), true);
             if (($player->gamemode & 0x1) === 0) {
                 $this->meta = 0;
             }
             return true;
         }
     }
     return false;
 }
All Usage Examples Of pocketmine\level\Level::setBlock
Level