From caff686827ffa53c0421ca1e2aa45b8d43ccdbd5 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 13 May 2017 20:06:15 +0100 Subject: [PATCH] Fixed grass growth and death, close #436 way too fast to grow back though :S --- src/pocketmine/block/Grass.php | 45 +++++++++++++++++++++++----------- src/pocketmine/level/Level.php | 20 +++++++-------- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/pocketmine/block/Grass.php b/src/pocketmine/block/Grass.php index 74ab1b5b9..a7648c592 100644 --- a/src/pocketmine/block/Grass.php +++ b/src/pocketmine/block/Grass.php @@ -61,24 +61,41 @@ class Grass extends Solid{ public function onUpdate($type){ if($type === Level::BLOCK_UPDATE_RANDOM){ - $block = $this->getLevel()->getBlock(new Vector3($this->x, $this->y, $this->z)); - if($block->getSide(1)->getLightLevel() < 4){ - Server::getInstance()->getPluginManager()->callEvent($ev = new BlockSpreadEvent($block, $this, new Dirt())); - }elseif($block->getSide(1)->getLightLevel() >= 9){ - for($l = 0; $l < 4; ++$l){ - $x = mt_rand($this->x - 1, $this->x + 1); - $y = mt_rand($this->y - 2, $this->y + 2); - $z = mt_rand($this->z - 1, $this->z + 1); - $block = $this->getLevel()->getBlock(new Vector3($x, $y, $z)); - if($block->getId() === Block::DIRT && $block->getDamage() === 0x0F && $block->getSide(1)->getLightLevel() >= 4 && $block->z <= 2){ - Server::getInstance()->getPluginManager()->callEvent($ev = new BlockSpreadEvent($block, $this, new Grass())); - if(!$ev->isCancelled()){ - $this->getLevel()->setBlock($block, $ev->getNewState()); - } + $lightAbove = $this->level->getFullLightAt($this->x, $this->y + 1, $this->z); + if($lightAbove < 4 and Block::$lightFilter[$this->level->getBlockIdAt($this->x, $this->y + 1, $this->z)] >= 3){ //2 plus 1 standard filter amount + //grass dies + $this->level->getServer()->getPluginManager()->callEvent($ev = new BlockSpreadEvent($this, $this, Block::get(Block::DIRT))); + if(!$ev->isCancelled()){ + $this->level->setBlock($this, $ev->getNewState(), false, false); + } + + return Level::BLOCK_UPDATE_RANDOM; + }elseif($lightAbove >= 9){ + //try grass spread + $vector = $this->asVector3(); + for($i = 0; $i < 4; ++$i){ + $vector->x = mt_rand($this->x - 1, $this->x + 1); + $vector->y = mt_rand($this->y - 3, $this->y + 1); + $vector->z = mt_rand($this->z - 1, $this->z + 1); + if( + $this->level->getBlockIdAt($vector->x, $vector->y, $vector->z) !== Block::DIRT or + $this->level->getFullLightAt($vector->x, $vector->y + 1, $vector->z) < 4 or + Block::$lightFilter[$this->level->getBlockIdAt($vector->x, $vector->y + 1, $vector->z)] >= 3 + ){ + continue; + } + + $this->level->getServer()->getPluginManager()->callEvent($ev = new BlockSpreadEvent($this->level->getBlock($vector), $this, Block::get(Block::GRASS))); + if(!$ev->isCancelled()){ + $this->level->setBlock($vector, $ev->getNewState(), false, false); } } + + return Level::BLOCK_UPDATE_RANDOM; } } + + return false; } public function onActivate(Item $item, Player $player = null){ diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 2cb346565..fdb59e817 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -1280,17 +1280,17 @@ class Level implements ChunkManager, Metadatable{ */ public function getFullLight(Vector3 $pos) : int{ - $chunk = $this->getChunk($pos->x >> 4, $pos->z >> 4, false); - $level = 0; - if($chunk !== null){ - $level = $chunk->getBlockSkyLight($pos->x & 0x0f, $pos->y, $pos->z & 0x0f); - //TODO: decrease light level by time of day - if($level < 15){ - $level = max($chunk->getBlockLight($pos->x & 0x0f, $pos->y, $pos->z & 0x0f), $level); - } - } + return $this->getFullLightAt($pos->x, $pos->y, $pos->z); + } - return $level; + public function getFullLightAt(int $x, int $y, int $z) : int{ + //TODO: decrease light level by time of day + $skyLight = $this->getBlockSkyLightAt($x, $y, $z); + if($skyLight < 15){ + return max($skyLight, $this->getBlockLightAt($x, $y, $z)); + }else{ + return $skyLight; + } } /**