Fixed wrong paths

This commit is contained in:
Shoghi Cervantes
2014-04-01 05:06:12 +02:00
parent 05a42712bf
commit dd17652aca
437 changed files with 41109 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\plugin;
use pocketmine\event\Event;
use pocketmine\event\Listener;
interface EventExecutor{
/**
* @param Listener $listener
* @param Event $event
*
* @return void
*/
public function execute(Listener $listener, Event $event);
}

View File

@@ -0,0 +1,147 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\plugin;
use pocketmine\event\plugin\PluginDisableEvent;
use pocketmine\event\plugin\PluginEnableEvent;
use pocketmine\Server;
/**
* Handles different types of plugins
*/
class FolderPluginLoader implements PluginLoader{
/** @var Server */
private $server;
/**
* @param Server $server
*/
public function __construct(Server $server){
$this->server = $server;
}
/**
* Loads the plugin contained in $file
*
* @param string $file
*
* @return Plugin
*/
public function loadPlugin($file){
if(is_dir($file) and file_exists($file . "/plugin.yml") and file_exists($file . "/src/")){
if(($description = $this->getPluginDescription($file)) instanceof PluginDescription){
console("[INFO] Loading " . $description->getFullName());
console("[WARNING] Non-packaged plugin ".$description->getName() ." detected, do not use on production.");
$dataFolder = dirname($file) . DIRECTORY_SEPARATOR . $description->getName();
if(file_exists($dataFolder) and !is_dir($dataFolder)){
trigger_error("Projected dataFolder '" . $dataFolder . "' for " . $description->getName() . " exists and is not a directory", E_USER_WARNING);
return null;
}
$className = $description->getMain();
$this->server->getLoader()->add(substr($className, 0, strpos($className, "\\")), array(
$file . "/src"
));
if(class_exists($className, true)){
$plugin = new $className();
$this->initPlugin($plugin, $description, $dataFolder, $file);
return $plugin;
}else{
trigger_error("Couldn't load plugin " . $description->getName() . ": main class not found", E_USER_WARNING);
return null;
}
}
}
return null;
}
/**
* Gets the PluginDescription from the file
*
* @param string $file
*
* @return PluginDescription
*/
public function getPluginDescription($file){
if(is_dir($file) and file_exists($file . "/plugin.yml")){
$yaml = @file_get_contents($file . "/plugin.yml");
if($yaml != ""){
return new PluginDescription($yaml);
}
}
return null;
}
/**
* Returns the filename patterns that this loader accepts
*
* @return array
*/
public function getPluginFilters(){
return "/[^\\.]/";
}
/**
* @param PluginBase $plugin
* @param PluginDescription $description
* @param string $dataFolder
* @param string $file
*/
private function initPlugin(PluginBase $plugin, PluginDescription $description, $dataFolder, $file){
$plugin->init($this, $this->server, $description, $dataFolder, $file);
$plugin->onLoad();
}
/**
* @param Plugin $plugin
*/
public function enablePlugin(Plugin $plugin){
if($plugin instanceof PluginBase and !$plugin->isEnabled()){
console("[INFO] Enabling " . $plugin->getDescription()->getFullName());
$plugin->setEnabled(true);
Server::getInstance()->getPluginManager()->callEvent(new PluginEnableEvent($plugin));
}
}
/**
* @param Plugin $plugin
*/
public function disablePlugin(Plugin $plugin){
if($plugin instanceof PluginBase and $plugin->isEnabled()){
console("[INFO] Disabling " . $plugin->getDescription()->getFullName());
Server::getInstance()->getPluginManager()->callEvent(new PluginDisableEvent($plugin));
$plugin->setEnabled(false);
}
}
}

View File

@@ -0,0 +1,38 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\plugin;
use pocketmine\event\Event;
use pocketmine\event\Listener;
class MethodEventExecutor implements EventExecutor{
private $method;
public function __construct($method){
$this->method = $method;
}
public function execute(Listener $listener, Event $event){
call_user_func(array($listener, $this->method), $event);
}
}

View File

