How to keep inventory and use it for later?

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

  1. Nickelberry

    Nickelberry Creeper

    Messages:
    3
    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 Slime

    Messages:
    99
    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

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

    Nickelberry Creeper

    Messages:
    3
    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 Creeper

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

    wolfdale Zombie

    Messages:
    369
    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 Slime

    Messages:
    99
    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
     

Share This Page

  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.