PocketMine-MP 5.37.1 git-da6732df2656426fbd1b7898ed06c8286969d2f1
Loading...
Searching...
No Matches
populator/Tree.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
32use pocketmine\world\generator\object\TreeType;
33
34class Tree implements Populator{
35 private int $randomAmount = 1;
36 private int $baseAmount = 0;
37 private TreeType $type;
38
42 public function __construct(?TreeType $type = null){
43 $this->type = $type ?? TreeType::OAK;
44 }
45
46 public function setRandomAmount(int $amount) : void{
47 $this->randomAmount = $amount;
48 }
49
50 public function setBaseAmount(int $amount) : void{
51 $this->baseAmount = $amount;
52 }
53
54 public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{
55 $amount = $random->nextRange(0, $this->randomAmount) + $this->baseAmount;
56 for($i = 0; $i < $amount; ++$i){
57 $x = $random->nextRange($chunkX << Chunk::COORD_BIT_SIZE, ($chunkX << Chunk::COORD_BIT_SIZE) + Chunk::EDGE_LENGTH);
58 $z = $random->nextRange($chunkZ << Chunk::COORD_BIT_SIZE, ($chunkZ << Chunk::COORD_BIT_SIZE) + Chunk::EDGE_LENGTH);
59 $y = $this->getHighestWorkableBlock($world, $x, $z);
60 if($y === -1){
61 continue;
62 }
63 $tree = TreeFactory::get($random, $this->type);
64 $transaction = $tree?->getBlockTransaction($world, $x, $y, $z, $random);
65 $transaction?->apply();
66 }
67 }
68
69 private function getHighestWorkableBlock(ChunkManager $world, int $x, int $z) : int{
70 $highestBlock = $world->getChunk($x >> Chunk::COORD_BIT_SIZE, $z >> Chunk::COORD_BIT_SIZE)?->getHighestBlockAt($x & Chunk::COORD_MASK, $z & Chunk::COORD_MASK);
71 if($highestBlock === null){
72 return -1;
73 }
74 for($y = $highestBlock; $y >= 0; --$y){
75 $b = $world->getBlockAt($x, $y, $z);
76 if($b->hasTypeTag(BlockTypeTags::DIRT) || $b->hasTypeTag(BlockTypeTags::MUD)){
77 return $y + 1;
78 }elseif($b->getTypeId() !== BlockTypeIds::AIR && $b->getTypeId() !== BlockTypeIds::SNOW_LAYER){
79 return -1;
80 }
81 }
82
83 return -1;
84 }
85}