ninja.leaping.configurate.yaml.YAMLConfigurationLoader Java Examples

The following examples show how to use ninja.leaping.configurate.yaml.YAMLConfigurationLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigLoader.java    From NuVotifier with GNU General Public License v3.0 6 votes vote down vote up
public static void loadConfig(NuVotifier pl) {
    if (!pl.getConfigDir().exists()) {
        if (!pl.getConfigDir().mkdirs()) {
            throw new RuntimeException("Unable to create the plugin data folder " + pl.getConfigDir());
        }
    }
    try {
        File config = new File(pl.getConfigDir(), "config.yml");
        if (!config.exists() && !config.createNewFile()) {
            throw new IOException("Unable to create the config file at " + config);
        }
        ConfigurationLoader loader = YAMLConfigurationLoader.builder().setFile(config).build();
        ConfigurationNode configNode = loader.load(ConfigurationOptions.defaults().setShouldCopyDefaults(true));
        spongeConfig = configNode.getValue(TypeToken.of(SpongeConfig.class), new SpongeConfig());
        loader.save(configNode);
    } catch (Exception e) {
        pl.getLogger().error("Could not load config.", e);
    }
}
 
Example #2
Source File: ConfigManager.java    From BlueMap with MIT License 5 votes vote down vote up
private ConfigurationLoader<? extends ConfigurationNode> getLoader(String filename, InputStream is){
	BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
	
	if (filename.endsWith(".json")) return GsonConfigurationLoader.builder().setSource(() -> reader).build();
	if (filename.endsWith(".yaml") || filename.endsWith(".yml")) return YAMLConfigurationLoader.builder().setSource(() -> reader).build();
	else return HoconConfigurationLoader.builder().setSource(() -> reader).build();
}
 
Example #3
Source File: ConfigurationFileUtil.java    From AntiVPN with MIT License 5 votes vote down vote up
public static Configuration getConfig(Plugin plugin, String resourcePath, File fileOnDisk) throws IOException {
    File parentDir = fileOnDisk.getParentFile();
    if (parentDir.exists() && !parentDir.isDirectory()) {
        Files.delete(parentDir.toPath());
    }
    if (!parentDir.exists()) {
        if (!parentDir.mkdirs()) {
            throw new IOException("Could not create parent directory structure.");
        }
    }
    if (fileOnDisk.exists() && fileOnDisk.isDirectory()) {
        Files.delete(fileOnDisk.toPath());
    }

    if (!fileOnDisk.exists()) {
        try (InputStreamReader reader = new InputStreamReader(plugin.getResourceAsStream(resourcePath));
             BufferedReader in = new BufferedReader(reader);
             FileWriter writer = new FileWriter(fileOnDisk);
             BufferedWriter out = new BufferedWriter(writer)) {
            String line;
            while ((line = in.readLine()) != null) {
                out.write(line + System.lineSeparator());
            }
        }
    }

    ConfigurationLoader<ConfigurationNode> loader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setFile(fileOnDisk).build();
    ConfigurationNode root = loader.load(ConfigurationOptions.defaults().setHeader("Comments are gone because update :(. Click here for new config + comments: https://www.spigotmc.org/resources/anti-vpn-bungee.58716/"));
    Configuration config = new Configuration(root);
    ConfigurationVersionUtil.conformVersion(loader, config, fileOnDisk);

    return config;
}
 
