52 private bool $isEnabled =
false;
54 private ?
Config $config =
null;
55 private string $configFile;
60 public function __construct(
63 private string $dataFolder,
65 private string $resourceFolder,
67 $this->dataFolder = rtrim($dataFolder,
"/" . DIRECTORY_SEPARATOR) .
"/";
68 $this->file = rtrim($file,
"/" . DIRECTORY_SEPARATOR) .
"/";
69 $this->resourceFolder = rtrim(str_replace(DIRECTORY_SEPARATOR,
"/", $resourceFolder),
"/") .
"/";
71 $this->configFile = Path::join($this->dataFolder,
"config.yml");
73 $prefix = $this->description->getPrefix();
74 $this->logger =
new PluginLogger($server->getLogger(), $prefix !==
"" ? $prefix : $this->getName());
79 $this->registerYamlCommands();
104 final public function isEnabled() : bool{
105 return $this->isEnabled;
116 if($this->isEnabled !== $enabled){
117 $this->isEnabled = $enabled;
118 if($this->isEnabled){
126 final public function isDisabled() : bool{
127 return !$this->isEnabled;
131 return $this->dataFolder;
135 return $this->description;
139 return $this->logger;
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(),
":")));
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(),
":")));
158 $aliasList[] = $alias;
161 $newCmd =
new PluginCommand(
162 $this->description->getName(),
166 $data->getDescription() ??
"",
167 $data->getUsageMessage()
170 $newCmd->setPermission($data->getPermission());
172 if(($permissionDeniedMessage = $data->getPermissionDeniedMessage()) !==
null){
173 $newCmd->setPermissionMessage($permissionDeniedMessage);
176 $this->
server->getCommandMap()->register($newCmd, $aliasList);
185 $command = $this->
server->getPluginCommand($name);
186 if($command ===
null || $command->getOwningPlugin() !== $this){
187 $command = $this->
server->getPluginCommand(strtolower($this->description->getName()) .
":" . $name);
190 if($command instanceof
PluginOwned && $command->getOwningPlugin() === $this){
209 return $this->resourceFolder;
219 return Path::join($this->getResourceFolder(), $filename);
225 public function saveResource(
string $filename,
bool $replace =
false) : bool{
226 if(trim($filename) ===
""){
230 $source = Path::join($this->resourceFolder, $filename);
231 if(!file_exists($source)){
235 $destination = Path::join($this->dataFolder, $filename);
236 if(file_exists($destination) && !$replace){
240 if(!file_exists(dirname($destination))){
241 mkdir(dirname($destination), 0755,
true);
244 return copy($source, $destination);
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;
267 public function getConfig() :
Config{
268 if($this->config === null){
269 $this->reloadConfig();
272 return $this->config;
275 public function saveConfig() : void{
276 $this->getConfig()->save();
279 public function saveDefaultConfig() : bool{
280 if(!file_exists($this->configFile)){
281 return $this->saveResource(
"config.yml",
false);
286 public function reloadConfig() : void{
287 $this->saveDefaultConfig();
288 $this->config =
new Config($this->configFile);
291 final public function getServer() : Server{
295 final public function getName() : string{
296 return $this->description->getName();
299 final public function getFullName() : string{
300 return $this->description->getFullName();
303 public function getFile() : string{
307 public function getScheduler() : TaskScheduler{
308 return $this->scheduler;