Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit f1519e6

Browse files
committed
Improved Level->getTile() to a direct lookup instead of linear search
1 parent 3b9a9bc commit f1519e6

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/pocketmine/level/Level.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,13 +1366,10 @@ public function getTile(Vector3 $pos){
13661366
if($pos instanceof Position and $pos->getLevel() !== $this){
13671367
return null;
13681368
}
1369-
$tiles = $this->getChunkTiles($pos->x >> 4, $pos->z >> 4);
1370-
if(count($tiles) > 0){
1371-
foreach($tiles as $tile){
1372-
if($tile->x === (int) $pos->x and $tile->y === (int) $pos->y and $tile->z === (int) $pos->z){
1373-
return $tile;
1374-
}
1375-
}
1369+
$chunk = $this->getChunk($pos->x >> 4, $pos->z >> 4);
1370+
1371+
if($chunk instanceof FullChunk){
1372+
return $chunk->getTile($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f);
13761373
}
13771374

13781375
return null;

src/pocketmine/level/format/FullChunk.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ public function getEntities();
226226
*/
227227
public function getTiles();
228228

229+
/**
230+
* @param int $x 0-15
231+
* @param int $y 0-127
232+
* @param int $z 0-15
233+
*/
234+
public function getTile($x, $y, $z);
235+
229236
/**
230237
* @return bool
231238
*/

src/pocketmine/level/format/generic/BaseFullChunk.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ abstract class BaseFullChunk implements FullChunk{
3737
/** @var Tile[] */
3838
protected $tiles = [];
3939

40+
/** @var Tile[] */
41+
protected $tileList = [];
42+
4043
/** @var string */
4144
protected $biomeIds;
4245

@@ -234,11 +237,13 @@ public function removeEntity(Entity $entity){
234237
235238
public function addTile(Tile $tile){
236239
$this->tiles[$tile->getID()] = $tile;
240+
$this->tiles[(($tile->z & 0x0f) << 8) | (($tile->x & 0x0f) << 4) | ($tile->y & 0x7f)] = $tile;
237241
$this->hasChanged = true;
238242
}
239243
240244
public function removeTile(Tile $tile){
241245
unset($this->tiles[$tile->getID()]);
246+
unset($this->tiles[(($tile->z & 0x0f) << 8) | (($tile->x & 0x0f) << 4) | ($tile->y & 0x7f)]);
242247
$this->hasChanged = true;
243248
}
244249
@@ -250,6 +255,11 @@ public function getTiles(){
250255
return $this->tiles;
251256
}
252257
258+
public function getTile($x, $y, $z){
259+
$index = ($z << 8) | ($x << 4) | $y;
260+
return isset($this->tileList[$index]) ? $this->tileList[$index] : null;
261+
}
262+
253263
public function isLoaded(){
254264
return $this->getProvider() === null ? false : $this->getProvider()->isChunkLoaded($this->getX(), $this->getZ());
255265
}

0 commit comments

Comments
 (0)