PocketMine-MP 5.28.1 git-88cdc2eb67c40075559c3ef51418b418cd5488e9
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 Server $server,
63 private PluginDescription $description,
64 private string $dataFolder,
65 private string $file,
66 private string $resourceFolder,
67 ){
68 $this->dataFolder = rtrim($dataFolder, "/" . DIRECTORY_SEPARATOR) . "/";
69 $this->file = rtrim($file, "/" . DIRECTORY_SEPARATOR) . "/";
70 $this->resourceFolder = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $resourceFolder), "/") . "/";
71
72 $this->configFile = Path::join($this->dataFolder, "config.yml");
73
74 $prefix = $this->description->getPrefix();
75 $this->logger = new PluginLogger($server->getLogger(), $prefix !== "" ? $prefix : $this->getName());
76 $this->scheduler = new TaskScheduler($this->getFullName());
77
78 $this->onLoad();
79
80 $this->registerYamlCommands();
81 }
82
86 protected function onLoad() : void{
87
88 }
89
93 protected function onEnable() : void{
94
95 }
96
101 protected function onDisable() : void{
102
103 }
104
105 final public function isEnabled() : bool{
106 return $this->isEnabled;
107 }
108
116 final public function onEnableStateChange(bool $enabled) : void{
117 if($this->isEnabled !== $enabled){
118 $this->isEnabled = $enabled;
119 if($this->isEnabled){
120 $this->onEnable();
121 }else{
122 $this->onDisable();
123 }
124 }
125 }
126
127 final public function isDisabled() : bool{
128 return !$this->isEnabled;
129 }
130
131 final public function getDataFolder() : string{
132 return $this->dataFolder;
133 }
134
135 final public function getDescription() : PluginDescription{
136 return $this->description;
137 }
138
139 public function getLogger() : \AttachableLogger{
140 return $this->logger;
141 }
142
146 private function registerYamlCommands() : void{
147 $pluginCmds = [];
148
149 foreach(Utils::stringifyKeys($this->description->getCommands()) as $key => $data){
150 if(str_contains($key, ":")){
151 $this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_commandError($key, $this->description->getFullName(), ":")));
152 continue;
153 }
154
155 $newCmd = new PluginCommand($key, $this, $this);
156 if(($description = $data->getDescription()) !== null){
157 $newCmd->setDescription($description);
158 }
159
160 if(($usageMessage = $data->getUsageMessage()) !== null){
161 $newCmd->setUsage($usageMessage);
162 }
163
164 $aliasList = [];
165 foreach($data->getAliases() as $alias){
166 if(str_contains($alias, ":")){
167 $this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_aliasError($alias, $this->description->getFullName(), ":")));
168 continue;
169 }
170 $aliasList[] = $alias;
171 }
172
173 $newCmd->setAliases($aliasList);
174
175 $newCmd->setPermission($data->getPermission());
176
177 if(($permissionDeniedMessage = $data->getPermissionDeniedMessage()) !== null){
178 $newCmd->setPermissionMessage($permissionDeniedMessage);
179 }
180
181 $pluginCmds[] = $newCmd;
182 }
183
184 if(count($pluginCmds) > 0){
185 $this->server->getCommandMap()->registerAll($this->description->getName(), $pluginCmds);
186 }
187 }
188
193 public function getCommand(string $name){
194 $command = $this->server->getPluginCommand($name);
195 if($command === null || $command->getOwningPlugin() !== $this){
196 $command = $this->server->getPluginCommand(strtolower($this->description->getName()) . ":" . $name);
197 }
198
199 if($command instanceof PluginOwned && $command->getOwningPlugin() === $this){
200 return $command;
201 }else{
202 return null;
203 }
204 }
205
209 public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{
210 return false;
211 }
212
217 public function getResourceFolder() : string{
218 return $this->resourceFolder;
219 }
220
227 public function getResourcePath(string $filename) : string{
228 return Path::join($this->getResourceFolder(), $filename);
229 }
230
234 public function saveResource(string $filename, bool $replace = false) : bool{
235 if(trim($filename) === ""){
236 return false;
237 }
238
239 $source = Path::join($this->resourceFolder, $filename);
240 if(!file_exists($source)){
241 return false;
242 }
243
244 $destination = Path::join($this->dataFolder, $filename);
245 if(file_exists($destination) && !$replace){
246 return false;
247 }
248
249 if(!file_exists(dirname($destination))){
250 mkdir(dirname($destination), 0755, true);
251 }
252
253 return copy($source, $destination);
254 }
255
261 public function getResources() : array{
262 $resources = [];
263 if(is_dir($this->resourceFolder)){
265 foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resourceFolder)) as $resource){
266 if($resource->isFile()){
267 $path = str_replace(DIRECTORY_SEPARATOR, "/", substr((string) $resource, strlen($this->resourceFolder)));
268 $resources[$path] = $resource;
269 }
270 }
271 }
272
273 return $resources;
274 }
275
276 public function getConfig() : Config{
277 if($this->config === null){
278 $this->reloadConfig();
279 }
280
281 return $this->config;
282 }
283
284 public function saveConfig() : void{
285 $this->getConfig()->save();
286 }
287
288 public function saveDefaultConfig() : bool{
289 if(!file_exists($this->configFile)){
290 return $this->saveResource("config.yml", false);
291 }
292 return false;
293 }
294
295 public function reloadConfig() : void{
296 $this->saveDefaultConfig();
297 $this->config = new Config($this->configFile);
298 }
299
300 final public function getServer() : Server{
301 return $this->server;
302 }
303
304 final public function getName() : string{
305 return $this->description->getName();
306 }
307
308 final public function getFullName() : string{
309 return $this->description->getFullName();
310 }
311
312 public function getFile() : string{
313 return $this->file;
314 }
315
316 public function getScheduler() : TaskScheduler{
317 return $this->scheduler;
318 }
319}
getResourcePath(string $filename)
saveResource(string $filename, bool $replace=false)
onEnableStateChange(bool $enabled)
onCommand(CommandSender $sender, Command $command, string $label, array $args)