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;
}