Starting winning over bugs :D

This commit is contained in:
Ad5001 2016-08-03 20:02:12 +03:00
parent b3e17d214b
commit abbcbae8a7
5 changed files with 49 additions and 17 deletions

View file

@ -17,5 +17,5 @@ InGame4: "> > > CLICK TO SPECTATE < < <"
# Now you can choose all worlds game's
# Example:
rougepierre: Example
redstone: Example
...

View file

@ -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) {}

View file

@ -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);

View file

@ -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());
}
}
}

View file

@ -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"));