Moved entity metadata methods out of Binary

This commit is contained in:
Dylan K. Taylor
2017-02-13 14:05:16 +00:00
parent 91fd99d76a
commit 8a3c30ee7e
6 changed files with 119 additions and 141 deletions

View File

@ -20,12 +20,10 @@
*/
/**
* Various Utilities used around the code
* Methods for working with binary strings
*/
namespace pocketmine\utils;
use pocketmine\entity\Entity;
use pocketmine\item\Item;
class Binary{
const BIG_ENDIAN = 0x00;
@ -81,124 +79,6 @@ class Binary{
return substr(pack("V", $value), 0, -1);
}
/**
* Writes a coded metadata string
*
* @param array $data
*
* @return string
*/
public static function writeMetadata(array $data){
$stream = new BinaryStream();
$stream->putUnsignedVarInt(count($data));
foreach($data as $key => $d){
$stream->putUnsignedVarInt($key); //data key
$stream->putUnsignedVarInt($d[0]); //data type
switch($d[0]){
case Entity::DATA_TYPE_BYTE:
$stream->putByte($d[1]);
break;
case Entity::DATA_TYPE_SHORT:
$stream->putLShort($d[1]); //SIGNED short!
break;
case Entity::DATA_TYPE_INT:
$stream->putVarInt($d[1]);
break;
case Entity::DATA_TYPE_FLOAT:
$stream->putLFloat($d[1]);
break;
case Entity::DATA_TYPE_STRING:
$stream->putString($d[1]);
break;
case Entity::DATA_TYPE_SLOT:
//TODO: change this implementation (use objects)
$stream->putSlot(Item::get($d[1][0], $d[1][2], $d[1][1])); //ID, damage, count
break;
case Entity::DATA_TYPE_POS:
//TODO: change this implementation (use objects)
$stream->putVarInt($d[1][0]); //x
$stream->putVarInt($d[1][1]); //y (SIGNED)
$stream->putVarInt($d[1][2]); //z
break;
case Entity::DATA_TYPE_LONG:
$stream->putVarInt($d[1]); //TODO: varint64 support
break;
case Entity::DATA_TYPE_VECTOR3F:
//TODO: change this implementation (use objects)
$stream->putVector3f($d[1][0], $d[1][1], $d[1][2]); //x, y, z
}
}
return $stream->getBuffer();
}
/**
* Reads a metadata coded string
*
* @param $value
* @param bool $types
*
* @return array
*/
public static function readMetadata($value, $types = false){
$stream = new BinaryStream();
$stream->setBuffer($value);
$count = $stream->getUnsignedVarInt();
$data = [];
for($i = 0; $i < $count; ++$i){
$key = $stream->getUnsignedVarInt();
$type = $stream->getUnsignedVarInt();
$value = null;
switch($type){
case Entity::DATA_TYPE_BYTE:
$value = $stream->getByte();
break;
case Entity::DATA_TYPE_SHORT:
$value = $stream->getLShort(true); //signed
break;
case Entity::DATA_TYPE_INT:
$value = $stream->getVarInt();
break;
case Entity::DATA_TYPE_FLOAT:
$value = $stream->getLFloat();
break;
case Entity::DATA_TYPE_STRING:
$value = $stream->getString();
break;
case Entity::DATA_TYPE_SLOT:
//TODO: use objects directly
$value = [];
$item = $stream->getSlot();
$value[0] = $item->getId();
$value[1] = $item->getCount();
$value[2] = $item->getDamage();
break;
case Entity::DATA_TYPE_POS:
$value = [];
$value[0] = $stream->getVarInt(); //x
$value[1] = $stream->getVarInt(); //y (SIGNED)
$value[2] = $stream->getVarInt(); //z
break;
case Entity::DATA_TYPE_LONG:
$value = $stream->getVarInt(); //TODO: varint64 proper support
break;
case Entity::DATA_TYPE_VECTOR3F:
$value = [0.0, 0.0, 0.0];
$stream->getVector3f($value[0], $value[1], $value[2]);
break;
default:
$value = [];
}
if($types === true){
$data[$key] = [$value, $type];
}else{
$data[$key] = $value;
}
}
return $data;
}
/**
* Reads a byte boolean
*