Skip to content

Commit

Permalink
Untested support for LongArrayTag
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Feb 14, 2018
1 parent f52d10e commit 5e073f7
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/BigEndianNBTStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,14 @@ public function putIntArray(array $array) : void{
$this->putInt(count($array));
$this->put(pack("N*", ...$array));
}

public function getLongArray() : array{
$len = $this->getInt();
return array_values(unpack("J*", $this->get($len * 8)));
}

public function putLongArray(array $array) : void{
$this->putInt(count($array));
$this->put(pack("J*", ...$array));
}
}
10 changes: 10 additions & 0 deletions src/LittleEndianNBTStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,14 @@ public function putIntArray(array $array) : void{
$this->putInt(count($array));
$this->put(pack("V*", ...$array));
}

public function getLongArray() : array{
$len = $this->getInt();
return array_values(unpack("P*", $this->get($len * 8)));
}

public function putLongArray(array $array) : void{
$this->putInt(count($array));
$this->put(pack("P*", ...$array));
}
}
1 change: 1 addition & 0 deletions src/NBT.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ abstract class NBT{
public const TAG_List = 9;
public const TAG_Compound = 10;
public const TAG_IntArray = 11;
public const TAG_LongArray = 12;

/**
* @param int $type
Expand Down
4 changes: 4 additions & 0 deletions src/NBTStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ abstract public function getIntArray() : array;

abstract public function putIntArray(array $array) : void;

abstract public function getLongArray() : array;

abstract public function putLongArray(array $array) : void;



public function getArray() : array{
Expand Down
17 changes: 17 additions & 0 deletions src/NetworkLittleEndianNBTStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,21 @@ public function putIntArray(array $array) : void{
$this->putInt($v); //varint
}
}

public function getLongArray() : array{
$len = $this->getInt(); //varint
$ret = [];
for($i = 0; $i < $len; ++$i){
$ret[] = $this->getLong(); //varlong
}

return $ret;
}

public function putLongArray(array $array) : void{
$this->putInt(count($array)); //varint
foreach($array as $v){
$this->putLong($v); //varlong
}
}
}
19 changes: 19 additions & 0 deletions src/tag/CompoundTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,17 @@ public function getIntArray(string $name, ?array $default = null, bool $badTagDe
return $this->getTagValue($name, IntArrayTag::class, $default, $badTagDefault);
}

/**
* @param string $name
* @param int[]|null $default
* @param bool $badTagDefault
*
* @return int[]
*/
public function getLongArray(string $name, ?array $default = null, bool $badTagDefault = false) : array{
return $this->getTagValue($name, LongArrayTag::class, $default, $badTagDefault);
}

/**
* Sets the value of the child tag at the specified offset, creating it if it does not exist. If the child tag
* exists and the value is of the wrong type, an exception will be thrown.
Expand Down Expand Up @@ -399,6 +410,14 @@ public function setIntArray(string $name, array $value, bool $force = false) : v
$this->setTagValue($name, IntArrayTag::class, $value, $force);
}

/**
* @param string $name
* @param int[] $value
* @param bool $force
*/
public function setLongArray(string $name, array $value, bool $force = false) : void{
$this->setTagValue($name, LongArrayTag::class, $value, $force);
}

/**
* @param string $offset
Expand Down
84 changes: 84 additions & 0 deletions src/tag/LongArrayTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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\nbt\tag;

use pocketmine\nbt\NBT;
use pocketmine\nbt\NBTStream;

class LongArrayTag extends NamedTag{
/** @var int[] */
protected $value = [];

/**
* LongArrayTag constructor.
*
* @param string $name
* @param int[] $value
*/
public function __construct(string $name = "", array $value = []){
parent::__construct($name, $value);
}

public function getType() : int{
return NBT::TAG_LongArray;
}

public function read(NBTStream $nbt) : void{
$this->value = $nbt->getLongArray();
}

public function write(NBTStream $nbt) : void{
$nbt->putLongArray($this->value);
}

public function __toString(){
$str = get_class($this) . "{\n";
$str .= implode(", ", $this->value);
return $str . "}";
}

/**
* @return int[]
*/
public function &getValue() : array{
return parent::getValue();
}

/**
* @param int[] $value
*
* @throws \TypeError
*/
public function setValue($value) : void{
if(!is_array($value)){
throw new \TypeError("LongArrayTag value must be of type int[], " . gettype($value) . " given");
}
assert(count(array_filter($value, function($v){
return !is_int($v);
})) === 0);

parent::setValue($value);
}

}

0 comments on commit 5e073f7

Please sign in to comment.