treeType = $treeType; } protected function writeStateToMeta() : int{ return ($this->ready ? BlockLegacyMetadata::SAPLING_FLAG_READY : 0); } public function readStateFromData(int $id, int $stateMeta) : void{ $this->ready = ($stateMeta & BlockLegacyMetadata::SAPLING_FLAG_READY) !== 0; } public function getStateBitmask() : int{ return 0b1000; } public function isReady() : bool{ return $this->ready; } /** @return $this */ public function setReady(bool $ready) : self{ $this->ready = $ready; return $this; } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ $down = $this->getSide(Facing::DOWN); if($down->getId() === BlockLegacyIds::GRASS || $down->getId() === BlockLegacyIds::DIRT || $down->getId() === BlockLegacyIds::FARMLAND){ return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } return false; } public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if($item instanceof Fertilizer && $this->grow($player)){ $item->pop(); return true; } return false; } public function onNearbyBlockChange() : void{ if($this->getSide(Facing::DOWN)->isTransparent()){ $this->position->getWorld()->useBreakOn($this->position); } } public function ticksRandomly() : bool{ return true; } public function onRandomTick() : void{ $world = $this->position->getWorld(); if($world->getFullLightAt($this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ()) >= 8 && mt_rand(1, 7) === 1){ if($this->ready){ $this->grow(null); }else{ $this->ready = true; $world->setBlock($this->position, $this); } } } private function grow(?Player $player) : bool{ $random = new Random(mt_rand()); $tree = TreeFactory::get($random, $this->treeType); $transaction = $tree?->getBlockTransaction($this->position->getWorld(), $this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ(), $random); if($transaction === null){ return false; } $ev = new StructureGrowEvent($this, $transaction, $player); $ev->call(); if(!$ev->isCancelled()){ return $transaction->apply(); } return false; } public function getFuelTime() : int{ return 100; } }