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

The following examples show how to use ninja.leaping.configurate.hocon.HoconConfigurationLoader#load() . 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: HOCONPlayerStorage.java    From EagleFactions with MIT License 6 votes vote down vote up
@Override
public Set<String> getServerPlayerNames()
{
    Set<String> playerSet = new HashSet<>();

    File playerDir = new File(playersDirectoryPath.toUri());
    File[] playerFiles = playerDir.listFiles();

    for(File playerFile : playerFiles)
    {
        HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setDefaultOptions(ConfigurateHelper.getDefaultOptions()).setPath(playerFile.toPath()).build();
        try
        {
            ConfigurationNode configurationNode = configurationLoader.load();
            playerSet.add(configurationNode.getNode("name").getString(""));
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    return playerSet;
}
 
Example 3
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 4
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 5
Source File: ConfigurateHelper.java    From EagleFactions with MIT License 5 votes vote down vote up
public static FactionPlayer getPlayerFromFile(final File file)
{
    HoconConfigurationLoader playerConfigLoader = HoconConfigurationLoader.builder().setFile(file).build();
    try
    {
        ConfigurationNode playerNode = playerConfigLoader.load();
        String playerName = playerNode.getNode("name").getString("");
        UUID playerUUID;
        try
        {
            playerUUID = UUID.fromString(file.getName().substring(0, file.getName().indexOf('.')));

        }
        catch(Exception exception)
        {
            exception.printStackTrace();
            Files.delete(file.toPath());
            return null;
        }
        String factionName = playerNode.getNode("faction").getString("");
        String factionMemberTypeString = playerNode.getNode("faction-member-type").getString("");
        float power = playerNode.getNode("power").getFloat(0.0f);
        float maxpower = playerNode.getNode("maxpower").getFloat(0.0f);
        boolean diedInWarZone = playerNode.getNode("death-in-warzone").getBoolean(false);
        FactionMemberType factionMemberType = null;

        if(!factionMemberTypeString.equals(""))
            factionMemberType = FactionMemberType.valueOf(factionMemberTypeString);

        return new FactionPlayerImpl(playerName, playerUUID, factionName, power, maxpower, factionMemberType, diedInWarZone);
    }
    catch(IOException e)
    {
        e.printStackTrace();
        return null;
    }
}
 
Example 6
Source File: HOCONPlayerStorage.java    From EagleFactions with MIT License 5 votes vote down vote up
@Override
public FactionPlayer getPlayer(final UUID playerUUID)
{
    final Path playerFilePath = this.playersDirectoryPath.resolve(playerUUID.toString() + ".conf");
    if (Files.notExists(playerFilePath))
        return null;

    HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setDefaultOptions(ConfigurateHelper.getDefaultOptions()).setPath(playerFilePath).build();
    try
    {
        ConfigurationNode configurationNode = configurationLoader.load();
        String playerName = configurationNode.getNode("name").getString("");
        String factionName = configurationNode.getNode("faction").getString("");
        String factionMemberTypeString = configurationNode.getNode("faction-member-type").getString("");
        float power = configurationNode.getNode("power").getFloat(0.0f);
        float maxpower = configurationNode.getNode("maxpower").getFloat(0.0f);
        boolean diedInWarZone = configurationNode.getNode("death-in-warzone").getBoolean(false);
        FactionMemberType factionMemberType = null;

        if(!factionMemberTypeString.equals(""))
            factionMemberType = FactionMemberType.valueOf(factionMemberTypeString);

        return new FactionPlayerImpl(playerName, playerUUID, factionName, power, maxpower, factionMemberType, diedInWarZone);
    }
    catch(IOException e)
    {
        e.printStackTrace();
        Sponge.getServer().getConsole().sendMessage(PluginInfo.ERROR_PREFIX.concat(Text.of("Could not get player from the file. Tried to get player for UUID: " + playerUUID)));
    }
    return null;
}
 
Example 7
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 8
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 9
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 10
Source File: HOCONPlayerStorage.java    From EagleFactions with MIT License 4 votes vote down vote up
@Override
public Set<FactionPlayer> getServerPlayers()
{
    final Set<FactionPlayer> playerSet = new HashSet<>();
    final File playerDir = new File(playersDirectoryPath.toUri());
    final File[] playerFiles = playerDir.listFiles();

    for(File playerFile : playerFiles)
    {
        HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setDefaultOptions(ConfigurateHelper.getDefaultOptions()).setPath(playerFile.toPath()).build();
        try
        {
            ConfigurationNode configurationNode = configurationLoader.load();
            String playerName = configurationNode.getNode("name").getString("");
            UUID playerUUID;
            try
            {
                playerUUID = UUID.fromString(playerFile.getName().substring(0, playerFile.getName().indexOf('.')));

            }
            catch(Exception exception)
            {
                exception.printStackTrace();
                Files.delete(playerFile.toPath());
                continue;
            }
            String factionName = configurationNode.getNode("faction").getString("");
            String factionMemberTypeString = configurationNode.getNode("faction-member-type").getString("");
            float power = configurationNode.getNode("power").getFloat(0.0f);
            float maxpower = configurationNode.getNode("maxpower").getFloat(0.0f);
            boolean diedInWarZone = configurationNode.getNode("death-in-warzone").getBoolean(false);
            FactionMemberType factionMemberType = null;

            if(!factionMemberTypeString.equals(""))
                factionMemberType = FactionMemberType.valueOf(factionMemberTypeString);

            FactionPlayer factionPlayer = new FactionPlayerImpl(playerName, playerUUID, factionName, power, maxpower, factionMemberType, diedInWarZone);
            playerSet.add(factionPlayer);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    return playerSet;
}
 
Example 11
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 12
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 13
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);
}
 
Example 14
Source File: PlotMe_Sponge.java    From PlotMe-Core with GNU General Public License v3.0 4 votes vote down vote up
ConfigurationNode loadDefaultConfiguration() throws IOException {
    URL defaultConfig = PlotMe_Sponge.class.getResource("default.conf");
    HoconConfigurationLoader loader = HoconConfigurationLoader.builder().setURL(defaultConfig).build();
    return loader.load();

}