Search in sources :

Example 1 with InvalidConfigurationException

use of org.bukkit.configuration.InvalidConfigurationException in project EliteMobs by MagmaGuy.

the class CustomConfigConstructor method reload.

/**
     * Reload configuration
     */
public void reload() {
    if (!file.exists()) {
        try {
            file.getParentFile().mkdirs();
            file.createNewFile();
        } catch (IOException exception) {
            exception.printStackTrace();
            plugin.getLogger().severe("Error while creating file " + file.getName());
        }
    }
    try {
        load(file);
        if (defaults != null) {
            InputStreamReader reader = new InputStreamReader(plugin.getResource(defaults));
            FileConfiguration defaultsConfig = YamlConfiguration.loadConfiguration(reader);
            setDefaults(defaultsConfig);
            options().copyDefaults(true);
            reader.close();
            save();
        }
    } catch (IOException exception) {
        exception.printStackTrace();
        plugin.getLogger().severe("Error while loading file " + file.getName());
    } catch (InvalidConfigurationException exception) {
        exception.printStackTrace();
        plugin.getLogger().severe("Error while loading file " + file.getName());
    }
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 2 with InvalidConfigurationException

use of org.bukkit.configuration.InvalidConfigurationException in project TotalFreedomMod by TotalFreedom.

the class MainConfig method load.

public void load() {
    try {
        YamlConfiguration config = new YamlConfiguration();
        config.load(getConfigFile());
        for (ConfigEntry entry : ConfigEntry.values()) {
            String path = entry.getConfigName();
            if (config.contains(path)) {
                Object value = config.get(path);
                if (value == null || entry.getType().isAssignableFrom(value.getClass())) {
                    entries.put(entry, value);
                } else {
                    FLog.warning("Value for " + entry.getConfigName() + " is of type " + value.getClass().getSimpleName() + ". Needs to be " + entry.getType().getSimpleName() + ". Using default value.");
                }
            } else {
                FLog.warning("Missing configuration entry " + entry.getConfigName() + ". Using default value.");
            }
        }
    } catch (IOException | InvalidConfigurationException ex) {
        FLog.severe(ex);
    }
}
Also used : IOException(java.io.IOException) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 3 with InvalidConfigurationException

use of org.bukkit.configuration.InvalidConfigurationException in project Bukkit by Bukkit.

the class YamlConfiguration method loadFromString.

@Override
public void loadFromString(String contents) throws InvalidConfigurationException {
    Validate.notNull(contents, "Contents cannot be null");
    Map<?, ?> input;
    try {
        input = (Map<?, ?>) yaml.load(contents);
    } catch (YAMLException e) {
        throw new InvalidConfigurationException(e);
    } catch (ClassCastException e) {
        throw new InvalidConfigurationException("Top level is not a Map.");
    }
    String header = parseHeader(contents);
    if (header.length() > 0) {
        options().header(header);
    }
    if (input != null) {
        convertMapsToSections(input, this);
    }
}
Also used : YAMLException(org.yaml.snakeyaml.error.YAMLException) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 4 with InvalidConfigurationException

use of org.bukkit.configuration.InvalidConfigurationException in project Glowstone by GlowstoneMC.

the class ServerConfig method migrate.

private boolean migrate() {
    boolean migrateStatus = false;
    File bukkitYml = new File("bukkit.yml");
    if (bukkitYml.exists()) {
        YamlConfiguration bukkit = new YamlConfiguration();
        try {
            bukkit.load(bukkitYml);
        } catch (InvalidConfigurationException e) {
            report(bukkitYml, e);
        } catch (IOException e) {
            GlowServer.logger.log(Level.WARNING, "Could not migrate from " + bukkitYml, e);
        }
        for (Key key : Key.values()) {
            if (key.migrate == Migrate.BUKKIT && bukkit.contains(key.migratePath)) {
                config.set(key.path, bukkit.get(key.migratePath));
                migrateStatus = true;
            }
        }
        config.set("aliases", bukkit.get("aliases"));
        config.set("worlds", bukkit.get("worlds"));
    }
    File serverProps = new File("server.properties");
    if (serverProps.exists()) {
        Properties props = new Properties();
        try {
            props.load(new FileInputStream(serverProps));
        } catch (IOException e) {
            GlowServer.logger.log(Level.WARNING, "Could not migrate from " + serverProps, e);
        }
        for (Key key : Key.values()) {
            if (key.migrate == Migrate.PROPS && props.containsKey(key.migratePath)) {
                String value = props.getProperty(key.migratePath);
                if (key.def instanceof Integer) {
                    try {
                        config.set(key.path, Integer.parseInt(value));
                    } catch (NumberFormatException e) {
                        GlowServer.logger.log(Level.WARNING, "Could not migrate " + key.migratePath + " from " + serverProps, e);
                        continue;
                    }
                } else if (key.def instanceof Boolean) {
                    config.set(key.path, Boolean.parseBoolean(value));
                } else {
                    config.set(key.path, value);
                }
                migrateStatus = true;
            }
        }
    }
    return migrateStatus;
}
Also used : YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) Properties(java.util.Properties) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 5 with InvalidConfigurationException

use of org.bukkit.configuration.InvalidConfigurationException in project Bukkit by Bukkit.

the class JavaPlugin method reloadConfig.

@SuppressWarnings("deprecation")
@Override
public void reloadConfig() {
    newConfig = YamlConfiguration.loadConfiguration(configFile);
    final InputStream defConfigStream = getResource("config.yml");
    if (defConfigStream == null) {
        return;
    }
    final YamlConfiguration defConfig;
    if (isStrictlyUTF8() || FileConfiguration.UTF8_OVERRIDE) {
        defConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream, Charsets.UTF_8));
    } else {
        final byte[] contents;
        defConfig = new YamlConfiguration();
        try {
            contents = ByteStreams.toByteArray(defConfigStream);
        } catch (final IOException e) {
            getLogger().log(Level.SEVERE, "Unexpected failure reading config.yml", e);
            return;
        }
        final String text = new String(contents, Charset.defaultCharset());
        if (!text.equals(new String(contents, Charsets.UTF_8))) {
            getLogger().warning("Default system encoding may have misread config.yml from plugin jar");
        }
        try {
            defConfig.loadFromString(text);
        } catch (final InvalidConfigurationException e) {
            getLogger().log(Level.SEVERE, "Cannot load configuration from jar", e);
        }
    }
    newConfig.setDefaults(defConfig);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Aggregations

InvalidConfigurationException (org.bukkit.configuration.InvalidConfigurationException)8 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)5 IOException (java.io.IOException)4 InputStreamReader (java.io.InputStreamReader)2 File (java.io.File)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 CharBuffer (java.nio.CharBuffer)1 CharsetDecoder (java.nio.charset.CharsetDecoder)1 CoderResult (java.nio.charset.CoderResult)1 Properties (java.util.Properties)1 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)1 PluginDescriptionFile (org.bukkit.plugin.PluginDescriptionFile)1 PluginLogger (org.bukkit.plugin.PluginLogger)1 YAMLException (org.yaml.snakeyaml.error.YAMLException)1