I need to get all item IDs in inventory and delete them Like this: $inv = $ player-> getInventory (); $ Player-> getInventory () -> RemoveItem ($ inv);
Ugh like this? PHP: $player = $this->getServer()->getOnlinePlayer();$player->getInventory()->removeItem(Item::get(youritemid, itemdata, count));
You will need to serialise the inventory contents if you want to save them to a file. PHP: $inventoryAsText = json_encode($player->getInventory()->getContents()); to put the items back again, you will need to deserialise it again: PHP: $inventoryArray = json_decode($inventoryAsText, true);foreach ($inventoryArray as $slot => $itemJson) { $item = Item::jsonDeserialize($itemJson); $inventoryArray[$slot] = $item;}$player->getInventory()->setContents($inventoryArray);
[QUOTE = "HimbeersaftLP, сообщение: 71331, участник: 50"] Вам нужно будет сериализовать содержимое инвентаря, если вы хотите сохранить его в файл. PHP: $ inventoryAsText = json_encode ($ player-> getInventory () -> getContents ());[/ PHP]чтобы вернуть предметы обратно, вам нужно будет снова десериализовать их:[PHP]$ inventoryArray = json_decode ($ inventoryAsText, true);foreach ($ inventoryArray как $ slot => $ itemJson) { $ item = Item :: jsonDeserialize ($ itemJson); $ inventoryArray [$ slot] = $ item;}$ Player-> getInventory () -> setContents ($ inventoryArray);[/ PHP] [/ QUOTE]Thanks , i will try this
It can not save $inventsur in config.json , file empty My code PHP: if($hot == 265){ if($player->hasPermission("smarTap.don.use")){ if($hot == 265 && $x == 0 && $y == 0 && $z == 0 && $bl == 0){ $player->sendTip("§9Режимы§7:§4 Ошибка"); return true; } if($player->getGamemode() == 0){ $inventsur = json_encode($player->getInventory()->getContents()); $player->getInventory()->clearAll(); $player->setGamemode(1); $player->sendTip("§9Режимы§7: Вы включили креатив"); return true; } if(!($player->getGamemode() == 0)){ $invent = json_encode($player->getInventory()->getContents()); $player->setGamemode(0); $inventoryArray = json_decode($inventsur, true); foreach ($inventoryArray as $slot => $itemJson) { $item = Item::jsonDeserialize($itemJson); $inventoryArray[$slot] = $item; } $player->getInventory()->setContents($inventoryArray); $player->sendTip("§9Режимы§7: Вы отключили креатив"); } }
You aren't saving it anywhere, of course the config is empty. I'm surprised that code even ran without crashing the server.
Try that: PHP: if($hot == 265){ if($player->hasPermission("smarTap.don.use")){ if($hot == 265 && $x == 0 && $y == 0 && $z == 0 && $bl == 0){ $player->sendTip("§9Режимы§7:§4 Ошибка"); return true; } $inventories = $this->getConfig()->get("inventories", []); if($player->getGamemode() === Player::SURVIVAL){ $inventsur = json_encode($player->getInventory()->getContents()); $inventories[$player->getLowerCaseName()] = $inventsur; $this->getConfig()->set("inventories", $inventories); $this->getConfig()->save(); $player->getInventory()->clearAll(); $player->setGamemode(1); $player->sendTip("§9Режимы§7: Вы включили креатив"); return true; } else { $player->setGamemode(0); if (isset($inventories[$player->getLowerCaseName()])) { $inventoryArray = json_decode($inventories[$player->getLowerCaseName()], true); foreach ($inventoryArray as $slot => $itemJson) { $item = Item::jsonDeserialize($itemJson); $inventoryArray[$slot] = $item; } $player->getInventory()->setContents($inventoryArray); unset($inventories[$player->getLowerCaseName()]); $this->getConfig()->set("inventories", $inventories); $this->getConfig()->save(); } $player->sendTip("§9Режимы§7: Вы отключили креатив"); } } Note that I haven't tested this and I'm not responsible for the loss over player inventories if my code doesn't work.