PocketMine-MP 5.16.1 git-585aee9386a787c95e73dd0a05ffca8329606b68
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
27use pocketmine\block\utils\AnalogRedstoneSignalEmitterTrait;
28use pocketmine\block\utils\HorizontalFacingTrait;
29use pocketmine\block\utils\PoweredByRedstoneTrait;
30use pocketmine\block\utils\StaticSupportTrait;
31use pocketmine\block\utils\SupportType;
39use function assert;
40
42 use HorizontalFacingTrait;
43 use AnalogRedstoneSignalEmitterTrait;
44 use PoweredByRedstoneTrait;
45 use StaticSupportTrait;
46
47 protected bool $isSubtractMode = false;
48
49 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
50 $w->horizontalFacing($this->facing);
51 $w->bool($this->isSubtractMode);
52 $w->bool($this->powered);
53 }
54
55 public function readStateFromWorld() : Block{
56 parent::readStateFromWorld();
57 $tile = $this->position->getWorld()->getTile($this->position);
58 if($tile instanceof Comparator){
59 $this->signalStrength = $tile->getSignalStrength();
60 }
61
62 return $this;
63 }
64
65 public function writeStateToWorld() : void{
66 parent::writeStateToWorld();
67 $tile = $this->position->getWorld()->getTile($this->position);
68 assert($tile instanceof Comparator);
69 $tile->setSignalStrength($this->signalStrength);
70 }
71
72 public function isSubtractMode() : bool{
73 return $this->isSubtractMode;
74 }
75
77 public function setSubtractMode(bool $isSubtractMode) : self{
78 $this->isSubtractMode = $isSubtractMode;
79 return $this;
80 }
81
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=[])