SkillAgentSearch skills...

InventoryManager

A PocketMine-MP plugin to easily save inventories in a database.

Install / Use

/learn @Nathan459770/InventoryManager
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<h1>InventoryManager<img src="chest.png" height="64" width="64" align="left"></img>&nbsp;</h1> <br />

A PocketMine-MP plugin to easily save inventories in a database.

Usage

  • 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 the InventoryManager object.

Be careful !

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();
    }

Basic Usage

Import the classes

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;

Access to managers

DO NOT use new ! <br/> YOU MUST USE getInstance() function as shown below.

$db = DatabaseManager::getInstance();
$api = InventoryManager::getInstance();

Functions

There are 7 main functions which are all in the InventoryManager class <br/>

  • 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;
        }
    }
View on GitHub
GitHub Stars4
CategoryData
Updated1y ago
Forks0

Languages

PHP

Security Score

70/100

Audited on Apr 28, 2024

No findings