From abbcbae8a70642cb34b9bdb0205f2bdf90b7b895 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Wed, 3 Aug 2016 20:02:12 +0300 Subject: [PATCH] Starting winning over bugs :D --- config.yml | 2 +- src/Ad5001/GameManager/Game.php | 8 ++++- src/Ad5001/GameManager/GameManager.php | 14 +++++--- src/Ad5001/GameManager/Main.php | 36 ++++++++++++++----- .../GameManager/tasks/SignReloadTask.php | 6 ++-- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/config.yml b/config.yml index fa6b4ae..003358c 100644 --- a/config.yml +++ b/config.yml @@ -17,5 +17,5 @@ InGame4: "> > > CLICK TO SPECTATE < < <" # Now you can choose all worlds game's # Example: -rougepierre: Example +redstone: Example ... \ No newline at end of file diff --git a/src/Ad5001/GameManager/Game.php b/src/Ad5001/GameManager/Game.php index 5ac094c..e6053f5 100644 --- a/src/Ad5001/GameManager/Game.php +++ b/src/Ad5001/GameManager/Game.php @@ -35,7 +35,7 @@ abstract class Game { public function getLevel() { - return $this->main; + return $this->level; } @@ -68,6 +68,7 @@ abstract class Game { } if($this->getLevel()->getPlayers() <= $this->getMaxPlayers() and !$this->isStarted()) { $player->teleport($this->getServer()->getDefaultLevel()->getDefaultSpawn()); + $player->sendMessage("Too many players already in the game !"); } } @@ -79,6 +80,11 @@ abstract class Game { } + public function getLogger() { + return $this->getPlugin()->getLogger(); + } + + public function onInteract(\pocketmine\event\player\PlayerInteractEvent $event) {} diff --git a/src/Ad5001/GameManager/GameManager.php b/src/Ad5001/GameManager/GameManager.php index d6615d5..eb7a151 100644 --- a/src/Ad5001/GameManager/GameManager.php +++ b/src/Ad5001/GameManager/GameManager.php @@ -67,6 +67,8 @@ class GameManager { } else { $this->main->getLogger()->warn("No game found with name $game"); } + } else { + $this->main->getLogger()->warn("{$level->getName()} is already registered."); } } @@ -88,15 +90,17 @@ class GameManager { public function restoreBackup(Level $level) { - $this->rrmdir($level->getFolderName()); - $this->copydir($this->server->getFilePath() . "worldsBackups/{$level->getName()}", $level->getFolderName()); + $this->rrmdir($this->server->getFilePath() . "worlds/" . $level->getFolderName()); + @mkdir($this->server->getFilePath() . "worlds/{$level->getFolderName()}"); + $this->copydir($this->server->getFilePath() . "worldsBackups/{$level->getName()}", $this->server->getFilePath() . "worlds/" . $level->getFolderName()); } public function backup(Level $level) { $this->rrmdir($this->server->getFilePath() . "worldsBackups/{$level->getName()}"); - $this->copydir($level->getFolderName(), $this->server->getFilePath() . "worldsBackup/{$level->getName()}"); + @mkdir($this->server->getFilePath() . "worldsBackup/{$level->getName()}"); + $this->copydir($this->server->getFilePath() . "worlds/" . $level->getFolderName(), $this->server->getFilePath() . "worldsBackup/{$level->getName()}"); } @@ -105,7 +109,7 @@ class GameManager { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { - if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); + if (filetype($dir."/".$object) == "dir") $this->rrmdir($dir."/".$object); else unlink($dir."/".$object); } } reset($objects); @@ -124,7 +128,7 @@ class GameManager { } $Entry = $source . '/' . $entry; if (is_dir($Entry)) { - copydir($Entry, $target . '/' . $entry); + $this->copydir($Entry, $target . '/' . $entry); continue; } copy($Entry, $target . '/' . $entry); diff --git a/src/Ad5001/GameManager/Main.php b/src/Ad5001/GameManager/Main.php index 6cf2af0..df7b049 100644 --- a/src/Ad5001/GameManager/Main.php +++ b/src/Ad5001/GameManager/Main.php @@ -10,7 +10,6 @@ use pocketmine\level\Level; use pocketmine\block\Block; use pocketmine\event\entity\EntityLevelChangeEvent; use pocketmine\Player; -use Ad5001\GameManager\GameManager; use Ad5001\GameManager\tasks\SignReloadTask; @@ -27,9 +26,12 @@ class Main extends PluginBase implements Listener { @mkdir($this->getDataFolder() . "games"); $this->manager = new GameManager($this); $this->getServer()->getScheduler()->scheduleRepeatingTask(new SignReloadTask($this), 5); - foreach(array_diff_key($this->getConfig()->getAll(), ["Game1" => "", "Game2" => "", "InGame3" => "", "InGame4" => "", "GameWait3" => "", "GameWait4" => ""]) as $worldname => $gamename) { - if($this->getServer()->getLevelByName($worldname) instanceof Level) { - $this->manager->registerLevel($this->getServer()->getLevelByName($worldname), $gamename); + foreach(array_diff_key($this->getConfig()->getAll(), ["Game1" => $this->getConfig()->get("Game1"), "Game2" => $this->getConfig()->get("Game2"), "InGame3" => $this->getConfig()->get("InGame3"), "InGame4" => $this->getConfig()->get("InGame4"), "GameWait3" => $this->getConfig()->get("InGame4"), "GameWait4" => $this->getConfig()->get("InGame4")]) as $worldname => $gamename) { + $lvl = $this->getLevelByName($worldname); + // $this->getLogger()->info(get_class($lvl)); + if($lvl instanceof Level) { + // $this->getLogger()->info("Registering $worldname"); + $this->manager->registerLevel($lvl, $gamename); } } } @@ -69,6 +71,16 @@ class Main extends PluginBase implements Listener { } + public function getLevelByName(string $name) { + foreach($this->getServer()->getLevels() as $level) { + if($level->getName() == $name) { + return $level; + } + } + return null; + } + + ############################ # # # Events for games # @@ -102,12 +114,20 @@ public function onInteract(PlayerInteractEvent $event) { } + + public function onLevelLoad(\pocketmine\event\level\LevelLoadEvent $event) { + if($this->getConfig()->get($event->getLevel()->getName()) !== null) { + $this->manager->registerLevel($event->getLevel(), $this->getConfig()->get($event->getLevel()->getName())); + } + } + + public function onEntityLevelChange(EntityLevelChangeEvent $event) { if(isset($this->manager->getLevels()[$event->getOrigin()->getName()]) and $event->getEntity() instanceof Player) { - $this->gameManager->getLevels()[$event->getOrigin()->getName()]->onQuit($event->getPlayer()); + $this->manager->getLevels()[$event->getOrigin()->getName()]->onQuit($event->getEntity()); } if(isset($this->manager->getLevels()[$event->getTarget()->getName()]) and $event->getEntity() instanceof Player) { - $this->gameManager->getLevels()[$event->getTarget()->getName()]->onJoin($event->getPlayer()); + $this->manager->getLevels()[$event->getTarget()->getName()]->onJoin($event->getEntity()); } } @@ -204,7 +224,7 @@ public function onInteract(PlayerInteractEvent $event) { } - public function onServerCommand(\pocketmine\event\server\DataPacketReceiveEvent $event) { + public function onServerCommand(\pocketmine\event\server\ServerCommandEvent $event) { foreach($this->manager->getLevels() as $lvl => $class) { $class->onServerCommand($event); } @@ -218,7 +238,7 @@ public function onInteract(PlayerInteractEvent $event) { public function onPlayerQuit(\pocketmine\event\player\PlayerQuitEvent $event) { if(isset($this->manager->getLevels()[$event->getPlayer()->getLevel()->getName()])) { - $this->manager->getLevels()[$event->getPlayer()->getLevel()->getName()]->onQuit($player); + $this->manager->getLevels()[$event->getPlayer()->getLevel()->getName()]->onQuit($event->getPlayer()); } } } \ No newline at end of file diff --git a/src/Ad5001/GameManager/tasks/SignReloadTask.php b/src/Ad5001/GameManager/tasks/SignReloadTask.php index d242fc5..5f8392e 100644 --- a/src/Ad5001/GameManager/tasks/SignReloadTask.php +++ b/src/Ad5001/GameManager/tasks/SignReloadTask.php @@ -30,7 +30,7 @@ class SignReloadTask extends PluginTask { if($t instanceof \pocketmine\tile\Sign) { // echo "Sign."; foreach($this->gameManager->getLevels() as $name => $class) { - echo $class->getLevel()->getName(); + // echo $class->getLevel()->getName(); if($t->getText()[0] == "[GAME]" and $class->getLevel()->getName() == $t->getText()[1]) { $texts = $t->getText(); foreach($texts as $key => $text) { @@ -42,8 +42,10 @@ class SignReloadTask extends PluginTask { $lvlex = explode("{level}", $this->cfg->get("Game2")); $lvl = str_ireplace($lvlex[0], "", $t->getText()[1]); $lvl = str_ireplace($lvlex[1], "", $lvl); + $lvl = $this->main->getServer()->getLevelByName($lvl); + // $this->main->getLogger()->info($name . " == " . $lvl . " . Game: " . $t->getText()[0]); if($name == $lvl) { - if($this->gm->getLevels()[$lvl->getName()]->isStarted()) { + if($this->gameManager->getLevels()[$lvl->getName()]->isStarted()) { $l3 = str_ireplace("{players}", count($lvl->getPlayers()), $this->cfg->get("InGame3")); $l3 = str_ireplace("{max}", $class->getMaxPlayers(), $l3); $l4 = str_ireplace("{players}", count($lvl->getPlayers()), $this->cfg->get("InGame4"));