mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 17:59:48 +00:00
Move some exceptions out of utils into their relevant namespaces, move some Chunk methods to ChunkUtils for I/O, refactor "colour" -> "color"
This commit is contained in:
@ -28,6 +28,7 @@ namespace pocketmine\level\format;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\entity\Entity;
|
||||
use pocketmine\level\format\io\ChunkException;
|
||||
use pocketmine\level\format\io\LevelProvider;
|
||||
use pocketmine\nbt\NBT;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
@ -35,7 +36,6 @@ use pocketmine\Player;
|
||||
use pocketmine\tile\Spawnable;
|
||||
use pocketmine\tile\Tile;
|
||||
use pocketmine\utils\BinaryStream;
|
||||
use pocketmine\utils\ChunkException;
|
||||
|
||||
class Chunk{
|
||||
|
||||
@ -1015,69 +1015,4 @@ class Chunk{
|
||||
public static function chunkBlockHash(int $x, int $y, int $z) : int{
|
||||
return ($x << 12) | ($z << 8) | $y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-orders a byte array (YZX -> XZY and vice versa)
|
||||
*
|
||||
* @param string $array length 4096
|
||||
*
|
||||
* @return string length 4096
|
||||
*/
|
||||
public static final function reorderByteArray(string $array) : string{
|
||||
$result = str_repeat("\x00", 4096);
|
||||
$i = 0;
|
||||
for($x = 0; $x < 16; ++$x){
|
||||
for($z = 0; $z < 256; $z += 16){
|
||||
$zx = ($z + $x);
|
||||
for($y = 0; $y < 4096; $y += 256){
|
||||
$result{$i} = $array{$y + $zx};
|
||||
++$i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-orders a nibble array (YZX -> XZY and vice versa)
|
||||
*
|
||||
* @param string $array length 2048
|
||||
*
|
||||
* @return string length 2048
|
||||
*/
|
||||
public static final function reorderNibbleArray(string $array) : string{
|
||||
$result = str_repeat("\x00", 2048);
|
||||
$i = 0;
|
||||
for($x = 0; $x < 8; ++$x){
|
||||
for($z = 0; $z < 16; ++$z){
|
||||
$zx = (($z << 3) | $x);
|
||||
for($y = 0; $y < 8; ++$y){
|
||||
$j = (($y << 8) | $zx);
|
||||
$i1 = ord($array{$j});
|
||||
$i2 = ord($array{$j | 0x80});
|
||||
$result{$i} = chr(($i2 << 4) | ($i1 & 0x0f));
|
||||
$result{$i | 0x80} = chr(($i1 >> 4) | ($i2 & 0xf0));
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
$i += 128;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts pre-MCPE-1.0 biome colour array to biome ID array. RIP BiomeColors :(
|
||||
*
|
||||
* @param int[] $array of biome colour values
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function convertBiomeColours(array $array) : string{
|
||||
$result = str_repeat("\x00", 256);
|
||||
foreach($array as $i => $colour){
|
||||
$result{$i} = chr(($array[$i] >> 24) & 0xff);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,14 +26,13 @@ namespace pocketmine\level\format\io;
|
||||
use pocketmine\level\format\Chunk;
|
||||
use pocketmine\level\generator\Generator;
|
||||
use pocketmine\level\Level;
|
||||
use pocketmine\level\LevelException;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\nbt\NBT;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\nbt\tag\IntTag;
|
||||
use pocketmine\nbt\tag\LongTag;
|
||||
use pocketmine\nbt\tag\StringTag;
|
||||
use pocketmine\utils\ChunkException;
|
||||
use pocketmine\utils\LevelException;
|
||||
|
||||
abstract class BaseLevelProvider implements LevelProvider{
|
||||
/** @var Level */
|
||||
|
26
src/pocketmine/level/format/io/ChunkException.php
Normal file
26
src/pocketmine/level/format/io/ChunkException.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\level\format\io;
|
||||
|
||||
class ChunkException extends \RuntimeException{
|
||||
|
||||
}
|
92
src/pocketmine/level/format/io/ChunkUtils.php
Normal file
92
src/pocketmine/level/format/io/ChunkUtils.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?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\level\format\io;
|
||||
|
||||
class ChunkUtils{
|
||||
|
||||
/**
|
||||
* Re-orders a byte array (YZX -> XZY and vice versa)
|
||||
*
|
||||
* @param string $array length 4096
|
||||
*
|
||||
* @return string length 4096
|
||||
*/
|
||||
public static final function reorderByteArray(string $array) : string{
|
||||
$result = str_repeat("\x00", 4096);
|
||||
$i = 0;
|
||||
for($x = 0; $x < 16; ++$x){
|
||||
for($z = 0; $z < 256; $z += 16){
|
||||
$zx = ($z + $x);
|
||||
for($y = 0; $y < 4096; $y += 256){
|
||||
$result{$i} = $array{$y + $zx};
|
||||
++$i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-orders a nibble array (YZX -> XZY and vice versa)
|
||||
*
|
||||
* @param string $array length 2048
|
||||
*
|
||||
* @return string length 2048
|
||||
*/
|
||||
public static final function reorderNibbleArray(string $array) : string{
|
||||
$result = str_repeat("\x00", 2048);
|
||||
$i = 0;
|
||||
for($x = 0; $x < 8; ++$x){
|
||||
for($z = 0; $z < 16; ++$z){
|
||||
$zx = (($z << 3) | $x);
|
||||
for($y = 0; $y < 8; ++$y){
|
||||
$j = (($y << 8) | $zx);
|
||||
$i1 = ord($array{$j});
|
||||
$i2 = ord($array{$j | 0x80});
|
||||
$result{$i} = chr(($i2 << 4) | ($i1 & 0x0f));
|
||||
$result{$i | 0x80} = chr(($i1 >> 4) | ($i2 & 0xf0));
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
$i += 128;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts pre-MCPE-1.0 biome color array to biome ID array.
|
||||
*
|
||||
* @param int[] $array of biome color values
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function convertBiomeColors(array $array) : string{
|
||||
$result = str_repeat("\x00", 256);
|
||||
foreach($array as $i => $color){
|
||||
$result{$i} = chr(($color >> 24) & 0xff);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
@ -23,7 +23,7 @@ declare(strict_types = 1);
|
||||
|
||||
namespace pocketmine\level\format\io;
|
||||
|
||||
use pocketmine\utils\LevelException;
|
||||
use pocketmine\level\LevelException;
|
||||
|
||||
abstract class LevelProviderManager{
|
||||
protected static $providers = [];
|
||||
|
@ -24,13 +24,14 @@ declare(strict_types = 1);
|
||||
namespace pocketmine\level\format\io\region;
|
||||
|
||||
use pocketmine\level\format\Chunk;
|
||||
use pocketmine\level\format\io\ChunkException;
|
||||
use pocketmine\level\format\io\ChunkUtils;
|
||||
use pocketmine\level\format\SubChunk;
|
||||
use pocketmine\nbt\NBT;
|
||||
use pocketmine\nbt\tag\{
|
||||
ByteArrayTag, ByteTag, CompoundTag, IntArrayTag, IntTag, ListTag, LongTag
|
||||
};
|
||||
use pocketmine\Player;
|
||||
use pocketmine\utils\ChunkException;
|
||||
use pocketmine\utils\MainLogger;
|
||||
|
||||
class Anvil extends McRegion{
|
||||
@ -57,10 +58,10 @@ class Anvil extends McRegion{
|
||||
}
|
||||
$nbt->Sections[++$subChunks] = new CompoundTag(null, [
|
||||
"Y" => new ByteTag("Y", $y),
|
||||
"Blocks" => new ByteArrayTag("Blocks", Chunk::reorderByteArray($subChunk->getBlockIdArray())), //Generic in-memory chunks are currently always XZY
|
||||
"Data" => new ByteArrayTag("Data", Chunk::reorderNibbleArray($subChunk->getBlockDataArray())),
|
||||
"SkyLight" => new ByteArrayTag("SkyLight", Chunk::reorderNibbleArray($subChunk->getSkyLightArray())),
|
||||
"BlockLight" => new ByteArrayTag("BlockLight", Chunk::reorderNibbleArray($subChunk->getBlockLightArray()))
|
||||
"Blocks" => new ByteArrayTag("Blocks", ChunkUtils::reorderByteArray($subChunk->getBlockIdArray())), //Generic in-memory chunks are currently always XZY
|
||||
"Data" => new ByteArrayTag("Data", ChunkUtils::reorderNibbleArray($subChunk->getBlockDataArray())),
|
||||
"SkyLight" => new ByteArrayTag("SkyLight", ChunkUtils::reorderNibbleArray($subChunk->getSkyLightArray())),
|
||||
"BlockLight" => new ByteArrayTag("BlockLight", ChunkUtils::reorderNibbleArray($subChunk->getBlockLightArray()))
|
||||
]);
|
||||
}
|
||||
|
||||
@ -115,17 +116,17 @@ class Anvil extends McRegion{
|
||||
foreach($chunk->Sections as $subChunk){
|
||||
if($subChunk instanceof CompoundTag){
|
||||
$subChunks[$subChunk->Y->getValue()] = new SubChunk(
|
||||
Chunk::reorderByteArray($subChunk->Blocks->getValue()),
|
||||
Chunk::reorderNibbleArray($subChunk->Data->getValue()),
|
||||
Chunk::reorderNibbleArray($subChunk->SkyLight->getValue()),
|
||||
Chunk::reorderNibbleArray($subChunk->BlockLight->getValue())
|
||||
ChunkUtils::reorderByteArray($subChunk->Blocks->getValue()),
|
||||
ChunkUtils::reorderNibbleArray($subChunk->Data->getValue()),
|
||||
ChunkUtils::reorderNibbleArray($subChunk->SkyLight->getValue()),
|
||||
ChunkUtils::reorderNibbleArray($subChunk->BlockLight->getValue())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($chunk->BiomeColors)){
|
||||
$biomeIds = Chunk::convertBiomeColours($chunk->BiomeColors->getValue()); //Convert back to PC format (RIP colours D:)
|
||||
$biomeIds = ChunkUtils::convertBiomeColors($chunk->BiomeColors->getValue()); //Convert back to original format
|
||||
}elseif(isset($chunk->Biomes)){
|
||||
$biomeIds = $chunk->Biomes->getValue();
|
||||
}else{
|
||||
|
@ -25,6 +25,8 @@ namespace pocketmine\level\format\io\region;
|
||||
|
||||
use pocketmine\level\format\Chunk;
|
||||
use pocketmine\level\format\io\BaseLevelProvider;
|
||||
use pocketmine\level\format\io\ChunkException;
|
||||
use pocketmine\level\format\io\ChunkUtils;
|
||||
use pocketmine\level\format\SubChunk;
|
||||
use pocketmine\level\generator\Generator;
|
||||
use pocketmine\level\Level;
|
||||
@ -129,7 +131,7 @@ class McRegion extends BaseLevelProvider{
|
||||
$chunk = $nbt->getData();
|
||||
|
||||
if(!isset($chunk->Level) or !($chunk->Level instanceof CompoundTag)){
|
||||
return null;
|
||||
throw new ChunkException("Invalid NBT format");
|
||||
}
|
||||
|
||||
$chunk = $chunk->Level;
|
||||
@ -169,7 +171,7 @@ class McRegion extends BaseLevelProvider{
|
||||
}
|
||||
|
||||
if(isset($chunk->BiomeColors)){
|
||||
$biomeIds = Chunk::convertBiomeColours($chunk->BiomeColors->getValue()); //Convert back to PC format (RIP colours D:)
|
||||
$biomeIds = ChunkUtils::convertBiomeColors($chunk->BiomeColors->getValue()); //Convert back to original format
|
||||
}elseif(isset($chunk->Biomes)){
|
||||
$biomeIds = $chunk->Biomes->getValue();
|
||||
}else{
|
||||
|
@ -24,13 +24,13 @@ declare(strict_types = 1);
|
||||
namespace pocketmine\level\format\io\region;
|
||||
|
||||
use pocketmine\level\format\Chunk;
|
||||
use pocketmine\level\format\io\ChunkException;
|
||||
use pocketmine\level\format\SubChunk;
|
||||
use pocketmine\nbt\NBT;
|
||||
use pocketmine\nbt\tag\{
|
||||
ByteArrayTag, ByteTag, CompoundTag, IntArrayTag, IntTag, ListTag, LongTag
|
||||
};
|
||||
use pocketmine\Player;
|
||||
use pocketmine\utils\ChunkException;
|
||||
use pocketmine\utils\MainLogger;
|
||||
|
||||
/**
|
||||
|
@ -24,9 +24,9 @@ declare(strict_types = 1);
|
||||
namespace pocketmine\level\format\io\region;
|
||||
|
||||
use pocketmine\level\format\Chunk;
|
||||
use pocketmine\level\format\io\ChunkException;
|
||||
use pocketmine\level\format\io\LevelProvider;
|
||||
use pocketmine\utils\Binary;
|
||||
use pocketmine\utils\ChunkException;
|
||||
use pocketmine\utils\MainLogger;
|
||||
|
||||
class RegionLoader{
|
||||
|
Reference in New Issue
Block a user