PocketMine-MP/src/world/format/BiomeArray.php
Dylan K. Taylor 82d361d75f extract a BiomeArray unit from Chunk
this now also properly validates data read from disk.
2020-05-16 17:36:22 +01:00

68 lines
1.7 KiB
PHP

<?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\format;
use function strlen;
final class BiomeArray{
/** @var string */
private $payload;
/**
* @param string $payload ZZZZXXXX key bits
*/
public function __construct(string $payload){
if(strlen($payload) !== 256){
throw new \InvalidArgumentException("Biome array is expected to be exactly 256 bytes");
}
$this->payload = $payload;
}
private static function idx(int $x, int $z) : int{
if($x >= 16 or $z >= 16){
throw new \InvalidArgumentException("x and z must be in the range 0-15");
}
return ($z << 4) | $x;
}
public function get(int $x, int $z) : int{
return ord($this->payload[self::idx($x, $z)]);
}
public function set(int $x, int $z, int $biomeId) : void{
if($biomeId < 0 or $biomeId >= 256){
throw new \InvalidArgumentException("Biome ID must be in the range 0-255");
}
$this->payload[self::idx($x, $z)] = chr($biomeId);
}
/**
* @return string ZZZZXXXX key bits
*/
public function getData() : string{
return $this->payload;
}
}