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()];```
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
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.
PHP: //save inventory /test on$this->inv[$sender->getName] = $sender->getInventory()->getContents();$sender->getInventory()->clearAll();//give items//restore inventory /test offif(isset($this->inv[$sender->getName])){$sender->getInventory->setContents($this->inv[$sender->getName]);unset($this->inv[$sender->getName]);}
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
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
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 offif(isset($this->inv[$sender->getName()])){$sender->getInventory->setContents($this->inv[$sender->getName()]);unset($this->inv[$sender->getName()]);}