Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion EconomyAPI/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: EconomyAPI
main: onebone\economyapi\EconomyAPI
version: "3.0.0-SNAPSHOT"
author: onebone
api: 3.9.0
api: 5.0.0

permissions:
economyapi:
Expand Down
215 changes: 178 additions & 37 deletions EconomyAPI/src/onebone/economyapi/EconomyAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@
use pocketmine\command\CommandSender;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\Player;
use pocketmine\player\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\Config;
use pocketmine\utils\Internet;
use pocketmine\utils\TextFormat;
use Throwable;

class EconomyAPI extends PluginBase implements Listener {
const API_VERSION = 4;
const API_VERSION = 5;
const PACKAGE_VERSION = "6.0";

/**
Expand Down Expand Up @@ -119,6 +118,101 @@ class EconomyAPI extends PluginBase implements Listener {

private $lang = [];

public function onLoad(): void {
self::$instance = $this;
}

public function onEnable(): void {
if(!file_exists($this->getDataFolder())) {
mkdir($this->getDataFolder());
}

$this->saveDefaultConfig();
$this->saveResource("lang_en.json");

$this->pluginConfig = new PluginConfig($this->getConfig());

// Load language files
$this->loadLanguages();

// Initialize providers
$this->initializeProviders();

// Initialize currencies
$this->initializeCurrencies();

// Register events
$this->getServer()->getPluginManager()->registerEvents($this, $this);
$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);

// Register commands
$this->registerCommands();

// Start save task
$this->getScheduler()->scheduleRepeatingTask(new SaveTask($this), $this->pluginConfig->getSaveInterval() * 20);

$this->getLogger()->info("EconomyAPI has been enabled");
}

public function onDisable(): void {
if($this->provider instanceof Provider) {
$this->provider->save();
$this->provider->close();
}
}

private function loadLanguages(): void {
$languages = ["ch", "cs", "de", "en", "fr", "id", "it", "ja", "ko", "nl", "ru", "zh"];

foreach($languages as $lang) {
$file = $this->getDataFolder() . "lang_" . $lang . ".json";
if(file_exists($file)) {
$this->lang[$lang] = json_decode(file_get_contents($file), true);
}
}
}

private function initializeProviders(): void {
$provider = strtolower($this->pluginConfig->getProvider());

switch($provider) {
case "yaml":
$this->provider = new YamlUserProvider($this->getDataFolder() . "user.yml");
break;
case "mysql":
// MySQL provider initialization
$this->provider = new DummyUserProvider(); // Fallback
break;
default:
$this->provider = new DummyUserProvider();
break;
}
}

private function initializeCurrencies(): void {
// Initialize default currency
$defaultCurrency = new CurrencyDollar();
$this->defaultCurrency = new CurrencyHolder("dollar", $defaultCurrency, null, null, null);
$this->currencies["dollar"] = $this->defaultCurrency;

// Initialize currency selector
$this->currencySelector = new SimpleCurrencySelector($this);
}

private function registerCommands(): void {
$commandMap = $this->getServer()->getCommandMap();

$commandMap->register("economyapi", new EconomyCommand($this));
$commandMap->register("economyapi", new GiveMoneyCommand($this));
$commandMap->register("economyapi", new MyMoneyCommand($this));
$commandMap->register("economyapi", new MyStatusCommand($this));
$commandMap->register("economyapi", new PayCommand($this));
$commandMap->register("economyapi", new SeeMoneyCommand($this));
$commandMap->register("economyapi", new SetMoneyCommand($this));
$commandMap->register("economyapi", new TakeMoneyCommand($this));
$commandMap->register("economyapi", new TopMoneyCommand($this));
}

/**
* Returns instance of EconomyAPI. Should be called after onLoad() phase.
* @return EconomyAPI
Expand Down Expand Up @@ -333,6 +427,70 @@ public function hasCurrency($val): bool {
return false;
}

/**
* @param string $id
* @return Currency|null
*/
public function getCurrency(string $id): ?Currency {
return isset($this->currencies[$id]) ? $this->currencies[$id]->getCurrency() : null;
}

/**
* @param Currency $currency
* @return string|null
*/
public function getCurrencyId(Currency $currency): ?string {
foreach($this->currencies as $id => $holder) {
if($holder->getCurrency() === $currency) {
return $id;
}
}
return null;
}

/**
* @param Currency|null $currency
* @return CurrencyHolder|null
*/
public function getCurrencyHolder(?Currency $currency): ?CurrencyHolder {
if($currency === null) {
return $this->defaultCurrency;
}

foreach($this->currencies as $holder) {
if($holder->getCurrency() === $currency) {
return $holder;
}
}
return null;
}

/**
* @param Currency|null $currency
* @param Player|string|null $player
* @return CurrencyHolder
*/
private function findCurrencyHolder(?Currency $currency, $player): CurrencyHolder {
if($currency !== null) {
$holder = $this->getCurrencyHolder($currency);
if($holder !== null) {
return $holder;
}
}

if($player !== null) {
$preferred = $this->getPlayerPreferredCurrency($player, true);
if($preferred !== null) {
$holder = $this->getCurrencyHolder($preferred);
if($holder !== null) {
return $holder;
}
}
}

return $this->defaultCurrency;
}

/**
* @return array
*/
Expand Down Expand Up @@ -480,7 +638,7 @@ private function canAddMoney($player, float $amount, bool $force, ?Issuer $issue
$ev = new AddMoneyEvent($this, $player, $holder->getCurrency(), $amount, $issuer);
$ev->call();

if($ev->setCancelled() and $force === false) {
if($ev->isCancelled() and $force === false) {
return self::RET_CANCELLED;
}

Expand Down Expand Up @@ -605,28 +763,23 @@ public function createAccount($player, $currency = null, $defaultMoney = false,

$ev = new CreateAccountEvent($this, $player, $holder->getCurrency(), $defaultMoney, $issuer);
$ev->call();
if($ev->isCancelled()) {
return false;
}

$holder->getBalanceRepository()->createAccount($player, $ev->getDefaultMoney());
return true;
return $holder->getBalanceRepository()->createAccount($player, $defaultMoney);
}

return false;
}

/**
* @experimental
*
* Executes multiple actions at the same time.
*
* Be aware that the function does not guarantee atomicity if actions in $transaction contains actions
* with multiple types of currencies. Also currency availability for a player is not considered during
* the transaction.
*
* @param Transaction $transaction
* @param Issuer|null $issuer
* @return bool Returns true if succeed or false if failed. If actions contain multiple currencies,
* atomicity is not guaranteed.
* @param PlayerJoinEvent $event
* @priority MONITOR
* @ignoreCancelled true
*/
<<<<<<< HEAD
public function onPlayerJoin(PlayerJoinEvent $event): void {
=======
public function executeTransaction(Transaction $transaction, ?Issuer $issuer = null): bool {
$transactionMap = [];
foreach($transaction->getActions() as $action) {
Expand Down Expand Up @@ -913,24 +1066,12 @@ private function registerCommands() {
]);
}

public function onJoin(PlayerJoinEvent $event) {
public function onPlayerJoin(PlayerJoinEvent $event) {
>>>>>>> 1011d39ed1ea3ab6f6329818ddb2078ec03b1144
$player = $event->getPlayer();

if(!$this->defaultCurrency->getBalanceRepository()->hasAccount($player)) {
$this->getLogger()->debug("UserInfo of '" . $player->getName() . "' is not found. Creating account...");
$this->createAccount($player, $this->defaultCurrency->getCurrency());
}
}

public function onDisable() {
foreach($this->currencies as $currency) {
$currency->close();
}
}

public function saveAll() {
foreach($this->currencies as $currency) {
$currency->save();

if(!$this->hasAccount($player)) {
$this->createAccount($player);
}
}
}
}
Loading