PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
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
71 protected function recalculateCollisionBoxes() : array{
72 //TODO: this zero-height BB is intended to stay in lockstep with a MCPE bug
73 return [AxisAlignedBB::one()->trim(Facing::UP, $this->layers >= 4 ? 0.5 : 1)];
74 }
75
76 public function getSupportType(int $facing) : SupportType{
77 if(!$this->canBeReplaced()){
78 return SupportType::FULL;
79 }
80 return SupportType::NONE;
81 }
82
83 private function canBeSupportedAt(Block $block) : bool{
84 return $block->getAdjacentSupportType(Facing::DOWN) === SupportType::FULL;
85 }
86
87 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
88 if($blockReplace instanceof SnowLayer){
89 if($blockReplace->layers >= self::MAX_LAYERS){
90 return false;
91 }
92 $this->layers = $blockReplace->layers + 1;
93 }
94 if($this->canBeSupportedAt($blockReplace)){
95 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
96 }
97
98 return false;
99 }
100
101 public function ticksRandomly() : bool{
102 return true;
103 }
104
105 public function onRandomTick() : void{
106 $world = $this->position->getWorld();
107 if($world->getBlockLightAt($this->position->x, $this->position->y, $this->position->z) >= 12){
108 BlockEventHelper::melt($this, VanillaBlocks::AIR());
109 }
110 }
111
112 public function getDropsForCompatibleTool(Item $item) : array{
113 return [
114 VanillaItems::SNOWBALL()->setCount(max(1, (int) floor($this->layers / 2)))
115 ];
116 }
117}
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition SnowLayer.php:49
getDropsForCompatibleTool(Item $item)
getSupportType(int $facing)
Definition SnowLayer.php:76
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition SnowLayer.php:87