This commit is contained in:
Ad5001 2016-07-28 09:39:24 +03:00
parent 2f2f627f19
commit 3e400f65c4
5 changed files with 197 additions and 0 deletions

9
plugin.yml Normal file
View file

@ -0,0 +1,9 @@
---
name: GameManager
author: Ad5001
version: 1.0
api: [2.0.0]
main: Ad5001\GameManager\Main
commands: []
permissions: []
...

3
resources/config.yml Normal file
View file

@ -0,0 +1,3 @@
---
# This is the default config generated with ImagicalPlugCreator. (C) ImagicalPlugCreator - Ad5001 2016
...

View file

@ -0,0 +1,85 @@
<?php
namespace Ad5001\GameManager;
use pocketmine\Server;
use pocketmine\Player;
use pocketmine\block\Block;
use pocketmine\utils\Config;
use Ad5001\GameManager\Main;
abstract class Game {
protected $name;
protected $level;
public function __construct(string $name, Level $level) {
$this->server = $level->getServer();
$this->level = $level;
$this->name = $name;
$this->main = $this->server->getPlugin("GameManager");
}
public function getPlugin() {
return $this->main;
}
public function getLevel() {
return $this->main;
}
public function getLevel() {
return $this->main;
}
public function onGameStart();
public function stopGame();
public function onJoin(Player $player) {}
public function onQuit(Player $player) {}
public function onBlockBreak(Player $player, Block $block) {}
public function onBlockPlace(Player $player, Block $block) {}
public function getConfig() {
return new Config($this->main->getDataFolder() . "games/$this->name");
}
public function saveDefaultConfig() {
$this->main->saveResource("/games/$this->name/config.yml");
}
public function getName() : string;
public function getMinPlayers() : int;
public function getMaxPlayers() : int;
public function useEvent(\pocketmine\event\Event $event) : bool;
}

View file

@ -0,0 +1,50 @@
<?php
namespace Ad5001\GameManager;
use pocketmine\Server;
use pocketmine\Player;
use pocketmine\level\Level;
use Ad5001\GameManager\Main;
class GameManager {
public function __construct(Main $main) {
$this->main = $main;
$this->server = $main->getServer();
$files = array_diff(scandir($this->getDataFolder() . "/games"), [".", ".."]);
$this->games = [];
$this->levels = [];
$this->startedgames = [];
foreach ($files as $file) {
require($file);
$classn = getClasses(file_get_contents($this->getDataFolder() . "/games/" . $file));
$this->games[explode(".php", $file)[0]] = $classn;
@mkdir($this->main->getDataFolder() . "games/$classn");
}
}
public function startGame(Level $level) {
if(isset($this->levels[$level->getName()]) and !isset($this->startedgames[$level->getName()])) {
$this->startedgames[$level->getName()] = true;
$this->levels[$level->getName()]->onGameStart();
return true;
}
return false;
}
public function registerLevel(Level $level, string $game) {
if(!array_key_exists($level->getName(), $this->levels)) {
if(isset($this->games[$game])) {
$this->levels[$level->getName()] = new $this->games[$game]($level)
}
}
}
}

View file

@ -0,0 +1,50 @@
<?php
namespace Ad5001\GameManager;
use pocketmine\command\CommandSender;
use pocketmine\command\Command;
use pocketmine\event\Listener;
use pocketmine\plugin\PluginBase;
use pocketmine\Server;
use pocketmine\Player;
use Ad5001\GameManager\GameManager;
class Main extends PluginBase{
public function onEnable(){
$this->reloadConfig();
$this->getServer()->getPluginManager()->registerEvents($this, $this);
$this->manager = new GameManager($this);
}
public function onLoad(){
$this->saveDefaultConfig();
}
public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
switch($cmd->getName()){
case "default":
break;
}
return false;
}
public function getClasses(string $file) {
$tokens = token_get_all($php_file);
$class_token = false;
foreach ($tokens as $token) {
if (is_array($token)) {
if ($token[0] == T_CLASS) {
$class_token = true;
} else if ($class_token && $token[0] == T_STRING) {
return $token[1];
}
}
}
}
}