PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/pocketmine/plugin/PluginBase.php

https://gitlab.com/matthww/Elywing
PHP | 298 lines | 155 code | 49 blank | 94 comment | 15 complexity | 51c56e86b165fed8ebf284b479ddf6bd MD5 | raw file
  1. <?php
  2. /*
  3. *
  4. * ____ _ _ __ __ _ __ __ ____
  5. * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
  6. * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
  7. * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
  8. * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * @author PocketMine Team
  16. * @link http://www.pocketmine.net/
  17. *
  18. *
  19. */
  20. namespace pocketmine\plugin;
  21. use pocketmine\command\Command;
  22. use pocketmine\command\CommandSender;
  23. use pocketmine\command\PluginIdentifiableCommand;
  24. use pocketmine\Server;
  25. use pocketmine\utils\Config;
  26. abstract class PluginBase implements Plugin{
  27. /** @var PluginLoader */
  28. private $loader;
  29. /** @var \pocketmine\Server */
  30. private $server;
  31. /** @var bool */
  32. private $isEnabled = false;
  33. /** @var bool */
  34. private $initialized = false;
  35. /** @var PluginDescription */
  36. private $description;
  37. /** @var string */
  38. private $dataFolder;
  39. private $config;
  40. /** @var string */
  41. private $configFile;
  42. private $file;
  43. /** @var PluginLogger */
  44. private $logger;
  45. /**
  46. * Called when the plugin is loaded, before calling onEnable()
  47. */
  48. public function onLoad(){
  49. }
  50. public function onEnable(){
  51. }
  52. public function onDisable(){
  53. }
  54. /**
  55. * @return bool
  56. */
  57. public final function isEnabled(){
  58. return $this->isEnabled === true;
  59. }
  60. /**
  61. * @param bool $boolean
  62. */
  63. public final function setEnabled($boolean = true){
  64. if($this->isEnabled !== $boolean){
  65. $this->isEnabled = $boolean;
  66. if($this->isEnabled === true){
  67. $this->onEnable();
  68. }else{
  69. $this->onDisable();
  70. }
  71. }
  72. }
  73. /**
  74. * @return bool
  75. */
  76. public final function isDisabled(){
  77. return $this->isEnabled === false;
  78. }
  79. public final function getDataFolder(){
  80. return $this->dataFolder;
  81. }
  82. public final function getDescription(){
  83. return $this->description;
  84. }
  85. public final function init(PluginLoader $loader, Server $server, PluginDescription $description, $dataFolder, $file){
  86. if($this->initialized === false){
  87. $this->initialized = true;
  88. $this->loader = $loader;
  89. $this->server = $server;
  90. $this->description = $description;
  91. $this->dataFolder = rtrim($dataFolder, "\\/") . "/";
  92. $this->file = rtrim($file, "\\/") . "/";
  93. $this->configFile = $this->dataFolder . "config.yml";
  94. $this->logger = new PluginLogger($this);
  95. }
  96. }
  97. /**
  98. * @return PluginLogger
  99. */
  100. public function getLogger(){
  101. return $this->logger;
  102. }
  103. /**
  104. * @return bool
  105. */
  106. public final function isInitialized(){
  107. return $this->initialized;
  108. }
  109. /**
  110. * @param string $name
  111. *
  112. * @return Command|PluginIdentifiableCommand
  113. */
  114. public function getCommand($name){
  115. $command = $this->getServer()->getPluginCommand($name);
  116. if($command === null or $command->getPlugin() !== $this){
  117. $command = $this->getServer()->getPluginCommand(strtolower($this->description->getName()) . ":" . $name);
  118. }
  119. if($command instanceof PluginIdentifiableCommand and $command->getPlugin() === $this){
  120. return $command;
  121. }else{
  122. return null;
  123. }
  124. }
  125. /**
  126. * @param CommandSender $sender
  127. * @param Command $command
  128. * @param string $label
  129. * @param array $args
  130. *
  131. * @return bool
  132. */
  133. public function onCommand(CommandSender $sender, Command $command, $label, array $args){
  134. return false;
  135. }
  136. /**
  137. * @return bool
  138. */
  139. protected function isPhar(){
  140. return substr($this->file, 0, 7) === "phar://";
  141. }
  142. /**
  143. * Gets an embedded resource on the plugin file.
  144. * WARNING: You must close the resource given using fclose()
  145. *
  146. * @param string $filename
  147. *
  148. * @return resource Resource data, or null
  149. */
  150. public function getResource($filename){
  151. $filename = rtrim(str_replace("\\", "/", $filename), "/");
  152. if(file_exists($this->file . "resources/" . $filename)){
  153. return fopen($this->file . "resources/" . $filename, "rb");
  154. }
  155. return null;
  156. }
  157. /**
  158. * @param string $filename
  159. * @param bool $replace
  160. *
  161. * @return bool
  162. */
  163. public function saveResource($filename, $replace = false){
  164. if(trim($filename) === ""){
  165. return false;
  166. }
  167. if(($resource = $this->getResource($filename)) === null){
  168. return false;
  169. }
  170. $out = $this->dataFolder . $filename;
  171. if(!file_exists(dirname($out))){
  172. mkdir(dirname($out), 0755, true);
  173. }
  174. if(file_exists($out) and $replace !== true){
  175. return false;
  176. }
  177. $ret = stream_copy_to_stream($resource, $fp = fopen($out, "wb")) > 0;
  178. fclose($fp);
  179. fclose($resource);
  180. return $ret;
  181. }
  182. /**
  183. * Returns all the resources incrusted on the plugin
  184. *
  185. * @return string[]
  186. */
  187. public function getResources(){
  188. $resources = [];
  189. if(is_dir($this->file . "resources/")){
  190. foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->file . "resources/")) as $resource){
  191. $resources[] = $resource;
  192. }
  193. }
  194. return $resources;
  195. }
  196. /**
  197. * @return Config
  198. */
  199. public function getConfig(){
  200. if(!isset($this->config)){
  201. $this->reloadConfig();
  202. }
  203. return $this->config;
  204. }
  205. public function saveConfig(){
  206. if($this->getConfig()->save() === false){
  207. $this->getLogger()->critical("Could not save config to " . $this->configFile);
  208. }
  209. }
  210. public function saveDefaultConfig(){
  211. if(!file_exists($this->configFile)){
  212. $this->saveResource("config.yml", false);
  213. }
  214. }
  215. public function reloadConfig(){
  216. $this->config = new Config($this->configFile);
  217. if(($configStream = $this->getResource("config.yml")) !== null){
  218. $this->config->setDefaults(yaml_parse(config::fixYAMLIndexes(stream_get_contents($configStream))));
  219. fclose($configStream);
  220. }
  221. }
  222. /**
  223. * @return Server
  224. */
  225. public final function getServer(){
  226. return $this->server;
  227. }
  228. /**
  229. * @return string
  230. */
  231. public final function getName(){
  232. return $this->description->getName();
  233. }
  234. /**
  235. * @return string
  236. */
  237. public final function getFullName(){
  238. return $this->description->getFullName();
  239. }
  240. protected function getFile(){
  241. return $this->file;
  242. }
  243. /**
  244. * @return PluginLoader
  245. */
  246. public function getPluginLoader(){
  247. return $this->loader;
  248. }
  249. }