Remove circular dependency between Item and NBT modules (#121)

This commit is contained in:
Dylan K. Taylor
2016-11-25 10:17:50 +00:00
committed by GitHub
parent a0111d04ee
commit e51a2725de
7 changed files with 68 additions and 64 deletions

View File

@ -24,7 +24,6 @@
*/
namespace pocketmine\nbt;
use pocketmine\item\Item;
use pocketmine\nbt\tag\ByteArrayTag;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag;
@ -73,57 +72,6 @@ class NBT{
public $endianness;
private $data;
/**
* @param Item $item
* @param int $slot
* @return CompoundTag
*/
public static function putItemHelper(Item $item, $slot = null){
$tag = new CompoundTag(null, [
"id" => new ShortTag("id", $item->getId()),
"Count" => new ByteTag("Count", $item->getCount()),
"Damage" => new ShortTag("Damage", $item->getDamage())
]);
if($slot !== null){
$tag->Slot = new ByteTag("Slot", (int) $slot);
}
if($item->hasCompoundTag()){
$tag->tag = clone $item->getNamedTag();
$tag->tag->setName("tag");
}
return $tag;
}
/**
* @param CompoundTag $tag
* @return Item
*/
public static function getItemHelper(CompoundTag $tag){
if(!isset($tag->id) or !isset($tag->Count)){
return Item::get(0);
}
if($tag->id instanceof ShortTag){
$item = Item::get($tag->id->getValue(), !isset($tag->Damage) ? 0 : $tag->Damage->getValue(), $tag->Count->getValue());
}elseif($tag->id instanceof StringTag){ //PC item save format
$item = Item::fromString($tag->id->getValue());
$item->setDamage(!isset($tag->Damage) ? 0 : $tag->Damage->getValue());
$item->setCount($tag->Count->getValue());
}else{
throw new \InvalidArgumentException("Item CompoundTag ID must be an instance of StringTag or ShortTag, " . get_class($tag->id) . " given");
}
if(isset($tag->tag) and $tag->tag instanceof CompoundTag){
$item->setNamedTag($tag->tag);
}
return $item;
}
public static function matchList(ListTag $tag1, ListTag $tag2){
if($tag1->getName() !== $tag2->getName() or $tag1->getCount() !== $tag2->getCount()){
return false;