Skip to content

An extremely customizable Economy plugin designed for scalability and simplicity

License

Notifications You must be signed in to change notification settings

cooldogepm/BedrockEconomy

Repository files navigation

BedrockEconomy

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

Table of Contents

Commands

Name Description Usage Permission
balance Show your and others balance balance [player: string] bedrockeconomy.command.balance
pay Pay others with your balance pay <player: string> <amount: number> bedrockeconomy.command.pay
rich View the top balances rich [page: number] bedrockeconomy.command.rich
addbalance Add points to others balance addbalance <player: string> <amount: number> bedrockeconomy.command.addbalance
removebalance Remove points from others balance removebalance <player: string> <amount: number> bedrockeconomy.command.removebalance
setbalance Set others balance setbalance <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.