Example #4
Source File: ConfigurationFileUtil.java    From AntiVPN with MIT License 5 votes vote down vote up
public static Configuration getConfig(Plugin plugin, String resourcePath, File fileOnDisk) throws IOException {
    File parentDir = fileOnDisk.getParentFile();
    if (parentDir.exists() && !parentDir.isDirectory()) {
        Files.delete(parentDir.toPath());
    }
    if (!parentDir.exists()) {
        if (!parentDir.mkdirs()) {
            throw new IOException("Could not create parent directory structure.");
        }
    }
    if (fileOnDisk.exists() && fileOnDisk.isDirectory()) {
        Files.delete(fileOnDisk.toPath());
    }

    if (!fileOnDisk.exists()) {
        try (InputStreamReader reader = new InputStreamReader(plugin.getResource(resourcePath));
             BufferedReader in = new BufferedReader(reader);
             FileWriter writer = new FileWriter(fileOnDisk);
             BufferedWriter out = new BufferedWriter(writer)) {
            String line;
            while ((line = in.readLine()) != null) {
                out.write(line + System.lineSeparator());
            }
        }
    }

    ConfigurationLoader<ConfigurationNode> loader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setFile(fileOnDisk).build();
    ConfigurationNode root = loader.load(ConfigurationOptions.defaults().setHeader("Comments are gone because update :(. Click here for new config + comments: https://www.spigotmc.org/resources/anti-vpn.58291/"));
    Configuration config = new Configuration(root);
    ConfigurationVersionUtil.conformVersion(loader, config, fileOnDisk);

    return config;
}
 
Example #5
Source File: ConfigurationFileUtil.java    From AntiVPN with MIT License 5 votes vote down vote up
public static Configuration getConfig(Object plugin, String resourcePath, File fileOnDisk) throws IOException {
    File parentDir = fileOnDisk.getParentFile();
    if (parentDir.exists() && !parentDir.isDirectory()) {
        Files.delete(parentDir.toPath());
    }
    if (!parentDir.exists()) {
        if (!parentDir.mkdirs()) {
            throw new IOException("Could not create parent directory structure.");
        }
    }
    if (fileOnDisk.exists() && fileOnDisk.isDirectory()) {
        Files.delete(fileOnDisk.toPath());
    }

    if (!fileOnDisk.exists()) {
        try (InputStreamReader reader = new InputStreamReader(plugin.getClass().getClassLoader().getResourceAsStream(resourcePath));
             BufferedReader in = new BufferedReader(reader);
             FileWriter writer = new FileWriter(fileOnDisk);
             BufferedWriter out = new BufferedWriter(writer)) {
            String line;
            while ((line = in.readLine()) != null) {
                out.write(line + System.lineSeparator());
            }
        }
    }

    ConfigurationLoader<ConfigurationNode> loader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setFile(fileOnDisk).build();
    ConfigurationNode root = loader.load(ConfigurationOptions.defaults().setHeader("Comments are gone because update :(. Click here for new config + comments: https://forums.velocitypowered.com/t/anti-vpn-get-the-best-save-money-on-overpriced-plugins-and-block-vpn-users/207"));
    Configuration config = new Configuration(root);
    ConfigurationVersionUtil.conformVersion(loader, config, fileOnDisk);

    return config;
}
 
Example #6
Source File: ConfigFactory.java    From helper with MIT License 5 votes vote down vote up
@Nonnull
@Override
public YAMLConfigurationLoader loader(@Nonnull Path path) {
    YAMLConfigurationLoader.Builder builder = YAMLConfigurationLoader.builder()
            .setFlowStyle(DumperOptions.FlowStyle.BLOCK)
            .setIndent(2)
            .setSource(() -> Files.newBufferedReader(path, StandardCharsets.UTF_8))
            .setSink(() -> Files.newBufferedWriter(path, StandardCharsets.UTF_8));

    builder.setDefaultOptions(builder.getDefaultOptions().setSerializers(TYPE_SERIALIZERS));
    return builder.build();
}
 
Example #7
Source File: YamlLoader.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public ConfigurationLoader<? extends ConfigurationNode> loader(Path path) {
    return YAMLConfigurationLoader.builder()
            .setFlowStyle(DumperOptions.FlowStyle.BLOCK)
            .setIndent(2)
            .setSource(() -> Files.newBufferedReader(path, StandardCharsets.UTF_8))
            .setSink(() -> Files.newBufferedWriter(path, StandardCharsets.UTF_8))
            .build();
}
 
