PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
PluginBase.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\plugin;
23 
29 
30 abstract class PluginBase implements Plugin{
31 
33  private $loader;
34 
36  private $server;
37 
39  private $isEnabled = false;
40 
42  private $initialized = false;
43 
45  private $description;
46 
48  private $dataFolder;
49  private $config;
51  private $configFile;
52  private $file;
53 
55  private $logger;
56 
60  public function onLoad(){
61 
62  }
63 
64  public function onEnable(){
65 
66  }
67 
68  public function onDisable(){
69 
70  }
71 
75  public final function isEnabled(){
76  return $this->isEnabled === true;
77  }
78 
82  public final function setEnabled($boolean = true){
83  if($this->isEnabled !== $boolean){
84  $this->isEnabled = $boolean;
85  if($this->isEnabled === true){
86  $this->onEnable();
87  }else{
88  $this->onDisable();
89  }
90  }
91  }
92 
96  public final function isDisabled(){
97  return $this->isEnabled === false;
98  }
99 
100  public final function getDataFolder(){
101  return $this->dataFolder;
102  }
103 
104  public final function getDescription(){
105  return $this->description;
106  }
107 
108  public final function init(PluginLoader $loader, Server $server, PluginDescription $description, $dataFolder, $file){
109  if($this->initialized === false){
110  $this->initialized = true;
111  $this->loader = $loader;
112  $this->server = $server;
113  $this->description = $description;
114  $this->dataFolder = rtrim($dataFolder, "\\/") . "/";
115  $this->file = rtrim($file, "\\/") . "/";
116  $this->configFile = $this->dataFolder . "config.yml";
117  $this->logger = new PluginLogger($this);
118  }
119  }
120 
124  public function getLogger(){
125  return $this->logger;
126  }
127 
131  public final function isInitialized(){
132  return $this->initialized;
133  }
134 
140  public function getCommand($name){
141  $command = $this->getServer()->getPluginCommand($name);
142  if($command === null or $command->getPlugin() !== $this){
143  $command = $this->getServer()->getPluginCommand(strtolower($this->description->getName()) . ":" . $name);
144  }
145 
146  if($command instanceof PluginIdentifiableCommand and $command->getPlugin() === $this){
147  return $command;
148  }else{
149  return null;
150  }
151  }
152 
161  public function onCommand(CommandSender $sender, Command $command, $label, array $args){
162  return false;
163  }
164 
168  protected function isPhar(){
169  return substr($this->file, 0, 7) === "phar://";
170  }
171 
179  public function getResource($filename){
180  $filename = rtrim(str_replace("\\", "/", $filename), "/");
181  if(file_exists($this->file . "resources/" . $filename)){
182  return fopen($this->file . "resources/" . $filename, "rb");
183  }
184 
185  return null;
186  }
187 
194  public function saveResource($filename, $replace = false){
195  if(trim($filename) === ""){
196  return false;
197  }
198 
199  if(($resource = $this->getResource($filename)) === null){
200  return false;
201  }
202 
203  $out = $this->dataFolder . $filename;
204  if(!file_exists($this->dataFolder)){
205  @mkdir($this->dataFolder, 0755, true);
206  }
207 
208  if(file_exists($out) and $replace !== true){
209  return false;
210  }
211 
212  return stream_copy_to_stream($resource, fopen($out, "wb")) > 0;
213  }
214 
220  public function getResources(){
221  $resources = [];
222  if(is_dir($this->file . "resources/")){
223  foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->file . "resources/")) as $resource){
224  $resources[] = $resource;
225  }
226  }
227 
228  return $resources;
229  }
230 
234  public function getConfig(){
235  if(!isset($this->config)){
236  $this->reloadConfig();
237  }
238 
239  return $this->config;
240  }
241 
242  public function saveConfig(){
243  if($this->getConfig()->save() === false){
244  $this->getLogger()->critical("Could not save config to " . $this->configFile);
245  }
246  }
247 
248  public function saveDefaultConfig(){
249  if(!file_exists($this->configFile)){
250  $this->saveResource("config.yml", false);
251  }
252  }
253 
254  public function reloadConfig(){
255  $this->config = new Config($this->configFile);
256  if(($configStream = $this->getResource("config.yml")) !== null){
257  $this->config->setDefaults(yaml_parse(config::fixYAMLIndexes(stream_get_contents($configStream))));
258  }
259  }
260 
264  public final function getServer(){
265  return $this->server;
266  }
267 
271  public final function getName(){
272  return $this->description->getName();
273  }
274 
278  public final function getFullName(){
279  return $this->description->getFullName();
280  }
281 
282  protected function getFile(){
283  return $this->file;
284  }
285 
289  public function getPluginLoader(){
290  return $this->loader;
291  }
292 
293 }
saveResource($filename, $replace=false)
Definition: PluginBase.php:194
onCommand(CommandSender $sender, Command $command, $label, array $args)
Definition: PluginBase.php:161