diff --git a/plugin.yml b/plugin.yml index e9c0c86..9ff6dae 100644 --- a/plugin.yml +++ b/plugin.yml @@ -20,6 +20,7 @@ commands: description: Manage UHC scenarios usage: "/scenarios [add | remove | list] [scenario]" permission: uhc.command.scenarios + aliases: [scs] permissions: uhc.command.main: default: true diff --git a/scenarios/WorldReseter.php b/scenarios/WorldReseter.php index 61ce47d..a524f8d 100644 --- a/scenarios/WorldReseter.php +++ b/scenarios/WorldReseter.php @@ -21,18 +21,15 @@ class UHCWorldReseter extends \Ad5001\UHC\scenario\Scenario { } - public function onStop() { - $this->getLevel()->getLevel()->unload(); - $this->delDir($this->getServer()->getFilePath() . "worlds/" . $this->getLevel()->getLevel()->getName()); - $this->getServer()->generateLevel($this->getLevel()->getName(), intval(sha1(rand(0, 999999))), "pocketmine\\level\\generator\\normal\\Normal"); - $this->getServer()->loadLevel($this->getLevel()->getName()); - if(file_exists($this->getScenariosFolder() . "LobbysBackups/" .$this->getWorld()->getName() . ".json")) { - $bs = json_decode(file_get_contents($this->getScenariosFolder() . "LobbysBackups/" .$this->getWorld()->getName() . ".json"), true); + public function onJoin(\pocketmine\Player $player) { + if(file_exists($this->getScenariosFolder() . "LobbysBackups/" . $this->getLevel()->getLevel()->getName() . ".json")) { + $bs = json_decode(file_get_contents($this->getScenariosFolder() . "LobbysBackups/" . $this->getLevel()->getLevel()->getName() . ".json"), true); $i = 0; - for($x = $this->getLevel()->getLevel()->getSafeSpawn()->x + 10; $x >= $this->getLevel()->getLevel()->getSafeSpawn()->x - 10;$x++) { - for($y = $this->getLevel()->getLevel()->getSafeSpawn()->y + 10; $y >= $this->getLevel()->getLevel()->getSafeSpawn()->y - 10;$y++) { - for($z = $this->getLevel()->getLevel()->getSafeSpawn()->z + 10; $z >= $this->getLevel()->getLevel()->getSafeSpawn()->z - 10;$z++) { - $this->getWorld()->getLevel()->setBlock(new \pocketmine\math\Vector3($x, $y, $z), new \pocketmine\block\Block(Item::fromString($bs[$i]))); + $ss = $this->getLevel()->getLevel()->getSafeSpawn(); + for($x = $ss->x + 10; $x >= $ss->x - 10;$x--) { + for($y = $ss->y + 10; $y >= $ss->y - 10;$y--) { + for($z = $ss->z + 10; $z >= $ss->z - 10;$z--) { + $this->getLevel()->getLevel()->setBlock(new \pocketmine\math\Vector3($x, $y, $z), new \pocketmine\block\Block(Item::fromString($bs[$i]))); $i++; } } @@ -41,18 +38,49 @@ class UHCWorldReseter extends \Ad5001\UHC\scenario\Scenario { } + public function onStop(\pocketmine\Player $player) { + $this->getLogger()->info("Game in level " . $this->getLevel()->getName() . " ended. Reseting world...."); + $this->name = $this->getLevel()->getName(); + $this->ss = $this->getLevel()->getLevel()->getSafeSpawn(); + foreach($this->getLevel()->getLevel()->getPlayers() as $p) { + $player->teleport($this->getServer()->getLevelByName($this->getMain()->getConfig()->get("LobbyWorld"))->getSafeSpawn()); + } + $this->winner = $player; + $h = $this->getServer()->getScheduler()->scheduleRepeatingTask($t = new UHCWorldReseterFetchGenerateTask($this), 10); + $t->setHandler($h); + } + + public function delDir(string $path) { foreach(array_diff(scandir($path),[".", ".."]) as $p) { if(is_dir($path . "/" . $p)) { - if(count(array_diff(scandir($path . "/". $p),[".", ".."])) == 0) { - rmdir($path . "/" . $p); - } else { - $this->delDir($path . "/" . $p); - } + $this->delDir($path . "/" . $p); } else { unlink($path . "/" . $p); } } + rmdir($path); + } +} + + + +class UHCWorldReseterFetchGenerateTask extends \pocketmine\scheduler\PluginTask{ + + + public function __construct($main){ + parent::__construct($main->getMain()); + $this->main =$main; + } + + public function onRun($tick) { + if($this->main->getServer()->getLevelByName($this->main->name) !== null) { + $this->main->getLevel()->getLevel()->unload(); + $this->main->delDir($this->main->getServer()->getFilePath() . "worlds/" . $this->name); + $this->main->getServer()->generateLevel($this->name, intval(sha1(rand(0, 999999))), "pocketmine\\level\\generator\\normal\\Normal"); + $this->main->getLogger()->info("Level " . $this->main->name . " reseted !"); + $this->main->getServer()->getScheduler()->cancelTask($this->getTaskId()); + } } } \ No newline at end of file diff --git a/src/Ad5001/UHC/UHCGame.php b/src/Ad5001/UHC/UHCGame.php index 8017634..e315ff6 100644 --- a/src/Ad5001/UHC/UHCGame.php +++ b/src/Ad5001/UHC/UHCGame.php @@ -144,12 +144,10 @@ class UHCGame implements Listener{ public function stop(Player $winner) { $this->m->getServer()->getPluginManager()->callEvent($ev = new GameStopEvent($this, $this->world, $winner)); if(!$ev->cancelled) { - $this->m->getServer()->getPluginManager()->callEvent($event = new GameStopEvent($this, $this->world, $winner)); - if(!$event->isCancelled()) { - foreach($winner->getLevel()->getPlayers() as $player) { - $player->sendMessage(Main::PREFIX . C::YELLOW . $winner->getName() . " won the game ! Teleporting back to lobby..."); - $player->teleport($this->m->getServer()->getLevelByName($this->m->getConfig()->get("LobbyWorld"))->getSafeSpawn()); - } + foreach($winner->getLevel()->getPlayers() as $player) { + $player->sendMessage(Main::PREFIX . C::YELLOW . $winner->getName() . " won the game ! Teleporting back to lobby..."); + $player->teleport($this->m->getServer()->getLevelByName($this->m->getConfig()->get("LobbyWorld"))->getSafeSpawn()); + $this->m->UHCManager->stopUHC($this->world->getLevel(), $winner); } } } diff --git a/src/Ad5001/UHC/UHCManager.php b/src/Ad5001/UHC/UHCManager.php index 21be684..c8f72bb 100644 --- a/src/Ad5001/UHC/UHCManager.php +++ b/src/Ad5001/UHC/UHCManager.php @@ -52,12 +52,19 @@ class UHCManager { - public function stopUHC(Level $level) { - if(isset($this->getStartedUHCs()[$level->getName()])) { + public function stopUHC(Level $level, Player $player) { + if(isset($this->startedgames[$level->getName()])) { foreach($this->levels[$level->getName()]->scenarioManager->getUsedScenarios() as $sc) { - $sc->onStop(); + $sc->onStop($player); } - unset($this->startedgames[$level->getName()]); + $started = []; + foreach($this->startedgames as $name => $game) { + if($name !== $level->getName()) { + $started[$name] = $game; + } + } + $this->startedgames = $started; + $this->main->getLogger()->info("Game " . $level->getName() . " stoped"); return true; } return false; diff --git a/src/Ad5001/UHC/event/GameStopEvent.php b/src/Ad5001/UHC/event/GameStopEvent.php index 397cf48..ef06a4b 100644 --- a/src/Ad5001/UHC/event/GameStopEvent.php +++ b/src/Ad5001/UHC/event/GameStopEvent.php @@ -20,6 +20,8 @@ class GameStopEvent extends UHCEvent implements Cancellable { public static $handlerList = null; + public $cancelled = false; + public function __construct($game, $world, $winner) { diff --git a/src/Ad5001/UHC/scenario/Scenario.php b/src/Ad5001/UHC/scenario/Scenario.php index e8c2ecf..ed5834a 100644 --- a/src/Ad5001/UHC/scenario/Scenario.php +++ b/src/Ad5001/UHC/scenario/Scenario.php @@ -36,7 +36,7 @@ abstract class Scenario implements ScenarioInt, Listener { public function onStart() {} - public function onStop() {} + public function onStop(Player $player) {} public function onJoin(Player $player) {} diff --git a/src/Ad5001/UHC/scenario/ScenarioInt.php b/src/Ad5001/UHC/scenario/ScenarioInt.php index dad4b9b..acac07f 100644 --- a/src/Ad5001/UHC/scenario/ScenarioInt.php +++ b/src/Ad5001/UHC/scenario/ScenarioInt.php @@ -17,7 +17,7 @@ interface ScenarioInt { /* When the scenario is stoping (end of UHC) */ - public function onStop(); + public function onStop(\pocketmine\Player $player); /* Getting the main methods */