From 794bea7fd2263228d4f5bceb8e3621c699f68e01 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Thu, 28 Jul 2016 10:31:52 +0300 Subject: [PATCH] Moving forward... --- config.yml | 3 ++ src/Ad5001/GameManager/Game.php | 13 ++++++- src/Ad5001/GameManager/GameManager.php | 52 +++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 config.yml diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..22d7e6e --- /dev/null +++ b/config.yml @@ -0,0 +1,3 @@ +--- +# This is the default config generated with ImagicalPlugCreator. (C) ImagicalPlugCreator - Ad5001 2016 +... \ No newline at end of file diff --git a/src/Ad5001/GameManager/Game.php b/src/Ad5001/GameManager/Game.php index e27bff8..bb207a8 100644 --- a/src/Ad5001/GameManager/Game.php +++ b/src/Ad5001/GameManager/Game.php @@ -22,6 +22,7 @@ abstract class Game { $this->level = $level; $this->name = $name; $this->main = $this->server->getPlugin("GameManager"); + $this->main->backup($level); } @@ -43,7 +44,13 @@ abstract class Game { public function onGameStart(); - public function stopGame(); + public function onGameStop(); + + + public function stopGame() { + $this->main->getGameManager()->reloadLevel($this->level); + return true; + } public function onJoin(Player $player) {} @@ -81,5 +88,9 @@ abstract class Game { public function useEvent(\pocketmine\event\Event $event) : bool; + public function getDataFolder() { + return $this->main->getDataFolder() . "games/$this->name"; + } + } \ No newline at end of file diff --git a/src/Ad5001/GameManager/GameManager.php b/src/Ad5001/GameManager/GameManager.php index 1d3baca..57a6faf 100644 --- a/src/Ad5001/GameManager/GameManager.php +++ b/src/Ad5001/GameManager/GameManager.php @@ -43,8 +43,58 @@ class GameManager { public function registerLevel(Level $level, string $game) { if(!array_key_exists($level->getName(), $this->levels)) { if(isset($this->games[$game])) { - $this->levels[$level->getName()] = new $this->games[$game]($level) + $this->levels[$level->getName()] = new $this->games[$game]($level); + } else { + $this->main->getLogger()->warn("No game found with name $game"); } } } + + + + public function reloadLevel(Level $level) { + $this->backup($level); + rrmdir($this->main->getFilePath() . "worlds/{$level->getName()}"); + $this->restore($level); + } + + + private function rrmdir($dir) { // This is from PHP.NET + if (is_dir($dir)) { + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != "." && $object != "..") { + if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); + } + } + reset($objects); + rmdir($dir); + } + } + + + private function copydir( $source, $target ) { + if ( is_dir( $source ) ) { + @mkdir( $target ); + $d = dir( $source ); + while ( FALSE !== ( $entry = $d->read() ) ) { + if ( $entry == '.' || $entry == '..' ) { + continue; + } + $Entry = $source . '/' . $entry; + if ( is_dir( $Entry ) ) { + full_copy( $Entry, $target . '/' . $entry ); + continue; + } + copy( $Entry, $target . '/' . $entry ); + } + + $d->close(); + } else { + copy( $source, $target ); + } + } + + + } \ No newline at end of file