facing) | ($this->pressed ? BlockLegacyMetadata::BUTTON_FLAG_POWERED : 0); } public function readStateFromData(int $id, int $stateMeta) : void{ //TODO: in PC it's (6 - facing) for every meta except 0 (down) $this->facing = BlockDataSerializer::readFacing($stateMeta & 0x07); $this->pressed = ($stateMeta & BlockLegacyMetadata::BUTTON_FLAG_POWERED) !== 0; } public function getStateBitmask() : int{ return 0b1111; } public function isPressed() : bool{ return $this->pressed; } /** @return $this */ public function setPressed(bool $pressed) : self{ $this->pressed = $pressed; 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)){ $this->facing = $face; return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); } return false; } abstract protected function getActivationTime() : int; public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if(!$this->pressed){ $this->pressed = true; $this->position->getWorld()->setBlock($this->position, $this); $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, $this->getActivationTime()); $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOnSound()); } return true; } public function onScheduledUpdate() : void{ if($this->pressed){ $this->pressed = false; $this->position->getWorld()->setBlock($this->position, $this); $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOffSound()); } } public function onNearbyBlockChange() : void{ if(!$this->canBeSupportedBy($this->getSide(Facing::opposite($this->facing)), $this->facing)){ $this->position->getWorld()->useBreakOn($this->position); } } private function canBeSupportedBy(Block $support, int $face) : bool{ return $support->getSupportType($face)->hasCenterSupport(); } }