package de.jatitv.opsecurity.config.config; import de.jatitv.opsecurity.system.Main; import de.jatitv.opsecurity.system.NameHistory; import de.jatitv.opsecurity.util.send; 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.List; public class ConfigConvert { public static void convert() { try { renameConfig(); } catch (Exception e) { e.printStackTrace(); } try { configConvert(); } catch (Exception e) { e.printStackTrace(); } try { renameLanguages(); } catch (Exception e) { e.printStackTrace(); } } private static void renameConfig() { Path configOld = Paths.get(Main.getPath() + "/Config.yml"); Path config = Paths.get(Main.getPath() + "/config.yml"); if(Files.exists(configOld) && !Files.isDirectory(configOld)) { if(Files.exists(config) && !Files.isDirectory(config)) { return; } String replace = Main.getPath().toString() + "/"; send.convert(configOld.toString().replace(replace, ""), config.toString().replace(replace, "")); try { Files.move(configOld, config); } catch (IOException e) { e.printStackTrace(); } } } private static void configConvert() { File config = new File(Main.plugin.getDataFolder().getPath(), "config.yml"); YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(config); if (yamlConfiguration.get("ConfigVersion") == null) { try { if (yamlConfiguration.get("Plugin.language") != null) { switch (yamlConfiguration.getString("Plugin.language")) { case "de_DE": yamlConfiguration.set("Plugin.language", "german"); send.convert("config.yml", "Plugin.language: de_DE", "Plugin.language: german"); try { yamlConfiguration.save(config); } catch (IOException tac) { tac.printStackTrace(); } break; case "en_EN": yamlConfiguration.set("Plugin.language", "english"); send.convert("config.yml", "Plugin.language: en_EN", "Plugin.language: english"); try { yamlConfiguration.save(config); } catch (IOException tac) { tac.printStackTrace(); } break; case "no_NO": yamlConfiguration.set("Plugin.language", "norwegian"); send.convert("config.yml", "Plugin.language: no_NO", "Plugin.language: norwegian"); try { yamlConfiguration.save(config); } catch (IOException tac) { tac.printStackTrace(); } break; } } } catch (Exception e) { e.printStackTrace(); } if (yamlConfiguration.get("Timer") != null){ Boolean enable = yamlConfiguration.getBoolean("Timer.Enable"); Integer time = yamlConfiguration.getInt("Timer.RefreshTime_inSec"); yamlConfiguration.set("Check.Timer.Enable", enable); send.convert("config.yml", "Timer.Enable: " + enable, "Check.Timer.Enable: " + enable); yamlConfiguration.set("Check.Timer.RefreshTime_inSec", time); send.convert("config.yml", "Timer.RefreshTime_inSec: " + time, "Check.Timer.RefreshTime_inSec: " + time); yamlConfiguration.set("Timer", null); try { yamlConfiguration.save(config); } catch (IOException tac) { tac.printStackTrace(); } } List oldOPs = yamlConfiguration.getStringList("OP_Whitelist.Whitelist"); yamlConfiguration.set("OP_Whitelist.Whitelist", null); convertPlayer("OP_Whitelist.Whitelist", "OP_Whitelist.Whitelist.", oldOPs, yamlConfiguration); List oldPerms = yamlConfiguration.getStringList("Permission_Whitelist.Player"); yamlConfiguration.set("Permission_Whitelist.Player", null); convertPlayer("Permission_Whitelist.Player", "Permission_Whitelist.Whitelist.", oldPerms, yamlConfiguration); try { yamlConfiguration.save(config); } catch (IOException tac) { tac.printStackTrace(); } } } private static void convertPlayer(String oldPath, String newPath, List oldPlayer, YamlConfiguration yamlConfiguration) { for (String Player : oldPlayer) { try { String uuid = NameHistory.getPlayerUUID(Player); if (uuid == null) { send.warning(Main.plugin, "The UUID of the player §6" + Player + " §ecould not be found. Please check the config.yml and / or if the player exists / if the player name is correct!"); send.convert("config.yml", oldPath + ": " + Player, newPath + ": " + Player + ": UUID: Player UUID not found!"); yamlConfiguration.set(newPath + Player + ".UUID", "Player UUID not found!"); } else { send.convert("config.yml", oldPath + ": " + Player, newPath + ": " + Player + ": UUID: " + uuid); yamlConfiguration.set(newPath + Player + ".UUID", uuid); } } catch (IOException e) { e.printStackTrace(); } } } private static void renameLanguages() { String replace = Main.getPath().toString() + "/languages/"; Path messagesDEold = Paths.get(Main.getPath() + "/languages/de_DE_Messages.yml"); Path messagesDE = Paths.get(Main.getPath() + "/languages/german_messages.yml"); if(Files.exists(messagesDEold) && !Files.isDirectory(messagesDEold)) { if(Files.exists(messagesDE) && !Files.isDirectory(messagesDE)) { return; } send.convert(messagesDEold.toString().replace(replace, ""), messagesDE.toString().replace(replace, "")); try { Files.move(messagesDEold, messagesDE); } catch (IOException e) { e.printStackTrace(); } } Path messagesENold = Paths.get(Main.getPath() + "/languages/en_EN_Messages.yml"); Path messagesEN = Paths.get(Main.getPath() + "/languages/english_messages.yml"); if(Files.exists(messagesENold) && !Files.isDirectory(messagesENold)) { if(Files.exists(messagesEN) && !Files.isDirectory(messagesEN)) { return; } send.convert(messagesENold.toString().replace(replace, ""), messagesEN.toString().replace(replace, "")); try { Files.move(messagesENold, messagesEN); } catch (IOException e) { e.printStackTrace(); } } Path messagesNOold = Paths.get(Main.getPath() + "/languages/no_NO_Messages.yml"); Path messagesNO = Paths.get(Main.getPath() + "/languages/norwegian_messages.yml"); if(Files.exists(messagesNOold) && !Files.isDirectory(messagesNOold)) { if(Files.exists(messagesNO) && !Files.isDirectory(messagesNO)) { return; } send.convert(messagesNOold.toString().replace(replace, ""), messagesNO.toString().replace(replace, "")); try { Files.move(messagesNOold, messagesNO); } catch (IOException e) { e.printStackTrace(); } } } }