This document explains how to use ShardAPI inside other PocketMine-MP plugins. The API provides functions to retrieve, add, set, and manipulate shard balances for players.
The API uses SingletonTrait, so you can access it from anywhere:
use Rezvy\ShardAPI\ShardAPI;
/** @var ShardAPI $api */
$api = ShardAPI::getInstance();You can retrieve a player's shard balance using the getShards method:
/** @var Player $player */
$amount = $api->getShards($player);Use setShards to directly set a player's shard amount:
/** @var Player $player */
$api->setShards($player, 100);Use addShards to increase the player's shard balance:
/** @var Player $player */
$api->addShards($player, 25);Use reduceShards to remove shards from a player.
The method returns true if deduction succeeds, or false if the player lacks enough shards.
/** @var Player $player */
$success = $api->reduceShards($player, 10);
if ($success) {
// deduction successful
}Use hasShards to check whether a player meets a required amount:
/** @var Player $player */
if ($api->hasShards($player, 50)) {
// player can afford something
}getAllShards returns an associative array of all saved shard values:
$data = $api->getAllShards();{
"rezvy": 120,
"alex": 75,
"steve": 30
}Use getTopPlayers to retrieve players with the highest shard balance:
$top = $api->getTopPlayers(5);{
"rezvy": 120,
"alex": 75,
"steve": 30,
"david": 20,
"lucas": 10
}use Rezvy\ShardAPI\ShardAPI;
use pocketmine\player\Player;
function buyItem(Player $player, int $cost): void {
$api = ShardAPI::getInstance();
if (!$api->hasShards($player, $cost)) {
$player->sendMessage("§cYou don't have enough shards!");
return;
}
$api->reduceShards($player, $cost);
$player->sendMessage("§aPurchase successful!");
}