@@ -0,0 +1,145 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\plugin;
use pocketmine\event\plugin\PluginDisableEvent;
use pocketmine\event\plugin\PluginEnableEvent;
use pocketmine\Server;
/**
* Handles different types of plugins
*/
class PharPluginLoader implements PluginLoader{
/** @var Server */
private $server;
/**
* @param Server $server
*/
public function __construct(Server $server){
$this->server = $server;
}
/**
* Loads the plugin contained in $file
*
* @param string $file
*
* @return Plugin
*/
public function loadPlugin($file){
if(\Phar::isValidPharFilename($file) and ($description = $this->getPluginDescription($file)) instanceof PluginDescription){
console("[INFO] Loading " . $description->getFullName());
$dataFolder = dirname($file) . DIRECTORY_SEPARATOR . $description->getName();
if(file_exists($dataFolder) and !is_dir($dataFolder)){
trigger_error("Projected dataFolder '" . $dataFolder . "' for " . $description->getName() . " exists and is not a directory", E_USER_WARNING);
return null;
}
$file = "phar://$file";
$className = $description->getMain();
$this->server->getLoader()->add(substr($className, 0, strpos($className, "\\")), array(
"$file/src"
));
if(class_exists($className, true)){
$plugin = new $className();
$this->initPlugin($plugin, $description, $dataFolder, $file);
return $plugin;
}else{
trigger_error("Couldn't load plugin " . $description->getName() . ": main class not found", E_USER_WARNING);
return null;
}
}
return null;
}
/**
* Gets the PluginDescription from the file
*
* @param string $file
*
* @return PluginDescription
*/
public function getPluginDescription($file){
if(\Phar::isValidPharFilename($file)){
$phar = new \Phar($file);
if(isset($phar["plugin.yml"])){
$pluginYml = $phar["plugin.yml"];
if($pluginYml instanceof \PharFileInfo){
return new PluginDescription($pluginYml->getContent());
}
}
}
return null;
}
/**
* Returns the filename patterns that this loader accepts
*
* @return array
*/
public function getPluginFilters(){
return "/\\.phar$/i";
}
/**
* @param PluginBase $plugin
* @param PluginDescription $description
* @param string $dataFolder
* @param string $file
*/
private function initPlugin(PluginBase $plugin, PluginDescription $description, $dataFolder, $file){
$plugin->init($this, $this->server, $description, $dataFolder, $file);
$plugin->onLoad();
}
/**
* @param Plugin $plugin
*/
public function enablePlugin(Plugin $plugin){
if($plugin instanceof PluginBase and !$plugin->isEnabled()){
console("[INFO] Enabling " . $plugin->getDescription()->getFullName());
$plugin->setEnabled(true);
Server::getInstance()->getPluginManager()->callEvent(new PluginEnableEvent($plugin));
}
}
/**
* @param Plugin $plugin
*/
public function disablePlugin(Plugin $plugin){
if($plugin instanceof PluginBase and $plugin->isEnabled()){
console("[INFO] Disabling " . $plugin->getDescription()->getFullName());
Server::getInstance()->getPluginManager()->callEvent(new PluginDisableEvent($plugin));
$plugin->setEnabled(false);
}
}
}

View File

@@ -0,0 +1,83 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
/**
* Plugin related classes
*/
namespace pocketmine\plugin;
/**
* It is recommended to use PluginBase for the actual plugin
*
*/
interface Plugin{
/**
* Called when the plugin is loaded, before calling onEnable()
*/
public function onLoad();
public function onEnable();
public function isEnabled();
public function onDisable();
public function isDisabled();
public function getDataFolder();
/**
* @return PluginDescription
*/
public function getDescription();
/**
* Gets an embedded resource in the plugin file.
*/
public function getResource($filename);
public function saveResource($filename, $replace = false);
/**
* Returns all the resources incrusted in the plugin
*/
public function getResources();
public function getConfig();
public function saveConfig();
public function saveDefaultConfig();
public function reloadConfig();
public function getServer();
public function getName();
/**
* @return PluginLoader
*/
public function getPluginLoader();
}

View File

