mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-10 15:59:39 +00:00
BlockTransaction: Take world in constructor
This commit is contained in:
parent
0794c94b4b
commit
22a21ecfd6
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user