Starting scenario implementation

This commit is contained in:
Ad5001 2016-06-10 16:00:18 +02:00
parent 26a4c28ea1
commit 2d183661e9
8 changed files with 98 additions and 38 deletions

View file

@ -6,4 +6,4 @@ worlds:
nether: nether:
name: nether name: nether
maxplayers: 8 maxplayers: 8
raduis: 1000 radius: 1000

View file

@ -12,14 +12,21 @@ use pocketmine\command\Command;
use pocketmine\event\Listener; use pocketmine\event\Listener;
use pocketmine\event\level\LevelLoadEvent; use pocketmine\event\level\LevelLoadEvent;
use pocketmine\event\player\PlayerJoinEvent; use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\event\player\PlayerDeathEvent;
use pocketmine\event\player\PlayerChatEvent;
use pocketmine\event\player\PlayerQuitEvent;
use pocketmine\event\player\PlayerRespawnEvent;
use pocketmine\event\entity\EntityRegainHealthEvent;
use pocketmine\plugin\PluginBase; use pocketmine\plugin\PluginBase;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\event\entity\EntityLevelChangeEvent;
use pocketmine\utils\TextFormat as C; use pocketmine\utils\TextFormat as C;
use Ad5001\UHC\UHCWorld; use Ad5001\UHC\UHCWorld;
use Ad5001\UHC\UHCGame; use Ad5001\UHC\UHCGame;
use Ad5001\UHC\task\FetchPlayersTask; use Ad5001\UHC\task\FetchPlayersTask;
use Ad5001\UHC\task\StartGameTask;
use Ad5001\UHC\event\GameStartEvent; use Ad5001\UHC\event\GameStartEvent;
use Ad5001\UHC\event\GameFinishEvent; use Ad5001\UHC\event\GameFinishEvent;
class Main extends PluginBase implements Listener{ class Main extends PluginBase implements Listener{
@ -28,12 +35,13 @@ class Main extends PluginBase implements Listener{
public function startGame(UHCWorld $world) { public function startGame(UHCWorld $world) {
$this->games[$world->getName()] = new UHCGame($this, $world); $ft = $this->getServer()->getScheduler()->scheduleRepeatingTask(new StartGameTask($this, $world), 20);
// $this->games[$world->getName()] = new UHCGame($this, $world);
} }
public function onLevelChange(\pocketmine\event\entity\EntityLevelChangeEvent $event) { public function onLevelChange(EntityLevelChangeEvent $event) {
foreach($this->worlds as $world) { foreach($this->worlds as $world) {
if($event->getLevel()->getName() === $world->getName() and !isset($this->games[$world->getName()])) { if($event->getLevel()->getName() === $world->getName() and !isset($this->games[$world->getName()])) {
if(count($world->getLevel()->getPlayers) > $world->maxplayers) { if(count($world->getLevel()->getPlayers) > $world->maxplayers) {
@ -59,9 +67,13 @@ class Main extends PluginBase implements Listener{
public function onPlayerJoin(PlayerJoinEvent $event) { public function onPlayerJoin(PlayerJoinEvent $event) {
if(!isset($this->ft)) { if(!isset($this->ft)) {
$this->ft = $this->getServer()->getScheduler()->scheduleRepeatingTask(new FetchPlayersTask($this, $this->worlds), 10); $this->ft = $this->getServer()->getScheduler()->scheduleRepeatingTask(new FetchPlayersTask($this, $this->worlds), 10);
foreach($this->getConfig()->get("worlds") as $lvl) {
$this->worlds[$lvl["name"]] = new UHCWorld($this, $this->getServer()->getLevelByName($lvl["name"]), $lvl["name"], $lvl["maxplayers"], $lvl["radius"]);
$this->getLogger()->debug("Processing {$lvl["name"]}");
} }
} elseif(isset($this->quit[$event->getPlayer()])) { }
$quit = explode("/", $this->quit[$event->getPlayer()]); if(isset($this->quit[$event->getPlayer()->getName()])) {
$quit = explode("/", $this->quit[$event->getPlayer()->getName()]);
$event->getPlayer()->teleport($this->getServer()->getLevelByName($quit[4])); $event->getPlayer()->teleport($this->getServer()->getLevelByName($quit[4]));
$event->getPlayer()->teleport(new Vector3($quit[0], $quit[1], $quit[2])); $event->getPlayer()->teleport(new Vector3($quit[0], $quit[1], $quit[2]));
foreach($world->getLevel()->getPlayers() as $player) { foreach($world->getLevel()->getPlayers() as $player) {
@ -73,15 +85,6 @@ class Main extends PluginBase implements Listener{
public function onLevelLoad(LevelLoadEvent $event) {
foreach($this->getConfig()->get("worlds") as $lvl) {
$this->getLogger()->debug("Processing $lvl");
if($event->getLevel()->getName() === $lvl["name"]) {
$this->world[$lvl["name"]] = new UHCWorld($this, $this->getServer()->getLevelByName($lvl), $lvl["name"], $lvl["maxplayers"], $lvl["radius"]);
$this->getLogger()->debug("Processing $lvl = {$event->getLevel()->getName()}");
}
}
}
public function onEnable(){ public function onEnable(){
$this->reloadConfig(); $this->reloadConfig();
$this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getServer()->getPluginManager()->registerEvents($this, $this);
@ -98,7 +101,29 @@ $this->reloadConfig();
$this->saveDefaultConfig(); $this->saveDefaultConfig();
} }
public function onRespawn(PlayerRespawnEvent $event) {
foreach($this->games as $game) {
$game->onRespawn($event);
}
}
public function onPlayerQuit(PlayerQuitEvent $event) {
foreach($this->games as $game) {
$game->onPlayerQuit($event);
}
}
public function onPlayerDeath(PlayerDeathEvent $event) {
foreach($this->games as $game) {
$game->onPlayerDeath($event);
}
}
public function onHeal(EntityRegainHealthEvent $event) {
foreach($this->games as $game) {
$game->onHeal($event);
}
}
public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){ public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
@ -108,7 +133,7 @@ switch($cmd->getName()){
switch($args[0]) { switch($args[0]) {
case "start": case "start":
if(isset($this->worlds[$sender->getLevel()->getName()]) and !isset($this->games[$sender->getLevel()->getName()])) { if(isset($this->worlds[$sender->getLevel()->getName()]) and !isset($this->games[$sender->getLevel()->getName()])) {
$this->getLogger()->debug("Starting game {$this->worlds[$sender->getLevel()->getName()]}"); $this->getLogger()->debug("Starting game {$sender->getLevel()->getName()}");
foreach($sender->getLevel()->getPlayers() as $player) { foreach($sender->getLevel()->getPlayers() as $player) {
$player->sendMessage(self::PREFIX . "Starting game..."); $player->sendMessage(self::PREFIX . "Starting game...");
} }

View file

@ -11,14 +11,14 @@ use pocketmine\command\CommandSender;
use pocketmine\command\Command; use pocketmine\command\Command;
use pocketmine\event\Listener; use pocketmine\event\Listener;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\level\Level;
use pocketmine\plugin\Plugin;
use pocketmine\math\Vector3;
use pocketmine\event\player\PlayerDeathEvent; use pocketmine\event\player\PlayerDeathEvent;
use pocketmine\event\player\PlayerChatEvent; use pocketmine\event\player\PlayerChatEvent;
use pocketmine\event\player\PlayerQuitEvent; use pocketmine\event\player\PlayerQuitEvent;
use pocketmine\event\player\PlayerRespawnEvent; use pocketmine\event\player\PlayerRespawnEvent;
use pocketmine\event\entity\EntityRegainHealthEvent; use pocketmine\event\entity\EntityRegainHealthEvent;
use pocketmine\level\Level;
use pocketmine\plugin\Plugin;
use pocketmine\math\Vector3;
use pocketmine\utils\TextFormat as C; use pocketmine\utils\TextFormat as C;
use pocketmine\Player; use pocketmine\Player;
@ -36,8 +36,9 @@ class UHCGame implements Listener{
public function __construct(Plugin $plugin, UHCWorld $world) { public function __construct(Plugin $plugin, UHCWorld $world) {
$this->m = $plugin; $this->m = $plugin;
$this->world = $world; $this->world = $world;
$this->players = $world->getPlayers(); $this->players = $world->getLevel()->getPlayers();
$event = $this->getServer()->getPluginManager()->callEvent(new GameStartEvent($this, $world, $this->players)); $event = new GameStartEvent($this, $world, $this->players);
$this->m->getServer()->getPluginManager()->callEvent($event);
$this->cancelled = false; $this->cancelled = false;
$this->kills = []; $this->kills = [];
if($event->isCancelled()) { if($event->isCancelled()) {
@ -45,20 +46,19 @@ class UHCGame implements Listener{
} else { } else {
$radius = $world->radius; $radius = $world->radius;
foreach($this->players as $player) { foreach($this->players as $player) {
$x = rand($radius + $world->getLevel()->getDefaultSpawn(), $world->getLevel()->getDefaultSpawn() - $radius); $x = rand($radius + $world->getLevel()->getSpawnLocation()->x, $world->getLevel()->getSpawnLocation()->x - $radius);
$z = rand($radius + $world->getLevel()->getDefaultSpawn(), $world->getLevel()->getDefaultSpawn() - $radius); $z = rand($radius + $world->getLevel()->getSpawnLocation()->z, $world->getLevel()->getSpawnLocation()->z - $radius);
$pos = new Vector3($x, 128, $z); $pos = new Vector3($x, 128, $z);
$player->teleport($pos); $player->teleport($pos);
$effect = \pocketmine\entity\Effect::getEffect(11); $effect = \pocketmine\entity\Effect::getEffect(11);
$effect->setDuration(30); $effect->setDuration(30*20);
$effect->setAmplifier(99); $effect->setAmplifier(99);
$effect->setVisible(false); $effect->setVisible(false);
$player->addEffect($effect); $player->addEffect($effect);
$this->m->getServer()->getScheduler()->scheduleDelayedTask(new StopResTask($this, $this->worlds), 30*20); $this->m->getServer()->getScheduler()->scheduleDelayedTask(new StopResTask($this, $this->world), 30*20);
$player->sendMessage(Main::PREFIX . C::GREEN . "Game started ! Good luck {$player->getName()} !"); $player->sendMessage(Main::PREFIX . C::GREEN . "Game started ! Good luck {$player->getName()} !");
} }
} }
Server::getInstance()->getPluginManager()->registerEvents($this, $this);
} }
@ -146,11 +146,20 @@ class UHCGame implements Listener{
public function onPlayerChat(PlayerChatEvent $event) { public function onPlayerChat(PlayerChatEvent $event) {
if($event->getPlayer()->getLevel()->getName() === $this->world->getLevel()->getName() and $event->getPlayer()->getGamemode() === 3) { if($event->getPlayer()->getLevel()->getName() === $this->world->getLevel()->getName() and $event->getPlayer()->getGamemode() === 3) {
foreach($this->world->getLevel()->getPlayer()) if($event->getPlayer()->isSpectator()) {
foreach($this->world->getLevel()->getPlayer() as $player) {
$player->sendMessage(C::GRAY . "[SPECTATOR] {$event->getPlayer()->getName()} > " . $event->getMessage());
}
$event->setCancelled(true);
}
} }
} }
/*
Will be useful for scenarios:
@param player
*/
public function getKills(Player $player) { public function getKills(Player $player) {
if(isset($this->kills[$player->getName()])) { if(isset($this->kills[$player->getName()])) {
return $this->kills[$player->getName()]; return $this->kills[$player->getName()];

View file

@ -30,7 +30,7 @@ class UHCWorld {
$this->radius = $radius; $this->radius = $radius;
} }
public function getLevel() { public function getLevel() {
return $this->level; return $this->lvl;
} }
public function getName() { public function getName() {
return $this->name; return $this->name;
@ -53,6 +53,7 @@ class UHCWorld {
if(!in_array($player, $players)){ if(!in_array($player, $players)){
foreach($this->players as $pl) { foreach($this->players as $pl) {
$pl->sendMessage(Main::PREFIX . C::YELLOW . "{$player->getName()} joined the game."); $pl->sendMessage(Main::PREFIX . C::YELLOW . "{$player->getName()} joined the game.");
$part = new TextParticle(new FloatingTextParticle(new Vector3($this->x, $this->y, $this->z), C::GREEN . "Welcome to the UHC {$player->getName()} !\n" . C::GREEN . "To get help about the plugin , please type command /uhc howtoplay .", C::YELLOW . "-=<UHC>=-"), $this->level, $player);
} }
} }
} }

View file

@ -6,9 +6,9 @@
# | |__| | | | | |____ # | |__| | | | | |____
# \____/|_| |_|\_____| # \____/|_| |_|\_____|
# The most customisable UHC plugin for Minecraft PE ! # The most customisable UHC plugin for Minecraft PE !
namespace Ad5001\UHC\events; namespace Ad5001\UHC\event;
use pocketmine\event\Cancellable.php; use pocketmine\event\Cancellable.php;
use Ad5001\UHC\events\UHCEvent; use Ad5001\UHC\event\UHCEvent;
use Ad5001\UHC\UHCGame; use Ad5001\UHC\UHCGame;
use Ad5001\UHC\UHCWorld; use Ad5001\UHC\UHCWorld;

View file

@ -6,16 +6,18 @@
# | |__| | | | | |____ # | |__| | | | | |____
# \____/|_| |_|\_____| # \____/|_| |_|\_____|
# The most customisable UHC plugin for Minecraft PE ! # The most customisable UHC plugin for Minecraft PE !
namespace Ad5001\UHC\events; namespace Ad5001\UHC\event;
use pocketmine\event\Cancellable.php; use pocketmine\event\Cancellable;
use Ad5001\UHC\events\UHCEvent; use Ad5001\UHC\event\UHCEvent;
use Ad5001\UHC\UHCGame; use Ad5001\UHC\UHCGame;
use Ad5001\UHC\UHCWorld; use Ad5001\UHC\UHCWorld;
class GameStartEvent extends UHCEvent implements Cancellable {
protected $game; protected $game;
protected $world; protected $world;
protected $players; protected $players;
class GameStartEvent extends UHCEvent implements Cancellable { static $handlerList = null;
public function __construct($game, $world, $players) { public function __construct($game, $world, $players) {
$this->game = $game; $this->game = $game;
$this->world = $world; $this->world = $world;

View file

@ -6,7 +6,7 @@
# | |__| | | | | |____ # | |__| | | | | |____
# \____/|_| |_|\_____| # \____/|_| |_|\_____|
# The most customisable UHC plugin for Minecraft PE ! # The most customisable UHC plugin for Minecraft PE !
namespace Ad5001\UHC\events; namespace Ad5001\UHC\event;
use pocketmine\event\Event; use pocketmine\event\Event;
abstract class UHCEvent extends Event { abstract class UHCEvent extends Event {
public function getGame() { public function getGame() {

View file

@ -26,15 +26,35 @@ class StartGameTask extends PluginTask {
parent::__construct($main); parent::__construct($main);
$this->main = $main; $this->main = $main;
$this->world = $world; $this->world = $world;
$this->seconds = 0; $this->seconds = 30;
$this->close = false;
} }
public function close() {
$this->close = true;
}
public function onRun($tick) { public function onRun($tick) {
if(!$this->close) {
$this->main->getLogger()->debug($this->seconds);
switch($this->seconds) { switch($this->seconds) {
case 30: case 30:
foreach($this->world->getLevel()->getPlayers() as $player) { foreach($this->world->getLevel()->getPlayers() as $player) {
$player->sendMessage(Main::PREFIX . C::YELLOW . "30 seconds before the game starts"); $player->sendMessage(Main::PREFIX . C::YELLOW . "30 seconds before the game starts");
} }
break; break;
case 20:
foreach($this->world->getLevel()->getPlayers() as $player) {
$player->sendMessage(Main::PREFIX . C::YELLOW . "20 seconds before the game starts");
}
break;
case 15:
foreach($this->world->getLevel()->getPlayers() as $player) {
$player->sendMessage(Main::PREFIX . C::YELLOW . "15 seconds before the game starts");
}
break;
case 10: case 10:
foreach($this->world->getLevel()->getPlayers() as $player) { foreach($this->world->getLevel()->getPlayers() as $player) {
$player->sendMessage(Main::PREFIX . C::YELLOW . "10 seconds before the game starts"); $player->sendMessage(Main::PREFIX . C::YELLOW . "10 seconds before the game starts");
@ -67,8 +87,11 @@ class StartGameTask extends PluginTask {
break; break;
case 0: case 0:
$this->main->games[$this->world->getLevel()->getName()] = new UHCGame($this->main, $this->world); $this->main->games[$this->world->getLevel()->getName()] = new UHCGame($this->main, $this->world);
$this->close();
$this->seconds = -1;
break; break;
} }
$this->seconds--; $this->seconds--;
} }
} }
}