From 22a21ecfd643a12ef9121a903919ce14dcdfc218 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 16 Feb 2019 19:57:55 +0000 Subject: [PATCH] BlockTransaction: Take world in constructor --- src/pocketmine/block/Door.php | 4 +-- src/pocketmine/block/DoublePlant.php | 4 +-- src/pocketmine/level/BlockTransaction.php | 32 +++++++++---------- .../level/generator/object/SpruceTree.php | 4 +-- .../level/generator/object/Tree.php | 16 +++++----- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/pocketmine/block/Door.php b/src/pocketmine/block/Door.php index 361704d90..44ef368a6 100644 --- a/src/pocketmine/block/Door.php +++ b/src/pocketmine/block/Door.php @@ -125,10 +125,10 @@ abstract class Door extends Transparent{ $topHalf = clone $this; $topHalf->top = true; - $transaction = new BlockTransaction(); + $transaction = new BlockTransaction($this->level); $transaction->addBlock($blockReplace, $this)->addBlock($blockUp, $topHalf); - return $transaction->apply($this->level); + return $transaction->apply(); } return false; diff --git a/src/pocketmine/block/DoublePlant.php b/src/pocketmine/block/DoublePlant.php index b03e451df..f25c842dd 100644 --- a/src/pocketmine/block/DoublePlant.php +++ b/src/pocketmine/block/DoublePlant.php @@ -53,9 +53,9 @@ class DoublePlant extends Flowable{ $top = clone $this; $top->top = true; - $transaction = new BlockTransaction(); + $transaction = new BlockTransaction($this->level); $transaction->addBlock($blockReplace, $this)->addBlock($blockReplace->getSide(Facing::UP), $top); - return $transaction->apply($this->level); + return $transaction->apply(); } return false; diff --git a/src/pocketmine/level/BlockTransaction.php b/src/pocketmine/level/BlockTransaction.php index 6f5853461..d3aa55fc9 100644 --- a/src/pocketmine/level/BlockTransaction.php +++ b/src/pocketmine/level/BlockTransaction.php @@ -28,13 +28,17 @@ use pocketmine\math\Vector3; use pocketmine\utils\Utils; class BlockTransaction{ + /** @var ChunkManager */ + private $world; + /** @var Block[][][] */ private $blocks = []; /** @var \Closure[] */ private $validators = []; - public function __construct(){ + public function __construct(ChunkManager $world){ + $this->world = $world; $this->addValidator(function(ChunkManager $world, int $x, int $y, int $z) : bool{ return $world->isInWorld($x, $y, $z); }); @@ -71,47 +75,43 @@ class BlockTransaction{ * Reads a block from the given world, masked by the blocks in this transaction. This can be useful if you want to * add blocks to the transaction that depend on previous blocks should they exist. * - * @param ChunkManager $world - * @param Vector3 $pos + * @param Vector3 $pos * * @return Block */ - public function fetchBlock(ChunkManager $world, Vector3 $pos) : Block{ - return $this->fetchBlockAt($world, $pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ()); + public function fetchBlock(Vector3 $pos) : Block{ + return $this->fetchBlockAt($pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ()); } /** * @see BlockTransaction::fetchBlock() * - * @param ChunkManager $world - * @param int $x - * @param int $y - * @param int $z + * @param int $x + * @param int $y + * @param int $z * * @return Block */ - public function fetchBlockAt(ChunkManager $world, int $x, int $y, int $z) : Block{ - return $this->blocks[$x][$y][$z] ?? $world->getBlockAt($x, $y, $z); + public function fetchBlockAt(int $x, int $y, int $z) : Block{ + return $this->blocks[$x][$y][$z] ?? $this->world->getBlockAt($x, $y, $z); } /** * Validates and attempts to apply the transaction to the given world. If any part of the transaction fails to * validate, no changes will be made to the world. * - * @param ChunkManager $world - * * @return bool if the application was successful */ - public function apply(ChunkManager $world) : bool{ + public function apply() : bool{ foreach($this->getBlocks() as [$x, $y, $z, $_]){ foreach($this->validators as $validator){ - if(!$validator($world, $x, $y, $z)){ + if(!$validator($this->world, $x, $y, $z)){ return false; } } } foreach($this->getBlocks() as [$x, $y, $z, $block]){ - $world->setBlockAt($x, $y, $z, $block); + $this->world->setBlockAt($x, $y, $z, $block); } return true; } diff --git a/src/pocketmine/level/generator/object/SpruceTree.php b/src/pocketmine/level/generator/object/SpruceTree.php index e6a73f96a..cb5df458c 100644 --- a/src/pocketmine/level/generator/object/SpruceTree.php +++ b/src/pocketmine/level/generator/object/SpruceTree.php @@ -46,7 +46,7 @@ class SpruceTree extends Tree{ parent::placeObject($level, $x, $y, $z, $random); } - protected function placeCanopy(ChunkManager $level, int $x, int $y, int $z, Random $random, BlockTransaction $transaction) : void{ + protected function placeCanopy(int $x, int $y, int $z, Random $random, BlockTransaction $transaction) : void{ $topSize = $this->treeHeight - (1 + $random->nextBoundedInt(2)); $lRadius = 2 + $random->nextBoundedInt(2); $radius = $random->nextBoundedInt(2); @@ -64,7 +64,7 @@ class SpruceTree extends Tree{ continue; } - if(!$level->getBlockAt($xx, $yyy, $zz)->isSolid()){ + if(!$transaction->fetchBlockAt($xx, $yyy, $zz)->isSolid()){ $transaction->addBlockAt($xx, $yyy, $zz, $this->leafBlock); } } diff --git a/src/pocketmine/level/generator/object/Tree.php b/src/pocketmine/level/generator/object/Tree.php index 589f7218c..7074c3b33 100644 --- a/src/pocketmine/level/generator/object/Tree.php +++ b/src/pocketmine/level/generator/object/Tree.php @@ -107,29 +107,29 @@ abstract class Tree{ } public function placeObject(ChunkManager $level, int $x, int $y, int $z, Random $random) : void{ - $transaction = new BlockTransaction(); - $this->placeTrunk($level, $x, $y, $z, $random, $this->generateChunkHeight($random), $transaction); - $this->placeCanopy($level, $x, $y, $z, $random, $transaction); + $transaction = new BlockTransaction($level); + $this->placeTrunk($x, $y, $z, $random, $this->generateChunkHeight($random), $transaction); + $this->placeCanopy($x, $y, $z, $random, $transaction); - $transaction->apply($level); //TODO: handle return value on failure + $transaction->apply(); //TODO: handle return value on failure } protected function generateChunkHeight(Random $random) : int{ return $this->treeHeight - 1; } - protected function placeTrunk(ChunkManager $level, int $x, int $y, int $z, Random $random, int $trunkHeight, BlockTransaction $transaction) : void{ + protected function placeTrunk(int $x, int $y, int $z, Random $random, int $trunkHeight, BlockTransaction $transaction) : void{ // The base dirt block $transaction->addBlockAt($x, $y - 1, $z, BlockFactory::get(Block::DIRT)); for($yy = 0; $yy < $trunkHeight; ++$yy){ - if($this->canOverride($transaction->fetchBlockAt($level, $x, $y + $yy, $z))){ + if($this->canOverride($transaction->fetchBlockAt($x, $y + $yy, $z))){ $transaction->addBlockAt($x, $y + $yy, $z, $this->trunkBlock); } } } - protected function placeCanopy(ChunkManager $level, int $x, int $y, int $z, Random $random, BlockTransaction $transaction) : void{ + protected function placeCanopy(int $x, int $y, int $z, Random $random, BlockTransaction $transaction) : void{ for($yy = $y - 3 + $this->treeHeight; $yy <= $y + $this->treeHeight; ++$yy){ $yOff = $yy - ($y + $this->treeHeight); $mid = (int) (1 - $yOff / 2); @@ -140,7 +140,7 @@ abstract class Tree{ if($xOff === $mid and $zOff === $mid and ($yOff === 0 or $random->nextBoundedInt(2) === 0)){ continue; } - if(!$transaction->fetchBlockAt($level, $xx, $yy, $zz)->isSolid()){ + if(!$transaction->fetchBlockAt($xx, $yy, $zz)->isSolid()){ $transaction->addBlockAt($xx, $yy, $zz, $this->leafBlock); } }