@@ -0,0 +1,265 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\plugin;
use pocketmine\command\Command;
use pocketmine\command\CommandExecutor;
use pocketmine\command\CommandSender;
use pocketmine\command\PluginCommand;
use pocketmine\Server;
use pocketmine\utils\Config;
abstract class PluginBase implements Plugin, CommandExecutor{
/** @var PluginLoader */
private $loader;
/** @var \pocketmine\Server */
private $server;
/** @var bool */
private $isEnabled = false;
/** @var bool */
private $initialized = false;
/** @var PluginDescription */
private $description;
/** @var string */
private $dataFolder;
private $config;
/** @var string */
private $configFile;
private $file;
/**
* Called when the plugin is loaded, before calling onEnable()
*/
public function onLoad(){
}
public function onEnable(){
}
public function onDisable(){
}
/**
* @return bool
*/
public final function isEnabled(){
return $this->isEnabled === true;
}
/**
* @param bool $boolean
*/
public final function setEnabled($boolean = true){
if($this->isEnabled !== $boolean){
$this->isEnabled = $boolean;
if($this->isEnabled === true){
$this->onEnable();
}else{
$this->onDisable();
}
}
}
/**
* @return bool
*/
public final function isDisabled(){
return $this->isEnabled === false;
}
public final function getDataFolder(){
return $this->dataFolder;
}
public final function getDescription(){
return $this->description;
}
public final function init(PluginLoader $loader, Server $server, PluginDescription $description, $dataFolder, $file){
if($this->initialized === false){
$this->initialized = true;
$this->loader = $loader;
$this->server = $server;
$this->description = $description;
$this->dataFolder = $dataFolder;
$this->file = $file;
$this->configFile = $this->dataFolder . "config.yml";
}
}
public final function isInitialized(){
return $this->initialized;
}
/**
* @param CommandSender $sender
* @param Command $command
* @param string $label
* @param string[] $args
*
* @return bool
*/
public function onCommand(CommandSender $sender, Command $command, $label, array $args){
return false;
}
public function getCommand($name){
$command = $this->getServer()->getPluginCommand($name);
if($command === null or $command->getPlugin() !== $this){
$command = $this->getServer()->getPluginCommand(strtolower($this->description->getName()) . ":" . $name);
}
if($command instanceof PluginCommand and $command->getPlugin() === $this){
return $command;
}else{
return null;
}
}
/**
* @return bool
*/
protected function isPhar(){
return substr($this->file, 0, 7) === "phar://";
}
/**
* Gets an embedded resource on the plugin file.
*
* @param string $filename
*
* @return bool|string Resource data, or false
*/
public function getResource($filename){
$filename = str_replace("\\", "/", $filename);
if(file_exists($this->file . "resources/" . $filename)){
return file_get_contents($this->file . "resources/" . $filename);
}
return false;
}
/**
* @param string $filename
* @param bool $replace
*
* @return bool
*/
public function saveResource($filename, $replace = false){
if(trim($filename) === ""){
return false;
}
if(($resource = $this->getResource($filename)) === false){
return false;
}
$out = $this->file . $filename;
if(!file_exists($this->dataFolder)){
@mkdir($this->dataFolder, 0755, true);
}
if(file_exists($out) and $replace !== true){
return false;
}
return @file_put_contents($out, $resource) !== false;
}
/**
* Returns all the resources incrusted on the plugin
*
* @return string[]
*/
public function getResources(){
if(!$this->isPhar()){
$resources = array();
foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->file . "resources/")) as $resource){
$resources[] = $resource;
}
return $resources;
}
}
public function getConfig(){
if(!isset($this->config)){
$this->reloadConfig();
}
return $this->config;
}
public function saveConfig(){
if($this->getConfig()->save() === false){
console("[SEVERE] Could not save config to " . $this->configFile);
}
}
public function saveDefaultConfig(){
if(!file_exists($this->configFile)){
$this->saveResource("config.yml", false);
}
}
public function reloadConfig(){
$this->config = new Config($this->configFile);
if(($configStream = $this->getResource("config.yml")) !== false){
$this->config->setDefaults(yaml_parse(config::fixYAMLIndexes($configStream)));
}
}
/**
* @return Server
*/
public final function getServer(){
return $this->server;
}
/**
* @return string
*/
public final function getName(){
return $this->description->getName();
}
protected function getFile(){
return $this->file;
}
/**
* @return PluginLoader
*/
public function getPluginLoader(){
return $this->loader;
}
}

View File

