PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
BlockTransaction.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
24namespace pocketmine\world;
25
29
35 private array $blocks = [];
36
41 private array $validators = [];
42
43 public function __construct(private ChunkManager $world){
44 $this->addValidator(static function(ChunkManager $world, int $x, int $y, int $z) : bool{
45 return $world->isInWorld($x, $y, $z);
46 });
47 }
48
54 public function addBlock(Vector3 $pos, Block $state) : self{
55 return $this->addBlockAt($pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ(), $state);
56 }
57
63 public function addBlockAt(int $x, int $y, int $z, Block $state) : self{
64 $this->blocks[$x][$y][$z] = $state;
65 return $this;
66 }
67
72 public function fetchBlock(Vector3 $pos) : Block{
73 return $this->fetchBlockAt($pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ());
74 }
75
79 public function fetchBlockAt(int $x, int $y, int $z) : Block{
80 return $this->blocks[$x][$y][$z] ?? $this->world->getBlockAt($x, $y, $z);
81 }
82
89 public function apply() : bool{
90 foreach($this->getBlocks() as [$x, $y, $z, $_]){
91 foreach($this->validators as $validator){
92 if(!$validator($this->world, $x, $y, $z)){
93 return false;
94 }
95 }
96 }
97 $changedBlocks = 0;
98 foreach($this->getBlocks() as [$x, $y, $z, $block]){
99 $oldBlock = $this->world->getBlockAt($x, $y, $z);
100 if(!$oldBlock->isSameState($block)){
101 $this->world->setBlockAt($x, $y, $z, $block);
102 $changedBlocks++;
103 }
104 }
105 return $changedBlocks !== 0;
106 }
107
112 public function getBlocks() : \Generator{
113 foreach($this->blocks as $x => $yLine){
114 foreach($yLine as $y => $zLine){
115 foreach($zLine as $z => $block){
116 yield [$x, $y, $z, $block];
117 }
118 }
119 }
120 }
121
129 public function addValidator(\Closure $validator) : void{
130 Utils::validateCallableSignature([$this, 'dummyValidator'], $validator);
131 $this->validators[] = $validator;
132 }
133
140 public function dummyValidator(ChunkManager $world, int $x, int $y, int $z) : bool{
141 return true;
142 }
143}
addBlock(Vector3 $pos, Block $state)
addBlockAt(int $x, int $y, int $z, Block $state)
dummyValidator(ChunkManager $world, int $x, int $y, int $z)
fetchBlockAt(int $x, int $y, int $z)
isInWorld(int $x, int $y, int $z)