PocketMine-MP 5.32.2 git-237b304ef9858756b018e44e8f298093f66f823b
Loading...
Searching...
No Matches
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
27use pocketmine\block\utils\AnyFacingTrait;
36
37abstract class Button extends Flowable implements AnyFacing{
38 use AnyFacingTrait;
39
40 protected bool $pressed = false;
41
42 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
43 $w->facing($this->facing);
44 $w->bool($this->pressed);
45 }
46
47 public function isPressed() : bool{ return $this->pressed; }
48
50 public function setPressed(bool $pressed) : self{
51 $this->pressed = $pressed;
52 return $this;
53 }
54
55 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
56 if($this->canBeSupportedAt($blockReplace, $face)){
57 $this->facing = $face;
58 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
59 }
60 return false;
61 }
62
63 abstract protected function getActivationTime() : int;
64
65 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
66 if(!$this->pressed){
67 $this->pressed = true;
68 $world = $this->position->getWorld();
69 $world->setBlock($this->position, $this);
70 $world->scheduleDelayedBlockUpdate($this->position, $this->getActivationTime());
71 $world->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOnSound());
72 }
73
74 return true;
75 }
76
77 public function onScheduledUpdate() : void{
78 if($this->pressed){
79 $this->pressed = false;
80 $world = $this->position->getWorld();
81 $world->setBlock($this->position, $this);
82 $world->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOffSound());
83 }
84 }
85
86 public function onNearbyBlockChange() : void{
87 if(!$this->canBeSupportedAt($this, $this->facing)){
88 $this->position->getWorld()->useBreakOn($this->position);
89 }
90 }
91
92 private function canBeSupportedAt(Block $block, int $face) : bool{
93 return $block->getAdjacentSupportType(Facing::opposite($face))->hasCenterSupport();
94 }
95}
setPressed(bool $pressed)
Definition Button.php:50
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition Button.php:55
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition Button.php:65
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition Button.php:42