Skip to content

The basics: Hooking a custom economy provider

Andre | xZeroMCPE edited this page Sep 28, 2020 · 3 revisions

Now that you know how to grab an instance of PocketVault, you can start creating your own economy provider with the support of PocketVault. Let's say I am making an economy plugin, called ZeroCoins, my currency symbol will be Z, my currency won't save any Z's on shutdown, all your Z's are temporary (for the sake of my time...., you can do that on your own, maybe saving it on a plain YML, JSON, or more advance, MySQL, etc). And everyone has an initial Z's of 300 bandz.

$vault = /*Assuming this is your vault instance*/

try {
   $vault->getProviderManager()->addProvider(new ZeroEconomy());
  } catch (\Exception $e) {
   var_dump("There was an error while trying to register ZeroCoins economy");
}

The class ZeroEconomy must extend the EconomyProvider, and includes all the methods inside of the EconomyProvider class, you can add any other methods you may like after that, your choice.

Here's how my ZeroEconomy class looks like:

namespace Zero\ZeroEconomy;


use pocketmine\Player;
use xZeroMCPE\PocketVault\Provider\Types\Economy\EconomyProvider;

class ZeroEconomy extends EconomyProvider
{

    public $monies = [];

    public function getBalance(Player $player): int
    {
        if(!isset($this->monies[$player->getName()])) {
            $this->setMoney($player, 300000);
        }
        return $this->monies[$player->getName()];
    }

    public function addMoney(Player $player, int $money): void
    {
        $this->monies[$player->getName()] = $this->getBalance($player) + $money;
    }

    public function takeMoney(Player $player, int $money): void
    {
        $this->monies[$player->getName()] = $this->getBalance($player) - $money;
    }

    public function setMoney(Player $player, int $money): void
    {
        $this->monies[$player->getName()] = $money;
    }

    public function topMoney(int $sort = 0, $limit = 25): array
    {
        // TODO: Implement topMoney() method.

        /*
         * -_-
         * really, you probably thought I was gonna do this....
         * well, you get the point.
         */
        return [];
    }
    
    public function getCurrencySymbol(): string
    {
        return "Z";
    }
    
    public function getCurrencyNamePlural(): string
    {
        return "Zeros";
    }
    
    public function getCurrencyNameSingular(): string
    {
        return "Zero";
    }
}

There you go, and remember, you have to somehow handle saving/loading, etc, just be sure you add your EconomyProvider each time to PocketVault, Furthermore, if you have an idea, be sure to open an issue, or even if you're experiencing any issues. But nonetheless, if you make anything utilizing PocketVault, let me know, I'll be sure include it in the readme, -- or related.

Clone this wiki locally