Skip to content

Commit

Permalink
Update BedrockEconomyProvider for BedrockEconomy v4 (#13)
Browse files Browse the repository at this point in the history

Co-authored-by: Aericio <16523741+Aericio@users.noreply.github.com>
  • Loading branch information
Joestarfish and Aericio authored Mar 8, 2024
1 parent 3c4e02b commit 9a22d3a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
run: mkdir vendor
- name: Download Plugin Dependencies
run: |
wget -O vendor/BedrockEconomy.phar https://poggit.pmmp.io/r/211584/BedrockEconomy.phar
wget -O vendor/BedrockEconomy.phar https://poggit.pmmp.io/r/230746/BedrockEconomy.phar
wget -O vendor/EconomyAPI.phar https://poggit.pmmp.io/r/153507/EconomyAPI.phar
- name: Run PHPStan
uses: paroxity/pmmp-phpstan-action@5.1.2
uses: paroxity/pmmp-phpstan-action@5.12.0
with:
phpstan-config: phpstan.neon.dist
52 changes: 39 additions & 13 deletions src/DaPigGuy/libPiggyEconomy/providers/BedrockEconomyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,78 @@

namespace DaPigGuy\libPiggyEconomy\providers;

use cooldogedev\BedrockEconomy\api\legacy\ClosureContext;
use cooldogedev\BedrockEconomy\api\type\ClosureAPI;
use cooldogedev\BedrockEconomy\BedrockEconomy;
use cooldogedev\BedrockEconomy\api\BedrockEconomyAPI;
use cooldogedev\BedrockEconomy\api\version\LegacyBEAPI;
use cooldogedev\BedrockEconomy\currency\CurrencyManager;
use cooldogedev\BedrockEconomy\currency\Currency;
use cooldogedev\BedrockEconomy\database\cache\GlobalCache;
use pocketmine\player\Player;
use pocketmine\Server;

class BedrockEconomyProvider extends EconomyProvider
{
private LegacyBEAPI $api;
private CurrencyManager $currency;
private ClosureAPI $api;
private Currency $currency;

public static function checkDependencies(): bool
{
return Server::getInstance()->getPluginManager()->getPlugin("BedrockEconomy") !== null;
$plugin = Server::getInstance()->getPluginManager()->getPlugin("BedrockEconomy");
return $plugin !== null && version_compare($plugin->getDescription()->getVersion(), '4.0', '>=');
}

public function __construct()
{
$this->api = BedrockEconomyAPI::legacy();
$this->currency = BedrockEconomy::getInstance()->getCurrencyManager();
$this->api = BedrockEconomyAPI::CLOSURE();
$this->currency = BedrockEconomy::getInstance()->getCurrency();
}

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

public function getMoney(Player $player, callable $callback): void
{
$this->api->getPlayerBalance($player->getName(), ClosureContext::create(fn(?int $balance) => $callback($balance ?? $this->currency->getDefaultBalance())));
$entry = GlobalCache::ONLINE()->get($player->getName());
$callback($entry ? (float)"{$entry->amount}.{$entry->decimals}" : (float)"{$this->currency->defaultAmount}.{$this->currency->defaultDecimals}");
}

public function giveMoney(Player $player, float $amount, ?callable $callback = null): void
{
$this->api->addToPlayerBalance($player->getName(), (int)$amount, $callback ? ClosureContext::create($callback) : null);
$decimals = (int)(explode('.', strval($amount))[1] ?? 0);
$this->api->add(
$player->getXuid(),
$player->getName(),
(int)$amount,
$decimals,
fn () => $callback ? $callback(true) : null,
fn () => $callback ? $callback(false) : null
);
}

public function takeMoney(Player $player, float $amount, ?callable $callback = null): void
{
$this->api->subtractFromPlayerBalance($player->getName(), (int)$amount, $callback ? ClosureContext::create($callback) : null);
$decimals = (int)(explode('.', strval($amount))[1] ?? 0);
$this->api->subtract(
$player->getXuid(),
$player->getName(),
(int)$amount,
$decimals,
fn () => $callback ? $callback(true) : null,
fn () => $callback ? $callback(false) : null
);
}

public function setMoney(Player $player, float $amount, ?callable $callback = null): void
{
$this->api->setPlayerBalance($player->getName(), (int)$amount, $callback ? ClosureContext::create($callback) : null);
$decimals = (int)(explode('.', strval($amount))[1] ?? 0);
$this->api->set(
$player->getXuid(),
$player->getName(),
(int)$amount,
$decimals,
fn () => $callback ? $callback(true) : null,
fn () => $callback ? $callback(false) : null
);
}
}
2 changes: 1 addition & 1 deletion virion.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: libPiggyEconomy
version: 3.0.2
version: 3.0.3
antigen: DaPigGuy\libPiggyEconomy
api: 5.0.0
author: DaPigGuy

0 comments on commit 9a22d3a

Please sign in to comment.