PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
Loading...
Searching...
No Matches
SnowLayer.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\FallableTrait;
29use pocketmine\block\utils\SupportType;
38use function floor;
39use function max;
40
41class SnowLayer extends Flowable implements Fallable{
42 use FallableTrait;
43
44 public const MIN_LAYERS = 1;
45 public const MAX_LAYERS = 8;
46
47 protected int $layers = self::MIN_LAYERS;
48
49 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
50 $w->boundedIntAuto(self::MIN_LAYERS, self::MAX_LAYERS, $this->layers);
51 }
52
53 public function getLayers() : int{ return $this->layers; }
54
56 public function setLayers(int $layers) : self{
57 if($layers < self::MIN_LAYERS || $layers > self::MAX_LAYERS){
58 throw new \InvalidArgumentException("Layers must be in range " . self::MIN_LAYERS . " ... " . self::MAX_LAYERS);
59 }
60 $this->layers = $layers;
61 return $this;
62 }
63
64 public function canBeReplaced() : bool{
65 return $this->layers < self::MAX_LAYERS;
66 }
67
68 protected function recalculateCollisionBoxes() : array{
69 //TODO: this zero-height BB is intended to stay in lockstep with a MCPE bug
70 return [AxisAlignedBB::one()->trim(Facing::UP, $this->layers >= 4 ? 0.5 : 1)];
71 }
72
73 public function getSupportType(int $facing) : SupportType{
74 if(!$this->canBeReplaced()){
75 return SupportType::FULL;
76 }
77 return SupportType::NONE;
78 }
79
80 private function canBeSupportedAt(Block $block) : bool{
81 return $block->getAdjacentSupportType(Facing::DOWN) === SupportType::FULL;
82 }
83
84 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
85 if($blockReplace instanceof SnowLayer){
86 if($blockReplace->layers >= self::MAX_LAYERS){
87 return false;
88 }
89 $this->layers = $blockReplace->layers + 1;
90 }
91 if($this->canBeSupportedAt($blockReplace)){
92 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
93 }
94
95 return false;
96 }
97
98 public function ticksRandomly() : bool{
99 return true;
100 }
101
102 public function onRandomTick() : void{
103 $world = $this->position->getWorld();
104 if($world->getBlockLightAt($this->position->x, $this->position->y, $this->position->z) >= 12){
105 BlockEventHelper::melt($this, VanillaBlocks::AIR());
106 }
107 }
108
109 public function getDropsForCompatibleTool(Item $item) : array{
110 return [
111 VanillaItems::SNOWBALL()->setCount(max(1, (int) floor($this->layers / 2)))
112 ];
113 }
114}
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition SnowLayer.php:49
getDropsForCompatibleTool(Item $item)
getSupportType(int $facing)
Definition SnowLayer.php:73
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition SnowLayer.php:84