Skip to content

Commit

Permalink
Only update menu files with specifically added keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed May 17, 2024
1 parent f3eff4c commit 89a5ae4
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MenuFileManager {

Expand Down Expand Up @@ -50,24 +52,43 @@ public void updateMenus() {
ConfigurationNode embeddedConfig = FileUtil.loadEmbeddedYamlFile("menus/" + menuName + ".yml", plugin);
ConfigurationNode userConfig = FileUtil.loadYamlFile(userFile);

updateAndSave(embeddedConfig, userConfig, userFile);
updateAndSave(menuName, embeddedConfig, userConfig, userFile);
} catch (IOException e) {
plugin.logger().warn("Error updating menu file " + userFile.getName());
e.printStackTrace();
}
}
}

private void updateAndSave(ConfigurationNode embedded, ConfigurationNode user, File userFile) throws SerializationException {
int changed = 0;
changed += updateConfigSection("items", embedded, user);
changed += updateConfigSection("templates", embedded, user);
changed += updateConfigSection("components", embedded, user);
changed += updateStringSection("formats", embedded, user);
private void updateAndSave(String menuName, ConfigurationNode embedded, ConfigurationNode user, File userFile) throws SerializationException {
// Files that don't have updating enabled yet, since they haven't had changes
if (embedded.node("file_version").virtual()) return;

int embVersion = embedded.node("file_version").getInt();
int userVersion = user.node("file_version").getInt(0);

if (changed <= 0) {
// User file is already up-to-date
if (userVersion >= embVersion) {
return;
}

List<MenuFileUpdates> updates = MenuFileUpdates.getUpdates(menuName, userVersion, embVersion);
if (updates.isEmpty()) return;

int changed = 0;
for (MenuFileUpdates update : updates) {
List<String> addedItems = update.getAddedKeys().getOrDefault("items", new ArrayList<>());
changed += updateConfigSection("items", embedded, user, addedItems);
List<String> addedTemplates = update.getAddedKeys().getOrDefault("templates", new ArrayList<>());
changed += updateConfigSection("templates", embedded, user, addedTemplates);
List<String> addedComponents = update.getAddedKeys().getOrDefault("components", new ArrayList<>());
changed += updateConfigSection("components", embedded, user, addedComponents);
List<String> addedFormats = update.getAddedKeys().getOrDefault("formats", new ArrayList<>());
changed += updateStringSection("formats", embedded, user, addedFormats);
}

user.node("file_version").set(embVersion);

try {
YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
.file(userFile)
Expand All @@ -83,12 +104,14 @@ private void updateAndSave(ConfigurationNode embedded, ConfigurationNode user, F
}
}

private int updateConfigSection(String name, ConfigurationNode embedded, ConfigurationNode user) throws SerializationException {
private int updateConfigSection(String name, ConfigurationNode embedded, ConfigurationNode user, List<String> keys) throws SerializationException {
if (keys.isEmpty()) return 0;
int changed = 0;
if (!embedded.node(name).virtual() && !user.node(name).virtual()) {
for (ConfigurationNode embSec : embedded.node(name).childrenMap().values()) {
String key = (String) embSec.key();
if (key == null) continue;
if (!keys.contains(key)) continue; // Only update sections passed in the keys list
if (!embSec.isMap()) continue;
// User file does not have embedded key
if (user.node(name).node(key).virtual()) {
Expand All @@ -100,12 +123,14 @@ private int updateConfigSection(String name, ConfigurationNode embedded, Configu
return changed;
}

private int updateStringSection(String name, ConfigurationNode embedded, ConfigurationNode user) throws SerializationException {
private int updateStringSection(String name, ConfigurationNode embedded, ConfigurationNode user, List<String> keys) throws SerializationException {
if (keys.isEmpty()) return 0;
int changed = 0;
if (!embedded.node(name).virtual() && !user.node(name).virtual()) {
for (ConfigurationNode embSec : embedded.node(name).childrenMap().values()) {
String key = (String) embSec.key();
if (key == null) continue;
if (!keys.contains(key)) continue;

String value = embSec.getString();
if (value == null) continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.aurelium.auraskills.bukkit.menus;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public enum MenuFileUpdates {

SKILLS_1("skills", 1, Map.of(
"components", List.of("skill_job_active"))),
LEVEL_PROGRESSION_1("level_progression", 1, Map.of(
"items", List.of("job"),
"components", List.of("job_select", "job_active", "job_limit")));

private final String menu;
private final int version;
private final Map<String, List<String>> addedKeys;

MenuFileUpdates(String menu, int version, Map<String, List<String>> addedKeys) {
this.menu = menu;
this.version = version;
this.addedKeys = addedKeys;
}

public String getMenu() {
return menu;
}

public int getVersion() {
return version;
}

public Map<String, List<String>> getAddedKeys() {
return addedKeys;
}

public static List<MenuFileUpdates> getUpdates(String menu, int currentVersion, int updatedVersion) {
List<MenuFileUpdates> updates = new ArrayList<>();
for (MenuFileUpdates update : MenuFileUpdates.values()) {
if (!update.getMenu().equals(menu)) continue;

if (update.getVersion() > currentVersion && update.getVersion() <= updatedVersion) {
updates.add(update);
}
}
// Sort by increasing version
updates.sort(Comparator.comparingInt(MenuFileUpdates::getVersion));
return updates;
}

}
Loading

0 comments on commit 89a5ae4

Please sign in to comment.