pocketmine\entity\Human::saveNBT PHP Method

saveNBT() public method

public saveNBT ( )
    public function saveNBT()
    {
        parent::saveNBT();
        $this->namedtag->Inventory = new ListTag("Inventory", []);
        $this->namedtag->Inventory->setTagType(NBT::TAG_Compound);
        if ($this->inventory !== null) {
            //Hotbar
            for ($slot = 0; $slot < $this->inventory->getHotbarSize(); ++$slot) {
                $inventorySlotIndex = $this->inventory->getHotbarSlotIndex($slot);
                $item = $this->inventory->getItem($inventorySlotIndex);
                $tag = NBT::putItemHelper($item, $slot);
                $tag->TrueSlot = new ByteTag("TrueSlot", $inventorySlotIndex);
                $this->namedtag->Inventory[$slot] = $tag;
            }
            //Normal inventory
            $slotCount = $this->inventory->getSize() + $this->inventory->getHotbarSize();
            for ($slot = $this->inventory->getHotbarSize(); $slot < $slotCount; ++$slot) {
                $item = $this->inventory->getItem($slot - $this->inventory->getHotbarSize());
                //As NBT, real inventory slots are slots 9-44, NOT 0-35
                $this->namedtag->Inventory[$slot] = NBT::putItemHelper($item, $slot);
            }
            //Armour
            for ($slot = 100; $slot < 104; ++$slot) {
                $item = $this->inventory->getItem($this->inventory->getSize() + $slot - 100);
                if ($item instanceof ItemItem and $item->getId() !== ItemItem::AIR) {
                    $this->namedtag->Inventory[$slot] = NBT::putItemHelper($item, $slot);
                }
            }
        }
        if (strlen($this->getSkinData()) > 0) {
            $this->namedtag->Skin = new CompoundTag("Skin", ["Data" => new StringTag("Data", $this->getSkinData()), "Name" => new StringTag("Name", $this->getSkinId())]);
        }
        //Xp
        $this->namedtag->XpLevel = new IntTag("XpLevel", $this->getXpLevel());
        $this->namedtag->XpTotal = new IntTag("XpTotal", $this->getTotalXp());
        $this->namedtag->XpP = new FloatTag("XpP", $this->getXpProgress());
        $this->namedtag->XpSeed = new IntTag("XpSeed", $this->getXpSeed());
        //Food
        $this->namedtag->foodLevel = new IntTag("foodLevel", $this->getFood());
        $this->namedtag->foodExhaustionLevel = new FloatTag("foodExhaustionLevel", $this->getExhaustion());
        $this->namedtag->foodSaturationLevel = new FloatTag("foodSaturationLevel", $this->getSaturation());
        $this->namedtag->foodTickTimer = new IntTag("foodTickTimer", $this->foodTickTimer);
    }

Usage Example

示例#1
0
 /**
  * Handles player data saving
  */
 public function save($async = false)
 {
     if ($this->closed) {
         throw new \InvalidStateException("Tried to save closed player");
     }
     parent::saveNBT();
     if ($this->level instanceof Level) {
         $this->namedtag->Level = new String("Level", $this->level->getName());
         if ($this->spawnPosition instanceof Position and $this->spawnPosition->getLevel() instanceof Level) {
             $this->namedtag["SpawnLevel"] = $this->spawnPosition->getLevel()->getName();
             $this->namedtag["SpawnX"] = (int) $this->spawnPosition->x;
             $this->namedtag["SpawnY"] = (int) $this->spawnPosition->y;
             $this->namedtag["SpawnZ"] = (int) $this->spawnPosition->z;
         }
         foreach ($this->achievements as $achievement => $status) {
             $this->namedtag->Achievements[$achievement] = new Byte($achievement, $status === true ? 1 : 0);
         }
         $this->namedtag["playerGameType"] = $this->gamemode;
         $this->namedtag["lastPlayed"] = new Long("lastPlayed", floor(microtime(true) * 1000));
         if ($this->username != "" and $this->namedtag instanceof Compound) {
             $this->server->saveOfflinePlayerData($this->username, $this->namedtag, $async);
         }
     }
 }