BedrockEconomy

BedrockEconomy

por procatin0
No disponible
Economíapocketmine
127visitas
5descargas

BedrockEconomy

BedrockEconomy is an economy plugin made for PocketMine-MP focused on stability and simplicity.

Table of Contents

Commands

NameDescriptionUsagePermission
balanceShow your and others balancebalance [player: string]bedrockeconomy.command.balance
payPay others with your balancepay <player: string> <amount: number>bedrockeconomy.command.pay
richView the top balancesrich [page: number]bedrockeconomy.command.rich
addbalanceAdd points to others balanceaddbalance <player: string> <amount: number>bedrockeconomy.command.addbalance
removebalanceRemove points from others balanceremovebalance <player: string> <amount: number>bedrockeconomy.command.removebalance
setbalanceSet others balancesetbalance <player: string> <balance: number>bedrockeconomy.command.setbalance

Features

  • MySQL Database
  • SQLite Database
  • Async API
  • Closure API
  • Customizable
  • Easy to use
  • Lightweight
  • Fast and efficient
  • Cache system

Examples

Retrieving a Player's Balance

You can retrieve a player's balance using the get method. Here's an example:

BedrockEconomyAPI::CLOSURE()->get(
    xuid: "123456789",
    username: "Doge",
    onSuccess: static function (array $result): void {
        echo "Balance: " . $result["amount"] . " Decimals: " . $result["decimals"] . " Position: " . $result["position"];
    },
    onError: static function (SQLException $exception): void {
        if ($exception instanceof RecordNotFoundException) {
            echo "Record not found";
            return;
        }

        echo $exception->getMessage();
    }
);

// Using async-await
Await::f2c(
    function (): Generator {
        try {
            $result = yield from BedrockEconomyAPI::ASYNC()->get(
                xuid: "123456789",
                username: "Doge",
            );
        } catch (RecordNotFoundException) {
            echo "Account not found";
            return;
        } catch (SQLException) {
            echo "Database error";
            return;
        }
        
        echo "Balance: " . $result["amount"] . " Decimals: " . $result["decimals"] . " Position: " . $result["position"];
    }
);

Retrieving Multiple Player Balances

You can retrieve multiple player balances using the bulk method. Here's an example:

BedrockEconomyAPI::CLOSURE()->bulk(
    list: ["Doge", "123456789"], // You can use both username and xuid
    onSuccess: static function (array $result): void {
        foreach ($result as $data) {
            echo "Player: " . $data["username"] . " Balance: " . $data["amount"] . " Decimals: " . $data["decimals"] . " Position: " . $data["position"];
        }
    },
    onError: static function (SQLException $exception): void {
        if ($exception instanceof RecordNotFoundException) {
            echo "No records found";
            return;
        }

        echo $exception->getMessage();
    }
);

// Using async-await
Await::f2c(
    function (): Generator {
        try {
            $result = yield from BedrockEconomyAPI::ASYNC()->bulk(
                list: ["Doge", "123456789"], // You can use both username and xuid
            );
        } catch (RecordNotFoundException) {
            echo "No records found";
            return;
        } catch (SQLException) {
            echo "Database error";
            return;
        }
        
        foreach ($result as $data) {
            echo "Player: " . $data["username"] . " Balance: " . $data["amount"] . " Decimals: " . $data["decimals"] . " Position: " . $data["position"];
        }
    }
);

Adding Funds to a Player's Balance

You can add funds to a player's balance using the add method. Here's an example:

BedrockEconomyAPI::CLOSURE()->add(
    xuid: "123456789",
    username: "Doge",
    amount: 55,
    decimals: 25,
    onSuccess: static function (): void {
        echo 'Balance updated successfully.';
    },
    onError: static function (SQLException $exception): void {
        if ($exception instanceof RecordNotFoundException) {
            echo 'Account not found';
            return;
        }

        echo 'An error occurred while updating the balance.';
    }
);

// Using async-await
Await::f2c(
    function () use ($player): Generator {
        try {
            yield from BedrockEconomyAPI::ASYNC()->add(
                xuid: "123456789",
                username: "Doge",
                amount: 55,
                decimals: 25,
            );
            echo 'Balance updated successfully.';
        } catch (RecordNotFoundException) {
            echo 'Account not found';
        } catch (SQLException) {
            echo 'An error occurred while updating the balance.';
        }
    }
);

Subtracting Funds from a Player's Balance

You can subtract funds from a player's balance using the subtract method. Here's an example:

BedrockEconomyAPI::CLOSURE()->subtract(
    xuid: "123456789",
    username: "Doge",
    amount: 55,
    decimals: 25,
    onSuccess: static function (): void {
        echo 'Balance updated successfully.';
    },
    onError: static function (SQLException $exception): void {
        if ($exception instanceof RecordNotFoundException) {
            echo 'Account not found';
            return;
        }

        if ($exception instanceof InsufficientFundsException) {
            echo 'Insufficient funds';
            return;
        }

        echo 'An error occurred while updating the balance.';
    }
);

// Using async-await
Await::f2c(
    function () use ($player): Generator {
        try {
            yield from BedrockEconomyAPI::ASYNC()->subtract(
                xuid: "123456789",
                username: "Doge",
                amount: 55,
                decimals: 25,
            );
            echo 'Balance updated successfully.';
        } catch (RecordNotFoundException) {
            echo 'Account not found';
        } catch (InsufficientFundsException) {
            echo 'Insufficient funds';
        } catch (SQLException) {
            echo 'An error occurred while updating the balance.';
        }
    }
);

Transferring Funds Between Players

You can transfer funds from one player to another using the transfer method. Here's an example:

$sourcePlayer = ['xuid' => 'source_xuid', 'username' => 'source_username'];
$targetPlayer = ['xuid' => 'target_xuid', 'username' => 'target_username'];

BedrockEconomyAPI::CLOSURE()->transfer(
    source: $sourcePlayer,
    target: $targetPlayer,
    amount: 55,
    decimals: 25,
    onSuccess: static function (): void {
        echo 'Balance transfer successful.';
    },
    onError: static function (SQLException $exception): void {
        if ($exception instanceof RecordNotFoundException) {
            echo 'Account not found';
            return;
        }
        
        if ($exception instanceof InsufficientFundsException) {
            echo 'Insufficient funds';
            return;
        }

        echo 'An error occurred during the balance transfer.';
    }
);

// Using async-await
Await::f2c(
    function () use ($sourcePlayer, $targetPlayer): Generator {
        try {
            yield from BedrockEconomyAPI::ASYNC()->transfer(
                source: $sourcePlayer,
                target: $targetPlayer,
                amount: 55,
                decimals: 25,
            );
            echo 'Balance transfer successful.';
        } catch (RecordNotFoundException) {
            echo 'Account not found';
        } catch (InsufficientFundsException) {
            echo 'Insufficient funds';
        } catch (SQLException) {
            echo 'An error occurred during the balance transfer.';
        }
    }
);

These examples demonstrate how to perform common operations using the BedrockEconomy API, such as retrieving player balances, adding and subtracting funds, and transferring funds between players.

Tools

License

This project is released under the MIT License. For more information, please refer to the LICENSE file.