/** * Sets the raw block metadata. * * @param int $x * @param int $y * @param int $z * @param int $data 0-15 */ public function setBlockDataAt($x, $y, $z, $data) { unset($this->blockCache[Level::blockHash($x, $y, $z)]); $this->getChunk($x >> 4, $z >> 4, true)->setBlockData($x & 0xf, $y & 0x7f, $z & 0xf, $data & 0xf); if (!isset($this->changedBlocks[$index = Level::chunkHash($x >> 4, $z >> 4)])) { $this->changedBlocks[$index] = []; } $this->changedBlocks[$index][Level::blockHash($x, $y, $z)] = $v = new Vector3($x, $y, $z); foreach ($this->getChunkLoaders($x >> 4, $z >> 4) as $loader) { $loader->onBlockChanged($v); } }
public function explodeB() { $send = []; $updateBlocks = []; $source = (new Vector3($this->source->x, $this->source->y, $this->source->z))->floor(); $yield = 1 / $this->size * 100; if ($this->what instanceof Entity) { $this->level->getServer()->getPluginManager()->callEvent($ev = new EntityExplodeEvent($this->what, $this->source, $this->affectedBlocks, $yield)); if ($ev->isCancelled()) { return false; } else { $yield = $ev->getYield(); $this->affectedBlocks = $ev->getBlockList(); } } $explosionSize = $this->size * 2; $minX = Math::floorFloat($this->source->x - $explosionSize - 1); $maxX = Math::ceilFloat($this->source->x + $explosionSize + 1); $minY = Math::floorFloat($this->source->y - $explosionSize - 1); $maxY = Math::ceilFloat($this->source->y + $explosionSize + 1); $minZ = Math::floorFloat($this->source->z - $explosionSize - 1); $maxZ = Math::ceilFloat($this->source->z + $explosionSize + 1); $explosionBB = new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ); $list = $this->level->getNearbyEntities($explosionBB, $this->what instanceof Entity ? $this->what : null); foreach ($list as $entity) { $distance = $entity->distance($this->source) / $explosionSize; if ($distance <= 1) { $motion = $entity->subtract($this->source)->normalize(); $impact = (1 - $distance) * ($exposure = 1); $damage = (int) (($impact * $impact + $impact) / 2 * 8 * $explosionSize + 1); if ($this->what instanceof Entity) { $ev = new EntityDamageByEntityEvent($this->what, $entity, EntityDamageEvent::CAUSE_ENTITY_EXPLOSION, $damage); } elseif ($this->what instanceof Block) { $ev = new EntityDamageByBlockEvent($this->what, $entity, EntityDamageEvent::CAUSE_BLOCK_EXPLOSION, $damage); } else { $ev = new EntityDamageEvent($entity, EntityDamageEvent::CAUSE_BLOCK_EXPLOSION, $damage); } $entity->attack($ev->getFinalDamage(), $ev); $entity->setMotion($motion->multiply($impact)); } } $air = Item::get(Item::AIR); foreach ($this->affectedBlocks as $block) { if ($block->getId() === Block::TNT) { $mot = (new Random())->nextSignedFloat() * M_PI * 2; $tnt = Entity::createEntity("PrimedTNT", $this->level->getChunk($block->x >> 4, $block->z >> 4), new Compound("", ["Pos" => new Enum("Pos", [new Double("", $block->x + 0.5), new Double("", $block->y), new Double("", $block->z + 0.5)]), "Motion" => new Enum("Motion", [new Double("", -sin($mot) * 0.02), new Double("", 0.2), new Double("", -cos($mot) * 0.02)]), "Rotation" => new Enum("Rotation", [new Float("", 0), new Float("", 0)]), "Fuse" => new Byte("Fuse", mt_rand(10, 30))])); $tnt->spawnToAll(); } elseif (mt_rand(0, 100) < $yield) { foreach ($block->getDrops($air) as $drop) { $this->level->dropItem($block->add(0.5, 0.5, 0.5), Item::get(...$drop)); } } $this->level->setBlockIdAt($block->x, $block->y, $block->z, 0); $pos = new Vector3($block->x, $block->y, $block->z); for ($side = 0; $side < 5; $side++) { $sideBlock = $pos->getSide($side); if (!isset($this->affectedBlocks[$index = Level::blockHash($sideBlock->x, $sideBlock->y, $sideBlock->z)]) and !isset($updateBlocks[$index])) { $this->level->getServer()->getPluginManager()->callEvent($ev = new BlockUpdateEvent($this->level->getBlock($sideBlock))); if (!$ev->isCancelled()) { $ev->getBlock()->onUpdate(Level::BLOCK_UPDATE_NORMAL); } $updateBlocks[$index] = true; } } $send[] = new Vector3($block->x - $source->x, $block->y - $source->y, $block->z - $source->z); } $pk = new ExplodePacket(); $pk->x = $this->source->x; $pk->y = $this->source->y; $pk->z = $this->source->z; $pk->radius = $this->size; $pk->records = $send; $this->level->addChunkPacket($source->x >> 4, $source->z >> 4, $pk); return true; }
public function setRedstoneUpdateList($type, $power) { if ($type === Level::REDSTONE_UPDATE_NORMAL) { if ($power > $this->getPower() + 1) { $this_hash = Level::blockHash($this->x, $this->y, $this->z); $this->getLevel()->RedstoneUpdaters[$this_hash] = false; $thispower = $power - 1; $this->getLevel()->RedstoneUpdateList[$this_hash] = ['x' => $this->x, 'y' => $this->y, 'z' => $this->z, 'power' => $thispower]; for ($side = 2; $side <= 5; $side++) { $near = $this->getSide($side); if ($near instanceof RedstoneTransmitter) { $near->setRedstoneUpdateList($type, $thispower); } else { $around_down = $near->getSide(0); $around_up = $near->getSide(1); if ($near->getId() == Block::AIR and $around_down instanceof RedstoneTransmitter) { $around_down->setRedstoneUpdateList($type, $thispower); } elseif (!$near instanceof Transparent and $around_up instanceof RedstoneTransmitter) { $around_up->setRedstoneUpdateList($type, $thispower); } } } $this->getLevel()->RedstoneUpdaters[$this_hash] = true; $this->doRedstoneListUpdate(); } else { return; } } if ($type === Level::REDSTONE_UPDATE_REPOWER) { $type = Level::REDSTONE_UPDATE_NORMAL; $this_hash = Level::blockHash($this->x, $this->y, $this->z); unset($this->getLevel()->RedstoneRepowers[$this_hash]); $thispower = $this->getPower(); $this->getLevel()->RedstoneUpdateList[$this_hash] = ['x' => $this->x, 'y' => $this->y, 'z' => $this->z, 'power' => $thispower]; for ($side = 2; $side <= 5; $side++) { $near = $this->getSide($side); if ($near instanceof RedstoneTransmitter) { $near->setRedstoneUpdateList($type, $thispower); } else { $around_down = $near->getSide(0); $around_up = $near->getSide(1); if ($near->getId() == Block::AIR and $around_down instanceof RedstoneTransmitter) { $around_down->setRedstoneUpdateList($type, $thispower); } elseif (!$near instanceof Transparent and $around_up instanceof RedstoneTransmitter) { $around_up->setRedstoneUpdateList($type, $thispower); } } } $this->doRedstoneListUpdate(); } if ($type === Level::REDSTONE_UPDATE_LOSTPOWER) { if ($this->getPower() !== 0 and $power >= $this->getPower + 1) { $thispower = $this->getPower(); $this_hash = Level::blockHash($this->x, $this->y, $this->z); $this->getLevel()->RedstoneUpdateList[$this_hash] = ['x' => $this->x, 'y' => $this->y, 'z' => $this->z, 'power' => 0]; $FetchedMaxPower = $this->fetchMaxPower(); if ($FetchedMaxPower == Block::REDSTONESOURCEPOWER) { unset($this->getLevel()->RedstoneUpdateList[$this_hash]); $this->getLevel()->RedstoneRepowers[$this_hash] = ['x' => $this->x, 'y' => $this->y, 'z' => $this->z]; return; } $this->getLevel()->RedstoneUpdaters[$this_hash] = false; for ($side = 2; $side <= 5; $side++) { $near = $this->getSide($side); if ($near instanceof RedstoneTransmitter) { $near_hash = Level::blockHash($near->x, $near->y, $near->z); if (isset($this->getLevel()->RedstoneUpdateList[$near_hash])) { continue; } $near->setRedstoneUpdateList($type, $thispower); } else { $around_down = $near->getSide(0); $around_up = $near->getSide(1); if ($near->getId() == Block::AIR and $around_down instanceof RedstoneTransmitter) { $around_down_hash = Level::blockHash($around_down->x, $around_down->y, $around_down->z); if (isset($this->getLevel()->RedstoneUpdateList[$around_down_hash])) { continue; } $around_down->setRedstoneUpdateList($type, $thispower); } elseif (!$near instanceof Transparent and $around_up instanceof RedstoneTransmitter) { $around_up_hash = Level::blockHash($around_up->x, $around_up->y, $around_up->z); if (isset($this->getLevel()->RedstoneUpdateList[$around_up_hash])) { continue; } $around_up->setRedstoneUpdateList($type, $thispower); } } } $this->getLevel()->RedstoneUpdaters[$this_hash] = true; $this->doRedstoneListUpdate(); } } }
public function getHash() { return Level::blockHash($this->x, $this->y, $this->z); }