PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
FrostedIce.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\AgeableTrait;
28use function mt_rand;
29
30class FrostedIce extends Ice{
31 use AgeableTrait;
32
33 public const MAX_AGE = 3;
34
35 public function onNearbyBlockChange() : void{
36 $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40));
37 }
38
39 public function onRandomTick() : void{
40 $world = $this->position->getWorld();
41 if((!$this->checkAdjacentBlocks(4) || mt_rand(0, 2) === 0) &&
42 $world->getHighestAdjacentFullLightAt($this->position->x, $this->position->y, $this->position->z) >= 12 - $this->age){
43 if($this->tryMelt()){
44 foreach($this->getAllSides() as $block){
45 if($block instanceof FrostedIce){
46 $block->tryMelt();
47 }
48 }
49 }
50 }else{
51 $world->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40));
52 }
53 }
54
55 public function onScheduledUpdate() : void{
56 $this->onRandomTick();
57 }
58
59 private function checkAdjacentBlocks(int $requirement) : bool{
60 $found = 0;
61 for($x = -1; $x <= 1; ++$x){
62 for($z = -1; $z <= 1; ++$z){
63 if($x === 0 && $z === 0){
64 continue;
65 }
66 if(
67 $this->position->getWorld()->getBlockAt($this->position->x + $x, $this->position->y, $this->position->z + $z) instanceof FrostedIce &&
68 ++$found >= $requirement
69 ){
70 return true;
71 }
72 }
73 }
74 return false;
75 }
76
82 private function tryMelt() : bool{
83 $world = $this->position->getWorld();
84 if($this->age >= self::MAX_AGE){
85 BlockEventHelper::melt($this, VanillaBlocks::WATER());
86 return true;
87 }
88
89 $this->age++;
90 $world->setBlock($this->position, $this);
91 $world->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40));
92 return false;
93 }
94}