From 61cfd116cd67f5c68358c09dc12a17ae8ea0ea47 Mon Sep 17 00:00:00 2001 From: Falkirks Date: Mon, 24 Apr 2017 09:32:28 -0700 Subject: [PATCH] Closes #12 And also add permission nodes to commands --- plugin.yml | 22 ++++---- src/falkirks/minereset/MineReset.php | 27 +++++++++ .../minereset/command/AboutCommand.php | 8 ++- .../minereset/command/CreateCommand.php | 32 ++++++----- .../minereset/command/DestroyCommand.php | 42 +++++++------- .../minereset/command/ListCommand.php | 12 +++- .../minereset/command/ResetAllCommand.php | 22 +++++--- .../minereset/command/ResetCommand.php | 26 +++++---- src/falkirks/minereset/command/SetCommand.php | 56 ++++++++++--------- src/falkirks/minereset/task/ResetTask.php | 2 +- 10 files changed, 156 insertions(+), 93 deletions(-) diff --git a/plugin.yml b/plugin.yml index 622aa87..c4e598d 100644 --- a/plugin.yml +++ b/plugin.yml @@ -12,13 +12,15 @@ permissions: default: op description: Use MineReset commands children: - minereset.command.create: - description: Add new Mines - minereset.command.set: - description: Set existing mines - minereset.command.reset: - description: Reset mines - minereset.command.destroy: - description: Remove mines - minereset.command.reset-all: - description: Reset all mines \ No newline at end of file + minereset.command.about: + description: Read the about text + minereset.command.create: + description: Add new Mines + minereset.command.set: + description: Set existing mines + minereset.command.reset: + description: Reset mines + minereset.command.destroy: + description: Remove mines + minereset.command.resetall: + description: Reset all mines \ No newline at end of file diff --git a/src/falkirks/minereset/MineReset.php b/src/falkirks/minereset/MineReset.php index 569915a..a17f3d7 100644 --- a/src/falkirks/minereset/MineReset.php +++ b/src/falkirks/minereset/MineReset.php @@ -14,6 +14,7 @@ use falkirks\minereset\listener\RegionBlockerListener; use falkirks\minereset\store\EntityStore; use falkirks\minereset\store\YAMLStore; +use pocketmine\level\Level; use pocketmine\plugin\PluginBase; use pocketmine\utils\Config; @@ -34,11 +35,15 @@ class MineReset extends PluginBase{ private $regionBlockerListener; /** @var MineCommand */ private $mainCommand; + /** @var bool */ + private static $supportsChunkSetting; /** @var CreationListener */ private $creationListener; public function onEnable(){ + self::detectChunkSetting(); + @mkdir($this->getDataFolder()); $this->mineManager = new MineManager($this, new YAMLStore(new Config($this->getDataFolder() . "mines.yml", Config::YAML, []))); @@ -62,6 +67,11 @@ public function onEnable(){ $this->mainCommand->registerSubCommand("create", new CreateCommand($this)); $this->mainCommand->registerSubCommand("reset", new ResetCommand($this)); $this->mainCommand->registerSubCommand("reset-all", new ResetAllCommand($this)); + + if(!self::supportsChunkSetting()){ + $this->getLogger()->warning("Your server does not support setting chunks without unloading them. This will cause tiles and entities to be lost when resetting mines. Upgrade to a newer pmmp to resolve this."); + } + } public function onDisable(){ @@ -102,4 +112,21 @@ public function getCreationListener(): CreationListener{ public function getRegionBlockerListener(): RegionBlockerListener{ return $this->regionBlockerListener; } + + public static function supportsChunkSetting(): bool { + return static::$supportsChunkSetting; + } + + private static function detectChunkSetting(){ + $class = new \ReflectionClass(Level::class); + $func = $class->getMethod("setChunk"); + $filename = $func->getFileName(); + $start_line = $func->getStartLine() - 1; + $end_line = $func->getEndLine(); + $length = $end_line - $start_line; + + $source = file($filename); + $body = implode("", array_slice($source, $start_line, $length)); + self::$supportsChunkSetting = strpos($body, 'removeEntity') !== false; + } } \ No newline at end of file diff --git a/src/falkirks/minereset/command/AboutCommand.php b/src/falkirks/minereset/command/AboutCommand.php index c1e0381..885d1f5 100644 --- a/src/falkirks/minereset/command/AboutCommand.php +++ b/src/falkirks/minereset/command/AboutCommand.php @@ -5,9 +5,15 @@ use falkirks\minereset\task\AboutPullTask; use pocketmine\command\CommandSender; +use pocketmine\utils\TextFormat; class AboutCommand extends SubCommand{ public function execute(CommandSender $sender, $commandLabel, array $args){ - $this->getApi()->getServer()->getScheduler()->scheduleAsyncTask(new AboutPullTask($sender)); + if($sender->hasPermission("minereset.command.about")) { + $this->getApi()->getServer()->getScheduler()->scheduleAsyncTask(new AboutPullTask($sender)); + } + else{ + $sender->sendMessage(TextFormat::RED . "You do not have permission to run this command." . TextFormat::RESET); + } } } \ No newline at end of file diff --git a/src/falkirks/minereset/command/CreateCommand.php b/src/falkirks/minereset/command/CreateCommand.php index d9de537..976f4c5 100644 --- a/src/falkirks/minereset/command/CreateCommand.php +++ b/src/falkirks/minereset/command/CreateCommand.php @@ -12,27 +12,29 @@ class CreateCommand extends SubCommand{ public function execute(CommandSender $sender, $commandLabel, array $args){ - if($sender instanceof Player) { - if (isset($args[0])) { - if(!$this->getApi()->getCreationListener()->playerHasSession($sender)) { - if (!isset($this->getApi()->getMineManager()[$args[0]])) { - $this->getApi()->getCreationListener()->addSession(new MineCreationSession($args[0], $sender)); - $sender->sendMessage("Tap a block to set position A."); + if($sender->hasPermission("minereset.command.create")) { + if ($sender instanceof Player) { + if (isset($args[0])) { + if (!$this->getApi()->getCreationListener()->playerHasSession($sender)) { + if (!isset($this->getApi()->getMineManager()[$args[0]])) { + $this->getApi()->getCreationListener()->addSession(new MineCreationSession($args[0], $sender)); + $sender->sendMessage("Tap a block to set position A."); + } else { + $sender->sendMessage("That mine already exists. You must run \"/mine destroy {$args[0]}\" before creating a new one."); + } } else { - $sender->sendMessage("That mine already exists. You must run \"/mine destroy {$args[0]}\" before creating a new one."); + $sender->sendMessage("Hold up! You are already in the process of creating a mine. You need to finish that first."); } - } - else{ - $sender->sendMessage("Hold up! You are already in the process of creating a mine. You need to finish that first."); - } - } - else { - $sender->sendMessage("Usage: /mine create "); + } else { + $sender->sendMessage("Usage: /mine create "); + } + } else { + $sender->sendMessage(TextFormat::RED . "This command can only be run in-game." . TextFormat::RESET); } } else{ - $sender->sendMessage(TextFormat::RED . "This command can only be run in-game." . TextFormat::RESET); + $sender->sendMessage(TextFormat::RED . "You do not have permission to run this command." . TextFormat::RESET); } } } \ No newline at end of file diff --git a/src/falkirks/minereset/command/DestroyCommand.php b/src/falkirks/minereset/command/DestroyCommand.php index 5cb72ea..2bf9d6f 100644 --- a/src/falkirks/minereset/command/DestroyCommand.php +++ b/src/falkirks/minereset/command/DestroyCommand.php @@ -33,32 +33,34 @@ public function __construct(MineReset $mineReset){ public function execute(CommandSender $sender, $commandLabel, array $args){ - if(isset($args[0])){ - if(isset($this->getApi()->getMineManager()[$args[0]])){ - if(isset($args[1]) && isset($this->senders[$sender->getName()]) && $this->senders[$sender->getName()] === $args[1]){ - unset($this->getApi()->getMineManager()[$args[0]]); - unset($this->senders[$sender->getName()]); - $sender->sendMessage("{$args[0]} has been destroyed."); - } - else{ - $str = DestroyCommand::DESTROY_STRINGS[$this->offset]; - $sender->sendMessage("Run: " . TextFormat::AQUA . "/mine destroy {$args[0]} $str" . TextFormat::RESET); - $sender->sendMessage("To destroy mines faster, you can edit the config file directly."); - $this->senders[$sender->getName()] = $str; + if($sender->hasPermission("minereset.command.destroy")) { + if (isset($args[0])) { + if (isset($this->getApi()->getMineManager()[$args[0]])) { + if (isset($args[1]) && isset($this->senders[$sender->getName()]) && $this->senders[$sender->getName()] === $args[1]) { + unset($this->getApi()->getMineManager()[$args[0]]); + unset($this->senders[$sender->getName()]); + $sender->sendMessage("{$args[0]} has been destroyed."); + } else { + $str = DestroyCommand::DESTROY_STRINGS[$this->offset]; + $sender->sendMessage("Run: " . TextFormat::AQUA . "/mine destroy {$args[0]} $str" . TextFormat::RESET); + $sender->sendMessage("To destroy mines faster, you can edit the config file directly."); + $this->senders[$sender->getName()] = $str; - if($this->offset === count(DestroyCommand::DESTROY_STRINGS)-1){ - $this->offset = -1; - } + if ($this->offset === count(DestroyCommand::DESTROY_STRINGS) - 1) { + $this->offset = -1; + } - $this->offset++; + $this->offset++; + } + } else { + $sender->sendMessage("{$args[0]} is not a valid mine."); } - } - else{ - $sender->sendMessage("{$args[0]} is not a valid mine."); + } else { + $sender->sendMessage("Usage: /mine destroy "); } } else{ - $sender->sendMessage("Usage: /mine destroy "); + $sender->sendMessage(TextFormat::RED . "You do not have permission to run this command." . TextFormat::RESET); } } } \ No newline at end of file diff --git a/src/falkirks/minereset/command/ListCommand.php b/src/falkirks/minereset/command/ListCommand.php index 532f712..70df65c 100644 --- a/src/falkirks/minereset/command/ListCommand.php +++ b/src/falkirks/minereset/command/ListCommand.php @@ -4,13 +4,19 @@ use falkirks\minereset\Mine; use pocketmine\command\CommandSender; +use pocketmine\utils\TextFormat; class ListCommand extends SubCommand{ public function execute(CommandSender $sender, $commandLabel, array $args){ - foreach ($this->getApi()->getMineManager() as $mine){ - if($mine instanceof Mine) { - $sender->sendMessage($mine->getName()); + if($sender->hasPermission("minereset.command.list")) { + foreach ($this->getApi()->getMineManager() as $mine) { + if ($mine instanceof Mine) { + $sender->sendMessage($mine->getName()); + } } } + else{ + $sender->sendMessage(TextFormat::RED . "You do not have permission to run this command." . TextFormat::RESET); + } } } \ No newline at end of file diff --git a/src/falkirks/minereset/command/ResetAllCommand.php b/src/falkirks/minereset/command/ResetAllCommand.php index 9498459..8ae7dc6 100644 --- a/src/falkirks/minereset/command/ResetAllCommand.php +++ b/src/falkirks/minereset/command/ResetAllCommand.php @@ -4,19 +4,25 @@ use falkirks\minereset\Mine; use pocketmine\command\CommandSender; +use pocketmine\utils\TextFormat; class ResetAllCommand extends SubCommand{ public function execute(CommandSender $sender, $commandLabel, array $args){ - $success = 0; - foreach ($this->getApi()->getMineManager() as $mine){ - if($mine instanceof Mine) { - if($mine->reset()){ - $success++; - $this->getApi()->getResetProgressManager()->addObserver($mine->getName(), $sender); + if($sender->hasPermission("minereset.command.resetall")) { + $success = 0; + foreach ($this->getApi()->getMineManager() as $mine) { + if ($mine instanceof Mine) { + if ($mine->reset()) { + $success++; + $this->getApi()->getResetProgressManager()->addObserver($mine->getName(), $sender); + } } } + $count = count($this->getApi()->getMineManager()); + $sender->sendMessage("Queued reset for {$success}/{$count} mines."); + } + else{ + $sender->sendMessage(TextFormat::RED . "You do not have permission to run this command." . TextFormat::RESET); } - $count = count($this->getApi()->getMineManager()); - $sender->sendMessage("Queued reset for {$success}/{$count} mines."); } } \ No newline at end of file diff --git a/src/falkirks/minereset/command/ResetCommand.php b/src/falkirks/minereset/command/ResetCommand.php index a28b29e..9709dd7 100644 --- a/src/falkirks/minereset/command/ResetCommand.php +++ b/src/falkirks/minereset/command/ResetCommand.php @@ -4,25 +4,31 @@ use falkirks\minereset\Mine; use pocketmine\command\CommandSender; +use pocketmine\utils\TextFormat; class ResetCommand extends SubCommand{ public function execute(CommandSender $sender, $commandLabel, array $args){ - if(isset($args[0])){ - if(isset($this->getApi()->getMineManager()[$args[0]])){ - if($this->getApi()->getMineManager()[$args[0]]->reset()){ - $sender->sendMessage("Queued reset for {$args[0]}."); - $this->getApi()->getResetProgressManager()->addObserver($args[0], $sender); + if($sender->hasPermission("minereset.command.reset")) { + if (isset($args[0])) { + if (isset($this->getApi()->getMineManager()[$args[0]])) { + if ($this->getApi()->getMineManager()[$args[0]]->reset()) { + $sender->sendMessage("Queued reset for {$args[0]}."); + $this->getApi()->getResetProgressManager()->addObserver($args[0], $sender); + } + else { + $sender->sendMessage("Could not queue reset for {$args[0]}."); + } } - else{ - $sender->sendMessage("Could not queue reset for {$args[0]}."); + else { + $sender->sendMessage("{$args[0]} is not a valid mine."); } } - else{ - $sender->sendMessage("{$args[0]} is not a valid mine."); + else { + $sender->sendMessage("Usage: /mine reset "); } } else{ - $sender->sendMessage("Usage: /mine reset "); + $sender->sendMessage(TextFormat::RED . "You do not have permission to run this command." . TextFormat::RESET); } } } \ No newline at end of file diff --git a/src/falkirks/minereset/command/SetCommand.php b/src/falkirks/minereset/command/SetCommand.php index 3fa55c7..210f661 100644 --- a/src/falkirks/minereset/command/SetCommand.php +++ b/src/falkirks/minereset/command/SetCommand.php @@ -8,42 +8,48 @@ class SetCommand extends SubCommand{ public function execute(CommandSender $sender, $commandLabel, array $args){ - if(isset($args[0])){ - if(isset($this->getApi()->getMineManager()[$args[0]])){ - if(isset($args[2])){ - $sets = array_slice($args, 1); - $save = []; - if(count($sets) % 2 === 0) { - foreach ($sets as $key => $item) { - if(strpos($item, "%")) { - $sender->sendMessage(TextFormat::RED . "Your format string looks incorrect." . TextFormat::RESET); - return; - } - if ($key & 1) { - if (isset($save[$sets[$key - 1]])) { - $save[$sets[$key - 1]] += $item; - } else { - $save[$sets[$key - 1]] = $item; + if($sender->hasPermission("minereset.command.set")) { + if (isset($args[0])) { + if (isset($this->getApi()->getMineManager()[$args[0]])) { + if (isset($args[2])) { + $sets = array_slice($args, 1); + $save = []; + if (count($sets) % 2 === 0) { + foreach ($sets as $key => $item) { + if (strpos($item, "%")) { + $sender->sendMessage(TextFormat::RED . "Your format string looks incorrect." . TextFormat::RESET); + return; + } + if ($key & 1) { + if (isset($save[$sets[$key - 1]])) { + $save[$sets[$key - 1]] += $item; + } + else { + $save[$sets[$key - 1]] = $item; + } } } + $this->getApi()->getMineManager()[$args[0]]->setData($save); + $sender->sendMessage(TextFormat::GREEN . "Mine has been setted. Use /mine reset {$args[0]} to see your changes."); + } + else { + $sender->sendMessage(TextFormat::RED . "Your format string looks incorrect." . TextFormat::RESET); } - $this->getApi()->getMineManager()[$args[0]]->setData($save); - $sender->sendMessage(TextFormat::GREEN . "Mine has been setted. Use /mine reset {$args[0]} to see your changes."); } - else{ - $sender->sendMessage(TextFormat::RED . "Your format string looks incorrect." . TextFormat::RESET); + else { + $sender->sendMessage("You must provide at least one block with a chance value."); } } - else{ - $sender->sendMessage("You must provide at least one block with a chance value."); + else { + $sender->sendMessage("{$args[0]} is not a valid mine."); } } - else{ - $sender->sendMessage("{$args[0]} is not a valid mine."); + else { + $sender->sendMessage("Usage: /mine set "); } } else{ - $sender->sendMessage("Usage: /mine set "); + $sender->sendMessage(TextFormat::RED . "You do not have permission to run this command." . TextFormat::RESET); } } } \ No newline at end of file diff --git a/src/falkirks/minereset/task/ResetTask.php b/src/falkirks/minereset/task/ResetTask.php index 7e0f584..e6aec64 100644 --- a/src/falkirks/minereset/task/ResetTask.php +++ b/src/falkirks/minereset/task/ResetTask.php @@ -100,7 +100,7 @@ public function onCompletion(Server $server){ if ($level instanceof Level) { foreach ($chunks as $hash => $chunk) { Level::getXZ($hash, $x, $z); - $level->setChunk($x, $z, $chunk); + $level->setChunk($x, $z, $chunk, !MineReset::supportsChunkSetting()); } }