Example #8
Source File: ConfigManager.java    From BlueMap with MIT License 4 votes vote down vote up
private ConfigurationLoader<? extends ConfigurationNode> getLoader(URL url){
	if (url.getFile().endsWith(".json")) return GsonConfigurationLoader.builder().setURL(url).build();
	if (url.getFile().endsWith(".yaml") || url.getFile().endsWith(".yml")) return YAMLConfigurationLoader.builder().setURL(url).build();
	else return HoconConfigurationLoader.builder().setURL(url).build();
}
 
Example #9
Source File: ConfigManager.java    From BlueMap with MIT License 4 votes vote down vote up
private ConfigurationLoader<? extends ConfigurationNode> getLoader(File file){
	if (file.getName().endsWith(".json")) return GsonConfigurationLoader.builder().setFile(file).build();
	if (file.getName().endsWith(".yaml") || file.getName().endsWith(".yml")) return YAMLConfigurationLoader.builder().setFile(file).build();
	else return HoconConfigurationLoader.builder().setFile(file).build();
}
 
Example #10
Source File: LanguageFileUtil.java    From AntiVPN with MIT License 4 votes vote down vote up
public static Optional<File> getLanguage(Plugin plugin, Locale locale, boolean ignoreCountry) throws IOException {
    // Build resource path & file path for language
    // Use country is specified (and lang provides country)
    String resourcePath = ignoreCountry || locale.getCountry() == null || locale.getCountry().isEmpty() ? "lang_" + locale.getLanguage() + ".yml" : "lang_" + locale.getLanguage() + "_" + locale.getCountry() + ".yml";
    File langDir = new File(plugin.getDataFolder(), "lang");
    File fileOnDisk = new File(langDir, resourcePath);

    // Clean up/build language path on disk
    if (langDir.exists() && !langDir.isDirectory()) {
        Files.delete(langDir.toPath());
    }
    if (!langDir.exists()) {
        if (!langDir.mkdirs()) {
            throw new IOException("Could not create parent directory structure.");
        }
    }
    if (fileOnDisk.exists() && fileOnDisk.isDirectory()) {
        Files.delete(fileOnDisk.toPath());
    }

    // Check language version
    if (fileOnDisk.exists()) {
        try (InputStream inStream = plugin.getResourceAsStream(resourcePath)) {
            if (inStream != null) {
                ConfigurationLoader<ConfigurationNode> fileLoader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setFile(fileOnDisk).build();
                ConfigurationNode fileRoot = fileLoader.load();
                double fileVersion = fileRoot.getNode("acf-minecraft", "version").getDouble(1.0d);

                try (InputStreamReader reader = new InputStreamReader(inStream);
                     BufferedReader in = new BufferedReader(reader)) {
                    ConfigurationLoader<ConfigurationNode> streamLoader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setSource(() -> in).build();
                    ConfigurationNode streamRoot = streamLoader.load();
                    double streamVersion = streamRoot.getNode("acf-minecraft", "version").getDouble(1.0d);

                    if (streamVersion > fileVersion) {
                        // Version update, backup & delete file on disk
                        File backupFile = new File(fileOnDisk.getParent(), fileOnDisk.getName() + ".bak");
                        if (backupFile.exists()) {
                            Files.delete(backupFile.toPath());
                        }

                        com.google.common.io.Files.copy(fileOnDisk, backupFile);
                        Files.delete(fileOnDisk.toPath());
                    }
                }
            }
        }
    }

    // Write language file to disk if not exists
    if (!fileOnDisk.exists()) {
        try (InputStream inStream = plugin.getResourceAsStream(resourcePath)) {
            if (inStream != null) {
                try (InputStreamReader reader = new InputStreamReader(inStream);
                     BufferedReader in = new BufferedReader(reader);
                     FileWriter writer = new FileWriter(fileOnDisk);
                     BufferedWriter out = new BufferedWriter(writer)) {
                    String line;
                    while ((line = in.readLine()) != null) {
                        out.write(line + System.lineSeparator());
                    }
                }
            }
        }
    }

    if (fileOnDisk.exists()) {
        // Return file on disk
        return Optional.of(fileOnDisk);
    } else {
        // If we need a more generic language (eg. if we have "en_US" and we don't have "en_US.yml" but we do have "en.yml") then return the more generic language file
        // Otherwise, no language found
        return ignoreCountry ? Optional.empty() : getLanguage(plugin, locale, true);
    }
}
 
