Skip to content

Commit

Permalink
Closes #12
Browse files Browse the repository at this point in the history
And also add permission nodes to commands
  • Loading branch information
falkirks committed Apr 24, 2017
1 parent 2894b65 commit 61cfd11
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 93 deletions.
22 changes: 12 additions & 10 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
27 changes: 27 additions & 0 deletions src/falkirks/minereset/MineReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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, [])));
Expand All @@ -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(){
Expand Down Expand Up @@ -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;
}
}
8 changes: 7 additions & 1 deletion src/falkirks/minereset/command/AboutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
32 changes: 17 additions & 15 deletions src/falkirks/minereset/command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>");
} else {
$sender->sendMessage("Usage: /mine create <name>");
}
} 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);
}
}
}
42 changes: 22 additions & 20 deletions src/falkirks/minereset/command/DestroyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>");
}
}
else{
$sender->sendMessage("Usage: /mine destroy <name>");
$sender->sendMessage(TextFormat::RED . "You do not have permission to run this command." . TextFormat::RESET);
}
}
}
12 changes: 9 additions & 3 deletions src/falkirks/minereset/command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
22 changes: 14 additions & 8 deletions src/falkirks/minereset/command/ResetAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
26 changes: 16 additions & 10 deletions src/falkirks/minereset/command/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>");
}
}
else{
$sender->sendMessage("Usage: /mine reset <name>");
$sender->sendMessage(TextFormat::RED . "You do not have permission to run this command." . TextFormat::RESET);
}
}
}
56 changes: 31 additions & 25 deletions src/falkirks/minereset/command/SetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name> <data>");
}
}
else{
$sender->sendMessage("Usage: /mine set <name> <data>");
$sender->sendMessage(TextFormat::RED . "You do not have permission to run this command." . TextFormat::RESET);
}
}
}
2 changes: 1 addition & 1 deletion src/falkirks/minereset/task/ResetTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}
}
Expand Down

0 comments on commit 61cfd11

Please sign in to comment.