1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Save inventory of a player (items, blocks, NBTs, slots, count, etc...) in an array

Discussion in 'Development' started by moska, May 3, 2017.

  1. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    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) !
     
  2. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    @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.
     
  3. Hipster

    Hipster Zombie

    Messages:
    214
    Did you try this? here
     
  4. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    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
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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()]);
     
    corytortoise likes this.
  6. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    With there NBTs, there slots, etc ?
     
  7. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    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)
     
    Muqsit likes this.
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Yes, with everything.

    The array would look like this:
    PHP:
    $array = [
        
    "supermaxalex" => [
            
    => Item::class,
            
    => Item::class,
            (
    slot) => (item)
        ]
    ];
    If you want to send the player their inventory, you can directly just
    PHP:
    $player->getInventory()->setContents($array[$player->getLowerCaseName()]);
     
  9. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    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..).
     
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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(...)
     
  11. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    Just tested this, and it doesn't work. I will test the other one
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.