Search in sources :

Example 6 with InvalidConfigurationException

use of org.bukkit.configuration.InvalidConfigurationException in project Dragonet-Legacy by DragonetMC.

the class PluginAdapter method initialize.

public void initialize(String name) {
    if (initialized)
        return;
    this.name = name;
    dataFolder = new File(server.getDragonetServer().getPluginFolder(), this.getName().replace(".", "_").concat("-data"));
    if (dataFolder.isFile()) {
        server.getLogger().warning("Faild to load plugin [" + getName() + "] due to plugin folder is occupied by a regular file. ");
        throw new IllegalStateException("Plugin folder for [" + getName() + "] is occupied by a regular file. ");
    }
    config = new YamlConfiguration();
    try {
        config.load(new File(dataFolder, "config.yml"));
    } catch (IOException | InvalidConfigurationException ex) {
    }
    logger = new PluginLogger(this);
    initialized = true;
}
Also used : IOException(java.io.IOException) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) File(java.io.File) PluginDescriptionFile(org.bukkit.plugin.PluginDescriptionFile) PluginLogger(org.bukkit.plugin.PluginLogger) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 7 with InvalidConfigurationException

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

the class ServerConfig method getConfigFile.

////////////////////////////////////////////////////////////////////////////
// Fancy stuff
public ConfigurationSection getConfigFile(Key key) {
    String filename = getString(key);
    if (extraConfig.containsKey(filename)) {
        return extraConfig.get(filename);
    }
    YamlConfiguration conf = new YamlConfiguration();
    File file = getFile(filename), migrateFrom = new File(key.def.toString());
    // create file if it doesn't exist
    if (!file.exists()) {
        if (migrateFrom.exists()) {
            FileUtil.copy(migrateFrom, file);
        } else {
            copyDefaults(key.def.toString(), file);
        }
    }
    // read in config
    try {
        conf.load(file);
    } catch (IOException e) {
        GlowServer.logger.log(Level.SEVERE, "Failed to read config: " + file, e);
    } catch (InvalidConfigurationException e) {
        report(file, e);
    }
    extraConfig.put(filename, conf);
    return conf;
}
Also used : YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 8 with InvalidConfigurationException

use of org.bukkit.configuration.InvalidConfigurationException in project Essentials by drtshock.

the class EssentialsConf method load.

public synchronized void load() {
    if (pendingDiskWrites.get() != 0) {
        LOGGER.log(Level.INFO, "File {0} not read, because it''s not yet written to disk.", configFile);
        return;
    }
    if (!configFile.getParentFile().exists()) {
        if (!configFile.getParentFile().mkdirs()) {
            LOGGER.log(Level.SEVERE, tl("failedToCreateConfig", configFile.toString()));
        }
    }
    // This will delete files where the first character is 0. In most cases they are broken.
    if (configFile.exists() && configFile.length() != 0) {
        try {
            final InputStream input = new FileInputStream(configFile);
            try {
                if (input.read() == 0) {
                    input.close();
                    configFile.delete();
                }
            } catch (IOException ex) {
                LOGGER.log(Level.SEVERE, null, ex);
            } finally {
                try {
                    input.close();
                } catch (IOException ex) {
                    LOGGER.log(Level.SEVERE, null, ex);
                }
            }
        } catch (FileNotFoundException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        }
    }
    if (!configFile.exists()) {
        if (legacyFileExists()) {
            convertLegacyFile();
        } else if (altFileExists()) {
            convertAltFile();
        } else if (templateName != null) {
            LOGGER.log(Level.INFO, tl("creatingConfigFromTemplate", configFile.toString()));
            createFromTemplate();
        } else {
            return;
        }
    }
    try {
        try (FileInputStream inputStream = new FileInputStream(configFile)) {
            long startSize = configFile.length();
            if (startSize > Integer.MAX_VALUE) {
                throw new InvalidConfigurationException("File too big");
            }
            ByteBuffer buffer = ByteBuffer.allocate((int) startSize);
            int length;
            while ((length = inputStream.read(bytebuffer)) != -1) {
                if (length > buffer.remaining()) {
                    ByteBuffer resize = ByteBuffer.allocate(buffer.capacity() + length - buffer.remaining());
                    int resizePosition = buffer.position();
                    buffer.rewind();
                    resize.put(buffer);
                    resize.position(resizePosition);
                    buffer = resize;
                }
                buffer.put(bytebuffer, 0, length);
            }
            buffer.rewind();
            final CharBuffer data = CharBuffer.allocate(buffer.capacity());
            CharsetDecoder decoder = UTF8.newDecoder();
            CoderResult result = decoder.decode(buffer, data, true);
            if (result.isError()) {
                buffer.rewind();
                data.clear();
                LOGGER.log(Level.INFO, "File " + configFile.getAbsolutePath() + " is not utf-8 encoded, trying " + Charset.defaultCharset().displayName());
                decoder = Charset.defaultCharset().newDecoder();
                result = decoder.decode(buffer, data, true);
                if (result.isError()) {
                    throw new InvalidConfigurationException("Invalid Characters in file " + configFile.getAbsolutePath());
                } else {
                    decoder.flush(data);
                }
            } else {
                decoder.flush(data);
            }
            final int end = data.position();
            data.rewind();
            super.loadFromString(data.subSequence(0, end).toString());
        }
    } catch (IOException ex) {
        LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
    } catch (InvalidConfigurationException ex) {
        File broken = new File(configFile.getAbsolutePath() + ".broken." + System.currentTimeMillis());
        configFile.renameTo(broken);
        LOGGER.log(Level.SEVERE, "The file " + configFile.toString() + " is broken, it has been renamed to " + broken.toString(), ex.getCause());
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException) CoderResult(java.nio.charset.CoderResult)

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