Skip to content

Instantly share code, notes, and snippets.

@MrZoraman
Forked from SagaciousZed/ConfigAccessor.java
Last active June 15, 2016 19:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MrZoraman/a4dd438b75d48898f3b3 to your computer and use it in GitHub Desktop.
Save MrZoraman/a4dd438b75d48898f3b3 to your computer and use it in GitHub Desktop.
/*
* 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.io.InputStreamReader;
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");
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(new InputStreamReader(defConfigStream));
fileConfiguration.setDefaults(defConfig);
}
}
public FileConfiguration getConfig() {
if (fileConfiguration == null) {
this.reloadConfig();
}
return fileConfiguration;
}
public void saveConfig() {
if (fileConfiguration != null && configFile != null) {
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);
}
}
}
@deanveloper
Copy link

loadResource(InputStream) is now deprecated, instead use loadResource(Reader)

This is easily done by doing InputStream#reader()

Copy link

ghost commented Apr 5, 2016

Thanks man ive been struggling with this alot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment