PocketMine-MP 5.18.2 git-00e39821f06a4b6d728d35053c2621dbb19369ff
world/generator/populator/TallGrass.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
25
32
33class TallGrass implements Populator{
34 private int $randomAmount = 1;
35 private int $baseAmount = 0;
36
37 public function setRandomAmount(int $amount) : void{
38 $this->randomAmount = $amount;
39 }
40
41 public function setBaseAmount(int $amount) : void{
42 $this->baseAmount = $amount;
43 }
44
45 public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{
46 $amount = $random->nextRange(0, $this->randomAmount) + $this->baseAmount;
47
48 $block = VanillaBlocks::TALL_GRASS();
49 for($i = 0; $i < $amount; ++$i){
50 $x = $random->nextRange($chunkX * Chunk::EDGE_LENGTH, $chunkX * Chunk::EDGE_LENGTH + (Chunk::EDGE_LENGTH - 1));
51 $z = $random->nextRange($chunkZ * Chunk::EDGE_LENGTH, $chunkZ * Chunk::EDGE_LENGTH + (Chunk::EDGE_LENGTH - 1));
52 $y = $this->getHighestWorkableBlock($world, $x, $z);
53
54 if($y !== -1 && $this->canTallGrassStay($world, $x, $y, $z)){
55 $world->setBlockAt($x, $y, $z, $block);
56 }
57 }
58 }
59
60 private function canTallGrassStay(ChunkManager $world, int $x, int $y, int $z) : bool{
61 $b = $world->getBlockAt($x, $y, $z)->getTypeId();
62 return ($b === BlockTypeIds::AIR || $b === BlockTypeIds::SNOW_LAYER) && $world->getBlockAt($x, $y - 1, $z)->getTypeId() === BlockTypeIds::GRASS;
63 }
64
65 private function getHighestWorkableBlock(ChunkManager $world, int $x, int $z) : int{
66 for($y = 127; $y >= 0; --$y){
67 $b = $world->getBlockAt($x, $y, $z);
68 if($b->getTypeId() !== BlockTypeIds::AIR && !($b instanceof Leaves) && $b->getTypeId() !== BlockTypeIds::SNOW_LAYER){
69 return $y + 1;
70 }
71 }
72
73 return -1;
74 }
75}
nextRange(int $start=0, int $end=0x7fffffff)
Definition: Random.php:119
getBlockAt(int $x, int $y, int $z)
setBlockAt(int $x, int $y, int $z, Block $block)