mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 02:42:58 +00:00
Remove circular dependency between Item and NBT modules (#121)
This commit is contained in:
@ -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;
|
||||
|
Reference in New Issue
Block a user