PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
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 fopen;
41use function is_dir;
42use function mkdir;
43use function rtrim;
44use function str_contains;
45use function str_replace;
46use function strlen;
47use function strtolower;
48use function substr;
49use function trim;
50use const DIRECTORY_SEPARATOR;
51
52abstract class PluginBase implements Plugin, CommandExecutor{
53 private bool $isEnabled = false;
54
55 private ?Config $config = null;
56 private string $configFile;
57
58 private PluginLogger $logger;
59 private TaskScheduler $scheduler;
60
61 public function __construct(
62 private PluginLoader $loader,
63 private Server $server,
64 private PluginDescription $description,
65 private string $dataFolder,
66 private string $file,
67 private string $resourceFolder,
68 ){
69 $this->dataFolder = rtrim($dataFolder, "/" . DIRECTORY_SEPARATOR) . "/";
70 //TODO: this is accessed externally via reflection, not unused
71 $this->file = rtrim($file, "/" . DIRECTORY_SEPARATOR) . "/";
72 $this->resourceFolder = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $resourceFolder), "/") . "/";
73
74 $this->configFile = Path::join($this->dataFolder, "config.yml");
75
76 $prefix = $this->description->getPrefix();
77 $this->logger = new PluginLogger($server->getLogger(), $prefix !== "" ? $prefix : $this->getName());
78 $this->scheduler = new TaskScheduler($this->getFullName());
79
80 $this->onLoad();
81
82 $this->registerYamlCommands();
83 }
84
88 protected function onLoad() : void{
89
90 }
91
95 protected function onEnable() : void{
96
97 }
98
103 protected function onDisable() : void{
104
105 }
106
107 final public function isEnabled() : bool{
108 return $this->isEnabled;
109 }
110
118 final public function onEnableStateChange(bool $enabled) : void{
119 if($this->isEnabled !== $enabled){
120 $this->isEnabled = $enabled;
121 if($this->isEnabled){
122 $this->onEnable();
123 }else{
124 $this->onDisable();
125 }
126 }
127 }
128
129 final public function isDisabled() : bool{
130 return !$this->isEnabled;
131 }
132
133 final public function getDataFolder() : string{
134 return $this->dataFolder;
135 }
136
137 final public function getDescription() : PluginDescription{
138 return $this->description;
139 }
140
141 public function getLogger() : \AttachableLogger{
142 return $this->logger;
143 }
144
148 private function registerYamlCommands() : void{
149 $pluginCmds = [];
150
151 foreach(Utils::stringifyKeys($this->description->getCommands()) as $key => $data){
152 if(str_contains($key, ":")){
153 $this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_commandError($key, $this->description->getFullName(), ":")));
154 continue;
155 }
156
157 $newCmd = new PluginCommand($key, $this, $this);
158 if(($description = $data->getDescription()) !== null){
159 $newCmd->setDescription($description);
160 }
161
162 if(($usageMessage = $data->getUsageMessage()) !== null){
163 $newCmd->setUsage($usageMessage);
164 }
165
166 $aliasList = [];
167 foreach($data->getAliases() as $alias){
168 if(str_contains($alias, ":")){
169 $this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_aliasError($alias, $this->description->getFullName(), ":")));
170 continue;
171 }
172 $aliasList[] = $alias;
173 }
174
175 $newCmd->setAliases($aliasList);
176
177 $newCmd->setPermission($data->getPermission());
178
179 if(($permissionDeniedMessage = $data->getPermissionDeniedMessage()) !== null){
180 $newCmd->setPermissionMessage($permissionDeniedMessage);
181 }
182
183 $pluginCmds[] = $newCmd;
184 }
185
186 if(count($pluginCmds) > 0){
187 $this->server->getCommandMap()->registerAll($this->description->getName(), $pluginCmds);
188 }
189 }
190
195 public function getCommand(string $name){
196 $command = $this->server->getPluginCommand($name);
197 if($command === null || $command->getOwningPlugin() !== $this){
198 $command = $this->server->getPluginCommand(strtolower($this->description->getName()) . ":" . $name);
199 }
200
201 if($command instanceof PluginOwned && $command->getOwningPlugin() === $this){
202 return $command;
203 }else{
204 return null;
205 }
206 }
207
211 public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{
212 return false;
213 }
214
219 public function getResourceFolder() : string{
220 return $this->resourceFolder;
221 }
222
229 public function getResourcePath(string $filename) : string{
230 return Path::join($this->getResourceFolder(), $filename);
231 }
232
236 public function saveResource(string $filename, bool $replace = false) : bool{
237 if(trim($filename) === ""){
238 return false;
239 }
240
241 $source = Path::join($this->resourceFolder, $filename);
242 if(!file_exists($source)){
243 return false;
244 }
245
246 $destination = Path::join($this->dataFolder, $filename);
247 if(file_exists($destination) && !$replace){
248 return false;
249 }
250
251 if(!file_exists(dirname($destination))){
252 mkdir(dirname($destination), 0755, true);
253 }
254
255 return copy($source, $destination);
256 }
257
263 public function getResources() : array{
264 $resources = [];
265 if(is_dir($this->resourceFolder)){
267 foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resourceFolder)) as $resource){
268 if($resource->isFile()){
269 $path = str_replace(DIRECTORY_SEPARATOR, "/", substr((string) $resource, strlen($this->resourceFolder)));
270 $resources[$path] = $resource;
271 }
272 }
273 }
274
275 return $resources;
276 }
277
278 public function getConfig() : Config{
279 if($this->config === null){
280 $this->reloadConfig();
281 }
282
283 return $this->config;
284 }
285
286 public function saveConfig() : void{
287 $this->getConfig()->save();
288 }
289
290 public function saveDefaultConfig() : bool{
291 if(!file_exists($this->configFile)){
292 return $this->saveResource("config.yml", false);
293 }
294 return false;
295 }
296
297 public function reloadConfig() : void{
298 $this->saveDefaultConfig();
299 $this->config = new Config($this->configFile);
300 }
301
302 final public function getServer() : Server{
303 return $this->server;
304 }
305
306 final public function getName() : string{
307 return $this->description->getName();
308 }
309
310 final public function getFullName() : string{
311 return $this->description->getFullName();
312 }
313
314 protected function getFile() : string{
315 return $this->file;
316 }
317
318 public function getPluginLoader() : PluginLoader{
319 return $this->loader;
320 }
321
322 public function getScheduler() : TaskScheduler{
323 return $this->scheduler;
324 }
325}
getResourcePath(string $filename)
saveResource(string $filename, bool $replace=false)
onEnableStateChange(bool $enabled)
onCommand(CommandSender $sender, Command $command, string $label, array $args)