|
| 1 | +/* |
| 2 | + * MiniGamesBox - Library box with massive content that could be seen as minigames core. |
| 3 | + * Copyright (C) 2023 Plugily Projects - maintained by Tigerpanzer_02 and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | + |
| 20 | +package plugily.projects.buildbattle.handlers; |
| 21 | + |
| 22 | +import org.bukkit.configuration.file.FileConfiguration; |
| 23 | +import plugily.projects.buildbattle.Main; |
| 24 | +import plugily.projects.minigamesbox.classic.PluginMain; |
| 25 | +import plugily.projects.minigamesbox.classic.utils.configuration.ConfigUtils; |
| 26 | +import plugily.projects.minigamesbox.classic.utils.migrator.MigratorUtils; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.io.IOException; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Paths; |
| 32 | +import java.util.logging.Level; |
| 33 | + |
| 34 | +/* |
| 35 | + NOTE FOR CONTRIBUTORS - Please do not touch this class if you don't know how it works! You can break migrator modifying these values! |
| 36 | + */ |
| 37 | + |
| 38 | +/** |
| 39 | + * @author Tigerpanzer_02 |
| 40 | + * <p> |
| 41 | + * Created at 21.09.2021 |
| 42 | + */ |
| 43 | +@SuppressWarnings("deprecation") |
| 44 | +public class LanguageMigrator { |
| 45 | + |
| 46 | + public enum PluginFileVersion { |
| 47 | + /*ARENA_SELECTOR(0),*/ BUNGEE(1), CONFIG(1), LANGUAGE(2), |
| 48 | + /*LEADERBOARDS(0),*/ MYSQL(1), PERMISSIONS(1), POWERUPS(1), |
| 49 | + /*SIGNS(0),*/ SPECIAL_ITEMS(1), SPECTATOR(1)/*, STATS(0)*/; |
| 50 | + |
| 51 | + private final int version; |
| 52 | + |
| 53 | + PluginFileVersion(int version) { |
| 54 | + this.version = version; |
| 55 | + } |
| 56 | + |
| 57 | + public int getVersion() { |
| 58 | + return version; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private final Main plugin; |
| 63 | + |
| 64 | + public LanguageMigrator(Main plugin) { |
| 65 | + this.plugin = plugin; |
| 66 | + updatePluginFiles(); |
| 67 | + } |
| 68 | + |
| 69 | + private void updatePluginFiles() { |
| 70 | + for(PluginFileVersion pluginFileVersion : PluginFileVersion.values()) { |
| 71 | + String fileName = pluginFileVersion.name().toLowerCase(); |
| 72 | + int newVersion = pluginFileVersion.getVersion(); |
| 73 | + File file = new File(plugin.getDataFolder() + "/" + fileName + ".yml"); |
| 74 | + FileConfiguration configuration = ConfigUtils.getConfig(plugin, fileName, false); |
| 75 | + if(configuration == null) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + int oldVersion = configuration.getInt("Do-Not-Edit.File-Version", 0); |
| 79 | + if(oldVersion == newVersion) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + plugin.getDebugger().debug(Level.WARNING, "[System notify] The " + fileName + " file is outdated! Updating..."); |
| 83 | + for(int i = oldVersion; i < newVersion; i++) { |
| 84 | + executeUpdate(file, pluginFileVersion, i); |
| 85 | + } |
| 86 | + |
| 87 | + updatePluginFileVersion(file, configuration, oldVersion, newVersion); |
| 88 | + plugin.getDebugger().debug(Level.WARNING, "[System notify] " + fileName + " updated, no comments were removed :)"); |
| 89 | + plugin.getDebugger().debug(Level.WARNING, "[System notify] You're using latest " + fileName + " file now! Nice!"); |
| 90 | + |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void executeUpdate(File file, PluginFileVersion pluginFileVersion, int version) { |
| 95 | + switch(pluginFileVersion) { |
| 96 | + case LANGUAGE: |
| 97 | + switch(version) { |
| 98 | + case 1: |
| 99 | + MigratorUtils.insertAfterLine(file, " Heads:", " Database:\n" + |
| 100 | + " Lore: \"Get Head %value%\""); |
| 101 | + break; |
| 102 | + default: |
| 103 | + break; |
| 104 | + } |
| 105 | + break; |
| 106 | + default: |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public void updatePluginFileVersion(File file, FileConfiguration fileConfiguration, int oldVersion, int newVersion) { |
| 112 | + int coreVersion = fileConfiguration.getInt("Do-Not-Edit.Core-Version", 0); |
| 113 | + updateFileVersion(file, coreVersion, coreVersion, newVersion, oldVersion); |
| 114 | + } |
| 115 | + |
| 116 | + private void updateFileVersion(File file, int coreVersion, int oldCoreVersion, int fileVersion, int oldFileVersion) { |
| 117 | + MigratorUtils.removeLineFromFile(file, "# Don't edit it. But who's stopping you? It's your server!"); |
| 118 | + MigratorUtils.removeLineFromFile(file, "# Really, don't edit ;p"); |
| 119 | + MigratorUtils.removeLineFromFile(file, "# You edited it, huh? Next time hurt yourself!"); |
| 120 | + MigratorUtils.removeLineFromFile(file, "Do-Not-Edit:"); |
| 121 | + MigratorUtils.removeLineFromFile(file, " File-Version: " + oldFileVersion + ""); |
| 122 | + MigratorUtils.removeLineFromFile(file, " Core-Version: " + oldCoreVersion + ""); |
| 123 | + MigratorUtils.addNewLines(file, "# Don't edit it. But who's stopping you? It's your server!\r\n" + |
| 124 | + "# Really, don't edit ;p\r\n" + |
| 125 | + "# You edited it, huh? Next time hurt yourself!\r\n" + |
| 126 | + "Do-Not-Edit:\r\n" + |
| 127 | + " File-Version: " + fileVersion + "\r\n" + |
| 128 | + " Core-Version: " + coreVersion + "\r\n"); |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments