Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.robifoxx.block;
- import com.google.common.base.Charsets;
- import com.google.common.io.ByteStreams;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.nio.charset.Charset;
- import java.util.logging.Level;
- import me.robifoxx.block.Main;
- import org.bukkit.configuration.InvalidConfigurationException;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.plugin.PluginAwareness.Flags;
- public class Config {
- File f;
- FileConfiguration cfg;
- String path_;
- String fileName_;
- public Config(String path, String fileName) {
- this.path_ = path;
- this.fileName_ = fileName;
- }
- public void create() {
- this.f = new File(this.path_, this.fileName_);
- this.cfg = YamlConfiguration.loadConfiguration(this.f);
- }
- public void setDefault(String filename) {
- InputStream defConfigStream = Main.getProvidingPlugin(Main.class)
- .getResource(filename);
- if (defConfigStream != null) {
- YamlConfiguration defConfig;
- if (this.isStrictlyUTF8()) {
- defConfig = YamlConfiguration
- .loadConfiguration(new InputStreamReader(
- defConfigStream, Charsets.UTF_8));
- } else {
- defConfig = new YamlConfiguration();
- byte[] contents;
- try {
- contents = ByteStreams.toByteArray(defConfigStream);
- } catch (IOException arg7) {
- Main.getProvidingPlugin(Main.class)
- .getLogger()
- .log(Level.SEVERE,
- "Unexpected failure reading " + filename,
- arg7);
- return;
- }
- String text = new String(contents, Charset.defaultCharset());
- if (!text.equals(new String(contents, Charsets.UTF_8))) {
- Main.getProvidingPlugin(Main.class)
- .getLogger()
- .warning(
- "Default system encoding may have misread "
- + filename + " from plugin jar");
- }
- try {
- defConfig.loadFromString(text);
- } catch (InvalidConfigurationException arg6) {
- Main.getProvidingPlugin(Main.class)
- .getLogger()
- .log(Level.SEVERE,
- "Cannot load configuration from jar", arg6);
- }
- }
- this.cfg.setDefaults(defConfig);
- }
- }
- public InputStream getResource(String filename) {
- if (filename == null) {
- throw new IllegalArgumentException("Filename cannot be null");
- } else {
- try {
- ClassLoader classLoader = super.getClass().getClassLoader();
- URL url = classLoader.getResource(filename);
- if (url == null) {
- return null;
- } else {
- URLConnection connection = url.openConnection();
- connection.setUseCaches(false);
- return connection.getInputStream();
- }
- } catch (IOException arg4) {
- return null;
- }
- }
- }
- @SuppressWarnings("deprecation")
- private boolean isStrictlyUTF8() {
- return Main.getProvidingPlugin(Main.class).getDescription()
- .getAwareness().contains(Flags.UTF8);
- }
- public void saveConfig() {
- try {
- this.cfg.save(this.f);
- } catch (IOException arg1) {
- arg1.printStackTrace();
- }
- }
- public void reloadConfig() {
- this.cfg = YamlConfiguration.loadConfiguration(this.f);
- InputStream defConfigStream = this.getResource(this.fileName_);
- if (defConfigStream != null) {
- this.cfg.setDefaults(YamlConfiguration
- .loadConfiguration(new InputStreamReader(defConfigStream,
- Charsets.UTF_8)));
- }
- }
- public FileConfiguration getConfig() {
- return this.cfg;
- }
- public File toFile() {
- return this.f;
- }
- public boolean exists() {
- return this.f.exists();
- }
- }
Add Comment
Please, Sign In to add comment