|
| 1 | +/* |
| 2 | + * This file is part of ToolStats. |
| 3 | + * |
| 4 | + * ToolStats is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * ToolStats is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with ToolStats. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package lol.hyper.toolstats.tools.config.versions; |
| 19 | + |
| 20 | +import lol.hyper.toolstats.ToolStats; |
| 21 | +import org.bukkit.configuration.ConfigurationSection; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | + |
| 26 | +public class Version13 { |
| 27 | + |
| 28 | + private final ToolStats toolStats; |
| 29 | + |
| 30 | + /** |
| 31 | + * Used for updating from version 12 to 13. |
| 32 | + * |
| 33 | + * @param toolStats ToolStats instance. |
| 34 | + */ |
| 35 | + public Version13(ToolStats toolStats) { |
| 36 | + this.toolStats = toolStats; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Perform the config update. |
| 41 | + */ |
| 42 | + public void update() { |
| 43 | + // save the old config first |
| 44 | + try { |
| 45 | + toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config-12.yml"); |
| 46 | + } catch (IOException exception) { |
| 47 | + toolStats.logger.severe("Unable to save config-12.yml!"); |
| 48 | + throw new RuntimeException(exception); |
| 49 | + } |
| 50 | + |
| 51 | + toolStats.logger.info("Updating config.yml to version 13."); |
| 52 | + toolStats.config.set("config-version", 13); |
| 53 | + |
| 54 | + for (String key : toolStats.config.getConfigurationSection("tokens.data").getKeys(false)) { |
| 55 | + toolStats.logger.info("Adding tokens.data." + key + ".material"); |
| 56 | + toolStats.config.set("tokens.data." + key + ".material", "PAPER"); |
| 57 | + toolStats.logger.info("Adding tokens.data." + key + ".custom-model-data.enabled"); |
| 58 | + toolStats.config.set("tokens.data." + key + ".custom-model-data.enabled", false); |
| 59 | + toolStats.logger.info("Adding tokens.data." + key + ".custom-model-data.type"); |
| 60 | + toolStats.config.set("tokens.data." + key + ".custom-model-data.type", "float"); |
| 61 | + toolStats.logger.info("Adding tokens.data." + key + ".custom-model-data.value"); |
| 62 | + toolStats.config.set("tokens.data." + key + ".custom-model-data.value", 1001); |
| 63 | + } |
| 64 | + |
| 65 | + // save the config and reload it |
| 66 | + try { |
| 67 | + toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config.yml"); |
| 68 | + } catch (IOException exception) { |
| 69 | + toolStats.logger.severe("Unable to save config.yml!"); |
| 70 | + throw new RuntimeException(exception); |
| 71 | + } |
| 72 | + toolStats.loadConfig(); |
| 73 | + toolStats.logger.info("Config has been updated to version 13. A copy of version 12 has been saved as config-12.yml"); |
| 74 | + } |
| 75 | +} |
0 commit comments