facing = LeverFacing::UP_AXIS_X(); parent::__construct($idInfo, $name, $typeInfo); } public function describeState(RuntimeDataDescriber $w) : void{ $w->leverFacing($this->facing); $w->bool($this->activated); } public function getFacing() : LeverFacing{ return $this->facing; } /** @return $this */ public function setFacing(LeverFacing $facing) : self{ $this->facing = $facing; return $this; } public function isActivated() : bool{ return $this->activated; } /** @return $this */ public function setActivated(bool $activated) : self{ $this->activated = $activated; return $this; } 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::opposite($face)), $face)){ return false; } $selectUpDownPos = function(LeverFacing $x, LeverFacing $z) use ($player) : LeverFacing{ if($player !== null){ return Facing::axis($player->getHorizontalFacing()) === Axis::X ? $x : $z; } return $x; }; $this->facing = match($face){ Facing::DOWN => $selectUpDownPos(LeverFacing::DOWN_AXIS_X(), LeverFacing::DOWN_AXIS_Z()), Facing::UP => $selectUpDownPos(LeverFacing::UP_AXIS_X(), LeverFacing::UP_AXIS_Z()), Facing::NORTH => LeverFacing::NORTH(), Facing::SOUTH => LeverFacing::SOUTH(), Facing::WEST => LeverFacing::WEST(), Facing::EAST => LeverFacing::EAST(), default => throw new AssumptionFailedError("Bad facing value"), }; return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } public function onNearbyBlockChange() : void{ $facing = $this->facing->getFacing(); if(!$this->canBeSupportedBy($this->getSide(Facing::opposite($facing)), $facing)){ $this->position->getWorld()->useBreakOn($this->position); } } public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ $this->activated = !$this->activated; $world = $this->position->getWorld(); $world->setBlock($this->position, $this); $world->addSound( $this->position->add(0.5, 0.5, 0.5), $this->activated ? new RedstonePowerOnSound() : new RedstonePowerOffSound() ); return true; } private function canBeSupportedBy(Block $block, int $face) : bool{ return $block->getSupportType($face)->hasCenterSupport(); } //TODO }