SubChunkExplorer::moveTo() now returns a status

this can be used by SubChunkExplorer subclasses to implement specialized logic.
This commit is contained in:
Dylan K. Taylor
2020-10-26 16:25:21 +00:00
parent 390bc631c8
commit 31c2c3abb5
7 changed files with 63 additions and 14 deletions

View File

@ -55,7 +55,10 @@ class SubChunkExplorer{
$this->world = $world;
}
public function moveTo(int $x, int $y, int $z, bool $create) : bool{
/**
* @phpstan-return SubChunkExplorerStatus::*
*/
public function moveTo(int $x, int $y, int $z, bool $create) : int{
if($this->currentChunk === null or $this->currentX !== ($x >> 4) or $this->currentZ !== ($z >> 4)){
$this->currentX = $x >> 4;
$this->currentZ = $z >> 4;
@ -63,7 +66,7 @@ class SubChunkExplorer{
$this->currentChunk = $this->world->getChunk($this->currentX, $this->currentZ, $create);
if($this->currentChunk === null){
return false;
return SubChunkExplorerStatus::INVALID;
}
}
@ -72,7 +75,7 @@ class SubChunkExplorer{
if($this->currentY < 0 or $this->currentY >= $this->currentChunk->getHeight()){
$this->currentSubChunk = null;
return false;
return SubChunkExplorerStatus::INVALID;
}
$newSubChunk = $this->currentChunk->getSubChunk($y >> 4);
@ -81,12 +84,16 @@ class SubChunkExplorer{
if($this->onSubChunkChangeFunc !== null){
($this->onSubChunkChangeFunc)();
}
return SubChunkExplorerStatus::MOVED;
}
return true;
return SubChunkExplorerStatus::OK;
}
public function moveToChunk(int $chunkX, int $chunkY, int $chunkZ, bool $create) : bool{
/**
* @phpstan-return SubChunkExplorerStatus::*
*/
public function moveToChunk(int $chunkX, int $chunkY, int $chunkZ, bool $create) : int{
//this is a cold path, so we don't care much if it's a bit slower (extra fcall overhead)
return $this->moveTo($chunkX << 4, $chunkY << 4, $chunkZ << 4, $create);
}

View File

@ -0,0 +1,37 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\world\utils;
final class SubChunkExplorerStatus{
private function __construct(){
//NOOP
}
/** We encountered terrain not accessible by the current terrain provider */
public const INVALID = 0;
/** We remained inside the same (sub)chunk */
public const OK = 1;
/** We moved to a different (sub)chunk */
public const MOVED = 2;
}