Skip to content

Commit

Permalink
v3.0: PHP8, Callbacks, & BedrockEconomy (#1)
Browse files Browse the repository at this point in the history
* v3.0, switch to callbacks, added support for ParoxityEcon

* fix readme

* Formatting

* Cleanup

* Register ParoxityEconProvider

* Update for pm4 & Add BedrockEconomyProvider

* Remove ParoxityEcon and MultiEconomy

deprecated

* Wrong provider class

* Use more php8 features

* Fix BedrockEconomy callbacks

* Add PHPStan

* Update descriptions

* fix phpstan

* fix phpstan pt2

* Fix xp provider conditions

* Fix phpstan pt3

* Fix phpstan pt4

* Fix missing callback

* Fix callback pt2

* remove 4.0.0 branch from poggit

Co-authored-by: JackMD <jackmtaylor.jmt@gmail.com>
Co-authored-by: Aericio <16523741+Aericio@users.noreply.github.com>
Co-authored-by: DaPigGuy <mcpepig123@gmail.com>
  • Loading branch information
4 people authored Feb 21, 2022
1 parent d358c84 commit 06ccf15
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 132 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: PHPStan

on: [push, pull_request]

jobs:
phpstan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
- name: Create Vendor Directory
run: mkdir vendor
- name: Download Plugin Dependencies
run: |
wget -O vendor/BedrockEconomy.phar https://poggit.pmmp.io/r/162549/BedrockEconomy.phar
wget -O vendor/EconomyAPI.phar https://poggit.pmmp.io/r/153507/EconomyAPI.phar
- name: Run PHPStan
uses: paroxity/pmmp-phpstan-action@4.2.0
with:
phpstan-config: phpstan.neon.dist
7 changes: 2 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,5 @@ $RECYCLE.BIN/
*.lnk

# JetBrains IDE
\.idea

# Floobits
\.floo
\.flooignore
.idea
.dependencies
1 change: 0 additions & 1 deletion .poggit.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
--- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/DaPigGuy/libPiggyEconomy
branches:
- master
- pmmp-4.0.0
projects:
libPiggyEconomy:
model: virion
Expand Down
35 changes: 21 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
# libPiggyEconomy

libPiggyEconomy is a virion for easy support of multiple economy providers including EconomyAPI, MultiEconomy, & PMMP's XP levels.
libPiggyEconomy is a virion for easy support of multiple economy providers.

You may be wondering, why the absolute garbage name for a virion? Well, blame Aericio.
## Supported Providers

![](https://cdn.discordapp.com/attachments/305887490613444608/644764172273319936/unknown.png)
- [EconomyAPI](https://poggit.pmmp.io/p/EconomyAPI) by onebone/poggit-orphanage
- [BedrockEconomy](https://poggit.pmmp.io/p/BedrockEconomy) by cooldogedev
- Experience (PMMP)

## Usage

### Setup

```php
libPiggyEconomy::init()
```

### Using Economy Providers

```php
libPiggyEconomy::getProvider($providerInformation)
```
Provider information is an array with the keys ```provider``` and ```multieconomy-currency```. The latter is optional and used only for MultiEconomy.

`$providerInformation` is an array with the key ```provider```.

#### Economy Provider Methods
|Method|Description|
---|---
|```EconomyProvider::getMonetaryUnit(): string```|Returns symbol of currency|
|```EconomyProvider::getMoney(Player $player): float```|Get balance of a player|
|```EconomyProvider::giveMoney(Player $player, int $amount): void```|Give money to a player|
|```EconomyProvider::takeMoney(Player $player, int $amount): void```|Take money from a player|
|```EconomyProvider::setMoney(Player $player, int $amount): void```|Set balance of a player|

| Method | Description | Callback Signature | Callback Description |
|---------------------------------------------------------------------------------------------------|----------------------------|------------------------------------------------------|---------------------------------------------------------------------------|
| ```EconomyProvider::getMonetaryUnit(): string``` | Returns symbol of currency | `none` | `none` |
| ```EconomyProvider::getMoney(Player $player, callable $callback): void``` | Get balance of a player | <code>function(float&#124;int $amount) void{}</code> | Returns default balance if player wasn't found, float&#124;int otherwise. |
| ```EconomyProvider::giveMoney(Player $player, float $amount, ?callable $callback = null): void``` | Give money to a player | `function(bool $success): void{}` | Returns true if money was given successfully, otherwise false. |
| ```EconomyProvider::takeMoney(Player $player, float $amount, ?callable $callback = null): void``` | Take money from a player | `function(bool $success): void{}` | Returns true if money was taken successfully, otherwise false. |
| ```EconomyProvider::setMoney(Player $player, float $amount, ?callable $callback = null): void``` | Set balance of a player | `function(bool $success): void{}` | Returns true if money was set successfully, otherwise false. |

### Error Handling

There are several exceptions that can be thrown that you may want to handle in your plugin:

* MissingProviderDependencyException
* UnknownMultiEconomyCurrencyException
* UnknownProviderException

## Examples

config.yml

```yaml
economy:
provider: multieconomy
multieconomy-currency: pigcoins
provider: economyapi
```
AmazingPlugin.php
Expand Down
13 changes: 13 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
parameters:
paths:
- /source/src
level: 7
bootstrapFiles:
- phar:///pocketmine/PocketMine-MP.phar/vendor/autoload.php
scanDirectories:
- phar:///source/vendor/BedrockEconomy.phar/src/
- phar:///source/vendor/EconomyAPI.phar/src/
excludePaths:
analyse:
- source/vendor
checkMissingIterableValueType: false

This file was deleted.

30 changes: 19 additions & 11 deletions src/DaPigGuy/libPiggyEconomy/libPiggyEconomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,37 @@

namespace DaPigGuy\libPiggyEconomy;

use DaPigGuy\libPiggyEconomy\exceptions\UnknownProviderException;
use DaPigGuy\libPiggyEconomy\exceptions\MissingProviderDependencyException;
use DaPigGuy\libPiggyEconomy\exceptions\UnknownProviderException;
use DaPigGuy\libPiggyEconomy\providers\BedrockEconomyProvider;
use DaPigGuy\libPiggyEconomy\providers\EconomyProvider;
use DaPigGuy\libPiggyEconomy\providers\EconomySProvider;
use DaPigGuy\libPiggyEconomy\providers\MultiEconomyProvider;
use DaPigGuy\libPiggyEconomy\providers\XPProvider;

class libPiggyEconomy
{
/** @var bool */
public static $hasInitiated = false;
public static bool $hasInitiated = false;

/** @var EconomyProvider[] */
public static $economyProviders;
/**
* @var string[] $economyProviders
* @phpstan-var array<class-string<EconomyProvider>>
*/
public static array $economyProviders;

public static function init(): void
{
if (!self::$hasInitiated) {
self::$hasInitiated = true;

self::registerProvider(["economys", "economyapi"], EconomySProvider::class);
self::registerProvider(["multieconomy"], MultiEconomyProvider::class);
self::registerProvider(["bedrockeconomy"], BedrockEconomyProvider::class);
self::registerProvider(["xp", "exp", "experience"], XPProvider::class);
}
}

/**
* @phpstan-param class-string<EconomyProvider> $economyProvider
*/
public static function registerProvider(array $providerNames, string $economyProvider): void
{
foreach ($providerNames as $providerName) {
Expand All @@ -44,10 +49,13 @@ public static function registerProvider(array $providerNames, string $economyPro
*/
public static function getProvider(array $providerInformation): EconomyProvider
{
if (!isset(self::$economyProviders[strtolower($providerInformation["provider"])])) throw new UnknownProviderException("Provider " . $providerInformation["provider"] . " not found.");
if (!isset(self::$economyProviders[strtolower($providerInformation["provider"])])) {
throw new UnknownProviderException("Provider " . $providerInformation["provider"] . " not found.");
}
$provider = self::$economyProviders[strtolower($providerInformation["provider"])];
if (!$provider::checkDependencies()) throw new MissingProviderDependencyException("Dependencies for provider " . $providerInformation["provider"] . " not found.");
$provider = new $provider($providerInformation);
return $provider;
if (!$provider::checkDependencies()) {
throw new MissingProviderDependencyException("Dependencies for provider " . $providerInformation["provider"] . " not found.");
}
return new $provider($providerInformation);
}
}
54 changes: 54 additions & 0 deletions src/DaPigGuy/libPiggyEconomy/providers/BedrockEconomyProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace DaPigGuy\libPiggyEconomy\providers;

use cooldogedev\BedrockEconomy\api\BedrockEconomyAPI;
use cooldogedev\BedrockEconomy\BedrockEconomy;
use cooldogedev\BedrockEconomy\currency\CurrencyManager;
use cooldogedev\BedrockEconomy\libs\cooldogedev\libSQL\context\ClosureContext;
use pocketmine\player\Player;
use pocketmine\Server;

class BedrockEconomyProvider extends EconomyProvider
{
private BedrockEconomyAPI $api;
private CurrencyManager $currency;

public static function checkDependencies(): bool
{
return Server::getInstance()->getPluginManager()->getPlugin("BedrockEconomy") !== null;
}

public function __construct()
{
$this->api = BedrockEconomy::getInstance()->getAPI();
$this->currency = BedrockEconomy::getInstance()->getCurrencyManager();
}

public function getMonetaryUnit(): string
{
return $this->currency->getSymbol();
}

public function getMoney(Player $player, callable $callback): void
{
$this->api->getPlayerBalance($player->getName(), ClosureContext::create(fn(?int $balance) => $callback($balance ?? $this->currency->getDefaultBalance())));
}

public function giveMoney(Player $player, float $amount, ?callable $callback = null): void
{
$this->api->addToPlayerBalance($player->getName(), (int)$amount, $callback ? ClosureContext::create($callback) : null);
}

public function takeMoney(Player $player, float $amount, ?callable $callback = null): void
{
$this->api->subtractFromPlayerBalance($player->getName(), (int)$amount, $callback ? ClosureContext::create($callback) : null);
}

public function setMoney(Player $player, float $amount, ?callable $callback = null): void
{
$this->api->setPlayerBalance($player->getName(), (int)$amount, $callback ? ClosureContext::create($callback) : null);
}
}
31 changes: 23 additions & 8 deletions src/DaPigGuy/libPiggyEconomy/providers/EconomyProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace DaPigGuy\libPiggyEconomy\providers;

use pocketmine\player\Player;
Expand All @@ -17,11 +16,27 @@ public function getMonetaryUnit(): string
return "$";
}

abstract function getMoney(Player $player): float;

abstract function giveMoney(Player $player, float $amount): void;

abstract function takeMoney(Player $player, float $amount): void;

abstract function setMoney(Player $player, float $amount): void;
/**
* $callback -> function(float|int $amount): void{}
* $amount is the default balance if player wasn't found, float/int otherwise.
*/
abstract function getMoney(Player $player, callable $callback): void;

/**
* $callback -> function(bool $success): void{}
* $success is true if money was given successfully, otherwise false.
*/
abstract function giveMoney(Player $player, float $amount, ?callable $callback = null): void;

/**
* $callback -> function(bool $success): void{}
* $success is true if money was taken successfully, otherwise false.
*/
abstract function takeMoney(Player $player, float $amount, ?callable $callback = null): void;

/**
* $callback -> function(bool $success): void{}
* $success is true if money was set successfully, otherwise false.
*/
abstract function setMoney(Player $player, float $amount, ?callable $callback = null): void;
}
22 changes: 12 additions & 10 deletions src/DaPigGuy/libPiggyEconomy/providers/EconomySProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

class EconomySProvider extends EconomyProvider
{
/** @var EconomyAPI */
private $economyAPI;
private EconomyAPI $economyAPI;

public static function checkDependencies(): bool
{
Expand All @@ -28,23 +27,26 @@ public function getMonetaryUnit(): string
return $this->economyAPI->getMonetaryUnit();
}

public function getMoney(Player $player): float
public function getMoney(Player $player, callable $callback): void
{
return $this->economyAPI->myMoney($player);
$callback($this->economyAPI->myMoney($player) ?: 0);
}

public function giveMoney(Player $player, float $amount): void
public function giveMoney(Player $player, float $amount, ?callable $callback = null): void
{
$this->economyAPI->addMoney($player, $amount);
$ret = $this->economyAPI->addMoney($player, $amount);
if ($callback) $callback($ret === EconomyAPI::RET_SUCCESS);
}

public function takeMoney(Player $player, float $amount): void
public function takeMoney(Player $player, float $amount, ?callable $callback = null): void
{
$this->economyAPI->reduceMoney($player, $amount);
$ret = $this->economyAPI->reduceMoney($player, $amount);
if ($callback) $callback($ret === EconomyAPI::RET_SUCCESS);
}

public function setMoney(Player $player, float $amount): void
public function setMoney(Player $player, float $amount, ?callable $callback = null): void
{
$this->economyAPI->setMoney($player, $amount);
$ret = $this->economyAPI->setMoney($player, $amount);
if ($callback) $callback($ret === EconomyAPI::RET_SUCCESS);
}
}
Loading

0 comments on commit 06ccf15

Please sign in to comment.