Example #11
Source File: LanguageFileUtil.java    From AntiVPN with MIT License 4 votes vote down vote up
public static Optional<File> getLanguage(Plugin plugin, Locale locale, boolean ignoreCountry) throws IOException {
    // Build resource path & file path for language
    // Use country is specified (and lang provides country)
    String resourcePath = ignoreCountry || locale.getCountry() == null || locale.getCountry().isEmpty() ? "lang_" + locale.getLanguage() + ".yml" : "lang_" + locale.getLanguage() + "_" + locale.getCountry() + ".yml";
    File langDir = new File(plugin.getDataFolder(), "lang");
    File fileOnDisk = new File(langDir, resourcePath);

    // Clean up/build language path on disk
    if (langDir.exists() && !langDir.isDirectory()) {
        Files.delete(langDir.toPath());
    }
    if (!langDir.exists()) {
        if (!langDir.mkdirs()) {
            throw new IOException("Could not create parent directory structure.");
        }
    }
    if (fileOnDisk.exists() && fileOnDisk.isDirectory()) {
        Files.delete(fileOnDisk.toPath());
    }

    // Check language version
    if (fileOnDisk.exists()) {
        try (InputStream inStream = plugin.getResource(resourcePath)) {
            if (inStream != null) {
                ConfigurationLoader<ConfigurationNode> fileLoader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setFile(fileOnDisk).build();
                ConfigurationNode fileRoot = fileLoader.load();
                double fileVersion = fileRoot.getNode("acf-minecraft", "version").getDouble(1.0d);

                try (InputStreamReader reader = new InputStreamReader(inStream);
                     BufferedReader in = new BufferedReader(reader)) {
                    ConfigurationLoader<ConfigurationNode> streamLoader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setSource(() -> in).build();
                    ConfigurationNode streamRoot = streamLoader.load();
                    double streamVersion = streamRoot.getNode("acf-minecraft", "version").getDouble(1.0d);

                    if (streamVersion > fileVersion) {
                        // Version update, backup & delete file on disk
                        File backupFile = new File(fileOnDisk.getParent(), fileOnDisk.getName() + ".bak");
                        if (backupFile.exists()) {
                            Files.delete(backupFile.toPath());
                        }

                        com.google.common.io.Files.copy(fileOnDisk, backupFile);
                        Files.delete(fileOnDisk.toPath());
                    }
                }
            }
        }
    }

    // Write language file to disk if not exists
    if (!fileOnDisk.exists()) {
        try (InputStream inStream = plugin.getResource(resourcePath)) {
            if (inStream != null) {
                try (InputStreamReader reader = new InputStreamReader(inStream);
                     BufferedReader in = new BufferedReader(reader);
                     FileWriter writer = new FileWriter(fileOnDisk);
                     BufferedWriter out = new BufferedWriter(writer)) {
                    String line;
                    while ((line = in.readLine()) != null) {
                        out.write(line + System.lineSeparator());
                    }
                }
            }
        }
    }

    if (fileOnDisk.exists()) {
        // Return file on disk
        return Optional.of(fileOnDisk);
    } else {
        // If we need a more generic language (eg. if we have "en_US" and we don't have "en_US.yml" but we do have "en.yml") then return the more generic language file
        // Otherwise, no language found
        return ignoreCountry ? Optional.empty() : getLanguage(plugin, locale, true);
    }
}
 
Example #12
Source File: AntiVPN.java    From AntiVPN with MIT License 4 votes vote down vote up
public boolean loadYamlLanguageFile(VelocityLocales locales, File file, Locale locale) throws IOException {
    ConfigurationLoader<ConfigurationNode> fileLoader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setFile(file).build();
    return loadLanguage(locales, fileLoader.load(), locale);
}
 
