PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
DaylightSensor.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\AnalogRedstoneSignalEmitterTrait;
27use pocketmine\block\utils\SupportType;
34use function cos;
35use function max;
36use function round;
37use const M_PI;
38
40 use AnalogRedstoneSignalEmitterTrait;
41
42 protected bool $inverted = false;
43
44 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
45 $w->boundedIntAuto(0, 15, $this->signalStrength);
46 $w->bool($this->inverted);
47 }
48
49 public function isInverted() : bool{
50 return $this->inverted;
51 }
52
56 public function setInverted(bool $inverted = true) : self{
57 $this->inverted = $inverted;
58 return $this;
59 }
60
61 public function getFuelTime() : int{
62 return 300;
63 }
64
68 protected function recalculateCollisionBoxes() : array{
69 return [AxisAlignedBB::one()->trim(Facing::UP, 10 / 16)];
70 }
71
72 public function getSupportType(int $facing) : SupportType{
73 return SupportType::NONE;
74 }
75
76 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
77 $this->inverted = !$this->inverted;
78 $this->signalStrength = $this->recalculateSignalStrength();
79 $this->position->getWorld()->setBlock($this->position, $this);
80 return true;
81 }
82
83 public function onScheduledUpdate() : void{
84 $world = $this->position->getWorld();
85 $signalStrength = $this->recalculateSignalStrength();
86 if($this->signalStrength !== $signalStrength){
87 $this->signalStrength = $signalStrength;
88 $world->setBlock($this->position, $this);
89 }
90 $world->scheduleDelayedBlockUpdate($this->position, 20);
91 }
92
93 private function recalculateSignalStrength() : int{
94 $world = $this->position->getWorld();
95 $lightLevel = $world->getRealBlockSkyLightAt($this->position->x, $this->position->y, $this->position->z);
96 if($this->inverted){
97 return 15 - $lightLevel;
98 }
99
100 $sunAngle = $world->getSunAnglePercentage();
101 return max(0, (int) round($lightLevel * cos(($sunAngle + ((($sunAngle < 0.5 ? 0 : 1) - $sunAngle) / 5)) * 2 * M_PI)));
102 }
103
104 //TODO
105}
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
describeBlockOnlyState(RuntimeDataDescriber $w)
setInverted(bool $inverted=true)