Add typehints and PhpDoc to NBT API

This commit is contained in:
Dylan K. Taylor 2017-10-14 22:07:25 +01:00
parent ce67bc620a
commit 51906daad0
14 changed files with 60 additions and 56 deletions

View File

@ -39,15 +39,15 @@ class ByteArrayTag extends NamedTag{
parent::__construct($name, $value); parent::__construct($name, $value);
} }
public function getType(){ public function getType() : int{
return NBT::TAG_ByteArray; return NBT::TAG_ByteArray;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->get($nbt->getInt($network)); $this->value = $nbt->get($nbt->getInt($network));
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
$nbt->putInt(strlen($this->value), $network); $nbt->putInt(strlen($this->value), $network);
$nbt->put($this->value); $nbt->put($this->value);
} }
@ -64,7 +64,7 @@ class ByteArrayTag extends NamedTag{
* *
* @throws \TypeError * @throws \TypeError
*/ */
public function setValue($value){ public function setValue($value) : void{
if(!is_string($value)){ if(!is_string($value)){
throw new \TypeError("ByteArrayTag value must be of type string, " . gettype($value) . " given"); throw new \TypeError("ByteArrayTag value must be of type string, " . gettype($value) . " given");
} }

View File

@ -39,15 +39,15 @@ class ByteTag extends NamedTag{
parent::__construct($name, $value); parent::__construct($name, $value);
} }
public function getType(){ public function getType() : int{
return NBT::TAG_Byte; return NBT::TAG_Byte;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getSignedByte(); $this->value = $nbt->getSignedByte();
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
$nbt->putByte($this->value); $nbt->putByte($this->value);
} }
@ -63,7 +63,7 @@ class ByteTag extends NamedTag{
* *
* @throws \TypeError * @throws \TypeError
*/ */
public function setValue($value){ public function setValue($value) : void{
if(!is_int($value)){ if(!is_int($value)){
throw new \TypeError("ByteTag value must be of type int, " . gettype($value) . " given"); throw new \TypeError("ByteTag value must be of type int, " . gettype($value) . " given");
}elseif($value < -(2 ** 7) or $value > ((2 ** 7) - 1)){ }elseif($value < -(2 ** 7) or $value > ((2 ** 7) - 1)){

View File

@ -55,7 +55,7 @@ class CompoundTag extends NamedTag implements \ArrayAccess{
* *
* @throws \TypeError * @throws \TypeError
*/ */
public function setValue($value){ public function setValue($value) : void{
if(is_array($value)){ if(is_array($value)){
foreach($value as $name => $tag){ foreach($value as $name => $tag){
if($tag instanceof NamedTag){ if($tag instanceof NamedTag){
@ -385,11 +385,11 @@ class CompoundTag extends NamedTag implements \ArrayAccess{
unset($this->{$offset}); unset($this->{$offset});
} }
public function getType(){ public function getType() : int{
return NBT::TAG_Compound; return NBT::TAG_Compound;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
$this->value = []; $this->value = [];
do{ do{
$tag = $nbt->readTag($network); $tag = $nbt->readTag($network);
@ -399,7 +399,7 @@ class CompoundTag extends NamedTag implements \ArrayAccess{
}while(!($tag instanceof EndTag) and !$nbt->feof()); }while(!($tag instanceof EndTag) and !$nbt->feof());
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
foreach($this as $tag){ foreach($this as $tag){
if($tag instanceof Tag and !($tag instanceof EndTag)){ if($tag instanceof Tag and !($tag instanceof EndTag)){
$nbt->writeTag($tag, $network); $nbt->writeTag($tag, $network);

View File

@ -39,15 +39,15 @@ class DoubleTag extends NamedTag{
parent::__construct($name, $value); parent::__construct($name, $value);
} }
public function getType(){ public function getType() : int{
return NBT::TAG_Double; return NBT::TAG_Double;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getDouble(); $this->value = $nbt->getDouble();
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
$nbt->putDouble($this->value); $nbt->putDouble($this->value);
} }
@ -63,7 +63,7 @@ class DoubleTag extends NamedTag{
* *
* @throws \TypeError * @throws \TypeError
*/ */
public function setValue($value){ public function setValue($value) : void{
if(!is_float($value) and !is_int($value)){ if(!is_float($value) and !is_int($value)){
throw new \TypeError("DoubleTag value must be of type double, " . gettype($value) . " given"); throw new \TypeError("DoubleTag value must be of type double, " . gettype($value) . " given");
} }

View File

@ -27,15 +27,15 @@ use pocketmine\nbt\NBT;
class EndTag extends Tag{ class EndTag extends Tag{
public function getType(){ public function getType() : int{
return NBT::TAG_End; return NBT::TAG_End;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
} }
} }

View File

@ -39,15 +39,15 @@ class FloatTag extends NamedTag{
parent::__construct($name, $value); parent::__construct($name, $value);
} }
public function getType(){ public function getType() : int{
return NBT::TAG_Float; return NBT::TAG_Float;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getFloat(); $this->value = $nbt->getFloat();
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
$nbt->putFloat($this->value); $nbt->putFloat($this->value);
} }
@ -58,7 +58,7 @@ class FloatTag extends NamedTag{
return parent::getValue(); return parent::getValue();
} }
public function setValue($value){ public function setValue($value) : void{
if(!is_float($value) and !is_int($value)){ if(!is_float($value) and !is_int($value)){
throw new \TypeError("FloatTag value must be of type float, " . gettype($value) . " given"); throw new \TypeError("FloatTag value must be of type float, " . gettype($value) . " given");
} }

View File

@ -39,16 +39,16 @@ class IntArrayTag extends NamedTag{
parent::__construct($name, $value); parent::__construct($name, $value);
} }
public function getType(){ public function getType() : int{
return NBT::TAG_IntArray; return NBT::TAG_IntArray;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
$size = $nbt->getInt($network); $size = $nbt->getInt($network);
$this->value = array_values(unpack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", $nbt->get($size * 4))); $this->value = array_values(unpack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", $nbt->get($size * 4)));
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
$nbt->putInt(count($this->value), $network); $nbt->putInt(count($this->value), $network);
$nbt->put(pack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", ...$this->value)); $nbt->put(pack($nbt->endianness === NBT::LITTLE_ENDIAN ? "V*" : "N*", ...$this->value));
} }
@ -71,7 +71,7 @@ class IntArrayTag extends NamedTag{
* *
* @throws \TypeError * @throws \TypeError
*/ */
public function setValue($value){ public function setValue($value) : void{
if(!is_array($value)){ if(!is_array($value)){
throw new \TypeError("IntArrayTag value must be of type int[], " . gettype($value) . " given"); throw new \TypeError("IntArrayTag value must be of type int[], " . gettype($value) . " given");
} }

View File

@ -37,15 +37,15 @@ class IntTag extends NamedTag{
parent::__construct($name, $value); parent::__construct($name, $value);
} }
public function getType(){ public function getType() : int{
return NBT::TAG_Int; return NBT::TAG_Int;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getInt($network); $this->value = $nbt->getInt($network);
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
$nbt->putInt($this->value, $network); $nbt->putInt($this->value, $network);
} }
@ -61,7 +61,7 @@ class IntTag extends NamedTag{
* *
* @throws \TypeError * @throws \TypeError
*/ */
public function setValue($value){ public function setValue($value) : void{
if(!is_int($value)){ if(!is_int($value)){
throw new \TypeError("IntTag value must be of type int, " . gettype($value) . " given"); throw new \TypeError("IntTag value must be of type int, " . gettype($value) . " given");
}elseif($value < -(2 ** 31) or $value > ((2 ** 31) - 1)){ }elseif($value < -(2 ** 31) or $value > ((2 ** 31) - 1)){

View File

@ -63,7 +63,7 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
* *
* @throws \TypeError * @throws \TypeError
*/ */
public function setValue($value){ public function setValue($value) : void{
if(is_array($value)){ if(is_array($value)){
foreach($value as $name => $tag){ foreach($value as $name => $tag){
if($tag instanceof NamedTag){ if($tag instanceof NamedTag){
@ -129,7 +129,7 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
return $count; return $count;
} }
public function getType(){ public function getType() : int{
return NBT::TAG_List; return NBT::TAG_List;
} }
@ -141,7 +141,7 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
return $this->tagType; return $this->tagType;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
$this->value = []; $this->value = [];
$this->tagType = $nbt->getByte(); $this->tagType = $nbt->getByte();
$size = $nbt->getInt($network); $size = $nbt->getInt($network);
@ -154,7 +154,7 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
} }
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
if($this->tagType === NBT::TAG_End){ //previously empty list, try detecting type from tag children if($this->tagType === NBT::TAG_End){ //previously empty list, try detecting type from tag children
$id = NBT::TAG_End; $id = NBT::TAG_End;
foreach($this as $tag){ foreach($this as $tag){
@ -162,7 +162,7 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
if($id === NBT::TAG_End){ if($id === NBT::TAG_End){
$id = $tag->getType(); $id = $tag->getType();
}elseif($id !== $tag->getType()){ }elseif($id !== $tag->getType()){
return false; return; //TODO: throw exception?
} }
} }
} }
@ -182,8 +182,6 @@ class ListTag extends NamedTag implements \ArrayAccess, \Countable{
foreach($tags as $tag){ foreach($tags as $tag){
$tag->write($nbt, $network); $tag->write($nbt, $network);
} }
return true;
} }
public function __toString(){ public function __toString(){

View File

@ -39,15 +39,15 @@ class LongTag extends NamedTag{
parent::__construct($name, $value); parent::__construct($name, $value);
} }
public function getType(){ public function getType() : int{
return NBT::TAG_Long; return NBT::TAG_Long;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getLong($network); $this->value = $nbt->getLong($network);
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
$nbt->putLong($this->value, $network); $nbt->putLong($this->value, $network);
} }

View File

@ -25,7 +25,7 @@ namespace pocketmine\nbt\tag;
abstract class NamedTag extends Tag{ abstract class NamedTag extends Tag{
/** @var string */
protected $__name; protected $__name;
/** /**
@ -39,11 +39,17 @@ abstract class NamedTag extends Tag{
} }
} }
public function getName(){ /**
* @return string
*/
public function getName() : string{
return $this->__name; return $this->__name;
} }
public function setName($name){ /**
* @param string $name
*/
public function setName(string $name) : void{
$this->__name = $name; $this->__name = $name;
} }
} }

View File

@ -39,15 +39,15 @@ class ShortTag extends NamedTag{
parent::__construct($name, $value); parent::__construct($name, $value);
} }
public function getType(){ public function getType() : int{
return NBT::TAG_Short; return NBT::TAG_Short;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getSignedShort(); $this->value = $nbt->getSignedShort();
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
$nbt->putShort($this->value); $nbt->putShort($this->value);
} }
@ -63,7 +63,7 @@ class ShortTag extends NamedTag{
* *
* @throws \TypeError * @throws \TypeError
*/ */
public function setValue($value){ public function setValue($value) : void{
if(!is_int($value)){ if(!is_int($value)){
throw new \TypeError("ShortTag value must be of type int, " . gettype($value) . " given"); throw new \TypeError("ShortTag value must be of type int, " . gettype($value) . " given");
}elseif($value < -(2 ** 15) or $value > ((2 ** 15) - 1)){ }elseif($value < -(2 ** 15) or $value > ((2 ** 15) - 1)){

View File

@ -39,15 +39,15 @@ class StringTag extends NamedTag{
parent::__construct($name, $value); parent::__construct($name, $value);
} }
public function getType(){ public function getType() : int{
return NBT::TAG_String; return NBT::TAG_String;
} }
public function read(NBT $nbt, bool $network = false){ public function read(NBT $nbt, bool $network = false) : void{
$this->value = $nbt->getString($network); $this->value = $nbt->getString($network);
} }
public function write(NBT $nbt, bool $network = false){ public function write(NBT $nbt, bool $network = false) : void{
$nbt->putString($this->value, $network); $nbt->putString($this->value, $network);
} }
@ -63,7 +63,7 @@ class StringTag extends NamedTag{
* *
* @throws \TypeError * @throws \TypeError
*/ */
public function setValue($value){ public function setValue($value) : void{
if(!is_string($value)){ if(!is_string($value)){
throw new \TypeError("ShortTag value must be of type int, " . gettype($value) . " given"); throw new \TypeError("ShortTag value must be of type int, " . gettype($value) . " given");
} }

View File

@ -36,15 +36,15 @@ abstract class Tag extends \stdClass{
return $this->value; return $this->value;
} }
abstract public function getType(); abstract public function getType() : int;
public function setValue($value){ public function setValue($value) : void{
$this->value = $value; $this->value = $value;
} }
abstract public function write(NBT $nbt, bool $network = false); abstract public function write(NBT $nbt, bool $network = false) : void;
abstract public function read(NBT $nbt, bool $network = false); abstract public function read(NBT $nbt, bool $network = false) : void;
public function __toString(){ public function __toString(){
return (string) $this->value; return (string) $this->value;