53 private bool $isEnabled =
false;
55 private ?
Config $config =
null;
56 private string $configFile;
61 public function __construct(
64 private string $dataFolder,
66 private string $resourceFolder,
68 $this->dataFolder = rtrim($dataFolder,
"/" . DIRECTORY_SEPARATOR) .
"/";
69 $this->file = rtrim($file,
"/" . DIRECTORY_SEPARATOR) .
"/";
70 $this->resourceFolder = rtrim(str_replace(DIRECTORY_SEPARATOR,
"/", $resourceFolder),
"/") .
"/";
72 $this->configFile = Path::join($this->dataFolder,
"config.yml");
74 $prefix = $this->description->getPrefix();
75 $this->logger =
new PluginLogger($server->getLogger(), $prefix !==
"" ? $prefix : $this->getName());
80 $this->registerYamlCommands();
105 final public function isEnabled() : bool{
106 return $this->isEnabled;
117 if($this->isEnabled !== $enabled){
118 $this->isEnabled = $enabled;
119 if($this->isEnabled){
127 final public function isDisabled() : bool{
128 return !$this->isEnabled;
132 return $this->dataFolder;
136 return $this->description;
140 return $this->logger;
146 private function registerYamlCommands() : void{
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(),
":")));
155 $newCmd =
new PluginCommand($key, $this, $this);
156 if(($description = $data->getDescription()) !==
null){
157 $newCmd->setDescription($description);
160 if(($usageMessage = $data->getUsageMessage()) !==
null){
161 $newCmd->setUsage($usageMessage);
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(),
":")));
170 $aliasList[] = $alias;
173 $newCmd->setAliases($aliasList);
175 $newCmd->setPermission($data->getPermission());
177 if(($permissionDeniedMessage = $data->getPermissionDeniedMessage()) !==
null){
178 $newCmd->setPermissionMessage($permissionDeniedMessage);
181 $pluginCmds[] = $newCmd;
184 if(count($pluginCmds) > 0){
185 $this->
server->getCommandMap()->registerAll($this->description->getName(), $pluginCmds);
194 $command = $this->
server->getPluginCommand($name);
195 if($command ===
null || $command->getOwningPlugin() !== $this){
196 $command = $this->
server->getPluginCommand(strtolower($this->description->getName()) .
":" . $name);
199 if($command instanceof
PluginOwned && $command->getOwningPlugin() === $this){
218 return $this->resourceFolder;
228 return Path::join($this->getResourceFolder(), $filename);
234 public function saveResource(
string $filename,
bool $replace =
false) : bool{
235 if(trim($filename) ===
""){
239 $source = Path::join($this->resourceFolder, $filename);
240 if(!file_exists($source)){
244 $destination = Path::join($this->dataFolder, $filename);
245 if(file_exists($destination) && !$replace){
249 if(!file_exists(dirname($destination))){
250 mkdir(dirname($destination), 0755,
true);
253 return copy($source, $destination);
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;
276 public function getConfig() :
Config{
277 if($this->config === null){
278 $this->reloadConfig();
281 return $this->config;
284 public function saveConfig() : void{
285 $this->getConfig()->save();
288 public function saveDefaultConfig() : bool{
289 if(!file_exists($this->configFile)){
290 return $this->saveResource(
"config.yml",
false);
295 public function reloadConfig() : void{
296 $this->saveDefaultConfig();
297 $this->config =
new Config($this->configFile);
300 final public function getServer() : Server{
304 final public function getName() : string{
305 return $this->description->getName();
308 final public function getFullName() : string{
309 return $this->description->getFullName();
312 public function getFile() : string{
316 public function getScheduler() : TaskScheduler{
317 return $this->scheduler;