/** * Sets on Vector3 the data from a Block object, * does block updates and puts the changes to the send queue. * * 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. * * @param Vector3 $pos * @param Block $block * @param bool $direct @deprecated * @param bool $update * @param bool $extended * * @return bool Whether the block has been updated or not */ public function setBlock(Vector3 $pos, Block $block, $direct = false, $update = true, $extended = false) { 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); $ev->getBlock()->onUpdate(self::BLOCK_UPDATE_REDSTONE); } $this->updateAround($pos, $extended); } return true; } return false; }
/** * Sets on Vector3 the data from a Block object, * does block updates and puts the changes to the send queue. * * 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. * * @param Vector3 $pos * @param Block $block * @param bool $direct * @param bool $update * * @return bool Whether the block has been updated or not */ public function setBlock(Vector3 $pos, Block $block, $direct = false, $update = true) { if ($pos->y < 0 or $pos->y >= 128) { return false; } unset($this->blockCache[$index = PHP_INT_SIZE === 8 ? ($pos->x & 0xfffffff) << 35 | ($pos->y & 0x7f) << 28 | $pos->z & 0xfffffff : $pos->x . ":" . $pos->y . ":" . $pos->z]); 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); $index = PHP_INT_SIZE === 8 ? ($pos->x >> 4 & 4294967295.0) << 32 | $pos->z >> 4 & 4294967295.0 : ($pos->x >> 4) . ":" . ($pos->z >> 4); if (ADVANCED_CACHE == true) { Cache::remove("world:" . $this->getId() . ":" . $index); } if ($direct === true) { $pk = new UpdateBlockPacket(); $pk->x = $pos->x; $pk->y = $pos->y; $pk->z = $pos->z; $pk->block = $block->getId(); $pk->meta = $block->getDamage(); Server::broadcastPacket($this->getUsingChunk($pos->x >> 4, $pos->z >> 4), $pk); } else { if (!$pos instanceof Position) { $pos = $this->temporalPosition->setComponents($pos->x, $pos->y, $pos->z); } $block->position($pos); if (!isset($this->changedBlocks[$index])) { $this->changedBlocks[$index] = []; $this->changedCount[$index] = 0; } $Y = $pos->y >> 4; if (!isset($this->changedBlocks[$index][$Y])) { $this->changedBlocks[$index][$Y] = []; $this->changedCount[$index] |= 1 << $Y; } $this->changedBlocks[$index][$Y][] = clone $block; } if ($update === true) { $this->updateAllLight($block); $this->server->getPluginManager()->callEvent($ev = new BlockUpdateEvent($block)); if (!$ev->isCancelled()) { $ev->getBlock()->onUpdate(self::BLOCK_UPDATE_NORMAL); foreach ($this->getNearbyEntities(new AxisAlignedBB($block->x - 1, $block->y - 1, $block->z - 1, $block->x + 2, $block->y + 2, $block->z + 2)) as $entity) { $entity->scheduleUpdate(); } } $this->updateAround($pos); } return true; } return false; }
/** * Sets on Vector3 the data from a Block object, * does block updates and puts the changes to the send queue. * * 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. * * @param Vector3 $pos * @param Block $block * @param bool $direct * @param bool $update * * @return bool */ public function setBlock(Vector3 $pos, Block $block, $direct = false, $update = true) { if ($pos->y < 0 or $pos->y >= 128 or !$block instanceof Block) { return false; } if ($this->getChunkAt($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 = new Position($pos->x, $pos->y, $pos->z, $this); } $block->position($pos); $index = Level::chunkHash($pos->x >> 4, $pos->z >> 4); if (ADVANCED_CACHE == true) { Cache::remove("world:" . $this->getID() . ":" . $index); } if ($direct === true) { $pk = new UpdateBlockPacket(); $pk->x = $pos->x; $pk->y = $pos->y; $pk->z = $pos->z; $pk->block = $block->getID(); $pk->meta = $block->getDamage(); foreach ($this->getUsingChunk($pos->x >> 4, $pos->z >> 4) as $player) { /** @var Player $player */ $player->directDataPacket($pk); } } else { if (!$pos instanceof Position) { $pos = new Position($pos->x, $pos->y, $pos->z, $this); } $block->position($pos); if (!isset($this->changedBlocks[$index])) { $this->changedBlocks[$index] = []; $this->changedCount[$index] = 0; } $Y = $pos->y >> 4; if (!isset($this->changedBlocks[$index][$Y])) { $this->changedBlocks[$index][$Y] = []; $this->changedCount[$index] |= 1 << $Y; } $this->changedBlocks[$index][$Y][] = clone $block; } if ($update === true) { $this->updateAround($pos, self::BLOCK_UPDATE_NORMAL); $block->onUpdate(self::BLOCK_UPDATE_NORMAL); } } }