Skip to content

Commit

Permalink
Update Main.php
Browse files Browse the repository at this point in the history
  • Loading branch information
AzliRynz authored Jul 8, 2024
1 parent 41046a4 commit e2f469b
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/NurAzliYT/LandProtections/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@
use NurAzliYT\LandProtections\commands\AccessLandCommand;
use cooldogedev\BedrockEconomy\BedrockEconomy;
use cooldogedev\BedrockEconomy\api\BedrockEconomyAPI;
use cooldogedev\BedrockEconomy\api\BedrockEconomyAPIv1;
use pocketmine\player\Player;

class Main extends PluginBase implements Listener {

Check failure on line 23 in src/NurAzliYT/LandProtections/Main.php

View workflow job for this annotation

GitHub Actions / Check Code Style

Class NurAzliYT\LandProtections\Main extends unknown class pocketmine\plugin\PluginBase.

Check failure on line 23 in src/NurAzliYT/LandProtections/Main.php

View workflow job for this annotation

GitHub Actions / Check Code Style

Class NurAzliYT\LandProtections\Main implements unknown interface pocketmine\event\Listener.

private Config $config;

Check failure on line 24 in src/NurAzliYT/LandProtections/Main.php

View workflow job for this annotation

GitHub Actions / Check Code Style

Property NurAzliYT\LandProtections\Main::$config has unknown class pocketmine\utils\Config as its type.
private array $claimedChunks = [];
private array $chunkAccess = [];
private ?BedrockEconomyAPI $economyAPI = null;
private ?BedrockEconomyAPIv1 $economyAPI = null;

Check failure on line 27 in src/NurAzliYT/LandProtections/Main.php

View workflow job for this annotation

GitHub Actions / Check Code Style

Property NurAzliYT\LandProtections\Main::$economyAPI has unknown class cooldogedev\BedrockEconomy\api\BedrockEconomyAPIv1 as its type.

public function onEnable(): void {
$this->saveDefaultConfig();

Check failure on line 30 in src/NurAzliYT/LandProtections/Main.php

View workflow job for this annotation

GitHub Actions / Check Code Style

Call to an undefined method NurAzliYT\LandProtections\Main::saveDefaultConfig().
$this->config = new Config($this->getDataFolder() . "claimedChunks.yml", Config::YAML);

Check failure on line 31 in src/NurAzliYT/LandProtections/Main.php

View workflow job for this annotation

GitHub Actions / Check Code Style

Access to constant YAML on an unknown class pocketmine\utils\Config.

Check failure on line 31 in src/NurAzliYT/LandProtections/Main.php

View workflow job for this annotation

GitHub Actions / Check Code Style

Call to an undefined method NurAzliYT\LandProtections\Main::getDataFolder().

Check failure on line 31 in src/NurAzliYT/LandProtections/Main.php

View workflow job for this annotation

GitHub Actions / Check Code Style

Instantiated class pocketmine\utils\Config not found.
$this->claimedChunks = $this->config->getAll();

Check failure on line 32 in src/NurAzliYT/LandProtections/Main.php

View workflow job for this annotation

GitHub Actions / Check Code Style

Call to method getAll() on an unknown class pocketmine\utils\Config.

$this->chunkAccess = $this->config->get("chunkAccess", []);

Check failure on line 33 in src/NurAzliYT/LandProtections/Main.php

View workflow job for this annotation

GitHub Actions / Check Code Style

Call to method get() on an unknown class pocketmine\utils\Config.

// BedrockEconomy integration
$this->economyAPI = BedrockEconomy::getInstance()->getAPI();
$this->economyAPI = BedrockEconomy::getInstance()->getAPIv1();

$this->getServer()->getPluginManager()->registerEvents($this, $this);
$this->getServer()->getCommandMap()->register("claim", new ClaimCommand($this));
Expand All @@ -54,9 +53,7 @@ public function onPlayerInteract(PlayerInteractEvent $event): void {

if ($this->isChunkClaimed($position)) {
if (!$this->isChunkOwner($position, $player->getName()) && !$this->hasAccessToChunk($position, $player->getName())) {
if ($event instanceof Cancellable) {
$event->setCancelled();
}
$event->cancel();
$player->sendMessage("This chunk is claimed by someone else.");
}
}
Expand All @@ -69,9 +66,7 @@ public function onBlockPlace(BlockPlaceEvent $event): void {

if ($this->isChunkClaimed($position)) {
if (!$this->isChunkOwner($position, $player->getName()) && !$this->hasAccessToChunk($position, $player->getName())) {
if ($event instanceof Cancellable) {
$event->setCancelled();
}
$event->cancel();
$player->sendMessage("This chunk is claimed by someone else.");
}
}
Expand All @@ -84,9 +79,7 @@ public function onBlockBreak(BlockBreakEvent $event): void {

if ($this->isChunkClaimed($position)) {
if (!$this->isChunkOwner($position, $player->getName()) && !$this->hasAccessToChunk($position, $player->getName())) {
if ($event instanceof Cancellable) {
$event->setCancelled();
}
$event->cancel();
$player->sendMessage("This chunk is claimed by someone else.");
}
}
Expand All @@ -108,7 +101,7 @@ public function isChunkOwner(Position $position, string $owner): bool {
}

public function getChunkHash(Position $position): string {
return $position->getFloorX() >> 4 . ":" . $position->getFloorZ() >> 4;
return ($position->getFloorX() >> 4) . ":" . ($position->getFloorZ() >> 4);
}

public function chargePlayer(Player $player, int $amount): bool {
Expand All @@ -117,16 +110,28 @@ public function chargePlayer(Player $player, int $amount): bool {
return false;
}

$balance = $this->economyAPI->getPlayerBalance($player->getName());
$this->economyAPI->getPlayerBalance($player->getName(), function (?int $balance) use ($player, $amount) {
if ($balance === null) {
$player->sendMessage("Failed to retrieve balance.");
return false;
}

if ($balance >= $amount) {
$this->economyAPI->subtractFromPlayerBalance($player->getName(), $amount);
$player->sendMessage("You have been charged $amount coins.");
return true;
} else {
$player->sendMessage("You do not have enough coins to claim this chunk.");
return false;
}
if ($balance >= $amount) {
$this->economyAPI->subtractFromPlayerBalance($player->getName(), $amount, function (bool $success) use ($player, $amount) {
if ($success) {
$player->sendMessage("You have been charged $amount coins.");
} else {
$player->sendMessage("Failed to charge your balance.");
}
});
return true;
} else {
$player->sendMessage("You do not have enough coins to claim this chunk.");
return false;
}
});

return true; // This assumes the balance retrieval is always async and successful.
}

public function giveAccessToChunk(Position $position, string $playerName): void {
Expand Down

0 comments on commit e2f469b

Please sign in to comment.