@@ -0,0 +1,211 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\plugin;
use pocketmine\permission\Permission;
class PluginDescription{
private $name;
private $main;
private $api;
private $depend = array();
private $softDepend = array();
private $loadBefore = array();
private $version;
private $commands = array();
private $description = null;
private $authors = array();
private $website = null;
private $order = PluginLoadOrder::POSTWORLD;
/**
* @var Permission[]
*/
private $permissions = array();
/**
* @param string $yamlString
*/
public function __construct($yamlString){
$this->loadMap(\yaml_parse($yamlString));
}
private function loadMap(array $plugin){
$this->name = preg_replace("[^A-Za-z0-9 _.-]", "", $plugin["name"]);
if($this->name === ""){
trigger_error("Invalid PluginDescription name", E_USER_WARNING);
return;
}
$this->name = str_replace(" ", "_", $this->name);
$this->version = $plugin["version"];
$this->main = $plugin["main"];
$this->api = !is_array($plugin["api"]) ? array($plugin["api"]) : $plugin["api"];
if(stripos($this->main, "pocketmine\\") === 0){
trigger_error("Invalid PluginDescription main, cannot start within the PocketMine namespace", E_USER_ERROR);
return;
}
if(isset($plugin["commands"]) and is_array($plugin["commands"])){
$this->commands = $plugin["commands"];
}
if(isset($plugin["depend"])){
$this->depend = (array) $plugin["depend"];
}
if(isset($plugin["softdepend"])){
$this->softDepend = (array) $plugin["softdepend"];
}
if(isset($plugin["loadbefore"])){
$this->loadBefore = (array) $plugin["loadbefore"];
}
if(isset($plugin["website"])){
$this->website = $plugin["website"];
}
if(isset($plugin["description"])){
$this->description = $plugin["description"];
}
if(isset($plugin["load"])){
$order = strtoupper($plugin["load"]);
if(!defined("pocketmine\\plugin\\PluginLoadOrder::" . $order)){
trigger_error("Invalid PluginDescription load", E_USER_ERROR);
return;
}else{
$this->order = constant("pocketmine\\plugin\\PluginLoadOrder::" . $order);
}
}
$this->authors = array();
if(isset($plugin["author"])){
$this->authors[] = $plugin["author"];
}
if(isset($plugin["authors"])){
foreach($plugin["authors"] as $author){
$this->authors[] = $author;
}
}
if(isset($plugin["permissions"])){
$this->permissions = Permission::loadPermissions($plugin["permissions"]);
}
}
/**
* @return string
*/
public function getFullName(){
return $this->name . " v" . $this->version;
}
/**
* @return array
*/
public function getCompatibleApis(){
return $this->api;
}
/**
* @return array
*/
public function getAuthors(){
return $this->authors;
}
/**
* @return array
*/
public function getCommands(){
return $this->commands;
}
/**
* @return array
*/
public function getDepend(){
return $this->depend;
}
/**
* @return string
*/
public function getDescription(){
return $this->description;
}
/**
* @return array
*/
public function getLoadBefore(){
return $this->loadBefore;
}
/**
* @return string
*/
public function getMain(){
return $this->main;
}
/**
* @return string
*/
public function getName(){
return $this->name;
}
/**
* @return int
*/
public function getOrder(){
return $this->order;
}
/**
* @return Permission[]
*/
public function getPermissions(){
return $this->permissions;
}
/**
* @return array
*/
public function getSoftDepend(){
return $this->softDepend;
}
/**
* @return string
*/
public function getVersion(){
return $this->version;
}
/**
* @return string
*/
public function getWebsite(){
return $this->website;
}
}

View File

@@ -0,0 +1,35 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\plugin;
abstract class PluginLoadOrder{
/*
* The plugin will be loaded at startup
*/
const STARTUP = 0;
/*
* The plugin will be loaded after the first world has been loaded/created.
*/
const POSTWORLD = 1;
}

View File

@@ -0,0 +1,69 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\plugin;
/**
* Handles different types of plugins
*/
interface PluginLoader{
/**
* Loads the plugin contained in $file
*
* @param string $file
*
* @return Plugin
*/
public function loadPlugin($file);
/**
* Gets the PluginDescription from the file
*
* @param string $file
*
* @return PluginDescription
*/
public function getPluginDescription($file);
/**
* Returns the filename patterns that this loader accepts
*
* @return string[]
*/
public function getPluginFilters();
/**
* @param Plugin $plugin
*
* @return void
*/
public function enablePlugin(Plugin $plugin);
/**
* @param Plugin $plugin
*
* @return void
*/
public function disablePlugin(Plugin $plugin);
}

View File

