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 13, 2024
1 parent 115fc51 commit edc8e22
Showing 1 changed file with 45 additions and 98 deletions.
143 changes: 45 additions & 98 deletions src/NurAzliYT/LandProtections/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,147 +3,94 @@
namespace NurAzliYT\LandProtections;

use pocketmine\plugin\PluginBase;
use pocketmine\block\Block;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerInteractEvent;
use NurAzliYT\events\BlockPlaceEvent;
use pocketmine\event\block\BlockPlaceEvent;
use pocketmine\event\block\BlockBreakEvent;
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\world\Position;
use pocketmine\utils\Config;
use pocketmine\player\Player;
use pocketmine\utils\Config;
use pocketmine\world\Position;
use cooldogedev\BedrockEconomy\BedrockEconomy;
use cooldogedev\BedrockEconomy\api\BedrockEconomyAPI;
use NurAzliYT\LandProtections\commands\ClaimCommand;
use NurAzliYT\LandProtections\commands\AccessLandCommand;

class Main extends PluginBase implements Listener {
private Config $config;
private array $claimedChunks = [];
private array $chunkAccess = [];
private ?BedrockEconomyAPI $economyAPI = null;

public function onEnable(): void {
$this->saveDefaultConfig();
$this->config = new Config($this->getDataFolder() . "claimedChunks.yml", Config::YAML);
$this->claimedChunks = $this->config->getAll();
$this->chunkAccess = $this->config->get("chunkAccess", []);
class Main extends PluginBase implements Listener

Check failure on line 19 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 19 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 BedrockEconomyAPI $economyAPI;

Check failure on line 21 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\BedrockEconomyAPI as its type.
private Config $claimedChunks;

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

View workflow job for this annotation

GitHub Actions / Check Code Style

Property NurAzliYT\LandProtections\Main::$claimedChunks has unknown class pocketmine\utils\Config as its type.

// BedrockEconomy integration
protected function onEnable(): void
{
$this->economyAPI = BedrockEconomy::getInstance()->getAPI();

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

View workflow job for this annotation

GitHub Actions / Check Code Style

Call to static method getInstance() on an unknown class cooldogedev\BedrockEconomy\BedrockEconomy.
$this->claimedChunks = new Config($this->getDataFolder() . "claimedChunks.yml", Config::YAML);

Check failure on line 27 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 27 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 27 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->getServer()->getPluginManager()->registerEvents($this, $this);

Check failure on line 29 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::getServer().
$this->getServer()->getCommandMap()->register("claim", new ClaimCommand($this));
$this->getServer()->getCommandMap()->register("accessland", new AccessLandCommand($this));
$this->getServer()->getCommandMap()->register("landprotections", new ClaimCommand($this));

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::getServer().
}

public function onDisable(): void {
$this->config->setAll($this->claimedChunks);
$this->config->set("chunkAccess", $this->chunkAccess);
$this->config->save();
}

public function onPlayerInteract(PlayerInteractEvent $event): void {
public function onBlockPlace(BlockPlaceEvent $event): void
{
$player = $event->getPlayer();
$block = $event->getBlock();
$position = $block->getPosition();

if ($this->isChunkClaimed($position)) {
if (!$this->isChunkOwner($position, $player->getName()) && !$this->hasAccessToChunk($position, $player->getName())) {
$event->cancel();
$player->sendMessage("This chunk is claimed by someone else.");
}
if ($this->isChunkClaimed($this->getChunkHash($block->getPosition())) &&
!$this->isChunkOwner($player, $block->getPosition())) {
$event->cancel();
$player->sendMessage("This chunk is already claimed.");
}
}

public function onBlockPlace(BlockPlaceEvent $event): void {
public function onBlockBreak(BlockBreakEvent $event): void
{
$player = $event->getPlayer();
$block = $event->getBlock();
$position = $block->getPosition();

if ($this->isChunkClaimed($position)) {
if (!$this->isChunkOwner($position, $player->getName()) && !$this->hasAccessToChunk($position, $player->getName())) {
$event->cancel();
$player->sendMessage("This chunk is claimed by someone else.");
}
if ($this->isChunkClaimed($this->getChunkHash($block->getPosition())) &&
!$this->isChunkOwner($player, $block->getPosition())) {
$event->cancel();
$player->sendMessage("You cannot break blocks in this claimed chunk.");
}
}

public function onBlockBreak(BlockBreakEvent $event): void {
public function onPlayerInteract(PlayerInteractEvent $event): void
{
$player = $event->getPlayer();
$block = $event->getBlock();
$position = $block->getPosition();

if ($this->isChunkClaimed($position)) {
if (!$this->isChunkOwner($position, $player->getName()) && !$this->hasAccessToChunk($position, $player->getName())) {
$event->cancel();
$player->sendMessage("This chunk is claimed by someone else.");
}
if ($this->isChunkClaimed($this->getChunkHash($block->getPosition())) &&
!$this->isChunkOwner($player, $block->getPosition())) {
$event->cancel();
$player->sendMessage("You cannot interact with blocks in this claimed chunk.");
}
}

public function claimChunk(Position $position, string $owner): void {
public function claimChunk(Player $player, Position $position): void
{
$chunkHash = $this->getChunkHash($position);
$this->claimedChunks[$chunkHash] = $owner;
$this->claimedChunks->set($chunkHash, $player->getName());
$this->claimedChunks->save();
}

public function isChunkClaimed(Position $position): bool {
$chunkHash = $this->getChunkHash($position);
return isset($this->claimedChunks[$chunkHash]);
public function isChunkClaimed(string $chunkHash): bool
{
return $this->claimedChunks->exists($chunkHash);
}

public function isChunkOwner(Position $position, string $owner): bool {
public function isChunkOwner(Player $player, Position $position): bool
{
$chunkHash = $this->getChunkHash($position);
return isset($this->claimedChunks[$chunkHash]) && $this->claimedChunks[$chunkHash] === $owner;
return $this->claimedChunks->get($chunkHash) === $player->getName();
}

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

public function chargePlayer(Player $player, int $amount): bool {
if ($this->economyAPI === null) {
$player->sendMessage("Economy system not available.");
return false;
}

$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, 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 {
$chunkHash = $this->getChunkHash($position);
if (!isset($this->chunkAccess[$chunkHash])) {
$this->chunkAccess[$chunkHash] = [];
}
if (!in_array($playerName, $this->chunkAccess[$chunkHash])) {
$this->chunkAccess[$chunkHash][] = $playerName;
}
}

public function hasAccessToChunk(Position $position, string $playerName): bool {
$chunkHash = $this->getChunkHash($position);
return isset($this->chunkAccess[$chunkHash]) && in_array($playerName, $this->chunkAccess[$chunkHash]);
public function getBedrockEconomy(): BedrockEconomyAPI
{
return $this->economyAPI;
}
}

1 comment on commit edc8e22

@poggit-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear Poggit user,

This is an automatic message from Poggit-CI. Poggit-CI was triggered by this commit, but failed to create builds due to the following error:

Resend this delivery later. This commit is triggered by user NurAzliYT, who
has created 100 ≥ 100 Poggit-CI builds in the past 168 hours.

Contact SOFe [on Discord](https://discord.gg/XDugAkJ) to request for extra
quota. We will increase your quota for free if you are really contributing
to open-source plugins actively without abusing Git commits.

As a result, no builds could be created from this commit. More details might be available for repo admins at the webhook delivery response log — see the webhook starting with https://poggit.pmmp.io/webhooks.gh.repo and look for the delivery with ID 04d625bc-4107-11ef-8c25-f807534b6838.

Shall you need any assistance, you may contact us on on Discord, or more publicly, on GitHub. Commenting on this commit directly will not notify any Poggit staff.

Regards,
Poggit Bot (@poggit-bot)
The official Poggit automation account

Please sign in to comment.