AutoUpdateLegacyConfigLoader.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.gmail.nossr50.config;
  2. import com.gmail.nossr50.mcMMO;
  3. import org.bukkit.configuration.file.FileConfiguration;
  4. import org.bukkit.configuration.file.YamlConfiguration;
  5. import org.jetbrains.annotations.NotNull;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.HashSet;
  9. import java.util.Set;
  10. public abstract class AutoUpdateLegacyConfigLoader extends LegacyConfigLoader {
  11. public AutoUpdateLegacyConfigLoader(String relativePath, String fileName, File dataFolder) {
  12. super(relativePath, fileName, dataFolder);
  13. }
  14. public AutoUpdateLegacyConfigLoader(String fileName, File dataFolder) {
  15. super(fileName, dataFolder);
  16. }
  17. @Deprecated
  18. public AutoUpdateLegacyConfigLoader(String relativePath, String fileName) {
  19. super(relativePath, fileName);
  20. }
  21. @Deprecated
  22. public AutoUpdateLegacyConfigLoader(String fileName) {
  23. super(fileName);
  24. }
  25. protected void saveConfig() {
  26. try {
  27. mcMMO.p.getLogger().info("Saving changes to config file - " + fileName);
  28. config.options().indent(2);
  29. config.save(configFile);
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. protected @NotNull FileConfiguration getInternalConfig() {
  35. return YamlConfiguration.loadConfiguration(mcMMO.p.getResourceAsReader(fileName));
  36. }
  37. @Override
  38. protected void loadFile() {
  39. super.loadFile();
  40. FileConfiguration internalConfig = YamlConfiguration.loadConfiguration(mcMMO.p.getResourceAsReader(fileName));
  41. Set<String> configKeys = config.getKeys(true);
  42. Set<String> internalConfigKeys = internalConfig.getKeys(true);
  43. boolean needSave = false;
  44. // keys present in current config file that are not in the template
  45. Set<String> oldKeys = new HashSet<>(configKeys);
  46. oldKeys.removeAll(internalConfigKeys);
  47. if (!oldKeys.isEmpty()) {
  48. mcMMO.p.debug("old key(s) in \"" + fileName + "\"");
  49. for (String key : oldKeys) {
  50. mcMMO.p.debug(" old-key:" + key);
  51. }
  52. }
  53. // keys present in template that are not in current file
  54. Set<String> newKeys = new HashSet<>(internalConfigKeys);
  55. newKeys.removeAll(configKeys);
  56. if (!newKeys.isEmpty()) {
  57. needSave = true;
  58. }
  59. for (String key : newKeys) {
  60. mcMMO.p.debug("Adding new key: " + key + " = " + internalConfig.get(key));
  61. config.set(key, internalConfig.get(key));
  62. }
  63. if (needSave) {
  64. // Save it
  65. if (dataFolder == null) {
  66. mcMMO.p.getLogger().severe("Data folder should never be null!");
  67. return;
  68. }
  69. try {
  70. String saveName = fileName;
  71. // At this stage we cannot guarantee that Config has been loaded, so we do the check directly here
  72. if (!mcMMO.p.getConfig().getBoolean("General.Config_Update_Overwrite", true)) {
  73. saveName += ".new";
  74. }
  75. File newSaveFile = new File(dataFolder, saveName);
  76. YamlConfiguration yamlConfiguration = config;
  77. yamlConfiguration.options().indent(4);
  78. yamlConfiguration.save(newSaveFile);
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. }
  84. }