Example #13
Source File: LanguageFileUtil.java    From AntiVPN with MIT License 4 votes vote down vote up
public static Optional<File> getLanguage(Object plugin, PluginDescription description, Locale locale, boolean ignoreCountry) throws IOException {
    // Build resource path & file path for language
    // Use country is specified (and lang provides country)
    String resourcePath = ignoreCountry || locale.getCountry() == null || locale.getCountry().isEmpty() ? "lang_" + locale.getLanguage() + ".yml" : "lang_" + locale.getLanguage() + "_" + locale.getCountry() + ".yml";
    File langDir = new File(new File(description.getSource().get().getParent().toFile(), description.getName().get()), "lang");
    File fileOnDisk = new File(langDir, resourcePath);

    // Clean up/build language path on disk
    if (langDir.exists() && !langDir.isDirectory()) {
        Files.delete(langDir.toPath());
    }
    if (!langDir.exists()) {
        if (!langDir.mkdirs()) {
            throw new IOException("Could not create parent directory structure.");
        }
    }
    if (fileOnDisk.exists() && fileOnDisk.isDirectory()) {
        Files.delete(fileOnDisk.toPath());
    }

    // Check language version
    if (fileOnDisk.exists()) {
        try (InputStream inStream = plugin.getClass().getClassLoader().getResourceAsStream(resourcePath)) {
            if (inStream != null) {
                ConfigurationLoader<ConfigurationNode> fileLoader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setFile(fileOnDisk).build();
                ConfigurationNode fileRoot = fileLoader.load();
                double fileVersion = fileRoot.getNode("acf-minecraft", "version").getDouble(1.0d);

                try (InputStreamReader reader = new InputStreamReader(inStream);
                     BufferedReader in = new BufferedReader(reader)) {
                    ConfigurationLoader<ConfigurationNode> streamLoader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setSource(() -> in).build();
                    ConfigurationNode streamRoot = streamLoader.load();
                    double streamVersion = streamRoot.getNode("acf-minecraft", "version").getDouble(1.0d);

                    if (streamVersion > fileVersion) {
                        // Version update, backup & delete file on disk
                        File backupFile = new File(fileOnDisk.getParent(), fileOnDisk.getName() + ".bak");
                        if (backupFile.exists()) {
                            Files.delete(backupFile.toPath());
                        }

                        com.google.common.io.Files.copy(fileOnDisk, backupFile);
                        Files.delete(fileOnDisk.toPath());
                    }
                }
            }
        }
    }

    // Write language file to disk if not exists
    if (!fileOnDisk.exists()) {
        try (InputStream inStream = plugin.getClass().getClassLoader().getResourceAsStream(resourcePath)) {
            if (inStream != null) {
                try (InputStreamReader reader = new InputStreamReader(inStream);
                     BufferedReader in = new BufferedReader(reader);
                     FileWriter writer = new FileWriter(fileOnDisk);
                     BufferedWriter out = new BufferedWriter(writer)) {
                    String line;
                    while ((line = in.readLine()) != null) {
                        out.write(line + System.lineSeparator());
                    }
                }
            }
        }
    }

    if (fileOnDisk.exists()) {
        // Return file on disk
        return Optional.of(fileOnDisk);
    } else {
        // If we need a more generic language (eg. if we have "en_US" and we don't have "en_US.yml" but we do have "en.yml") then return the more generic language file
        // Otherwise, no language found
        return ignoreCountry ? Optional.empty() : getLanguage(plugin, description, locale, true);
    }
}
 
Example #14
Source File: ConfigFactory.java    From helper with MIT License 4 votes vote down vote up
@Nonnull
public static ConfigFactory<ConfigurationNode, YAMLConfigurationLoader> yaml() {
    return YAML;
}
 
Example #15
Source File: VelocityConfigAdapter.java    From LuckPerms with MIT License 4 votes vote down vote up
@Override
protected ConfigurationLoader<? extends ConfigurationNode> createLoader(Path path) {
    return YAMLConfigurationLoader.builder().setPath(path).build();
}