diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
new file mode 100644
index 0000000..a2380a6
--- /dev/null
+++ b/.github/workflows/main.yml
@@ -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
diff --git a/.gitignore b/.gitignore
index 2134002..cf3c7ab 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,8 +18,5 @@ $RECYCLE.BIN/
*.lnk
# JetBrains IDE
-\.idea
-
-# Floobits
-\.floo
-\.flooignore
\ No newline at end of file
+.idea
+.dependencies
\ No newline at end of file
diff --git a/.poggit.yml b/.poggit.yml
index 6f0d742..e2d4c4f 100644
--- a/.poggit.yml
+++ b/.poggit.yml
@@ -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
diff --git a/README.md b/README.md
index 3d7681d..8981c48 100644
--- a/README.md
+++ b/README.md
@@ -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 | function(float|int $amount) void{}
| Returns default balance if player wasn't found, float|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
diff --git a/phpstan.neon.dist b/phpstan.neon.dist
new file mode 100644
index 0000000..75b6311
--- /dev/null
+++ b/phpstan.neon.dist
@@ -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
\ No newline at end of file
diff --git a/src/DaPigGuy/libPiggyEconomy/exceptions/UnknownMultiEconomyCurrencyException.php b/src/DaPigGuy/libPiggyEconomy/exceptions/UnknownMultiEconomyCurrencyException.php
deleted file mode 100644
index 303986d..0000000
--- a/src/DaPigGuy/libPiggyEconomy/exceptions/UnknownMultiEconomyCurrencyException.php
+++ /dev/null
@@ -1,10 +0,0 @@
->
+ */
+ public static array $economyProviders;
public static function init(): void
{
@@ -25,11 +27,14 @@ public static function init(): void
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
+ */
public static function registerProvider(array $providerNames, string $economyProvider): void
{
foreach ($providerNames as $providerName) {
@@ -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);
}
}
\ No newline at end of file
diff --git a/src/DaPigGuy/libPiggyEconomy/providers/BedrockEconomyProvider.php b/src/DaPigGuy/libPiggyEconomy/providers/BedrockEconomyProvider.php
new file mode 100644
index 0000000..648f02a
--- /dev/null
+++ b/src/DaPigGuy/libPiggyEconomy/providers/BedrockEconomyProvider.php
@@ -0,0 +1,54 @@
+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);
+ }
+}
\ No newline at end of file
diff --git a/src/DaPigGuy/libPiggyEconomy/providers/EconomyProvider.php b/src/DaPigGuy/libPiggyEconomy/providers/EconomyProvider.php
index b4274a7..2a17d35 100644
--- a/src/DaPigGuy/libPiggyEconomy/providers/EconomyProvider.php
+++ b/src/DaPigGuy/libPiggyEconomy/providers/EconomyProvider.php
@@ -1,6 +1,5 @@
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;
}
\ No newline at end of file
diff --git a/src/DaPigGuy/libPiggyEconomy/providers/EconomySProvider.php b/src/DaPigGuy/libPiggyEconomy/providers/EconomySProvider.php
index 1b1ad31..6153249 100644
--- a/src/DaPigGuy/libPiggyEconomy/providers/EconomySProvider.php
+++ b/src/DaPigGuy/libPiggyEconomy/providers/EconomySProvider.php
@@ -10,8 +10,7 @@
class EconomySProvider extends EconomyProvider
{
- /** @var EconomyAPI */
- private $economyAPI;
+ private EconomyAPI $economyAPI;
public static function checkDependencies(): bool
{
@@ -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);
}
}
\ No newline at end of file
diff --git a/src/DaPigGuy/libPiggyEconomy/providers/MultiEconomyProvider.php b/src/DaPigGuy/libPiggyEconomy/providers/MultiEconomyProvider.php
deleted file mode 100644
index acdbf60..0000000
--- a/src/DaPigGuy/libPiggyEconomy/providers/MultiEconomyProvider.php
+++ /dev/null
@@ -1,62 +0,0 @@
-getPluginManager()->getPlugin("MultiEconomy") !== null;
- }
-
- /**
- * @throws UnknownMultiEconomyCurrencyException
- */
- public function __construct(array $providerInformation)
- {
- /** @var MultiEconomy $multiEconomy */
- $this->multiEconomy = Server::getInstance()->getPluginManager()->getPlugin("MultiEconomy");
- $this->currency = $this->multiEconomy->getCurrencies()[strtolower($providerInformation["multieconomy-currency"])] ?? null;
- if ($this->currency === null) {
- throw new UnknownMultiEconomyCurrencyException("MultiEconomy currency " . $providerInformation["multieconomy-currency"] . " not found.");
- }
- }
-
- public function getMonetaryUnit(): string
- {
- return $this->currency->getSymbol();
- }
-
- public function getMoney(Player $player): float
- {
- return $this->currency->getBalance($player->getName()) ?? $this->currency->getStartingAmount();
- }
-
- public function giveMoney(Player $player, float $amount): void
- {
- $this->currency->addToBalance($player->getName(), $amount);
- }
-
- public function takeMoney(Player $player, float $amount): void
- {
- $this->currency->removeFromBalance($player->getName(), $amount);
- }
-
- public function setMoney(Player $player, float $amount): void
- {
- $this->currency->setBalance($player->getName(), $amount);
- }
-}
\ No newline at end of file
diff --git a/src/DaPigGuy/libPiggyEconomy/providers/XPProvider.php b/src/DaPigGuy/libPiggyEconomy/providers/XPProvider.php
index d58de8a..66db02f 100644
--- a/src/DaPigGuy/libPiggyEconomy/providers/XPProvider.php
+++ b/src/DaPigGuy/libPiggyEconomy/providers/XPProvider.php
@@ -14,27 +14,27 @@ public function getMonetaryUnit(): string
return "Levels";
}
- public function getMoney(Player $player): float
+ public function getMoney(Player $player, callable $callback): void
{
- return $player->getXpManager()->getXpLevel() + $player->getXpManager()->getXpProgress();
+ $callback($player->getXpManager()->getXpLevel() + $player->getXpManager()->getXpProgress());
}
- public function giveMoney(Player $player, float $amount): void
+ public function giveMoney(Player $player, float $amount, ?callable $callback = null): void
{
$levels = (int)floor($amount);
- $player->getXpManager()->addXpLevels($levels);
- $player->getXpManager()->addXp((int)(ExperienceUtils::getXpToCompleteLevel($player->getXpManager()->getXpLevel()) * ($amount - $levels)));
+ $ret = $player->getXpManager()->addXpLevels($levels) && $player->getXpManager()->addXp((int)(ExperienceUtils::getXpToCompleteLevel($player->getXpManager()->getXpLevel()) * ($amount - $levels)));
+ if ($callback) $callback($ret);
}
- public function takeMoney(Player $player, float $amount): void
+ public function takeMoney(Player $player, float $amount, ?callable $callback = null): void
{
- $this->giveMoney($player, -$amount);
+ $this->giveMoney($player, -$amount, $callback);
}
- public function setMoney(Player $player, float $amount): void
+ public function setMoney(Player $player, float $amount, ?callable $callback = null): void
{
$levels = (int)floor($amount);
- $player->getXpManager()->setXpLevel($levels);
- $player->getXpManager()->setXpProgress($amount - $levels);
+ $ret = $player->getXpManager()->setXpLevel($levels) && $player->getXpManager()->setXpProgress($amount - $levels);
+ if ($callback) $callback($ret);
}
}
\ No newline at end of file
diff --git a/virion.yml b/virion.yml
index 9ade286..e42346d 100644
--- a/virion.yml
+++ b/virion.yml
@@ -1,5 +1,5 @@
name: libPiggyEconomy
-version: 2.0.0
+version: 3.0.0
antigen: DaPigGuy\libPiggyEconomy
api: 4.0.0
author: DaPigGuy
\ No newline at end of file