@@ -0,0 +1,686 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\plugin;
use pocketmine\command\PluginCommand;
use pocketmine\command\SimpleCommandMap;
use pocketmine\event\Event;
use pocketmine\event\EventPriority;
use pocketmine\event\HandlerList;
use pocketmine\event\Listener;
use pocketmine\permission\Permissible;
use pocketmine\permission\Permission;
use pocketmine\Server;
/**
* Manages all the plugins, Permissions and Permissibles
*/
class PluginManager{
/** @var PluginManager */
private static $instance = null;
/** @var Server */
private $server;
/** @var SimpleCommandMap */
private $commandMap;
/**
* @var Plugin[]
*/
protected $plugins = array();
/**
* @var Permission[]
*/
protected $permissions = array();
/**
* @var Permission[]
*/
protected $defaultPerms = array();
/**
* @var Permission[]
*/
protected $defaultPermsOp = array();
/**
* @var Permissible[]
*/
protected $permSubs = array();
/**
* @var Permissible[]
*/
protected $defSubs = array();
/**
* @var Permissible[]
*/
protected $defSubsOp = array();
/**
* @var PluginLoader[]
*/
protected $fileAssociations = array();
/**
* @return PluginManager
*/
public static function getInstance(){
return self::$instance;
}
/**
* @param Server $server
* @param SimpleCommandMap $commandMap
*/
public function __construct(Server $server, SimpleCommandMap $commandMap){
$this->server = $server;
$this->commandMap = $commandMap;
}
/**
* @param string $name
*
* @return null|Plugin
*/
public function getPlugin($name){
if(isset($this->plugins[$name])){
return $this->plugins[$name];
}
return null;
}
/**
* @param string $loader A PluginLoader class name
*
* @return boolean
*/
public function registerInterface($loader){
if(is_subclass_of($loader, "pocketmine\\plugin\\PluginLoader")){
$loader = new $loader($this->server);
}else{
return false;
}
$this->fileAssociations[spl_object_hash($loader)] = $loader;
return true;
}
/**
* @return Plugin[]
*/
public function getPlugins(){
return $this->plugins;
}
/**
* @param string $path
*
* @return Plugin
*/
public function loadPlugin($path){
foreach($this->fileAssociations as $loader){
if(preg_match($loader->getPluginFilters(), basename($path)) > 0){
$description = $loader->getPluginDescription($path);
if($description instanceof PluginDescription){
if(($plugin = $loader->loadPlugin($path)) instanceof Plugin){
$this->plugins[$plugin->getDescription()->getName()] = $plugin;
return $plugin;
}
}
}
}
return null;
}
/**
* @param $directory
*
* @return Plugin[]
*/
public function loadPlugins($directory){
if(is_dir($directory)){
$plugins = array();
$loadedPlugins = array();
$dependencies = array();
$softDependencies = array();
foreach(new \IteratorIterator(new \DirectoryIterator($directory)) as $file){
if($file === "." or $file === ".."){
continue;
}
$file = $directory . $file;
foreach($this->fileAssociations as $loader){
if(preg_match($loader->getPluginFilters(), basename($file)) > 0){
$description = $loader->getPluginDescription($file);
if($description instanceof PluginDescription){
$name = $description->getName();
if(stripos($name, "pocketmine") !== false or stripos($name, "minecraft") !== false or stripos($name, "mojang") !== false){
console("[ERROR] Could not load plugin '" . $name . "': restricted name");
continue;
}elseif(strpos($name, " ") !== false){
console("[WARNING] Plugin '" . $name . "' uses spaces in its name, this is discouraged");
}
if(isset($plugins[$name])){
console("[ERROR] Could not load duplicate plugin '" . $name . "': plugin exists");
continue;
}
$compatible = false;
//Check multiple dependencies
foreach($description->getCompatibleApis() as $version){
//Format: majorVersion.minorVersion.patch
$version = array_map("intval", explode(".", $version));
$apiVersion = array_map("intval", explode(".", $this->server->getApiVersion()));
//Completely different API version
if($version[0] !== $apiVersion[0]){
continue;
}
//If the plugin requires new API features, being backwards compatible
if($version[1] > $apiVersion[1]){
continue;
}
$compatible = true;
break;
}
if($compatible === false){
console("[ERROR] Could not load plugin '" . $name . "': API version not compatible");
continue;
}
$plugins[$name] = $file;
$softDependencies[$name] = (array) $description->getSoftDepend();
$dependencies[$name] = (array) $description->getDepend();
foreach($description->getLoadBefore() as $before){
if(isset($softDependencies[$before])){
$softDependencies[$before][] = $name;
}else{
$softDependencies[$before] = array($name);
}
}
break;
}
}
}
}
while(count($plugins) > 0){
$missingDependency = true;
foreach($plugins as $name => $file){
if(isset($dependencies[$name])){
foreach($dependencies[$name] as $key => $dependency){
if(isset($loadedPlugins[$dependency])){
unset($dependencies[$name][$key]);
}elseif(!isset($plugins[$dependency])){
console("[SEVERE] Could not load plugin '" . $name . "': Unknown dependency");
break;
}
}
if(count($dependencies[$name]) === 0){
unset($dependencies[$name]);
}
}
if(isset($softDependencies[$name])){
foreach($softDependencies[$name] as $key => $dependency){
if(isset($loadedPlugins[$dependency])){
unset($softDependencies[$name][$key]);
}
}
if(count($softDependencies[$name]) === 0){
unset($softDependencies[$name]);
}
}
if(!isset($dependencies[$name]) and !isset($softDependencies[$name])){
unset($plugins[$name]);
$missingDependency = false;
if($plugin = $this->loadPlugin($file) and $plugin instanceof Plugin){
$loadedPlugins[$name] = $plugin;
}else{
console("[SEVERE] Could not load plugin '" . $name . "'");
}
}
}
if($missingDependency === true){
foreach($plugins as $name => $file){
if(!isset($dependencies[$name])){
unset($softDependencies[$name]);
unset($plugins[$name]);
$missingDependency = false;
if($plugin = $this->loadPlugin($file) and $plugin instanceof Plugin){
$loadedPlugins[$name] = $plugin;
}else{
console("[SEVERE] Could not load plugin '" . $name . "'");
}
}
}
//No plugins loaded :(
if($missingDependency === true){
foreach($plugins as $name => $file){
console("[SEVERE] Could not load plugin '" . $name . "': circular dependency detected");
}
$plugins = array();
}
}
}
return $loadedPlugins;
}else{
return array();
}
}
/**
* @param string $name
*
* @return null|Permission
*/
public function getPermission($name){
if(isset($this->permissions[$name])){
return $this->permissions[$name];
}
return null;
}
/**
* @param Permission $permission
*
* @return bool
*/
public function addPermission(Permission $permission){
if(!isset($this->permissions[$permission->getName()])){
$this->permissions[$permission->getName()] = $permission;
$this->calculatePermissionDefault($permission);
return true;
}
return false;
}
/**
* @param string|Permission $permission
*/
public function removePermission($permission){
if($permission instanceof Permission){
unset($this->permissions[$permission->getName()]);
}else{
unset($this->permissions[$permission]);
}
}
/**
* @param boolean $op
*
* @return Permission[]
*/
public function getDefaultPermissions($op){
if($op === true){
return $this->defaultPermsOp;
}else{
return $this->defaultPerms;
}
}
/**
* @param Permission $permission
*/
public function recalculatePermissionDefaults(Permission $permission){
if(isset($this->permissions[$permission->getName()])){
unset($this->defaultPermsOp[$permission->getName()]);
unset($this->defaultPerms[$permission->getName()]);
$this->calculatePermissionDefault($permission);
}
}
/**
* @param Permission $permission
*/
private function calculatePermissionDefault(Permission $permission){
if($permission->getDefault() === Permission::DEFAULT_OP or $permission->getDefault() === Permission::DEFAULT_TRUE){
$this->defaultPermsOp[$permission->getName()] = $permission;
$this->dirtyPermissibles(true);
}
if($permission->getDefault() === Permission::DEFAULT_NOT_OP or $permission->getDefault() === Permission::DEFAULT_TRUE){
$this->defaultPerms[$permission->getName()] = $permission;
$this->dirtyPermissibles(false);
}
}
/**
* @param boolean $op
*/
private function dirtyPermissibles($op){
foreach($this->getDefaultPermSubscriptions($op) as $p){
$p->recalculatePermissions();
}
}
/**
* @param string $permission
* @param Permissible $permissible
*/
public function subscribeToPermission($permission, Permissible $permissible){
if(!isset($this->permSubs[$permission])){
//TODO: Use WeakRef
$this->permSubs[$permission] = array();
}
$this->permSubs[$permission][spl_object_hash($permissible)] = $permissible;
}
/**
* @param string $permission
* @param Permissible $permissible
*/
public function unsubscribeFromPermission($permission, Permissible $permissible){
if(isset($this->permSubs[$permission])){
unset($this->permSubs[$permission][spl_object_hash($permissible)]);
}
}
/**
* @param string $permission
*
* @return Permissible[]
*/
public function getPermissionSubscriptions($permission){
if(isset($this->permSubs[$permission])){
return $this->permSubs[$permission];
}
return array();
}
/**
* @param boolean $op
* @param Permissible $permissible
*/
public function subscribeToDefaultPerms($op, Permissible $permissible){
if($op === true){
$this->defSubsOp[spl_object_hash($permissible)] = $permissible;
}else{
$this->defSubs[spl_object_hash($permissible)] = $permissible;
}
}
/**
* @param boolean $op
* @param Permissible $permissible
*/
public function unsubscribeFromDefaultPerms($op, Permissible $permissible){
if($op === true){
unset($this->defSubsOp[spl_object_hash($permissible)]);
}else{
unset($this->defSubs[spl_object_hash($permissible)]);
}
}
/**
* @param boolean $op
*
* @return Permissible[]
*/
public function getDefaultPermSubscriptions($op){
if($op === true){
return $this->defSubsOp;
}else{
return $this->defSubs;
}
}
/**
* @return Permission[]
*/
public function getPermissions(){
return $this->permissions;
}
/**
* @param Plugin $plugin
*
* @return bool
*/
public function isPluginEnabled(Plugin $plugin){
if($plugin instanceof Plugin and isset($this->plugins[$plugin->getDescription()->getName()])){
return $plugin->isEnabled();
}else{
return false;
}
}
/**
* @param Plugin $plugin
*/
public function enablePlugin(Plugin $plugin){
if(!$plugin->isEnabled()){
$pluginCommands = $this->parseYamlCommands($plugin);
if(count($pluginCommands) > 0){
$this->commandMap->registerAll($plugin->getDescription()->getName(), $pluginCommands);
}
foreach($plugin->getDescription()->getPermissions() as $perm){
$this->addPermission($perm);
}
$plugin->getPluginLoader()->enablePlugin($plugin);
}
}
/**
* @param Plugin $plugin
*
* @return PluginCommand[]
*/
protected function parseYamlCommands(Plugin $plugin){
$pluginCmds = array();
foreach($plugin->getDescription()->getCommands() as $key => $data){
if(strpos($key, ":") !== false){
console("[SEVERE] Could not load command " . $key . " for plugin " . $plugin->getDescription()->getName());
continue;
}
if(is_array($data)){
$newCmd = new PluginCommand($key, $plugin);
if(isset($data["description"])){
$newCmd->setDescription($data["description"]);
}
if(isset($data["usage"])){
$newCmd->setUsage($data["usage"]);
}
if(isset($data["aliases"]) and is_array($data["aliases"])){
$aliasList = array();
foreach($data["aliases"] as $alias){
if(strpos($alias, ":") !== false){
console("[SEVERE] Could not load alias " . $alias . " for plugin " . $plugin->getDescription()->getName());
continue;
}
$aliasList[] = $alias;
}
$newCmd->setAliases($aliasList);
}
if(isset($data["permission"])){
$newCmd->setPermission($data["permission"]);
}
if(isset($data["permission-message"])){
$newCmd->setPermissionMessage($data["permission-message"]);
}
$pluginCmds[] = $newCmd;
}
}
return $pluginCmds;
}
public function disablePlugins(){
foreach($this->getPlugins() as $plugin){
$this->disablePlugin($plugin);
}
}
/**
* @param Plugin $plugin
*/
public function disablePlugin(Plugin $plugin){
if($plugin->isEnabled()){
$plugin->getPluginLoader()->disablePlugin($plugin);
$this->server->getScheduler()->cancelTasks($plugin);
HandlerList::unregisterAll($plugin);
foreach($plugin->getDescription()->getPermissions() as $perm){
$this->removePermission($perm);
}
}
}
public function clearPlugins(){
$this->disablePlugins();
$this->plugins = array();
$this->fileAssociations = array();
$this->permissions = array();
$this->defaultPerms = array();
$this->defaultPermsOp = array();
}
/**
* Calls an event
*
* @param Event $event
*/
public function callEvent(Event $event){
$this->fireEvent($event);
}
private function fireEvent(Event $event){
$handlers = $event->getHandlers();
$listeners = $handlers->getRegisteredListeners();
foreach($listeners as $registration){
if(!$registration->getPlugin()->isEnabled()){
continue;
}
$registration->callEvent($event);
}
}
/**
* Registers all the events in the given Listener class
*
* @param Listener $listener
* @param Plugin $plugin
*/
public function registerEvents(Listener $listener, Plugin $plugin){
if(!$plugin->isEnabled()){
trigger_error("Plugin attempted to register ".get_class($listener)." while not enabled", E_USER_WARNING);
return;
}
$reflection = new \ReflectionClass(get_class($listener));
foreach($reflection->getMethods() as $method){
if(!$method->isStatic()){
$priority = EventPriority::NORMAL;
$ignoreCancelled = false;
if(preg_match("/^[\t ]*\\* @priority[\t ]{1,}([a-zA-Z]{1,})$/m", (string) $method->getDocComment(), $matches) > 0){
$matches[1] = strtoupper($matches[1]);
if(defined("pocketmine\\event\\EventPriority::".$matches[1])){
$priority = constant("pocketmine\\event\\EventPriority::".$matches[1]);
}
}
if(preg_match("/^[\t ]*\\* @ignoreCancelled[\t ]{1,}([a-zA-Z]{1,})$/m", (string) $method->getDocComment(), $matches) > 0){
$matches[1] = strtolower($matches[1]);
if($matches[1] === "false"){
$ignoreCancelled = false;
}elseif($matches[1] === "true"){
$ignoreCancelled = true;
}
}
$parameters = $method->getParameters();
if(count($parameters) === 1 and $parameters[0]->getClass() instanceof \ReflectionClass and is_subclass_of($parameters[0]->getClass()->getName(), "pocketmine\\event\\Event")){
$this->registerEvent($parameters[0]->getClass()->getName(), $listener, $priority, new MethodEventExecutor($method->getName()), $plugin, $ignoreCancelled);
}
}
}
}
/**
* @param string $event Class name that extends Event
* @param Listener $listener
* @param int $priority
* @param EventExecutor $executor
* @param Plugin $plugin
* @param bool $ignoreCancelled
*/
public function registerEvent($event, Listener $listener, $priority, EventExecutor $executor, Plugin $plugin, $ignoreCancelled = false){
if(!is_subclass_of($event, "pocketmine\\event\\Event")){
trigger_error($event . " is not a valid Event", E_USER_WARNING);
return;
}
if(!$plugin->isEnabled()){
trigger_error("Plugin attempted to register " . $event . " while not enabled");
return;
}
$this->getEventListeners($event)->register(new RegisteredListener($listener, $executor, $priority, $plugin, $ignoreCancelled));
}
/**
* @param string $event
*
* @return HandlerList
*/
private function getEventListeners($event){
if($event::$handlerList === null){
$event::$handlerList = new HandlerList();
}
return $event::$handlerList;
}
}

