facing = LeverFacing::UP_AXIS_X(); parent::__construct($idInfo, $name, $breakInfo); } public function getRequiredStateDataBits() : int{ return 4; } protected function decodeState(BlockDataReader $r) : void{ $this->facing = BlockDataReaderHelper::readLeverFacing($r); $this->activated = $r->readBool(); } protected function encodeState(BlockDataWriter $w) : void{ BlockDataWriterHelper::writeLeverFacing($w, $this->facing); $w->writeBool($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($blockClicked, $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) : bool{ $this->activated = !$this->activated; $this->position->getWorld()->setBlock($this->position, $this); $this->position->getWorld()->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 }