Created
April 10, 2020 20:08
-
-
Save ryanhaticus/c3b0a5949e8a07a7131e397340579e33 to your computer and use it in GitHub Desktop.
An updating YamlConfiguration wrapper for Spigot plugins.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package me.view.anchorbans.data; | |
| import org.bukkit.configuration.InvalidConfigurationException; | |
| import org.bukkit.configuration.file.FileConfiguration; | |
| import org.bukkit.configuration.file.YamlConfiguration; | |
| import org.bukkit.plugin.java.JavaPlugin; | |
| import java.io.File; | |
| import java.io.IOException; | |
| public class UpdatingYamlConfiguration extends YamlConfiguration { | |
| private File configFile; | |
| private FileConfiguration config; | |
| private String fileName; | |
| private String rawPrefix; | |
| public UpdatingYamlConfiguration(JavaPlugin plugin, String name) { | |
| fileName = name + ".yml"; | |
| rawPrefix = "[" + plugin.getName() + "] "; | |
| configFile = new File(plugin.getDataFolder(), fileName); | |
| if (!configFile.exists()) { | |
| configFile.getParentFile().mkdirs(); | |
| plugin.saveResource(fileName, false); | |
| } | |
| loadFile(); | |
| String configVersion = getString("version"); | |
| String pluginVersion = plugin.getDescription().getVersion(); | |
| if (configVersion == null) { | |
| set("version", pluginVersion); | |
| save(); | |
| return; | |
| } | |
| if (configVersion.equals(pluginVersion)) { | |
| return; | |
| } | |
| YamlConfiguration clonedConfig; | |
| try { | |
| clonedConfig = (YamlConfiguration) this.clone(); | |
| } catch (CloneNotSupportedException e) { | |
| System.out.println(rawPrefix + e.getStackTrace()); | |
| return; | |
| } | |
| plugin.saveResource(fileName, true); | |
| for (String key : clonedConfig.getKeys(true)) { | |
| set(key, clonedConfig.get(key)); | |
| } | |
| save(); | |
| } | |
| private void loadFile() { | |
| try { | |
| load(configFile); | |
| } catch (InvalidConfigurationException | IOException e) { | |
| System.out.println(rawPrefix + e.getStackTrace()); | |
| } | |
| } | |
| public void save() { | |
| try { | |
| save(configFile); | |
| } catch (IOException e) { | |
| System.out.println(rawPrefix + e.getStackTrace()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment