PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
Lever.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\LeverFacing;
37
38class Lever extends Flowable{
39 protected LeverFacing $facing = LeverFacing::UP_AXIS_X;
40 protected bool $activated = false;
41
42 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
43 $w->enum($this->facing);
44 $w->bool($this->activated);
45 }
46
47 public function getFacing() : LeverFacing{ return $this->facing; }
48
50 public function setFacing(LeverFacing $facing) : self{
51 $this->facing = $facing;
52 return $this;
53 }
54
55 public function isActivated() : bool{ return $this->activated; }
56
58 public function setActivated(bool $activated) : self{
59 $this->activated = $activated;
60 return $this;
61 }
62
63 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
64 if(!$this->canBeSupportedAt($blockReplace, Facing::opposite($face))){
65 return false;
66 }
67
68 $selectUpDownPos = function(LeverFacing $x, LeverFacing $z) use ($player) : LeverFacing{
69 if($player !== null){
70 return Facing::axis($player->getHorizontalFacing()) === Axis::X ? $x : $z;
71 }
72 return $x;
73 };
74 $this->facing = match($face){
75 Facing::DOWN => $selectUpDownPos(LeverFacing::DOWN_AXIS_X, LeverFacing::DOWN_AXIS_Z),
76 Facing::UP => $selectUpDownPos(LeverFacing::UP_AXIS_X, LeverFacing::UP_AXIS_Z),
77 Facing::NORTH => LeverFacing::NORTH,
78 Facing::SOUTH => LeverFacing::SOUTH,
79 Facing::WEST => LeverFacing::WEST,
80 Facing::EAST => LeverFacing::EAST,
81 default => throw new AssumptionFailedError("Bad facing value"),
82 };
83
84 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
85 }
86
87 public function onNearbyBlockChange() : void{
88 if(!$this->canBeSupportedAt($this, Facing::opposite($this->facing->getFacing()))){
89 $this->position->getWorld()->useBreakOn($this->position);
90 }
91 }
92
93 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
94 $this->activated = !$this->activated;
95 $world = $this->position->getWorld();
96 $world->setBlock($this->position, $this);
97 $world->addSound(
98 $this->position->add(0.5, 0.5, 0.5),
99 $this->activated ? new RedstonePowerOnSound() : new RedstonePowerOffSound()
100 );
101 return true;
102 }
103
104 private function canBeSupportedAt(Block $block, int $face) : bool{
105 return $block->getAdjacentSupportType($face)->hasCenterSupport();
106 }
107
108 //TODO
109}
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition Lever.php:63
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition Lever.php:42
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition Lever.php:93
setFacing(LeverFacing $facing)
Definition Lever.php:50
setActivated(bool $activated)
Definition Lever.php:58