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:
Dylan K. Taylor 2017-01-15 01:17:45 +00:00
parent 4577f3ee22
commit b28e38ab26
23 changed files with 124 additions and 98 deletions

View File

@ -60,6 +60,7 @@ use pocketmine\level\generator\Generator;
use pocketmine\level\generator\hell\Nether; use pocketmine\level\generator\hell\Nether;
use pocketmine\level\generator\normal\Normal; use pocketmine\level\generator\normal\Normal;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\level\LevelException;
use pocketmine\metadata\EntityMetadataStore; use pocketmine\metadata\EntityMetadataStore;
use pocketmine\metadata\LevelMetadataStore; use pocketmine\metadata\LevelMetadataStore;
use pocketmine\metadata\PlayerMetadataStore; use pocketmine\metadata\PlayerMetadataStore;
@ -98,7 +99,6 @@ use pocketmine\tile\Tile;
use pocketmine\updater\AutoUpdater; use pocketmine\updater\AutoUpdater;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
use pocketmine\utils\Config; use pocketmine\utils\Config;
use pocketmine\utils\LevelException;
use pocketmine\utils\MainLogger; use pocketmine\utils\MainLogger;
use pocketmine\utils\Terminal; use pocketmine\utils\Terminal;
use pocketmine\utils\TextFormat; use pocketmine\utils\TextFormat;

View File

@ -104,7 +104,6 @@ use pocketmine\Server;
use pocketmine\tile\Chest; use pocketmine\tile\Chest;
use pocketmine\tile\Tile; use pocketmine\tile\Tile;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
use pocketmine\utils\LevelException;
use pocketmine\utils\Random; use pocketmine\utils\Random;
use pocketmine\utils\ReversePriorityQueue; use pocketmine\utils\ReversePriorityQueue;

View File

