Skip to content

Commit

Permalink
Remove BedrockEconomy warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Muqsit committed Mar 17, 2022
1 parent d674282 commit c357235
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 31 deletions.
3 changes: 1 addition & 2 deletions resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@ economy:
# issue at https://github.com/Muqsit/ChestShop/issues
plugin: EconomyAPI
# Extra configuration options if BedrockEconomy plugin is chosen
BedrockEconomy:
unsafe-warn: true
BedrockEconomy: []
...
32 changes: 3 additions & 29 deletions src/muqsit/chestshop/economy/BedrockEconomyIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
use cooldogedev\BedrockEconomy\BedrockEconomy;
use cooldogedev\BedrockEconomy\libs\cooldogedev\libSQL\context\ClosureContext;
use InvalidArgumentException;
use muqsit\chestshop\Loader;
use muqsit\chestshop\util\PlayerIdentity;
use pocketmine\Server;
use function assert;

final class BedrockEconomyIntegration implements EconomyIntegration{

Expand All @@ -29,14 +27,6 @@ public function __construct(){
}

public function init(array $config) : void{
if(($config["unsafe-warn"] ?? true) && count($this->plugin->getConnector()->getThreads()) > 1){
/** @var Loader|null $chest_shop */
$chest_shop = Server::getInstance()->getPluginManager()->getPlugin("ChestShop");
assert($chest_shop !== null);
$chest_shop->getLogger()->warning("{$this->plugin->getName()} is unsafe to use with > 1 database threads");
$chest_shop->getLogger()->warning("To correct this issue, set 'threads' to 1 in {$this->plugin->getConfig()->getPath()}");
$chest_shop->getLogger()->warning("To suppress this error, set 'unsafe-warn' to false in {$chest_shop->getConfig()->getPath()}");
}
}

public function getMoney(PlayerIdentity $player, Closure $callback) : void{
Expand All @@ -50,26 +40,10 @@ public function addMoney(PlayerIdentity $player, float $money) : void{
}

public function removeMoney(PlayerIdentity $player, float $money, Closure $callback) : void{
/**
* SAFETY NOTICE
* This method executes two asynchronous processes simultaneously:
* getPlayerBalance(), subtractFromPlayerBalance()
*
* It is completely possible for a race condition to happen and an unexpected value
* be subtracted as a consequence.
* BedrockEconomy lets player balances go below 0, so directly invoking
* subtractFromPlayerBalance() is not possible.
*/
$money_int = (int) ceil($money);
$this->getMoney($player, static function(float $balance) use($player, $money_int, $callback) : void{
if($balance >= $money_int){
BedrockEconomyAPI::getInstance()->subtractFromPlayerBalance($player->getGamertag(), $money_int, ClosureContext::create(static function(bool $success) use($callback) : void{
$callback($success);
}));
}else{
$callback(false);
}
});
BedrockEconomyAPI::getInstance()->subtractFromPlayerBalance($player->getGamertag(), $money_int, ClosureContext::create(static function(bool $success) use($callback) : void{
$callback($success);
}));
}

public function formatMoney(float $money) : string{
Expand Down

0 comments on commit c357235

Please sign in to comment.