Java Code Examples for ninja.leaping.configurate.hocon.HoconConfigurationLoader#save()

The following examples show how to use ninja.leaping.configurate.hocon.HoconConfigurationLoader#save() . 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: HOCONPlayerStorage.java    From EagleFactions with MIT License 6 votes vote down vote up
@Override
public boolean savePlayer(FactionPlayer player)
{
    Path playerFile = playersDirectoryPath.resolve(player.getUniqueId().toString() + ".conf");

    HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setDefaultOptions(ConfigurateHelper.getDefaultOptions()).setPath(playerFile).build();
    try
    {
        ConfigurationNode configurationNode = configurationLoader.load();
        configurationNode.getNode("name").setValue(player.getName());
        configurationNode.getNode("faction").setValue(player.getFactionName().orElse(""));
        configurationNode.getNode("power").setValue(player.getPower());
        configurationNode.getNode("maxpower").setValue(player.getMaxPower());
        configurationNode.getNode("death-in-warzone").setValue(false);
        configurationLoader.save(configurationNode);
        return true;
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

    return false;
}
 
Example 2
Source File: Settings.java    From FlexibleLogin with MIT License 6 votes vote down vote up
private <T> void loadMapper(ObjectMapper<T>.BoundInstance mapper, Path file, ConfigurationOptions options) {
    ConfigurationNode rootNode;
    if (mapper != null) {
        HoconConfigurationLoader loader = HoconConfigurationLoader.builder().setPath(file).build();
        try {
            rootNode = loader.load(options.setShouldCopyDefaults(true));
            ConfigurationNode hashNode = rootNode.getNode("hashAlgo");
            if ("bcrypt".equalsIgnoreCase(hashNode.getString())) {
                hashNode.setValue("BCrypt");
            }

            //load the config into the object
            mapper.populate(rootNode);

            //add missing default values
            loader.save(rootNode);
        } catch (ObjectMappingException objMappingExc) {
            logger.error("Error loading the configuration", objMappingExc);
        } catch (IOException ioExc) {
            logger.error("Error saving the default configuration", ioExc);
        }
    }
}
 
Example 3
Source File: Metrics.java    From EagleFactions with MIT License 5 votes vote down vote up
/**
 * Loads the bStats configuration.
 *
 * @throws IOException If something did not work :(
 */
private void loadConfig() throws IOException {
    Path configPath = configDir.resolve("bStats");
    configPath.toFile().mkdirs();
    File configFile = new File(configPath.toFile(), "config.conf");
    HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build();
    CommentedConfigurationNode node;
    if (!configFile.exists())
    {
        configFile.createNewFile();
        node = configurationLoader.load();

        // Add default values
        node.getNode("enabled").setValue(true);
        // Every server gets it's unique random id.
        node.getNode("serverUuid").setValue(UUID.randomUUID().toString());
        // Should failed request be logged?
        node.getNode("logFailedRequests").setValue(false);

        // Add information about bStats
        node.getNode("enabled").setComment(
                "bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
                        "To honor their work, you should not disable it.\n" +
                        "This has nearly no effect on the server performance!\n" +
                        "Check out https://bStats.org/ to learn more :)"
        );

        configurationLoader.save(node);
    }
    else
    {
        node = configurationLoader.load();
    }

    // Load configuration
    enabled = node.getNode("enabled").getBoolean(true);
    serverUUID = node.getNode("serverUuid").getString();
    logFailedRequests = node.getNode("logFailedRequests").getBoolean(false);
}
 
Example 4
Source File: Metrics.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Loads the bStats configuration.
 *
 * @throws IOException If something did not work :(
 */
private void loadConfig() throws IOException {
    Path configPath = configDir.resolve("bStats");
    configPath.toFile().mkdirs();
    File configFile = new File(configPath.toFile(), "config.conf");
    HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build();
    CommentedConfigurationNode node;
    if (!configFile.exists()) {
        configFile.createNewFile();
        node = configurationLoader.load();

        // Add default values
        node.getNode("enabled").setValue(true);
        // Every server gets it's unique random id.
        node.getNode("serverUuid").setValue(UUID.randomUUID().toString());
        // Should failed request be logged?
        node.getNode("logFailedRequests").setValue(false);

        // Add information about bStats
        node
            .getNode("enabled")
            .setComment(
                "bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
                "To honor their work, you should not disable it.\n" +
                "This has nearly no effect on the server performance!\n" +
                "Check out https://bStats.org/ to learn more :)"
            );

        configurationLoader.save(node);
    } else {
        node = configurationLoader.load();
    }

    // Load configuration
    enabled = node.getNode("enabled").getBoolean(true);
    serverUUID = node.getNode("serverUuid").getString();
    logFailedRequests = node.getNode("logFailedRequests").getBoolean(false);
}
 
Example 5
Source File: Metrics.java    From SubServers-2 with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the bStats configuration.
 *
 * @throws IOException If something did not work :(
 */
private void loadConfig() throws IOException {
    Path configPath = configDir.resolve("bStats");
    configPath.toFile().mkdirs();
    File configFile = new File(configPath.toFile(), "config.conf");
    HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build();
    CommentedConfigurationNode node;
    if (!configFile.exists()) {
        configFile.createNewFile();
        node = configurationLoader.load();

        // Add default values
        node.getNode("enabled").setValue(true);
        // Every server gets it's unique random id.
        node.getNode("serverUuid").setValue(UUID.randomUUID().toString());
        // Should failed request be logged?
        node.getNode("logFailedRequests").setValue(false);

        // Add information about bStats
        node.getNode("enabled").setComment(
                "bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
                        "To honor their work, you should not disable it.\n" +
                        "This has nearly no effect on the server performance!\n" +
                        "Check out https://bStats.org/ to learn more :)"
        );

        configurationLoader.save(node);
    } else {
        node = configurationLoader.load();
    }

    // Load configuration
    enabled = node.getNode("enabled").getBoolean(true);
    serverUUID = node.getNode("serverUuid").getString();
    logFailedRequests = node.getNode("logFailedRequests").getBoolean(false);
}
 
Example 6
Source File: Metrics.java    From EconomyLite with MIT License 5 votes vote down vote up
/**
 * Loads the bStats configuration.
 *
 * @throws IOException If something did not work :(
 */
private void loadConfig() throws IOException {
    Path configPath = configDir.resolve("bStats");
    configPath.toFile().mkdirs();
    File configFile = new File(configPath.toFile(), "config.conf");
    HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build();
    CommentedConfigurationNode node;
    if (!configFile.exists()) {
        configFile.createNewFile();
        node = configurationLoader.load();

        // Add default values
        node.getNode("enabled").setValue(true);
        // Every server gets it's unique random id.
        node.getNode("serverUuid").setValue(UUID.randomUUID().toString());
        // Should failed request be logged?
        node.getNode("logFailedRequests").setValue(false);

        // Add information about bStats
        node.getNode("enabled").setComment(
                "bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
                        "To honor their work, you should not disable it.\n" +
                        "This has nearly no effect on the server performance!\n" +
                        "Check out https://bStats.org/ to learn more :)"
                );

        configurationLoader.save(node);
    } else {
        node = configurationLoader.load();
    }

    // Load configuration
    enabled = node.getNode("enabled").getBoolean(true);
    serverUUID = node.getNode("serverUuid").getString();
    logFailedRequests = node.getNode("logFailedRequests").getBoolean(false);
}
 
Example 7
Source File: BackupStorage.java    From EagleFactions with MIT License 4 votes vote down vote up
public boolean createBackup()
{
    try
    {
        final Path backupPath = this.backupsPath.resolve("backup-" + DATE_TIME_FORMATTER.format(LocalDateTime.now()));
        createFileIfNotExists(this.backupsPath, true);
        createFileIfNotExists(backupPath, true);

        final Path factionsDir = backupPath.resolve("factions");
        final Path playersDir = backupPath.resolve("players");
        createFileIfNotExists(factionsDir, false);
        createFileIfNotExists(playersDir, true);

        // Backup factions
        final Set<Faction> factions = factionStorage.getFactions();
        for (final Faction faction : factions)
        {
            final Path factionFilePath = factionsDir.resolve(faction.getName().toLowerCase() + ".conf");
            final HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setDefaultOptions(ConfigurateHelper.getDefaultOptions()).setPath(factionFilePath).build();
            final ConfigurationNode configurationNode = configurationLoader.createEmptyNode();
            ConfigurateHelper.putFactionInNode(configurationNode, faction);
            configurationLoader.save(configurationNode);
        }

        // Backup players
        final Set<FactionPlayer> players = playerStorage.getServerPlayers();
        for (final FactionPlayer factionPlayer : players)
        {
            final Path playerFile = playersDir.resolve(factionPlayer.getUniqueId().toString() + ".conf");
            final HoconConfigurationLoader playerConfigLoader = HoconConfigurationLoader.builder().setPath(playerFile).build();
            final ConfigurationNode playerNode = playerConfigLoader.createEmptyNode();
            ConfigurateHelper.putPlayerInNode(playerNode, factionPlayer);
            playerConfigLoader.save(playerNode);
        }

        // Now when factions and players are ready, we can move them into a zip file.
        FileOutputStream fileOutputStream = new FileOutputStream(backupPath.toAbsolutePath().toString() + ".zip");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream);
        File file = backupPath.toFile();

        zipFile(file, file.getName(), zipOutputStream);
        bufferedOutputStream.flush();
        zipOutputStream.close();
        bufferedOutputStream.close();
        fileOutputStream.close();

        //Delete temp files
        deleteDirectory(backupPath.toFile());

        return true;
    }
    catch (final Exception e)
    {
        e.printStackTrace();
        return false;
    }
}
 
Example 8
Source File: Metrics2.java    From bStats-Metrics with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Loads the bStats configuration.
 *
 * @throws IOException If something did not work :(
 */
private void loadConfig() throws IOException {
    File configPath = configDir.resolve("bStats").toFile();
    configPath.mkdirs();
    File configFile = new File(configPath, "config.conf");
    HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build();
    CommentedConfigurationNode node;
    if (!configFile.exists()) {
        configFile.createNewFile();
        node = configurationLoader.load();

        // Add default values
        node.getNode("enabled").setValue(false);
        // Every server gets it's unique random id.
        node.getNode("serverUuid").setValue(UUID.randomUUID().toString());
        // Should failed request be logged?
        node.getNode("logFailedRequests").setValue(false);
        // Should the sent data be logged?
        node.getNode("logSentData").setValue(false);
        // Should the response text be logged?
        node.getNode("logResponseStatusText").setValue(false);

        node.getNode("enabled").setComment(
                "Enabling bStats in this file is deprecated. At least one of your plugins now uses the\n" +
                        "Sponge config to control bStats. Leave this value as you want it to be for outdated plugins,\n" +
                        "but look there for further control");
        // Add information about bStats
        node.getNode("serverUuid").setComment(
                "bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
                        "To control whether this is enabled or disabled, see the Sponge configuration file.\n" +
                        "Check out https://bStats.org/ to learn more :)"
        );
        node.getNode("configVersion").setValue(2);

        configurationLoader.save(node);
    } else {
        node = configurationLoader.load();

        if (!node.getNode("configVersion").isVirtual()) {

            node.getNode("configVersion").setValue(2);

            node.getNode("enabled").setComment(
                    "Enabling bStats in this file is deprecated. At least one of your plugins now uses the\n" +
                            "Sponge config to control bStats. Leave this value as you want it to be for outdated plugins,\n" +
                            "but look there for further control");

            node.getNode("serverUuid").setComment(
                    "bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
                            "To control whether this is enabled or disabled, see the Sponge configuration file.\n" +
                            "Check out https://bStats.org/ to learn more :)"
            );

            configurationLoader.save(node);
        }
    }

    // Load configuration
    serverUUID = node.getNode("serverUuid").getString();
    logFailedRequests = node.getNode("logFailedRequests").getBoolean(false);
    logSentData = node.getNode("logSentData").getBoolean(false);
    logResponseStatusText = node.getNode("logResponseStatusText").getBoolean(false);
}
 
Example 9
Source File: MetricsLite2.java    From bStats-Metrics with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Loads the bStats configuration.
 *
 * @throws IOException If something did not work :(
 */
private void loadConfig() throws IOException {
    Path configPath = configDir.resolve("bStats");
    configPath.toFile().mkdirs();
    File configFile = new File(configPath.toFile(), "config.conf");
    HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build();
    CommentedConfigurationNode node;
    if (!configFile.exists()) {
        configFile.createNewFile();
        node = configurationLoader.load();

        // Add default values
        node.getNode("enabled").setValue(false);
        // Every server gets it's unique random id.
        node.getNode("serverUuid").setValue(UUID.randomUUID().toString());
        // Should failed request be logged?
        node.getNode("logFailedRequests").setValue(false);
        // Should the sent data be logged?
        node.getNode("logSentData").setValue(false);
        // Should the response text be logged?
        node.getNode("logResponseStatusText").setValue(false);

        node.getNode("enabled").setComment(
                "Enabling bStats in this file is deprecated. At least one of your plugins now uses the\n" +
                        "Sponge config to control bStats. Leave this value as you want it to be for outdated plugins,\n" +
                        "but look there for further control");
        // Add information about bStats
        node.getNode("serverUuid").setComment(
                "bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
                        "To control whether this is enabled or disabled, see the Sponge configuration file.\n" +
                        "Check out https://bStats.org/ to learn more :)"
        );
        node.getNode("configVersion").setValue(2);

        configurationLoader.save(node);
    } else {
        node = configurationLoader.load();

        if (!node.getNode("configVersion").isVirtual()) {

            node.getNode("configVersion").setValue(2);

            node.getNode("enabled").setComment(
                    "Enabling bStats in this file is deprecated. At least one of your plugins now uses the\n" +
                            "Sponge config to control bStats. Leave this value as you want it to be for outdated plugins,\n" +
                            "but look there for further control");

            node.getNode("serverUuid").setComment(
                    "bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
                            "To control whether this is enabled or disabled, see the Sponge configuration file.\n" +
                            "Check out https://bStats.org/ to learn more :)"
            );

            configurationLoader.save(node);
        }
    }

    // Load configuration
    serverUUID = node.getNode("serverUuid").getString();
    logFailedRequests = node.getNode("logFailedRequests").getBoolean(false);
    logSentData = node.getNode("logSentData").getBoolean(false);
    logResponseStatusText = node.getNode("logResponseStatusText").getBoolean(false);
}
 
Example 10
Source File: Metrics2.java    From GriefPrevention with MIT License 4 votes vote down vote up
/**
 * Loads the bStats configuration.
 *
 * @throws IOException If something did not work :(
 */
private void loadConfig() throws IOException {
    Path configPath = configDir.resolve("bStats");
    configPath.toFile().mkdirs();
    File configFile = new File(configPath.toFile(), "config.conf");
    HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build();
    CommentedConfigurationNode node;
    if (!configFile.exists()) {
        configFile.createNewFile();
        node = configurationLoader.load();

        // Add default values
        node.getNode("enabled").setValue(false);
        // Every server gets it's unique random id.
        node.getNode("serverUuid").setValue(UUID.randomUUID().toString());
        // Should failed request be logged?
        node.getNode("logFailedRequests").setValue(false);
        // Should the sent data be logged?
        node.getNode("logSentData").setValue(false);
        // Should the response text be logged?
        node.getNode("logResponseStatusText").setValue(false);

        node.getNode("enabled").setComment(
                "Enabling bStats in this file is deprecated. At least one of your plugins now uses the\n" +
                        "Sponge config to control bStats. Leave this value as you want it to be for outdated plugins,\n" +
                        "but look there for further control");
        // Add information about bStats
        node.getNode("serverUuid").setComment(
                "bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
                        "To control whether this is enabled or disabled, see the Sponge configuration file.\n" +
                        "Check out https://bStats.org/ to learn more :)"
        );
        node.getNode("configVersion").setValue(2);

        configurationLoader.save(node);
    } else {
        node = configurationLoader.load();

        if (!node.getNode("configVersion").isVirtual()) {

            node.getNode("configVersion").setValue(2);

            node.getNode("enabled").setComment(
                    "Enabling bStats in this file is deprecated. At least one of your plugins now uses the\n" +
                            "Sponge config to control bStats. Leave this value as you want it to be for outdated plugins,\n" +
                            "but look there for further control");

            node.getNode("serverUuid").setComment(
                    "bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
                            "To control whether this is enabled or disabled, see the Sponge configuration file.\n" +
                            "Check out https://bStats.org/ to learn more :)"
            );

            configurationLoader.save(node);
        }
    }

    // Load configuration
    serverUUID = node.getNode("serverUuid").getString();
    logFailedRequests = node.getNode("logFailedRequests").getBoolean(false);
    logSentData = node.getNode("logSentData").getBoolean(false);
    logResponseStatusText = node.getNode("logResponseStatusText").getBoolean(false);
}