diff --git a/.php-cs-fixer.cache b/.php-cs-fixer.cache index aa3d397..bd0a9f5 100644 --- a/.php-cs-fixer.cache +++ b/.php-cs-fixer.cache @@ -1 +1 @@ -{"php":"8.0.22","version":"3.12.0:v3.12.0#eae11d945e2885d86e1c080eec1bb30a2aa27998","indent":"\t","lineEnding":"\n","rules":{"align_multiline_comment":{"comment_type":"phpdocs_only"},"array_indentation":true,"array_syntax":{"syntax":"short"},"binary_operator_spaces":{"default":"single_space"},"blank_line_after_namespace":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["declare"]},"cast_spaces":{"space":"single"},"concat_space":{"spacing":"one"},"declare_strict_types":true,"elseif":true,"fully_qualified_strict_types":true,"global_namespace_import":{"import_constants":true,"import_functions":true,"import_classes":null},"indentation_type":true,"logical_operators":true,"native_function_invocation":{"scope":"namespaced","include":["@all"]},"new_with_braces":{"named_class":true,"anonymous_class":false},"no_closing_tag":true,"no_empty_phpdoc":true,"no_extra_blank_lines":true,"no_superfluous_phpdoc_tags":{"allow_mixed":true},"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"no_whitespace_in_blank_line":true,"no_unused_imports":true,"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"alpha"},"phpdoc_line_span":{"property":"single","method":null,"const":null},"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"return_type_declaration":{"space_before":"one"},"single_blank_line_at_eof":true,"single_import_per_statement":true,"strict_param":true,"unary_operator_spaces":true},"hashes":{"src\\NhanAZ\\libBedrock\\libBedrock.php":591365733}} \ No newline at end of file +{"php":"8.0.22","version":"3.12.0:v3.12.0#eae11d945e2885d86e1c080eec1bb30a2aa27998","indent":"\t","lineEnding":"\n","rules":{"align_multiline_comment":{"comment_type":"phpdocs_only"},"array_indentation":true,"array_syntax":{"syntax":"short"},"binary_operator_spaces":{"default":"single_space"},"blank_line_after_namespace":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["declare"]},"cast_spaces":{"space":"single"},"concat_space":{"spacing":"one"},"declare_strict_types":true,"elseif":true,"fully_qualified_strict_types":true,"global_namespace_import":{"import_constants":true,"import_functions":true,"import_classes":null},"indentation_type":true,"logical_operators":true,"native_function_invocation":{"scope":"namespaced","include":["@all"]},"new_with_braces":{"named_class":true,"anonymous_class":false},"no_closing_tag":true,"no_empty_phpdoc":true,"no_extra_blank_lines":true,"no_superfluous_phpdoc_tags":{"allow_mixed":true},"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"no_whitespace_in_blank_line":true,"no_unused_imports":true,"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"alpha"},"phpdoc_line_span":{"property":"single","method":null,"const":null},"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"return_type_declaration":{"space_before":"one"},"single_blank_line_at_eof":true,"single_import_per_statement":true,"strict_param":true,"unary_operator_spaces":true},"hashes":{"src\\NhanAZ\\libBedrock\\libBedrock.php":3938656178}} \ No newline at end of file diff --git a/src/NhanAZ/libBedrock/libBedrock.php b/src/NhanAZ/libBedrock/libBedrock.php index 1379ce4..4431e46 100644 --- a/src/NhanAZ/libBedrock/libBedrock.php +++ b/src/NhanAZ/libBedrock/libBedrock.php @@ -5,11 +5,70 @@ namespace NhanAZ\libBedrock; use pocketmine\plugin\PluginBase; +use pocketmine\scheduler\ClosureTask; +use pocketmine\utils\Config; +use function basename; +use function explode; +use function rename; +use function str_replace; +use function strval; +use function trim; +use function version_compare; class libBedrock { - public function __construct( - private PluginBase $plugin /** @phpstan-ignore-line */ - ) { + /** + * Base: https://github.com/ifera-mc/ConfigUpdater + */ + public static function checkConfigVersion(PluginBase $plugin, Config $config, string $configKey, string $latestVersion, string $updateMessage = "") : bool { + if (($config->exists($configKey)) && !version_compare(strval($config->get($configKey)), $latestVersion, "<>")) { + return false; + } + + $configData = self::getConfigData($config); + $configPath = $configData["configPath"]; + $originalConfig = $configData["configName"]; + $oldConfig = $configData["oldConfigName"]; + + if (trim($updateMessage) === "") { + $updateMessage = "Your $originalConfig file is outdated. Your old $originalConfig has been saved as $oldConfig and a new $originalConfig file has been generated. Please update accordingly."; + } + + rename($configPath . $originalConfig, $configPath . $oldConfig); + + $plugin->saveDefaultConfig(); + $plugin->getConfig()->reload(); + + $task = new ClosureTask(function () use ($plugin, $updateMessage) : void { + $plugin->getLogger()->critical($updateMessage); + }); + + $plugin->getScheduler()->scheduleDelayedTask($task, 3 * 20); + + return true; + } + + /** + * @return array + */ + private static function getConfigData(Config $config) : array { + $configPath = $config->getPath(); + $configData = explode(".", basename($configPath)); + + $configName = $configData[0]; + $configExtension = $configData[1]; + + $originalConfigName = $configName . "." . $configExtension; + $oldConfigName = $configName . "_old." . $configExtension; + + $configPath = str_replace($originalConfigName, "", $configPath); + $pluginPath = str_replace("plugin_data", "plugins", $configPath); + + return [ + "configPath" => $configPath, + "pluginPath" => $pluginPath, + "configName" => $originalConfigName, + "oldConfigName" => $oldConfigName + ]; } }