Files
PocketMine-MP/src/pocketmine/level/generator/populator/TallGrass.php
Dylan K. Taylor 99038c752c Avoid usage of for-loop vars outside of for-loop context
these problems were reported by PHPStan strict rules. They aren't actually bugs, but they could become bugs in the future.
2020-02-05 13:16:09 +00:00

87 lines
2.4 KiB
PHP

<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\level\generator\populator;
use pocketmine\block\Block;
use pocketmine\level\ChunkManager;
use pocketmine\utils\Random;
class TallGrass extends Populator{
/** @var ChunkManager */
private $level;
/** @var int */
private $randomAmount = 1;
/** @var int */
private $baseAmount = 0;
/**
* @param int $amount
*
* @return void
*/
public function setRandomAmount($amount){
$this->randomAmount = $amount;
}
/**
* @param int $amount
*
* @return void
*/
public function setBaseAmount($amount){
$this->baseAmount = $amount;
}
public function populate(ChunkManager $level, int $chunkX, int $chunkZ, Random $random){
$this->level = $level;
$amount = $random->nextRange(0, $this->randomAmount) + $this->baseAmount;
for($i = 0; $i < $amount; ++$i){
$x = $random->nextRange($chunkX * 16, $chunkX * 16 + 15);
$z = $random->nextRange($chunkZ * 16, $chunkZ * 16 + 15);
$y = $this->getHighestWorkableBlock($x, $z);
if($y !== -1 and $this->canTallGrassStay($x, $y, $z)){
$this->level->setBlockIdAt($x, $y, $z, Block::TALL_GRASS);
$this->level->setBlockDataAt($x, $y, $z, 1);
}
}
}
private function canTallGrassStay(int $x, int $y, int $z) : bool{
$b = $this->level->getBlockIdAt($x, $y, $z);
return ($b === Block::AIR or $b === Block::SNOW_LAYER) and $this->level->getBlockIdAt($x, $y - 1, $z) === Block::GRASS;
}
private function getHighestWorkableBlock(int $x, int $z) : int{
for($y = 127; $y >= 0; --$y){
$b = $this->level->getBlockIdAt($x, $y, $z);
if($b !== Block::AIR and $b !== Block::LEAVES and $b !== Block::LEAVES2 and $b !== Block::SNOW_LAYER){
return $y + 1;
}
}
return -1;
}
}