53 private bool $isEnabled =
false;
55 private ?
Config $config =
null;
56 private string $configFile;
61 public function __construct(
65 private string $dataFolder,
67 private string $resourceFolder,
69 $this->dataFolder = rtrim($dataFolder,
"/" . DIRECTORY_SEPARATOR) .
"/";
71 $this->file = rtrim($file,
"/" . DIRECTORY_SEPARATOR) .
"/";
72 $this->resourceFolder = rtrim(str_replace(DIRECTORY_SEPARATOR,
"/", $resourceFolder),
"/") .
"/";
74 $this->configFile = Path::join($this->dataFolder,
"config.yml");
76 $prefix = $this->description->getPrefix();
77 $this->logger =
new PluginLogger($server->getLogger(), $prefix !==
"" ? $prefix : $this->getName());
82 $this->registerYamlCommands();
107 final public function isEnabled() : bool{
108 return $this->isEnabled;
119 if($this->isEnabled !== $enabled){
120 $this->isEnabled = $enabled;
121 if($this->isEnabled){
129 final public function isDisabled() : bool{
130 return !$this->isEnabled;
134 return $this->dataFolder;
138 return $this->description;
142 return $this->logger;
148 private function registerYamlCommands() : void{
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(),
":")));
157 $newCmd =
new PluginCommand($key, $this, $this);
158 if(($description = $data->getDescription()) !==
null){
159 $newCmd->setDescription($description);
162 if(($usageMessage = $data->getUsageMessage()) !==
null){
163 $newCmd->setUsage($usageMessage);
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(),
":")));
172 $aliasList[] = $alias;
175 $newCmd->setAliases($aliasList);
177 $newCmd->setPermission($data->getPermission());
179 if(($permissionDeniedMessage = $data->getPermissionDeniedMessage()) !==
null){
180 $newCmd->setPermissionMessage($permissionDeniedMessage);
183 $pluginCmds[] = $newCmd;
186 if(count($pluginCmds) > 0){
187 $this->
server->getCommandMap()->registerAll($this->description->getName(), $pluginCmds);
196 $command = $this->
server->getPluginCommand($name);
197 if($command ===
null || $command->getOwningPlugin() !== $this){
198 $command = $this->
server->getPluginCommand(strtolower($this->description->getName()) .
":" . $name);
201 if($command instanceof
PluginOwned && $command->getOwningPlugin() === $this){
220 return $this->resourceFolder;
230 return Path::join($this->getResourceFolder(), $filename);
236 public function saveResource(
string $filename,
bool $replace =
false) : bool{
237 if(trim($filename) ===
""){
241 $source = Path::join($this->resourceFolder, $filename);
242 if(!file_exists($source)){
246 $destination = Path::join($this->dataFolder, $filename);
247 if(file_exists($destination) && !$replace){
251 if(!file_exists(dirname($destination))){
252 mkdir(dirname($destination), 0755,
true);
255 return copy($source, $destination);
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;
278 public function getConfig() :
Config{
279 if($this->config === null){
280 $this->reloadConfig();
283 return $this->config;
286 public function saveConfig() : void{
287 $this->getConfig()->save();
290 public function saveDefaultConfig() : bool{
291 if(!file_exists($this->configFile)){
292 return $this->saveResource(
"config.yml",
false);
297 public function reloadConfig() : void{
298 $this->saveDefaultConfig();
299 $this->config =
new Config($this->configFile);
302 final public function getServer() : Server{
306 final public function getName() : string{
307 return $this->description->getName();
310 final public function getFullName() : string{
311 return $this->description->getFullName();
314 protected function getFile() : string{
318 public function getPluginLoader() : PluginLoader{
319 return $this->loader;
322 public function getScheduler() : TaskScheduler{
323 return $this->scheduler;