ready = ($stateMeta & BlockLegacyMetadata::SAPLING_FLAG_READY) !== 0; } protected function writeStateToMeta() : int{ return $this->ready ? 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; } private function canBeSupportedBy(Block $block) : bool{ //TODO: tags would be better for this return $block instanceof Dirt || $block instanceof Grass || $block instanceof Gravel || $block instanceof Sand || $block instanceof Mycelium || $block instanceof Podzol; } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if(!$this->canBeSupportedBy($blockReplace->position->getWorld()->getBlock($blockReplace->position->down()))){ return false; } return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if($item instanceof Fertilizer || $item instanceof ItemBamboo){ if($this->grow()){ $item->pop(); return true; } } return false; } public function onNearbyBlockChange() : void{ if(!$this->canBeSupportedBy($this->position->getWorld()->getBlock($this->position->down()))){ $this->position->getWorld()->useBreakOn($this->position); } } private function grow() : bool{ $world = $this->position->getWorld(); if(!$world->getBlock($this->position->up())->canBeReplaced()){ return false; } $tx = new BlockTransaction($world); $bamboo = VanillaBlocks::BAMBOO(); $tx->addBlock($this->position, $bamboo) ->addBlock($this->position->up(), (clone $bamboo)->setLeafSize(Bamboo::SMALL_LEAVES)); $ev = new StructureGrowEvent($this, $tx); $ev->call(); if($ev->isCancelled()){ return false; } return $tx->apply(); } public function ticksRandomly() : bool{ return true; } public function onRandomTick() : void{ $world = $this->position->getWorld(); if($this->ready){ $this->ready = false; if($world->getFullLight($this->position) < 9 || !$this->grow()){ $world->setBlock($this->position, $this); } }elseif($world->getBlock($this->position->up())->canBeReplaced()){ $this->ready = true; $world->setBlock($this->position, $this); } } public function asItem() : Item{ return VanillaBlocks::BAMBOO()->asItem(); } }