Browse Source

Lot of fixes, and almost finished WorldReset

pull/14/head
Ad5001 3 years ago
parent
commit
ed8320ff9d
No account linked to committer's email address

+ 1
- 0
plugin.yml View File

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

+ 44
- 16
scenarios/WorldReseter.php View File

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

+ 4
- 6
src/Ad5001/UHC/UHCGame.php View File

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

+ 11
- 4
src/Ad5001/UHC/UHCManager.php View File

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

+ 2
- 0
src/Ad5001/UHC/event/GameStopEvent.php View File

@@ -20,6 +20,8 @@ class GameStopEvent extends UHCEvent implements Cancellable {

public static $handlerList = null;

public $cancelled = false;



public function __construct($game, $world, $winner) {

+ 1
- 1
src/Ad5001/UHC/scenario/Scenario.php View File

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

+ 1
- 1
src/Ad5001/UHC/scenario/ScenarioInt.php View File

@@ -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 */

Loading…
Cancel
Save