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