Skip to content

Commit

Permalink
feat: add config updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-EmPee committed May 30, 2024
1 parent 9807f90 commit 7fc42e2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void reload() {
private void loadMessages() {
var shouldReplaceExistingMessage = plugin.isDevelop();
var messages = findDefaultMessages().stream()
.map(path -> new ResourceConfig(plugin, path, shouldReplaceExistingMessage))
.map(path -> new ResourceConfig(plugin, path, shouldReplaceExistingMessage, 2))
.toList();

for (ResourceConfig message : messages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class PluginConfig extends ResourceConfig {

public PluginConfig(MysticalBarriers plugin) {
super(plugin, "configs/config.yml", plugin.isDevelop());
super(plugin, "configs/config.yml", plugin.isDevelop(), 1);

Messenger.setPrefix(getPrefix());
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/core/src/main/resources/configs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ block-chorus-teleportation: true
block-movement:
projectiles:
enabled: true
only-from-player: false # Enabled only for projectiles shot by a player
only-from-player: false # Enabled only for projectiles shot by a player
2 changes: 2 additions & 0 deletions plugin/core/src/main/resources/messages/generic.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 2

errors:
internal_error: "&cAn internal error occurred (╯°□°)╯︵ ┻━┻"
barrier_not_found: "&cBarrier not found"
2 changes: 2 additions & 0 deletions plugin/core/src/main/resources/messages/guis/common.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 2

common.buttons:
previous-page:
title: "&ePrevious page"
Expand Down
38 changes: 36 additions & 2 deletions plugin/utils/src/main/java/utils/files/ResourceConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.Getter;
import lombok.SneakyThrows;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import utils.IReloadable;
import utils.Messenger;

import java.io.File;
import java.io.InputStreamReader;
import java.nio.file.Files;

/**
Expand All @@ -25,11 +27,25 @@ public class ResourceConfig implements IReloadable {
@Getter
private YamlConfiguration config;

private static void patch(ConfigurationSection target, ConfigurationSection patch) {
for (String key : patch.getKeys(false)) {
if (!target.contains(key)) {
target.set(key, patch.get(key));
continue;
}

var patchedValue = patch.get(key);
if (patchedValue instanceof ConfigurationSection) {
patch((ConfigurationSection) target.get(key), (ConfigurationSection) patchedValue);
}
}
}

/**
* Copy and load the resource file from the JAR to the file system if it does not exist.
*/
@SneakyThrows
public ResourceConfig(JavaPlugin plugin, String path, boolean replace) {
public ResourceConfig(JavaPlugin plugin, String path, boolean replace, int version) {
this.file = new File(plugin.getDataFolder(), path);

if (replace || !file.exists()) {
Expand All @@ -39,7 +55,25 @@ public ResourceConfig(JavaPlugin plugin, String path, boolean replace) {
Messenger.log("Extracted '{}' from JAR to plugin directory", path);
}

this.config = YamlConfiguration.loadConfiguration(file);
config = YamlConfiguration.loadConfiguration(file);
if (getVersion() < version) {
Messenger.log("Updating config {} to version {}", file, version);
migrate(version);

var patchConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(plugin.getResource(path)));
patch(config, patchConfig);

config.set("version", version);
config.save(file);
}
}

protected void migrate(int targetVersion) {

}

public int getVersion() {
return config.getInt("version", 1);
}

/**
Expand Down

0 comments on commit 7fc42e2

Please sign in to comment.