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

How to keep inventory and use it for later?

Discussion in 'Development' started by Nickelberry, Oct 12, 2019.

  1. Nickelberry

    Nickelberry Spider

    Messages:
    6
    Hello,
    Im currently in the middle of making a plugin, and I can't seem to find out how to store someone's inventory and use it for later.
    One command is supposed to clear your inventory and give you specific items from the command, and the other command would restore your inventory back to normal.

    I've tried doing:
    $array = [];
    $inventory = new \pocketmine\inventory\PlayerInventory($sender);
    $inventory->setContents($sender->getInventory->getContents());
    $array[$sender->getLowerCaseName()] = $inventory;
    $inv = array[$sender->getLowerCaseName()];```
     
  2. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    There's no need of creating a new inventory, you should create a config for each server player to then save an array into it, like this

    PHP:
    // Creating the inventory array, note this isn't 100% accurate because it's not saving many other values like enchantments, lores or custom names.
    $inventory = [];
    foreach(
    $player->getInventory()->getContents() as $content) {
        
    $inventory[] = [$content->getId(), $content->getDamage(), $content->getCount()]
    }
    PHP:
    // Saving the inventory to player's config
    $config->set("inventory"$inventory);
    $config->save();
    PHP:
    // Setting the inventory to the player
    $inventory $config->get("inventory");
    $contents = [];
    foreach(
    $inventory as $item) {
        
    $contents[] = Item::get($item[0], $item[1], $item[2]);
    }
    $player->getInventory()->setContents($contents);
    I don't know if it would work like this because I haven't tested it, however I hope this can help you
     
  3. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    By later do you mean over server restarts or only temporarily on runtime and lost on restart?
     
    jasonwynn10 likes this.
  4. Nickelberry

    Nickelberry Spider

    Messages:
    6
    This works, however, it is storing the inventory the players has at that exact moment.

    How am I supposed to get the senders inventory before they used the /test on command instead of getting the inventory they have after using the /test on command.

    For example, with doing “/test on” the sender will receive items in there inventory hotbar.
    With doing /test off the player will get back their original inventory, and the items will disappear.
     
  5. Nickelberry

    Nickelberry Spider

    Messages:
    6
    By later I mean over a course of a few minutes.
     
  6. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    PHP:
    //save inventory /test on
    $this->inv[$sender->getName] = $sender->getInventory()->getContents();
    $sender->getInventory()->clearAll();
    //give items

    //restore inventory /test off
    if(isset($this->inv[$sender->getName])){
    $sender->getInventory->setContents($this->inv[$sender->getName]);
    unset(
    $this->inv[$sender->getName]);
    }
     
    Fadhel likes this.
  7. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    1. Save the inventory at first when executing /test on, clear the inventory and give new items.
    2. Clear the inventory at the end when executing /test off, then set the player's inventory to the one you saved.

    Also I just realized there's no need of creating a config, it'd be much better to create a global array or an array for each player instead of storing it into a config. Also to avoid losing items when leaving the server without having executed /test off, just set again the player's inventory to the one you saved
     
  8. Phqzing

    Phqzing Spider

    Messages:
    8
    GitHub:
    phqzing
    Does this have typos? Like for example the "[$sender->getName]" isnt it supposed to be "[$sender->getName()]" Im new to ccoding and sorry if Im wrong
     
  9. Axon

    Axon Zombie

    Messages:
    276
    Yeah, you are correct.
    some small typos
    Here's the untypo code.
    Enjoy!
    PHP:
    //save inventory /test on
    $this->inv[$sender->getName()] = $sender->getInventory()->getContents();
    $sender->getInventory()->clearAll();
    //give items

    //restore inventory /test off
    if(isset($this->inv[$sender->getName()])){
    $sender->getInventory->setContents($this->inv[$sender->getName()]);
    unset(
    $this->inv[$sender->getName()]);
    }
     
    Last edited: May 6, 2021
    Agent likes this.
  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.