boundedInt(5, 0, self::MAX_AGE, $this->age); $w->bool($this->berries); $w->bool($this->head); } public function hasBerries() : bool{ return $this->berries; } /** @return $this */ public function setBerries(bool $berries) : self{ $this->berries = $berries; return $this; } public function isHead() : bool{ return $this->head; } /** @return $this */ public function setHead(bool $head) : self{ $this->head = $head; return $this; } public function getAge() : int{ return $this->age; } /** @return $this */ public function setAge(int $age) : self{ if($age < 0 || $age > self::MAX_AGE){ throw new \InvalidArgumentException("Age must be in range 0-" . self::MAX_AGE); } $this->age = $age; return $this; } public function canClimb() : bool{ return true; } public function getLightLevel() : int{ return $this->berries ? 14 : 0; } private function canBeSupportedBy(Block $block) : bool{ return $block->getSupportType(Facing::DOWN)->equals(SupportType::FULL()) || $block->hasSameTypeId($this); } public function onNearbyBlockChange() : void{ if(!$this->canBeSupportedBy($this->getSide(Facing::UP))){ $this->position->getWorld()->useBreakOn($this->position); } } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if(!$this->canBeSupportedBy($blockReplace->getSide(Facing::UP))){ return false; } $this->age = mt_rand(0, self::MAX_AGE); return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($this->berries){ $this->position->getWorld()->dropItem($this->position, $this->asItem()); $this->position->getWorld()->addSound($this->position, new GlowBerriesPickSound()); $this->position->getWorld()->setBlock($this->position, $this->setBerries(false)); return true; } if($item instanceof Fertilizer){ $ev = new BlockGrowEvent($this, (clone $this) ->setBerries(true) ->setHead(!$this->getSide(Facing::DOWN)->hasSameTypeId($this)) ); $ev->call(); if($ev->isCancelled()){ return false; } $item->pop(); $this->position->getWorld()->setBlock($this->position, $ev->getNewState()); return true; } return false; } public function onRandomTick() : void{ $head = !$this->getSide(Facing::DOWN)->hasSameTypeId($this); if($head !== $this->head){ $this->position->getWorld()->setBlock($this->position, $this->setHead($head)); } if($this->age < self::MAX_AGE && mt_rand(1, 10) === 1){ $growthPos = $this->position->getSide(Facing::DOWN); $world = $growthPos->getWorld(); if($world->isInWorld($growthPos->getFloorX(), $growthPos->getFloorY(), $growthPos->getFloorZ())){ $block = $world->getBlock($growthPos); if($block->getTypeId() === BlockTypeIds::AIR){ $ev = new BlockGrowEvent($block, VanillaBlocks::CAVE_VINES() ->setAge($this->age + 1) ->setBerries(mt_rand(1, 9) === 1) ); $ev->call(); if(!$ev->isCancelled()){ $world->setBlock($growthPos, $ev->getNewState()); } } } } } public function ticksRandomly() : bool{ return true; } protected function recalculateCollisionBoxes() : array{ return []; } public function hasEntityCollision() : bool{ return true; } public function onEntityInside(Entity $entity) : bool{ $entity->resetFallDistance(); return false; } public function getDropsForCompatibleTool(Item $item) : array{ return $this->berries ? [$this->asItem()] : []; } public function isAffectedBySilkTouch() : bool{ return true; } public function asItem() : Item{ return VanillaItems::GLOW_BERRIES(); } public function getSupportType(int $facing) : SupportType{ return SupportType::NONE(); } }