@ -19,7 +19,9 @@
* *
*/ */
namespace pocketmine\utils; namespace pocketmine\level;
use pocketmine\utils\ServerException;
class LevelException extends ServerException{ class LevelException extends ServerException{

View File

@ -22,7 +22,6 @@
namespace pocketmine\level; namespace pocketmine\level;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\utils\LevelException;
use pocketmine\utils\MainLogger; use pocketmine\utils\MainLogger;
class Position extends Vector3{ class Position extends Vector3{

View File

@ -23,7 +23,6 @@ namespace pocketmine\level;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\utils\LevelException;
class WeakPosition extends Position{ class WeakPosition extends Position{

View File

@ -28,6 +28,7 @@ namespace pocketmine\level\format;
use pocketmine\block\Block; use pocketmine\block\Block;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\level\format\io\ChunkException;
use pocketmine\level\format\io\LevelProvider; use pocketmine\level\format\io\LevelProvider;
use pocketmine\nbt\NBT; use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
@ -35,7 +36,6 @@ use pocketmine\Player;
use pocketmine\tile\Spawnable; use pocketmine\tile\Spawnable;
use pocketmine\tile\Tile; use pocketmine\tile\Tile;
use pocketmine\utils\BinaryStream; use pocketmine\utils\BinaryStream;
use pocketmine\utils\ChunkException;
class Chunk{ class Chunk{
@ -1015,69 +1015,4 @@ class Chunk{
public static function chunkBlockHash(int $x, int $y, int $z) : int{ public static function chunkBlockHash(int $x, int $y, int $z) : int{
return ($x << 12) | ($z << 8) | $y; 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;
}
} }

View File

@ -26,14 +26,13 @@ namespace pocketmine\level\format\io;
use pocketmine\level\format\Chunk; use pocketmine\level\format\Chunk;
use pocketmine\level\generator\Generator; use pocketmine\level\generator\Generator;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\level\LevelException;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\nbt\NBT; use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\LongTag; use pocketmine\nbt\tag\LongTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\utils\ChunkException;
use pocketmine\utils\LevelException;
abstract class BaseLevelProvider implements LevelProvider{ abstract class BaseLevelProvider implements LevelProvider{
/** @var Level */ /** @var Level */

View File

@ -19,7 +19,7 @@
* *
*/ */
namespace pocketmine\utils; namespace pocketmine\level\format\io;
class ChunkException extends \RuntimeException{ class ChunkException extends \RuntimeException{

View 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;
}
}

View File

@ -23,7 +23,7 @@ declare(strict_types = 1);
namespace pocketmine\level\format\io; namespace pocketmine\level\format\io;
use pocketmine\utils\LevelException; use pocketmine\level\LevelException;
abstract class LevelProviderManager{ abstract class LevelProviderManager{
protected static $providers = []; protected static $providers = [];

View File

@ -24,13 +24,14 @@ declare(strict_types = 1);
namespace pocketmine\level\format\io\region; namespace pocketmine\level\format\io\region;
use pocketmine\level\format\Chunk; use pocketmine\level\format\Chunk;
use pocketmine\level\format\io\ChunkException;
use pocketmine\level\format\io\ChunkUtils;
use pocketmine\level\format\SubChunk; use pocketmine\level\format\SubChunk;
use pocketmine\nbt\NBT; use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\{ use pocketmine\nbt\tag\{
ByteArrayTag, ByteTag, CompoundTag, IntArrayTag, IntTag, ListTag, LongTag ByteArrayTag, ByteTag, CompoundTag, IntArrayTag, IntTag, ListTag, LongTag
}; };
use pocketmine\Player; use pocketmine\Player;
use pocketmine\utils\ChunkException;
use pocketmine\utils\MainLogger; use pocketmine\utils\MainLogger;
class Anvil extends McRegion{ class Anvil extends McRegion{
@ -57,10 +58,10 @@ class Anvil extends McRegion{
} }
$nbt->Sections[++$subChunks] = new CompoundTag(null, [ $nbt->Sections[++$subChunks] = new CompoundTag(null, [
"Y" => new ByteTag("Y", $y), "Y" => new ByteTag("Y", $y),
"Blocks" => new ByteArrayTag("Blocks", Chunk::reorderByteArray($subChunk->getBlockIdArray())), //Generic in-memory chunks are currently always XZY "Blocks" => new ByteArrayTag("Blocks", ChunkUtils::reorderByteArray($subChunk->getBlockIdArray())), //Generic in-memory chunks are currently always XZY
"Data" => new ByteArrayTag("Data", Chunk::reorderNibbleArray($subChunk->getBlockDataArray())), "Data" => new ByteArrayTag("Data", ChunkUtils::reorderNibbleArray($subChunk->getBlockDataArray())),
"SkyLight" => new ByteArrayTag("SkyLight", Chunk::reorderNibbleArray($subChunk->getSkyLightArray())), "SkyLight" => new ByteArrayTag("SkyLight", ChunkUtils::reorderNibbleArray($subChunk->getSkyLightArray())),
"BlockLight" => new ByteArrayTag("BlockLight", Chunk::reorderNibbleArray($subChunk->getBlockLightArray())) "BlockLight" => new ByteArrayTag("BlockLight", ChunkUtils::reorderNibbleArray($subChunk->getBlockLightArray()))
]); ]);
} }
@ -115,17 +116,17 @@ class Anvil extends McRegion{
foreach($chunk->Sections as $subChunk){ foreach($chunk->Sections as $subChunk){
if($subChunk instanceof CompoundTag){ if($subChunk instanceof CompoundTag){
$subChunks[$subChunk->Y->getValue()] = new SubChunk( $subChunks[$subChunk->Y->getValue()] = new SubChunk(
Chunk::reorderByteArray($subChunk->Blocks->getValue()), ChunkUtils::reorderByteArray($subChunk->Blocks->getValue()),
Chunk::reorderNibbleArray($subChunk->Data->getValue()), ChunkUtils::reorderNibbleArray($subChunk->Data->getValue()),
Chunk::reorderNibbleArray($subChunk->SkyLight->getValue()), ChunkUtils::reorderNibbleArray($subChunk->SkyLight->getValue()),
Chunk::reorderNibbleArray($subChunk->BlockLight->getValue()) ChunkUtils::reorderNibbleArray($subChunk->BlockLight->getValue())
); );
} }
} }
} }
if(isset($chunk->BiomeColors)){ 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)){ }elseif(isset($chunk->Biomes)){
$biomeIds = $chunk->Biomes->getValue(); $biomeIds = $chunk->Biomes->getValue();
}else{ }else{

View File

@ -25,6 +25,8 @@ namespace pocketmine\level\format\io\region;
use pocketmine\level\format\Chunk; use pocketmine\level\format\Chunk;
use pocketmine\level\format\io\BaseLevelProvider; 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\format\SubChunk;
use pocketmine\level\generator\Generator; use pocketmine\level\generator\Generator;
use pocketmine\level\Level; use pocketmine\level\Level;
@ -129,7 +131,7 @@ class McRegion extends BaseLevelProvider{
$chunk = $nbt->getData(); $chunk = $nbt->getData();
if(!isset($chunk->Level) or !($chunk->Level instanceof CompoundTag)){ if(!isset($chunk->Level) or !($chunk->Level instanceof CompoundTag)){
return null; throw new ChunkException("Invalid NBT format");
} }
$chunk = $chunk->Level; $chunk = $chunk->Level;
@ -169,7 +171,7 @@ class McRegion extends BaseLevelProvider{
} }
if(isset($chunk->BiomeColors)){ 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)){ }elseif(isset($chunk->Biomes)){
$biomeIds = $chunk->Biomes->getValue(); $biomeIds = $chunk->Biomes->getValue();
}else{ }else{

View File

@ -24,13 +24,13 @@ declare(strict_types = 1);
namespace pocketmine\level\format\io\region; namespace pocketmine\level\format\io\region;
use pocketmine\level\format\Chunk; use pocketmine\level\format\Chunk;
use pocketmine\level\format\io\ChunkException;
use pocketmine\level\format\SubChunk; use pocketmine\level\format\SubChunk;
use pocketmine\nbt\NBT; use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\{ use pocketmine\nbt\tag\{
ByteArrayTag, ByteTag, CompoundTag, IntArrayTag, IntTag, ListTag, LongTag ByteArrayTag, ByteTag, CompoundTag, IntArrayTag, IntTag, ListTag, LongTag
}; };
use pocketmine\Player; use pocketmine\Player;
use pocketmine\utils\ChunkException;
use pocketmine\utils\MainLogger; use pocketmine\utils\MainLogger;
/** /**

View File

@ -24,9 +24,9 @@ declare(strict_types = 1);
namespace pocketmine\level\format\io\region; namespace pocketmine\level\format\io\region;
use pocketmine\level\format\Chunk; use pocketmine\level\format\Chunk;
use pocketmine\level\format\io\ChunkException;
use pocketmine\level\format\io\LevelProvider; use pocketmine\level\format\io\LevelProvider;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
use pocketmine\utils\ChunkException;
use pocketmine\utils\MainLogger; use pocketmine\utils\MainLogger;
class RegionLoader{ class RegionLoader{

View File

@ -25,7 +25,7 @@
namespace pocketmine\metadata; namespace pocketmine\metadata;
use pocketmine\plugin\Plugin; use pocketmine\plugin\Plugin;
use pocketmine\utils\PluginException; use pocketmine\plugin\PluginException;
abstract class MetadataStore{ abstract class MetadataStore{
/** @var \WeakMap[] */ /** @var \WeakMap[] */

View File

@ -23,8 +23,8 @@ namespace pocketmine\permission;
use pocketmine\event\Timings; use pocketmine\event\Timings;
use pocketmine\plugin\Plugin; use pocketmine\plugin\Plugin;
use pocketmine\plugin\PluginException;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\utils\PluginException;
class PermissibleBase implements Permissible{ class PermissibleBase implements Permissible{
/** @var ServerOperator */ /** @var ServerOperator */

View File

@ -22,7 +22,7 @@
namespace pocketmine\permission; namespace pocketmine\permission;
use pocketmine\plugin\Plugin; use pocketmine\plugin\Plugin;
use pocketmine\utils\PluginException; use pocketmine\plugin\PluginException;
class PermissionAttachment{ class PermissionAttachment{
/** @var PermissionRemovedExecutor */ /** @var PermissionRemovedExecutor */

View File

@ -24,7 +24,6 @@ namespace pocketmine\plugin;
use pocketmine\event\plugin\PluginDisableEvent; use pocketmine\event\plugin\PluginDisableEvent;
use pocketmine\event\plugin\PluginEnableEvent; use pocketmine\event\plugin\PluginEnableEvent;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\utils\PluginException;
/** /**
* Handles different types of plugins * Handles different types of plugins

View File

@ -22,7 +22,6 @@
namespace pocketmine\plugin; namespace pocketmine\plugin;
use pocketmine\permission\Permission; use pocketmine\permission\Permission;
use pocketmine\utils\PluginException;
class PluginDescription{ class PluginDescription{
private $name; private $name;

View File

@ -19,7 +19,9 @@
* *
*/ */
namespace pocketmine\utils; namespace pocketmine\plugin;
use pocketmine\utils\ServerException;
class PluginException extends ServerException{ class PluginException extends ServerException{

View File

@ -33,7 +33,6 @@ use pocketmine\event\TimingsHandler;
use pocketmine\permission\Permissible; use pocketmine\permission\Permissible;
use pocketmine\permission\Permission; use pocketmine\permission\Permission;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\utils\PluginException;
/** /**
* Manages all the plugins, Permissions and Permissibles * Manages all the plugins, Permissions and Permissibles

View File

@ -24,7 +24,6 @@ namespace pocketmine\plugin;
use pocketmine\event\plugin\PluginDisableEvent; use pocketmine\event\plugin\PluginDisableEvent;
use pocketmine\event\plugin\PluginEnableEvent; use pocketmine\event\plugin\PluginEnableEvent;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\utils\PluginException;
/** /**
* Simple script loader, not for plugin development * Simple script loader, not for plugin development

View File

@ -25,8 +25,8 @@
namespace pocketmine\scheduler; namespace pocketmine\scheduler;
use pocketmine\plugin\Plugin; use pocketmine\plugin\Plugin;
use pocketmine\plugin\PluginException;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\utils\PluginException;
use pocketmine\utils\ReversePriorityQueue; use pocketmine\utils\ReversePriorityQueue;
class ServerScheduler{ class ServerScheduler{