View File

@@ -0,0 +1,97 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\plugin;
use pocketmine\event\Cancellable;
use pocketmine\event\Event;
use pocketmine\event\Listener;
class RegisteredListener{
/** @var Listener */
private $listener;
/** @var int */
private $priority;
/** @var Plugin */
private $plugin;
/** @var EventExecutor */
private $executor;
/** @var bool */
private $ignoreCancelled;
/**
* @param Listener $listener
* @param EventExecutor $executor
* @param int $priority
* @param Plugin $plugin
* @param boolean $ignoreCancelled
*/
public function __construct(Listener $listener, EventExecutor $executor, $priority, Plugin $plugin, $ignoreCancelled){
$this->listener = $listener;
$this->priority = $priority;
$this->plugin = $plugin;
$this->executor = $executor;
$this->ignoreCancelled = $ignoreCancelled;
}
/**
* @return Listener
*/
public function getListener(){
return $this->listener;
}
/**
* @return Plugin
*/
public function getPlugin(){
return $this->plugin;
}
/**
* @return int
*/
public function getPriority(){
return $this->priority;
}
/**
* @param Event $event
*/
public function callEvent(Event $event){
if($event instanceof Cancellable and $event->isCancelled() and $this->isIgnoringCancelled()){
return;
}
$this->executor->execute($this->listener, $event);
}
/**
* @return bool
*/
public function isIgnoringCancelled(){
return $this->ignoreCancelled === true;
}
}