PocketMine-MP 5.15.1 git-ed158f8a1b0cfe334ac5f45febc0f633602014f2
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
22declare(strict_types=1);
23
24namespace pocketmine\plugin;
25
35use Symfony\Component\Filesystem\Path;
36use function copy;
37use function count;
38use function dirname;
39use function file_exists;
40use function mkdir;
41use function rtrim;
42use function str_contains;
43use function strtolower;
44use function trim;
45use const DIRECTORY_SEPARATOR;
46
47abstract class PluginBase implements Plugin, CommandExecutor{
48 private bool $isEnabled = false;
49
50 private string $resourceFolder;
51
52 private ?Config $config = null;
53 private string $configFile;
54
55 private PluginLogger $logger;
56 private TaskScheduler $scheduler;
57
58 public function __construct(
59 private PluginLoader $loader,
60 private Server $server,
61 private PluginDescription $description,
62 private string $dataFolder,
63 private string $file,
64 private ResourceProvider $resourceProvider
65 ){
66 $this->dataFolder = rtrim($dataFolder, "/" . DIRECTORY_SEPARATOR) . "/";
67 //TODO: this is accessed externally via reflection, not unused
68 $this->file = rtrim($file, "/" . DIRECTORY_SEPARATOR) . "/";
69 $this->resourceFolder = Path::join($this->file, "resources") . "/";
70
71 $this->configFile = Path::join($this->dataFolder, "config.yml");
72
73 $prefix = $this->description->getPrefix();
74 $this->logger = new PluginLogger($server->getLogger(), $prefix !== "" ? $prefix : $this->getName());
75 $this->scheduler = new TaskScheduler($this->getFullName());
76
77 $this->onLoad();
78
79 $this->registerYamlCommands();
80 }
81
85 protected function onLoad() : void{
86
87 }
88
92 protected function onEnable() : void{
93
94 }
95
100 protected function onDisable() : void{
101
102 }
103
104 final public function isEnabled() : bool{
105 return $this->isEnabled;
106 }
107
115 final public function onEnableStateChange(bool $enabled) : void{
116 if($this->isEnabled !== $enabled){
117 $this->isEnabled = $enabled;
118 if($this->isEnabled){
119 $this->onEnable();
120 }else{
121 $this->onDisable();
122 }
123 }
124 }
125
126 final public function isDisabled() : bool{
127 return !$this->isEnabled;
128 }
129
130 final public function getDataFolder() : string{
131 return $this->dataFolder;
132 }
133
134 final public function getDescription() : PluginDescription{
135 return $this->description;
136 }
137
138 public function getLogger() : \AttachableLogger{
139 return $this->logger;
140 }
141
145 private function registerYamlCommands() : void{
146 $pluginCmds = [];
147
148 foreach(Utils::stringifyKeys($this->description->getCommands()) as $key => $data){
149 if(str_contains($key, ":")){
150 $this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_commandError($key, $this->description->getFullName(), ":")));
151 continue;
152 }
153
154 $newCmd = new PluginCommand($key, $this, $this);
155 if(($description = $data->getDescription()) !== null){
156 $newCmd->setDescription($description);
157 }
158
159 if(($usageMessage = $data->getUsageMessage()) !== null){
160 $newCmd->setUsage($usageMessage);
161 }
162
163 $aliasList = [];
164 foreach($data->getAliases() as $alias){
165 if(str_contains($alias, ":")){
166 $this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_aliasError($alias, $this->description->getFullName(), ":")));
167 continue;
168 }
169 $aliasList[] = $alias;
170 }
171
172 $newCmd->setAliases($aliasList);
173
174 $newCmd->setPermission($data->getPermission());
175
176 if(($permissionDeniedMessage = $data->getPermissionDeniedMessage()) !== null){
177 $newCmd->setPermissionMessage($permissionDeniedMessage);
178 }
179
180 $pluginCmds[] = $newCmd;
181 }
182
183 if(count($pluginCmds) > 0){
184 $this->server->getCommandMap()->registerAll($this->description->getName(), $pluginCmds);
185 }
186 }
187
192 public function getCommand(string $name){
193 $command = $this->server->getPluginCommand($name);
194 if($command === null || $command->getOwningPlugin() !== $this){
195 $command = $this->server->getPluginCommand(strtolower($this->description->getName()) . ":" . $name);
196 }
197
198 if($command instanceof PluginOwned && $command->getOwningPlugin() === $this){
199 return $command;
200 }else{
201 return null;
202 }
203 }
204
208 public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{
209 return false;
210 }
211
216 public function getResourceFolder() : string{
217 return $this->resourceFolder;
218 }
219
226 public function getResourcePath(string $filename) : string{
227 return Path::join($this->getResourceFolder(), $filename);
228 }
229
239 public function getResource(string $filename){
240 return $this->resourceProvider->getResource($filename);
241 }
242
246 public function saveResource(string $filename, bool $replace = false) : bool{
247 if(trim($filename) === ""){
248 return false;
249 }
250
251 $source = Path::join($this->resourceFolder, $filename);
252 if(!file_exists($source)){
253 return false;
254 }
255
256 $destination = Path::join($this->dataFolder, $filename);
257 if(file_exists($destination) && !$replace){
258 return false;
259 }
260
261 if(!file_exists(dirname($destination))){
262 mkdir(dirname($destination), 0755, true);
263 }
264
265 return copy($source, $destination);
266 }
267
273 public function getResources() : array{
274 return $this->resourceProvider->getResources();
275 }
276
277 public function getConfig() : Config{
278 if($this->config === null){
279 $this->reloadConfig();
280 }
281
282 return $this->config;
283 }
284
285 public function saveConfig() : void{
286 $this->getConfig()->save();
287 }
288
289 public function saveDefaultConfig() : bool{
290 if(!file_exists($this->configFile)){
291 return $this->saveResource("config.yml", false);
292 }
293 return false;
294 }
295
296 public function reloadConfig() : void{
297 $this->saveDefaultConfig();
298 $this->config = new Config($this->configFile);
299 }
300
301 final public function getServer() : Server{
302 return $this->server;
303 }
304
305 final public function getName() : string{
306 return $this->description->getName();
307 }
308
309 final public function getFullName() : string{
310 return $this->description->getFullName();
311 }
312
313 protected function getFile() : string{
314 return $this->file;
315 }
316
317 public function getPluginLoader() : PluginLoader{
318 return $this->loader;
319 }
320
321 public function getScheduler() : TaskScheduler{
322 return $this->scheduler;
323 }
324}
getResourcePath(string $filename)
Definition: PluginBase.php:226
saveResource(string $filename, bool $replace=false)
Definition: PluginBase.php:246
onEnableStateChange(bool $enabled)
Definition: PluginBase.php:115
onCommand(CommandSender $sender, Command $command, string $label, array $args)
Definition: PluginBase.php:208
getResource(string $filename)
Definition: PluginBase.php:239