Update Config.java

Legacy File Conversions.
This commit is contained in:
jameslfc19 2020-07-02 14:08:33 +01:00
parent 6b09dec908
commit 2bca010f13

View File

@ -1,24 +1,29 @@
package com.jamesdpeters.minecraft.chests.serialize;
import com.google.common.base.Charsets;
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.storage.AutoCraftingStorageType;
import com.jamesdpeters.minecraft.chests.storage.ChestLinkStorageType;
import com.jamesdpeters.minecraft.chests.storage.StorageType;
import com.jamesdpeters.minecraft.chests.storage.autocraft.AutoCraftingStorageType;
import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorageType;
import com.jamesdpeters.minecraft.chests.storage.abstracts.StorageType;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class Config {
private static LinkedChest store;
private static ConfigStorage store;
private static FileConfiguration config;
private static String saveName = "/data/storage.yml";
/* ALL STORAGE TYPES */
private static ChestLinkStorageType chestLinkStorageType;
private static AutoCraftingStorageType autoCraftingStorageType;
@ -26,12 +31,19 @@ public class Config {
private static List<StorageType> storageTypes;
public Config() {
legacyConverter();
try {
config = YamlConfiguration.loadConfiguration(new File("chests.yml"));
store = (LinkedChest) config.get("chests++", new HashMap<String, HashMap<String, List<Location>>>());
config = YamlConfiguration.loadConfiguration(getStorageFile());
} catch (IllegalArgumentException e){
ChestsPlusPlus.PLUGIN.getLogger().severe("Config was null or couldn't be read!");
config = new YamlConfiguration();
}
try {
store = (ConfigStorage) config.get("chests++", new ConfigStorage());
} catch (Exception e) {
store = new LinkedChest();
save();
store = new ConfigStorage();
saveASync();
}
chestLinkStorageType = new ChestLinkStorageType(store);
autoCraftingStorageType = new AutoCraftingStorageType(store);
@ -46,7 +58,7 @@ public class Config {
if (config == null) config = new YamlConfiguration();
config.set("chests++", store);
try {
config.save("chests.yml");
config.save(getStorageFile());
} catch (IOException e) {
e.printStackTrace();
}
@ -76,4 +88,37 @@ public class Config {
}
return null;
}
private static File getStorageFile(){
File pluginDataFolder = ChestsPlusPlus.PLUGIN.getDataFolder();
return new File(pluginDataFolder, saveName);
}
private File getLegacyFile(){
return new File("chests.yml");
}
private void legacyConverter(){
File legacyFile = getLegacyFile();
if(!legacyFile.exists()) return;
ChestsPlusPlus.PLUGIN.getLogger().info("Found a Legacy config! Converting to new data-format and moving to: /plugins/ChestsPlusPlus/data/storage.yml");
ChestsPlusPlus.PLUGIN.getLogger().info("If you are having issues with data-loss the plugin may not have permissions to delete the legacy file 'chests.yml'");
try {
Path path = Paths.get(legacyFile.toURI());
String content = new String(Files.readAllBytes(path),Charsets.UTF_8);
content = legacyContentConverter(content);
Files.write(getStorageFile().toPath(), content.getBytes(Charsets.UTF_8));
legacyFile.createNewFile();
legacyFile.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
private String legacyContentConverter(String content){
content = content.replaceAll("==: LinkedChest", "==: ConfigStorage");
content = content.replaceAll("==: com.jamesdpeters.minecraft.chests.storage.InventoryStorage", "==: ChestLinkStorage");
return content;
}
}