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