Skip to content

Commit

Permalink
checkConfigVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
NhanAZ committed Oct 18, 2022
1 parent de8c5c8 commit ee62356
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -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}}
{"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}}
65 changes: 62 additions & 3 deletions src/NhanAZ/libBedrock/libBedrock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
*/
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
];
}
}

0 comments on commit ee62356

Please sign in to comment.