Hey everyone, So, I'm doing a shop on the inventory of a player directly, but I wanted to know, before his inventory is cleared and items' shop are set, I want to save his inventory in an array with NBTs, slots, count of all items he had before the clear. PS: After this, I need to use some getItem(), addItem(), clear() etc on the array who content the inventory of the player. Sorry for my bad English, I'm in a hurry Thanks for reading (and for the help if somebody do it lol) !
@Muqsit already did it on his plugin PrivateVaults, I know, but it doesn't save the slot of armors / items and it's impossible to do a foreach of all items on the config because the console return me "CompoundTag" and not an array.
That's actually what I said. It tried to use this but the server returns me a CompoundTag and not an array, so impossible to use a foreach and so impossible to use all functions what I need
You can save their Inventory::getContents() PHP: /** @var Player $player */$array = [];$array[$player->getLowerCaseName()] = $player->getInventory()->getContents();$player->getInventory()->clearAll();$player->getInventory()->setContents($array[$player->getLowerCaseName()]);
I jus tested your code (again) I did this : PHP: foreach($array[$player->getLowerCaseName()] as $inventory){$iCost = /*An item*/;$dCost = /*His damage*/;$aoI = $inventory->all(Item::get($iCost, $dCost));//etc..} At a moment, I just used @aoI and I have got this error. :/ Error: "Call to undefined method pocketmine\item\ItemBlock::all()" (EXCEPTION)
Yes, with everything. The array would look like this: PHP: $array = [ "supermaxalex" => [ 0 => Item::class, 1 => Item::class, (slot) => (item) ]]; If you want to send the player their inventory, you can directly just PHP: $player->getInventory()->setContents($array[$player->getLowerCaseName()]);
Oh okay ! There is nothing to do to have the possibility to use all(); getItem(); setItem(); addItem(); functions ? It's very necessary to access to this array to remove an item and if you buy something (for example, on BedWars when you are buying something with bricks, irons, golds, etc..).
PHP: $inv = $array[$player->getLowerCaseName()];//setItem($slot, $item)$inv[$slot] = $item//addItem($item)if(count($inv) < 27){ $inv[] = $item}else{ //inventory is full}//getItem($slot)$item = $inv[$slot];//all($item)$slots = [];$checkDamage = !$item->hasAnyDamageValue();$checkTags = $item->hasCompoundTag();foreach($inv as $index => $i){ if($item->equals($i, $checkDamage, $checkTags)){ $slots[$index] = $i; }}return $slots; I know its kinda confusing, but there isn't any better option. You can try this or else, I am not sure if it'll work: PHP: /** @var Player $player */$array = [];$inventory = new \pocketmine\inventory\PlayerInventory($player);$inventory->setContents($player->getInventory()->getContents());$array[$player->getLowerCaseName()] = $inventory;$inv = $array[$player->getLowerCaseName()];//$inv->setItem(...)//$inv->all(...)