PocketMine-MP 5.16.1 git-585aee9386a787c95e73dd0a05ffca8329606b68
Button.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block;
25
26use pocketmine\block\utils\AnyFacingTrait;
35
36abstract class Button extends Flowable{
37 use AnyFacingTrait;
38
39 protected bool $pressed = false;
40
41 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
42 $w->facing($this->facing);
43 $w->bool($this->pressed);
44 }
45
46 public function isPressed() : bool{ return $this->pressed; }
47
49 public function setPressed(bool $pressed) : self{
50 $this->pressed = $pressed;
51 return $this;
52 }
53
54 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
55 if($this->canBeSupportedAt($blockReplace, $face)){
56 $this->facing = $face;
57 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
58 }
59 return false;
60 }
61
62 abstract protected function getActivationTime() : int;
63
64 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
65 if(!$this->pressed){
66 $this->pressed = true;
67 $world = $this->position->getWorld();
68 $world->setBlock($this->position, $this);
69 $world->scheduleDelayedBlockUpdate($this->position, $this->getActivationTime());
70 $world->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOnSound());
71 }
72
73 return true;
74 }
75
76 public function onScheduledUpdate() : void{
77 if($this->pressed){
78 $this->pressed = false;
79 $world = $this->position->getWorld();
80 $world->setBlock($this->position, $this);
81 $world->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOffSound());
82 }
83 }
84
85 public function onNearbyBlockChange() : void{
86 if(!$this->canBeSupportedAt($this, $this->facing)){
87 $this->position->getWorld()->useBreakOn($this->position);
88 }
89 }
90
91 private function canBeSupportedAt(Block $block, int $face) : bool{
92 return $block->getAdjacentSupportType(Facing::opposite($face))->hasCenterSupport();
93 }
94}
setPressed(bool $pressed)
Definition: Button.php:49
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition: Button.php:54
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition: Button.php:64
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition: Button.php:41