PocketMine-MP 5.32.2 git-237b304ef9858756b018e44e8f298093f66f823b
Loading...
Searching...
No Matches
RedstoneComparator.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
28use pocketmine\block\utils\AnalogRedstoneSignalEmitterTrait;
30use pocketmine\block\utils\HorizontalFacingTrait;
32use pocketmine\block\utils\PoweredByRedstoneTrait;
33use pocketmine\block\utils\StaticSupportTrait;
34use pocketmine\block\utils\SupportType;
42use function assert;
43
45 use HorizontalFacingTrait;
46 use AnalogRedstoneSignalEmitterTrait;
47 use PoweredByRedstoneTrait;
48 use StaticSupportTrait;
49
50 protected bool $isSubtractMode = false;
51
52 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
53 $w->horizontalFacing($this->facing);
54 $w->bool($this->isSubtractMode);
55 $w->bool($this->powered);
56 }
57
58 public function readStateFromWorld() : Block{
59 parent::readStateFromWorld();
60 $tile = $this->position->getWorld()->getTile($this->position);
61 if($tile instanceof Comparator){
62 $this->signalStrength = $tile->getSignalStrength();
63 }
64
65 return $this;
66 }
67
68 public function writeStateToWorld() : void{
69 parent::writeStateToWorld();
70 $tile = $this->position->getWorld()->getTile($this->position);
71 assert($tile instanceof Comparator);
72 $tile->setSignalStrength($this->signalStrength);
73 }
74
75 public function isSubtractMode() : bool{
76 return $this->isSubtractMode;
77 }
78
80 public function setSubtractMode(bool $isSubtractMode) : self{
81 $this->isSubtractMode = $isSubtractMode;
82 return $this;
83 }
84
85 protected function recalculateCollisionBoxes() : array{
86 return [AxisAlignedBB::one()->trim(Facing::UP, 7 / 8)];
87 }
88
89 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
90 if($player !== null){
91 $this->facing = Facing::opposite($player->getHorizontalFacing());
92 }
93 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
94 }
95
96 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
97 $this->isSubtractMode = !$this->isSubtractMode;
98 $this->position->getWorld()->setBlock($this->position, $this);
99 return true;
100 }
101
102 private function canBeSupportedAt(Block $block) : bool{
103 return $block->getAdjacentSupportType(Facing::DOWN) !== SupportType::NONE;
104 }
105
106 //TODO: redstone functionality
107}
describeBlockOnlyState(RuntimeDataDescriber $w)
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])