A PocketMine-MP plugin to easily save inventories in a database.
- Download this plugin
- You can put it on your server or put the api in the src of your plugin
- To use it you'll have to import
DatabaseManager
object and theInventoryManager
object.
If you have put the api in your folder, you need to run these lines of code in the onEnable function of your main file
$db = DatabaseManager::getInstance();
if(!$db->isEnabled()){
$db->init();
}
You'll need to import these classes in order to easily use it within our code.
<?php
use Nathan45\Inventories\DatabaseManager;
use Nathan45\Inventories\InventoryManager;
DO NOT use new
!
YOU MUST USE getInstance()
function as shown below.
$db = DatabaseManager::getInstance();
$api = InventoryManager::getInstance();
There are 7 main functions which are all in the InventoryManager class
- getAllData() > it returns an array with all existing player inventories, it follows this pattern:
$array[$player][$id] = [item1, item2, etc].
- setAllData(array $inventories) > It replaces all existing player inventories by the given array.
- getInventoriesFor(Player|string $player) > This function returns all inventories of the given player in the order.
- getInventoryFor(Player|string $player, string|int $inventoryId) > This function returns the player's inventory corresponding to the given id.
- setInventoriesFor(Player|string $player, array $inventories) > This function sets all ths inventories of the given player in the order.
- setInventoryFor(Player|string $player, string|int $id, array $inventories) > This function sets the player's inventory corresponding to the given id.
- updateDataFor(string $player) > This function stores the player's inventories in the database.
##Here is a basic example with an event
public function onUseItem(PlayerItemUseEvent $event): void{
$api = InventoryManager::getInstance();
$player = $event->getPlayer();
switch ($event->getItem()->getId()){
case ItemIds::GOLD_INGOT:
$api->setInventoryFor($player, "gold_inventory", $player->getInventory()->getContents()); // You can also put an int as id.
break;
case ItemIds::GOLD_BLOCK:
$player->getInventory()->setContents($api->getInventoryFor($player, "gold_inventory"));
break;
}
}