Adding commands !

This commit is contained in:
Ad5001 2016-08-04 15:35:44 +03:00
parent c913fd1580
commit b44bda9bf0
4 changed files with 70 additions and 5 deletions

View file

@ -162,6 +162,9 @@ abstract class Game {
} }
public function onCommand(\pocketmine\command\CommandSender $sender, \pocketmine\command\Command $cmd, $label, array $args) {}
abstract public function getName() : string; abstract public function getName() : string;
@ -177,4 +180,12 @@ abstract class Game {
} }
public function registerCommand(string $cmd, string $desc, string $usage, array $aliases, string $perm = "gamemanager.command.use") {
if(!isset($this->main->cmds[$cmd])) {
$this->main->cmds[$cmd] = new GameCommand($this->main, $cmd, $desc, $usage, $aliases, $this->gm->getGames()[$this->getName()], $perm);
$this->getServer()->getCommandMap()->register($cmd, $this->main->cmds[$cmds]);
}
}
} }

View file

@ -0,0 +1,21 @@
<?php
namespace Ad5001\GameManager;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\PluginIdentifiableCommand;
use pocketmine\Player;
class GameCommand extends Command implements PluginIdentifiableCommand {
public function __construct(Main $main, string $name, string $desc, string $usage, array $aliases, Game $game, string $perm){
parent::__construct($name, $desc, $usage, $aliases);
$this->setPermission($perm);
$this->main = $main;
$this->game = $game;
}
public function execute(CommandSender $sender, $label, array $args) {
return $this->game->onCommand($sender, $this, $label, $args);
}
}

View file

@ -5,8 +5,6 @@ use pocketmine\Server;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\level\Level; use pocketmine\level\Level;
use Ad5001\GameManager\Main;
class GameManager { class GameManager {
@ -30,8 +28,8 @@ class GameManager {
if(!is_dir($this->main->getDataFolder() . "/games/" . $file)) { if(!is_dir($this->main->getDataFolder() . "/games/" . $file)) {
require($this->main->getDataFolder() . "/games/" . $file); require($this->main->getDataFolder() . "/games/" . $file);
$classn = $this->main->getClasses(file_get_contents($this->main->getDataFolder() . "/games/" . $file)); $classn = $this->main->getClasses(file_get_contents($this->main->getDataFolder() . "/games/" . $file));
$this->games[explode(".php", $file)[0]] = $classn; $this->games[explode("\\", $classn)[count(explode("\\", $classn)) - 1]] = $classn;
@mkdir($this->main->getDataFolder() . "games/" . explode(".php", $file)[0]); @mkdir($this->main->getDataFolder() . "games/" . explode("\\", $classn)[count(explode("\\", $classn)) - 1]);
} }
} }
} }

View file

@ -49,9 +49,44 @@ class Main extends PluginBase implements Listener {
public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){ public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
switch($cmd->getName()){ switch($cmd->getName()){
case "default": case "games":
if(!isset($args[0])) {
$games = [];
foreach($this->manager->getGames() as $g) {
array_push($games, explode("\\", $g)[count(explode("\\", $g)) - 1]);
}
$sender->sendMessage("§l§o[§r§lGameManager§o]§r Current existings games: " . implode(", ", $games) . ". \nUse /games <game_name> to get all the levels of a game.");
} else {
$sender->sendMessage("§l§o[§r§lGameManager§o]§r Current levels running $args[0]:");
foreach($this->manager->getLevels() as $levelname => $game) {
if(strtolower($game->getName()) == strtolower($args[0])) {
$p = 0;
$s = 0;
foreach($game->getLevel()->getPlayers() as $pl) {
if($this->getServer()->getPluginManager()->getPlugin("SpectatorPlus") !== null) {
if($this->getServer()->getPluginManager()->getPlugin("SpectatorPlus")->isSpectator($pl)) {
array_push($s, $pl);
} else {
array_push($p, $pl);
}
} else {
if($pl->isSpectator()) {
array_push($s, $pl);
} else {
array_push($p, $pl);
}
}
}
$sender->sendMessage("§l§o[§r§lGameManager§o]§r§a " . $levelname . " Is started: " . $game->isStarted() . " Players: " . count($p) . " Spectators: " . count($s));
}
}
}
return true;
break; break;
} }
if(isset($this->cmds[$cmd->getName()])) {
$this->cmds[$cmd->getName()]->onCommand($sender, $cmd, $label, $args);
}
return false; return false;
} }