Last active
May 1, 2021 14:13
-
-
Save SagaciousZed/3174347 to your computer and use it in GitHub Desktop.
Utility to access yaml files in bukkit
This file contains 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
/* | |
* Copyright (C) 2012 | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.logging.Level; | |
import org.bukkit.configuration.file.FileConfiguration; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class ConfigAccessor { | |
private final String fileName; | |
private final JavaPlugin plugin; | |
private File configFile; | |
private FileConfiguration fileConfiguration; | |
public ConfigAccessor(JavaPlugin plugin, String fileName) { | |
if (plugin == null) | |
throw new IllegalArgumentException("plugin cannot be null"); | |
if (!plugin.isInitialized()) | |
throw new IllegalArgumentException("plugin must be initialized"); | |
this.plugin = plugin; | |
this.fileName = fileName; | |
File dataFolder = plugin.getDataFolder(); | |
if (dataFolder == null) | |
throw new IllegalStateException(); | |
this.configFile = new File(plugin.getDataFolder(), fileName); | |
} | |
public void reloadConfig() { | |
fileConfiguration = YamlConfiguration.loadConfiguration(configFile); | |
// Look for defaults in the jar | |
InputStream defConfigStream = plugin.getResource(fileName); | |
if (defConfigStream != null) { | |
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream); | |
fileConfiguration.setDefaults(defConfig); | |
} | |
} | |
public FileConfiguration getConfig() { | |
if (fileConfiguration == null) { | |
this.reloadConfig(); | |
} | |
return fileConfiguration; | |
} | |
public void saveConfig() { | |
if (fileConfiguration == null || configFile == null) { | |
return; | |
} else { | |
try { | |
getConfig().save(configFile); | |
} catch (IOException ex) { | |
plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex); | |
} | |
} | |
} | |
public void saveDefaultConfig() { | |
if (!configFile.exists()) { | |
this.plugin.saveResource(fileName, false); | |
} | |
} | |
} |
What do I have to change if I want to have the Defaults in a different place in the Jar?
YamlConfiguration.loadConfiguration(InputStream stream) is now deprecated. I believe it should be replaced with the loadConfiguration(Reader reader) method, like what I did here: https://gist.github.com/MrZoraman/a4dd438b75d48898f3b3. I did a little testing and it didn't seem to break anything.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why don't we package this as a CustomConfig class in the bukkit api?