how can i save PlayerInventory object without holder only whole inventopry because i want to save player inventory and add another items. But when i make $inv = $p->getInventory() i can´t add items into saved inventory without add items to real player´s inventory. and then i can make PHP: $inv = saved inventory;$p->getInventory(setContents($inv->getContents()));
Don't! That will result in memory leaks. Save it item by item. Convert each item between array and Item object (save ID, damage and count in array) and save it using a data serialization language like YAML or JSON.
i know but it is harder get free slots or adding items to the saved inventory and i want to save inventory only for a while so memory will free in few seconds
Saving a reference to an object that should be garbaged. For example, referencing to a player object that should be garbaged after player quit.
If you mean to save it into the harddisk, save it in a YAML or JSON file. PHP: function saveInventory(Inventory $inventory, $file){ $items = []; foreach($inventory->getContents() as $slot => $item){ $items[$slot] = [$item->getId(), $item->getDamage(), $item->getCount()]; } file_put_contents($file, json_encode($items));}function readInventory(Inventory $inv, $file){ $items = json_decode(file_get_contents($file)); $inv->setContents([]); foreach($items as $k => $v) $inv->setItem($k, $v);}
no i want to save it only for few time. I want to make shop and i need to check if saved inventory contains specified amount of specified item and if yes so ass some Item to saved inventory. my not working code: PHP: public function clickItem(Player $p, Item $buyItem, Item $costItem, $inv){ //$inv is saved inventory $buy = false; foreach($inv as $slot => $item){ if($buy === false){ if($item->getId() === $costItem->getId() && $item->getDamage() === $costItem->getDamage() && $item->getCount() >= $costItem->getCount()){ $inv[$slot] = Item::get($costItem->getId(), $costItem->getDamage(), $item->getCount() - $costItem->getCount()); $buy = true; continue; } } if($item->getId() === Item::AIR or $item->getCount() <= 0){ $item = $buyItem; $p->sendMessage($this->plugin->plugin->getPrefix().TextFormat::GREEN."Koupil jsi {$buyItem->getName()})"); } } $p->sendMessage($this->plugin->plugin->getPrefix().TextFormat::RED."Nemas dostatek surovin"); } i think check if player have space in saved inventory doesn´t work
If you're doing BedWars Shop (I think so), I think that you can save player inventory, then he buys something an you load his inventory and add item in it. It's method I use in BedWars