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