mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 10:53:05 +00:00
For blocks, we now use 'block-item state' and 'block-only state', which should be much clearer for people implementing custom stuff. 'block-item state', as the name suggests, sticks to the item when the block is acquired as an item. 'block-only state' applies only to the block and is discarded when the block is acquired as an item. 'type data' for items was also renamed, since 'type' is too ambiguous to be anything but super confusing.
106 lines
3.0 KiB
PHP
106 lines
3.0 KiB
PHP
<?php
|
|
|
|
/*
|
|
*
|
|
* ____ _ _ __ __ _ __ __ ____
|
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* @author PocketMine Team
|
|
* @link http://www.pocketmine.net/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace pocketmine\block;
|
|
|
|
use pocketmine\block\utils\AnalogRedstoneSignalEmitterTrait;
|
|
use pocketmine\block\utils\SupportType;
|
|
use pocketmine\data\runtime\RuntimeDataDescriber;
|
|
use pocketmine\item\Item;
|
|
use pocketmine\math\AxisAlignedBB;
|
|
use pocketmine\math\Facing;
|
|
use pocketmine\math\Vector3;
|
|
use pocketmine\player\Player;
|
|
use function cos;
|
|
use function max;
|
|
use function round;
|
|
use const M_PI;
|
|
|
|
class DaylightSensor extends Transparent{
|
|
use AnalogRedstoneSignalEmitterTrait;
|
|
|
|
protected bool $inverted = false;
|
|
|
|
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
|
|
$w->boundedInt(4, 0, 15, $this->signalStrength);
|
|
$w->bool($this->inverted);
|
|
}
|
|
|
|
public function isInverted() : bool{
|
|
return $this->inverted;
|
|
}
|
|
|
|
/**
|
|
* @return $this
|
|
*/
|
|
public function setInverted(bool $inverted = true) : self{
|
|
$this->inverted = $inverted;
|
|
return $this;
|
|
}
|
|
|
|
public function getFuelTime() : int{
|
|
return 300;
|
|
}
|
|
|
|
/**
|
|
* @return AxisAlignedBB[]
|
|
*/
|
|
protected function recalculateCollisionBoxes() : array{
|
|
return [AxisAlignedBB::one()->trim(Facing::UP, 10 / 16)];
|
|
}
|
|
|
|
public function getSupportType(int $facing) : SupportType{
|
|
return SupportType::NONE();
|
|
}
|
|
|
|
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
|
|
$this->inverted = !$this->inverted;
|
|
$this->signalStrength = $this->recalculateSignalStrength();
|
|
$this->position->getWorld()->setBlock($this->position, $this);
|
|
return true;
|
|
}
|
|
|
|
public function onScheduledUpdate() : void{
|
|
$world = $this->position->getWorld();
|
|
$signalStrength = $this->recalculateSignalStrength();
|
|
if($this->signalStrength !== $signalStrength){
|
|
$this->signalStrength = $signalStrength;
|
|
$world->setBlock($this->position, $this);
|
|
}
|
|
$world->scheduleDelayedBlockUpdate($this->position, 20);
|
|
}
|
|
|
|
private function recalculateSignalStrength() : int{
|
|
$world = $this->position->getWorld();
|
|
$lightLevel = $world->getRealBlockSkyLightAt($this->position->x, $this->position->y, $this->position->z);
|
|
if($this->inverted){
|
|
return 15 - $lightLevel;
|
|
}
|
|
|
|
$sunAngle = $world->getSunAnglePercentage();
|
|
return max(0, (int) round($lightLevel * cos(($sunAngle + ((($sunAngle < 0.5 ? 0 : 1) - $sunAngle) / 5)) * 2 * M_PI)));
|
|
}
|
|
|
|
//TODO
|
|
}
|