Updated with ListTag changes from PocketMine-NBT

This commit is contained in:
Dylan K. Taylor 2018-02-07 18:14:58 +00:00
parent 4a85311c5f
commit 9abfd54cc1
8 changed files with 71 additions and 125 deletions

View File

@ -24,7 +24,7 @@
"pocketmine/raklib": "dev-master#9b50878021ca545684fc1fd2d9333911ab2988e1", "pocketmine/raklib": "dev-master#9b50878021ca545684fc1fd2d9333911ab2988e1",
"pocketmine/pocketmine-spl": "^0.2.0", "pocketmine/pocketmine-spl": "^0.2.0",
"pocketmine/pocketmine-binaryutils": "dev-master#a7cd5303a3b215d26bf9be76682ce9311f40e887", "pocketmine/pocketmine-binaryutils": "dev-master#a7cd5303a3b215d26bf9be76682ce9311f40e887",
"pocketmine/pocketmine-nbt": "dev-master#3bb6fb1b1b7b3aa40939764a5479b2758f3c135e" "pocketmine/pocketmine-nbt": "dev-master#37ad66b279fa8e746ccc68a451eef8ef9421aaa5"
}, },
"autoload": { "autoload": {
"psr-0": { "psr-0": {

15
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "08158e4b18863a13bde2505e4b274f0c", "content-hash": "19400d9f6e78537580761ca473974158",
"packages": [ "packages": [
{ {
"name": "pocketmine/pocketmine-binaryutils", "name": "pocketmine/pocketmine-binaryutils",
@ -45,12 +45,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/pmmp/PocketMine-NBT.git", "url": "https://github.com/pmmp/PocketMine-NBT.git",
"reference": "3bb6fb1b1b7b3aa40939764a5479b2758f3c135e" "reference": "37ad66b279fa8e746ccc68a451eef8ef9421aaa5"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/pmmp/PocketMine-NBT/zipball/3bb6fb1b1b7b3aa40939764a5479b2758f3c135e", "url": "https://api.github.com/repos/pmmp/PocketMine-NBT/zipball/37ad66b279fa8e746ccc68a451eef8ef9421aaa5",
"reference": "3bb6fb1b1b7b3aa40939764a5479b2758f3c135e", "reference": "37ad66b279fa8e746ccc68a451eef8ef9421aaa5",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -63,6 +63,11 @@
"pocketmine\\nbt\\": "src/" "pocketmine\\nbt\\": "src/"
} }
}, },
"autoload-dev": {
"psr-4": {
"pocketmine\\nbt\\": "tests/phpunit/"
}
},
"license": [ "license": [
"LGPL-3.0" "LGPL-3.0"
], ],
@ -71,7 +76,7 @@
"source": "https://github.com/pmmp/PocketMine-NBT/tree/master", "source": "https://github.com/pmmp/PocketMine-NBT/tree/master",
"issues": "https://github.com/pmmp/PocketMine-NBT/issues" "issues": "https://github.com/pmmp/PocketMine-NBT/issues"
}, },
"time": "2018-02-11T15:53:29+00:00" "time": "2018-02-13T14:33:27+00:00"
}, },
{ {
"name": "pocketmine/pocketmine-spl", "name": "pocketmine/pocketmine-spl",

View File

@ -99,7 +99,7 @@ class StandingBanner extends Transparent{
$tile = $this->level->getTile($this); $tile = $this->level->getTile($this);
$drop = ItemFactory::get(Item::BANNER, ($tile instanceof TileBanner ? $tile->getBaseColor() : 0)); $drop = ItemFactory::get(Item::BANNER, ($tile instanceof TileBanner ? $tile->getBaseColor() : 0));
if($tile instanceof TileBanner and ($patterns = $tile->namedtag->getListTag(TileBanner::TAG_PATTERNS)) !== null and $patterns->getCount() > 0){ if($tile instanceof TileBanner and ($patterns = $tile->namedtag->getListTag(TileBanner::TAG_PATTERNS)) !== null and !$patterns->empty()){
$drop->setNamedTagEntry($patterns); $drop->setNamedTagEntry($patterns);
} }

View File

@ -515,8 +515,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
foreach($inventoryTag as $i => $item){ foreach($inventoryTag as $i => $item){
$slot = $item->getByte("Slot"); $slot = $item->getByte("Slot");
if($slot >= 0 and $slot < 9){ //Hotbar if($slot >= 0 and $slot < 9){ //Hotbar
//Old hotbar saving stuff, remove it (useless now) //Old hotbar saving stuff, ignore it
unset($inventoryTag[$i]);
}elseif($slot >= 100 and $slot < 104){ //Armor }elseif($slot >= 100 and $slot < 104){ //Armor
$this->armorInventory->setItem($slot - 100, ItemItem::nbtDeserialize($item)); $this->armorInventory->setItem($slot - 100, ItemItem::nbtDeserialize($item));
}else{ }else{
@ -647,7 +646,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
for($slot = $this->inventory->getHotbarSize(); $slot < $slotCount; ++$slot){ for($slot = $this->inventory->getHotbarSize(); $slot < $slotCount; ++$slot){
$item = $this->inventory->getItem($slot - 9); $item = $this->inventory->getItem($slot - 9);
if(!$item->isNull()){ if(!$item->isNull()){
$inventoryTag[$slot] = $item->nbtSerialize($slot); $inventoryTag->push($item->nbtSerialize($slot));
} }
} }
@ -655,7 +654,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
for($slot = 100; $slot < 104; ++$slot){ for($slot = 100; $slot < 104; ++$slot){
$item = $this->armorInventory->getItem($slot - 100); $item = $this->armorInventory->getItem($slot - 100);
if(!$item->isNull()){ if(!$item->isNull()){
$inventoryTag[$slot] = $item->nbtSerialize($slot); $inventoryTag->push($item->nbtSerialize($slot));
} }
} }

View File

@ -77,22 +77,17 @@ class Banner extends Item{
* @return int ID of pattern. * @return int ID of pattern.
*/ */
public function addPattern(string $pattern, int $color) : int{ public function addPattern(string $pattern, int $color) : int{
$patternId = 0;
if($this->getPatternCount() !== 0){
$patternId = max($this->getPatternIds()) + 1;
}
$patternsTag = $this->getNamedTag()->getListTag(self::TAG_PATTERNS); $patternsTag = $this->getNamedTag()->getListTag(self::TAG_PATTERNS);
assert($patternsTag !== null); assert($patternsTag !== null);
$patternsTag[$patternId] = new CompoundTag("", [ $patternsTag->push(new CompoundTag("", [
new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f), new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f),
new StringTag(self::TAG_PATTERN_NAME, $pattern) new StringTag(self::TAG_PATTERN_NAME, $pattern)
]); ]));
$this->setNamedTagEntry($patternsTag); $this->setNamedTagEntry($patternsTag);
return $patternId; return $patternsTag->count() - 1;
} }
/** /**
@ -104,7 +99,7 @@ class Banner extends Item{
*/ */
public function patternExists(int $patternId) : bool{ public function patternExists(int $patternId) : bool{
$this->correctNBT(); $this->correctNBT();
return isset($this->getNamedTag()->getListTag(self::TAG_PATTERNS)[$patternId]); return $this->getNamedTag()->getListTag(self::TAG_PATTERNS)->isset($patternId);
} }
/** /**
@ -121,7 +116,7 @@ class Banner extends Item{
$patternsTag = $this->getNamedTag()->getListTag(self::TAG_PATTERNS); $patternsTag = $this->getNamedTag()->getListTag(self::TAG_PATTERNS);
assert($patternsTag !== null); assert($patternsTag !== null);
$pattern = $patternsTag[$patternId]; $pattern = $patternsTag->get($patternId);
assert($pattern instanceof CompoundTag); assert($pattern instanceof CompoundTag);
return [ return [
@ -148,10 +143,10 @@ class Banner extends Item{
$patternsTag = $this->getNamedTag()->getListTag(self::TAG_PATTERNS); $patternsTag = $this->getNamedTag()->getListTag(self::TAG_PATTERNS);
assert($patternsTag !== null); assert($patternsTag !== null);
$patternsTag[$patternId] = new CompoundTag("", [ $patternsTag->set($patternId, new CompoundTag("", [
new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f), new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f),
new StringTag(self::TAG_PATTERN_NAME, $pattern) new StringTag(self::TAG_PATTERN_NAME, $pattern)
]); ]));
$this->setNamedTagEntry($patternsTag); $this->setNamedTagEntry($patternsTag);
return true; return true;
@ -172,7 +167,7 @@ class Banner extends Item{
$patternsTag = $this->getNamedTag()->getListTag(self::TAG_PATTERNS); $patternsTag = $this->getNamedTag()->getListTag(self::TAG_PATTERNS);
if($patternsTag instanceof ListTag){ if($patternsTag instanceof ListTag){
unset($patternsTag[$patternId]); $patternsTag->remove($patternId);
$this->setNamedTagEntry($patternsTag); $this->setNamedTagEntry($patternsTag);
} }
@ -186,12 +181,7 @@ class Banner extends Item{
* @return bool indicating whether the banner was empty or not. * @return bool indicating whether the banner was empty or not.
*/ */
public function deleteTopPattern() : bool{ public function deleteTopPattern() : bool{
$keys = $this->getPatternIds(); return $this->deletePattern($this->getPatternCount() - 1);
if(empty($keys)){
return false;
}
return $this->deletePattern(max($keys));
} }
/** /**
@ -201,27 +191,7 @@ class Banner extends Item{
* @return bool indicating whether the banner was empty or not. * @return bool indicating whether the banner was empty or not.
*/ */
public function deleteBottomPattern() : bool{ public function deleteBottomPattern() : bool{
$keys = $this->getPatternIds(); return $this->deletePattern(0);
if(empty($keys)){
return false;
}
return $this->deletePattern(min($keys));
}
/**
* Returns an array containing all pattern IDs
*
* @return array
*/
public function getPatternIds() : array{
$this->correctNBT();
$keys = array_keys((array) ($this->getNamedTag()->getListTag(self::TAG_PATTERNS) ?? []));
return array_filter($keys, function($key){
return is_numeric($key);
}, ARRAY_FILTER_USE_KEY);
} }
/** /**
@ -230,7 +200,7 @@ class Banner extends Item{
* @return int * @return int
*/ */
public function getPatternCount() : int{ public function getPatternCount() : int{
return count($this->getPatternIds()); return $this->getNamedTag()->getListTag(self::TAG_PATTERNS)->count();
} }
public function correctNBT() : void{ public function correctNBT() : void{

View File

@ -345,7 +345,7 @@ class Item implements ItemIds, \JsonSerializable{
/** @var CompoundTag $entry */ /** @var CompoundTag $entry */
foreach($ench as $k => $entry){ foreach($ench as $k => $entry){
if($entry->getShort("id") === $id and ($level === -1 or $entry->getShort("lvl") === $level)){ if($entry->getShort("id") === $id and ($level === -1 or $entry->getShort("lvl") === $level)){
unset($ench[$k]); $ench->remove($k);
break; break;
} }
} }
@ -370,10 +370,10 @@ class Item implements ItemIds, \JsonSerializable{
/** @var CompoundTag $entry */ /** @var CompoundTag $entry */
foreach($ench as $k => $entry){ foreach($ench as $k => $entry){
if($entry->getShort("id") === $enchantment->getId()){ if($entry->getShort("id") === $enchantment->getId()){
$ench[$k] = new CompoundTag("", [ $ench->set($k, new CompoundTag("", [
new ShortTag("id", $enchantment->getId()), new ShortTag("id", $enchantment->getId()),
new ShortTag("lvl", $enchantment->getLevel()) new ShortTag("lvl", $enchantment->getLevel())
]); ]));
$found = true; $found = true;
break; break;
} }
@ -381,10 +381,10 @@ class Item implements ItemIds, \JsonSerializable{
} }
if(!$found){ if(!$found){
$ench[count($ench)] = new CompoundTag("", [ $ench->push(new CompoundTag("", [
new ShortTag("id", $enchantment->getId()), new ShortTag("id", $enchantment->getId()),
new ShortTag("lvl", $enchantment->getLevel()) new ShortTag("lvl", $enchantment->getLevel())
]); ]));
} }
$this->setNamedTagEntry($ench); $this->setNamedTagEntry($ench);

View File

@ -46,7 +46,7 @@ class WritableBook extends Item{
* @return bool * @return bool
*/ */
public function pageExists(int $pageId) : bool{ public function pageExists(int $pageId) : bool{
return isset($this->getPages()[$pageId]); return $this->getPagesTag()->isset($pageId);
} }
/** /**
@ -62,7 +62,7 @@ class WritableBook extends Item{
return null; return null;
} }
$page = $pages[$pageId] ?? null; $page = $pages->get($pageId);
if($page instanceof CompoundTag){ if($page instanceof CompoundTag){
return $page->getString(self::TAG_PAGE_TEXT, ""); return $page->getString(self::TAG_PAGE_TEXT, "");
} }
@ -85,13 +85,13 @@ class WritableBook extends Item{
$created = true; $created = true;
} }
$namedTag = $this->getNamedTag(); /** @var CompoundTag[]|ListTag $pagesTag */
/** @var CompoundTag[]|ListTag $pages */ $pagesTag = $this->getPagesTag();
$pages = $namedTag->getListTag(self::TAG_PAGES); /** @var CompoundTag $page */
assert($pages instanceof ListTag); $page = $pagesTag->get($pageId);
$pages[$pageId]->setString(self::TAG_PAGE_TEXT, $pageText); $page->setString(self::TAG_PAGE_TEXT, $pageText);
$this->setNamedTag($namedTag); $this->setNamedTagEntry($pagesTag);
return $created; return $created;
} }
@ -107,18 +107,16 @@ class WritableBook extends Item{
throw new \InvalidArgumentException("Page number \"$pageId\" is out of range"); throw new \InvalidArgumentException("Page number \"$pageId\" is out of range");
} }
$pages = $this->getPages(); $pagesTag = $this->getPagesTag();
for($id = 0; $id <= $pageId; $id++){ for($current = $pagesTag->count(); $current <= $pageId; $current++){
if(!isset($pages[$id])){ $pagesTag->push(new CompoundTag("", [
$pages[$id] = new CompoundTag("", [
new StringTag(self::TAG_PAGE_TEXT, ""), new StringTag(self::TAG_PAGE_TEXT, ""),
new StringTag(self::TAG_PAGE_PHOTONAME, "") new StringTag(self::TAG_PAGE_PHOTONAME, "")
]); ]));
}
} }
$this->setPages($pages); $this->setNamedTagEntry($pagesTag);
} }
/** /**
@ -129,10 +127,9 @@ class WritableBook extends Item{
* @return bool indicating success * @return bool indicating success
*/ */
public function deletePage(int $pageId) : bool{ public function deletePage(int $pageId) : bool{
$pages = $this->getPages(); $pagesTag = $this->getPagesTag();
unset($pages[$pageId]); $pagesTag->remove($pageId);
$this->setNamedTagEntry($pagesTag);
$this->setPages(array_values($pages));
return true; return true;
} }
@ -146,16 +143,14 @@ class WritableBook extends Item{
* @return bool indicating success * @return bool indicating success
*/ */
public function insertPage(int $pageId, string $pageText = "") : bool{ public function insertPage(int $pageId, string $pageText = "") : bool{
$pages = $this->getPages(); $pagesTag = $this->getPagesTag();
$this->setPages(array_merge( $pagesTag->insert($pageId, new CompoundTag("", [
array_slice($pages, 0, $pageId),
[new CompoundTag("", [
new StringTag(self::TAG_PAGE_TEXT, $pageText), new StringTag(self::TAG_PAGE_TEXT, $pageText),
new StringTag(self::TAG_PAGE_PHOTONAME, "") new StringTag(self::TAG_PAGE_PHOTONAME, "")
])], ]));
array_slice($pages, $pageId)
)); $this->setNamedTagEntry($pagesTag);
return true; return true;
} }
@ -199,6 +194,10 @@ class WritableBook extends Item{
return $pages->getValue(); return $pages->getValue();
} }
protected function getPagesTag() : ListTag{
return $this->getNamedTag()->getListTag(self::TAG_PAGES) ?? new ListTag(self::TAG_PAGES, [], NBT::TAG_Compound);
}
/** /**
* *
* @param CompoundTag[] $pages * @param CompoundTag[] $pages

View File

@ -130,18 +130,6 @@ class Banner extends Spawnable implements Nameable{
$this->onChanged(); $this->onChanged();
} }
/**
* Returns an array containing all pattern IDs
*
* @return array
*/
public function getPatternIds() : array{
$keys = array_keys((array) $this->namedtag->getTag(self::TAG_PATTERNS));
return array_filter($keys, function(string $key){
return is_numeric($key);
}, ARRAY_FILTER_USE_KEY);
}
/** /**
* Applies a new pattern on the banner with the given color. * Applies a new pattern on the banner with the given color.
* *
@ -151,20 +139,15 @@ class Banner extends Spawnable implements Nameable{
* @return int ID of pattern. * @return int ID of pattern.
*/ */
public function addPattern(string $pattern, int $color) : int{ public function addPattern(string $pattern, int $color) : int{
$patternId = 0;
if($this->getPatternCount() !== 0){
$patternId = max($this->getPatternIds()) + 1;
}
$list = $this->namedtag->getListTag(self::TAG_PATTERNS); $list = $this->namedtag->getListTag(self::TAG_PATTERNS);
assert($list !== null); assert($list !== null);
$list[$patternId] = new CompoundTag("", [ $list->push(new CompoundTag("", [
new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f), new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f),
new StringTag(self::TAG_PATTERN_NAME, $pattern) new StringTag(self::TAG_PATTERN_NAME, $pattern)
]); ]));
$this->onChanged(); $this->onChanged();
return $patternId; return $list->count() - 1; //Last offset in the list
} }
/** /**
@ -175,7 +158,7 @@ class Banner extends Spawnable implements Nameable{
* @return bool * @return bool
*/ */
public function patternExists(int $patternId) : bool{ public function patternExists(int $patternId) : bool{
return isset($this->namedtag->getListTag(self::TAG_PATTERNS)[$patternId]); return $this->namedtag->getListTag(self::TAG_PATTERNS)->isset($patternId);
} }
/** /**
@ -192,7 +175,7 @@ class Banner extends Spawnable implements Nameable{
$list = $this->namedtag->getListTag(self::TAG_PATTERNS); $list = $this->namedtag->getListTag(self::TAG_PATTERNS);
assert($list instanceof ListTag); assert($list instanceof ListTag);
$patternTag = $list[$patternId]; $patternTag = $list->get($patternId);
assert($patternTag instanceof CompoundTag); assert($patternTag instanceof CompoundTag);
return [ return [
@ -218,10 +201,10 @@ class Banner extends Spawnable implements Nameable{
$list = $this->namedtag->getListTag(self::TAG_PATTERNS); $list = $this->namedtag->getListTag(self::TAG_PATTERNS);
assert($list instanceof ListTag); assert($list instanceof ListTag);
$list[$patternId] = new CompoundTag("", [ $list->set($patternId, new CompoundTag("", [
new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f), new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f),
new StringTag(self::TAG_PATTERN_NAME, $pattern) new StringTag(self::TAG_PATTERN_NAME, $pattern)
]); ]));
$this->onChanged(); $this->onChanged();
return true; return true;
@ -241,7 +224,7 @@ class Banner extends Spawnable implements Nameable{
$list = $this->namedtag->getListTag(self::TAG_PATTERNS); $list = $this->namedtag->getListTag(self::TAG_PATTERNS);
if($list !== null){ if($list !== null){
unset($list[$patternId]); $list->remove($patternId);
} }
$this->onChanged(); $this->onChanged();
@ -254,12 +237,7 @@ class Banner extends Spawnable implements Nameable{
* @return bool indicating whether the banner was empty or not. * @return bool indicating whether the banner was empty or not.
*/ */
public function deleteTopPattern() : bool{ public function deleteTopPattern() : bool{
$keys = $this->getPatternIds(); return $this->deletePattern($this->getPatternCount() - 1);
if(empty($keys)){
return false;
}
return $this->deletePattern(max($keys));
} }
/** /**
@ -268,12 +246,7 @@ class Banner extends Spawnable implements Nameable{
* @return bool indicating whether the banner was empty or not. * @return bool indicating whether the banner was empty or not.
*/ */
public function deleteBottomPattern() : bool{ public function deleteBottomPattern() : bool{
$keys = $this->getPatternIds(); return $this->deletePattern(0);
if(empty($keys)){
return false;
}
return $this->deletePattern(min($keys));
} }
/** /**
@ -282,7 +255,7 @@ class Banner extends Spawnable implements Nameable{
* @return int * @return int
*/ */
public function getPatternCount() : int{ public function getPatternCount() : int{
return count($this->getPatternIds()); return $this->namedtag->getListTag(self::TAG_PATTERNS)->count();
} }
protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{ protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{