PocketMine-MP 5.35.1 git-f412a390b8f63d0311cc1d1c81046404153b8440
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 dirname;
38use function file_exists;
39use function fopen;
40use function is_dir;
41use function mkdir;
42use function rtrim;
43use function str_contains;
44use function str_replace;
45use function strlen;
46use function strtolower;
47use function substr;
48use function trim;
49use const DIRECTORY_SEPARATOR;
50
51abstract class PluginBase implements Plugin, CommandExecutor{
52 private bool $isEnabled = false;
53
54 private ?Config $config = null;
55 private string $configFile;
56
57 private PluginLogger $logger;
58 private TaskScheduler $scheduler;
59
60 public function __construct(
61 private Server $server,
62 private PluginDescription $description,
63 private string $dataFolder,
64 private string $file,
65 private string $resourceFolder,
66 ){
67 $this->dataFolder = rtrim($dataFolder, "/" . DIRECTORY_SEPARATOR) . "/";
68 $this->file = rtrim($file, "/" . DIRECTORY_SEPARATOR) . "/";
69 $this->resourceFolder = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $resourceFolder), "/") . "/";
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 foreach(Utils::stringifyKeys($this->description->getCommands()) as $key => $data){
147 if(str_contains($key, ":")){
148 $this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_commandError($key, $this->description->getFullName(), ":")));
149 continue;
150 }
151
152 $aliasList = [];
153 foreach($data->getAliases() as $alias){
154 if(str_contains($alias, ":")){
155 $this->logger->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_plugin_aliasError($alias, $this->description->getFullName(), ":")));
156 continue;
157 }
158 $aliasList[] = $alias;
159 }
160
161 $newCmd = new PluginCommand(
162 $this->description->getName(),
163 $key,
164 $this,
165 $this,
166 $data->getDescription() ?? "",
167 $data->getUsageMessage()
168 );
169
170 $newCmd->setPermission($data->getPermission());
171
172 if(($permissionDeniedMessage = $data->getPermissionDeniedMessage()) !== null){
173 $newCmd->setPermissionMessage($permissionDeniedMessage);
174 }
175
176 $this->server->getCommandMap()->register($newCmd, $aliasList);
177 }
178 }
179
184 public function getCommand(string $name){
185 $command = $this->server->getPluginCommand($name);
186 if($command === null || $command->getOwningPlugin() !== $this){
187 $command = $this->server->getPluginCommand(strtolower($this->description->getName()) . ":" . $name);
188 }
189
190 if($command instanceof PluginOwned && $command->getOwningPlugin() === $this){
191 return $command;
192 }else{
193 return null;
194 }
195 }
196
200 public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{
201 return false;
202 }
203
208 public function getResourceFolder() : string{
209 return $this->resourceFolder;
210 }
211
218 public function getResourcePath(string $filename) : string{
219 return Path::join($this->getResourceFolder(), $filename);
220 }
221
225 public function saveResource(string $filename, bool $replace = false) : bool{
226 if(trim($filename) === ""){
227 return false;
228 }
229
230 $source = Path::join($this->resourceFolder, $filename);
231 if(!file_exists($source)){
232 return false;
233 }
234
235 $destination = Path::join($this->dataFolder, $filename);
236 if(file_exists($destination) && !$replace){
237 return false;
238 }
239
240 if(!file_exists(dirname($destination))){
241 mkdir(dirname($destination), 0755, true);
242 }
243
244 return copy($source, $destination);
245 }
246
252 public function getResources() : array{
253 $resources = [];
254 if(is_dir($this->resourceFolder)){
256 foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resourceFolder)) as $resource){
257 if($resource->isFile()){
258 $path = str_replace(DIRECTORY_SEPARATOR, "/", substr((string) $resource, strlen($this->resourceFolder)));
259 $resources[$path] = $resource;
260 }
261 }
262 }
263
264 return $resources;
265 }
266
267 public function getConfig() : Config{
268 if($this->config === null){
269 $this->reloadConfig();
270 }
271
272 return $this->config;
273 }
274
275 public function saveConfig() : void{
276 $this->getConfig()->save();
277 }
278
279 public function saveDefaultConfig() : bool{
280 if(!file_exists($this->configFile)){
281 return $this->saveResource("config.yml", false);
282 }
283 return false;
284 }
285
286 public function reloadConfig() : void{
287 $this->saveDefaultConfig();
288 $this->config = new Config($this->configFile);
289 }
290
291 final public function getServer() : Server{
292 return $this->server;
293 }
294
295 final public function getName() : string{
296 return $this->description->getName();
297 }
298
299 final public function getFullName() : string{
300 return $this->description->getFullName();
301 }
302
303 public function getFile() : string{
304 return $this->file;
305 }
306
307 public function getScheduler() : TaskScheduler{
308 return $this->scheduler;
309 }
310}
getResourcePath(string $filename)
saveResource(string $filename, bool $replace=false)
onEnableStateChange(bool $enabled)
onCommand(CommandSender $sender, Command $command, string $label, array $args)