Added config.yml autoupdate

This commit is contained in:
Auxilor
2020-10-15 12:14:49 +01:00
committed by BuildTools
parent e9e92a1d3c
commit cd1feb4f42
5 changed files with 25 additions and 16 deletions

View File

@@ -12,7 +12,6 @@ import java.util.Set;
public class ConfigManager {
public static final HashMap<String, Double> configVersions = new HashMap<String, Double>() {{
put("config", 5.0);
put("target", 1.0);
put("rarity", 1.0);
}};
@@ -29,7 +28,7 @@ public class ConfigManager {
*/
public static void updateConfigs() {
LANG.update();
CONFIG.reload();
CONFIG.update();
TARGET.reload();
RARITY.reload();
updateEnchantmentConfigs();

View File

@@ -11,34 +11,38 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public abstract class UpdatingLang {
public abstract class UpdatingYamlConfig {
public YamlConfiguration config;
private File configFile;
private final String name;
private final boolean removeUnused;
public UpdatingLang() {
public UpdatingYamlConfig(String name, boolean removeUnused) {
this.name = name + ".yml";
this.removeUnused = removeUnused;
init();
}
private void init() {
if (!new File(EcoEnchantsPlugin.getInstance().getDataFolder(), "lang.yml").exists()) {
if (!new File(EcoEnchantsPlugin.getInstance().getDataFolder(), name).exists()) {
createFile();
}
this.configFile = new File(EcoEnchantsPlugin.getInstance().getDataFolder(), "lang.yml");
this.configFile = new File(EcoEnchantsPlugin.getInstance().getDataFolder(), name);
this.config = YamlConfiguration.loadConfiguration(configFile);
update();
}
private void createFile() {
EcoEnchantsPlugin.getInstance().saveResource("lang.yml", false);
EcoEnchantsPlugin.getInstance().saveResource(name, false);
}
public void update() {
try {
config.load(configFile);
InputStream newIn = EcoEnchantsPlugin.getInstance().getResource("lang.yml");
InputStream newIn = EcoEnchantsPlugin.getInstance().getResource(name);
BufferedReader reader = new BufferedReader(new InputStreamReader(newIn, StandardCharsets.UTF_8));
YamlConfiguration newConfig = new YamlConfiguration();
newConfig.load(reader);
@@ -52,6 +56,14 @@ public abstract class UpdatingLang {
}
}));
if(this.removeUnused) {
config.getKeys(true).forEach((s -> {
if(!newConfig.getKeys(true).contains(s)) {
config.set(s, null);
}
}));
}
config.save(configFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();

View File

@@ -1,6 +1,6 @@
package com.willfp.ecoenchants.config.configs;
import com.willfp.ecoenchants.config.YamlConfig;
import com.willfp.ecoenchants.config.UpdatingYamlConfig;
import org.bukkit.inventory.ItemStack;
import java.util.List;
@@ -8,9 +8,9 @@ import java.util.List;
/**
* Wrapper for config.yml
*/
public class Config extends YamlConfig {
public class Config extends UpdatingYamlConfig {
public Config() {
super("config");
super("config", true);
}
public int getInt(String path) {

View File

@@ -1,6 +1,6 @@
package com.willfp.ecoenchants.config.configs;
import com.willfp.ecoenchants.config.UpdatingLang;
import com.willfp.ecoenchants.config.UpdatingYamlConfig;
import com.willfp.ecoenchants.util.StringUtils;
import java.util.List;
@@ -8,9 +8,9 @@ import java.util.List;
/**
* Wrapper for lang.yml
*/
public class Lang extends UpdatingLang {
public class Lang extends UpdatingYamlConfig {
public Lang() {
super();
super("lang", false);
}
public String getString(String path) {

View File

@@ -3,8 +3,6 @@
# by Auxilor
#
config-version: 5.0 # Don't edit this.
anvil:
allow-unsafe-levels: false # Allow unsafe enchantments like Sharpness 6 by combining 2 Sharp 5.
allow-combining-unsafe: true # Allow further combining unsafe levels, eg Sharp 6 + Sharp 6 = Sharp 7.