Java Code Examples for ninja.leaping.configurate.loader.ConfigurationLoader#save()

The following examples show how to use ninja.leaping.configurate.loader.ConfigurationLoader#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: 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: HOCONFactionStorage.java    From EagleFactions with MIT License 5 votes vote down vote up
@Override
public boolean saveFaction(final Faction faction)
{
    ConfigurationLoader<? extends ConfigurationNode> configurationLoader = this.factionLoaders.get(faction.getName().toLowerCase() + ".conf");
    try
    {
        if (configurationLoader == null)
        {
            final Path factionFilePath = this.factionsDir.resolve(faction.getName().toLowerCase() + ".conf");
            Files.createFile(factionFilePath);
            configurationLoader = HoconConfigurationLoader.builder().setDefaultOptions(ConfigurateHelper.getDefaultOptions()).setPath(factionFilePath).build();
            this.factionLoaders.put(factionFilePath.getFileName().toString(), configurationLoader);
        }

        final ConfigurationNode configurationNode = configurationLoader.load();
        final boolean didSucceed = ConfigurateHelper.putFactionInNode(configurationNode, faction);
        if (didSucceed)
        {
            configurationLoader.save(configurationNode);
            return true;
        }
        else return false;
    }
    catch (final IOException e)
    {
        e.printStackTrace();
    }
    return false;
}
 
Example 3
Source File: RedProtectUtil.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
private void saveConf(CommentedConfigurationNode fileDB, ConfigurationLoader<CommentedConfigurationNode> regionManager) {
    try {
        regionManager.save(fileDB);
    } catch (IOException e) {
        printJarVersion();
        e.printStackTrace();
    }
}
 
Example 4
Source File: WorldFlatFileRegionManager.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
private void saveConf(CommentedConfigurationNode fileDB, ConfigurationLoader<CommentedConfigurationNode> regionManager) {
    try {
        regionManager.save(fileDB);
    } catch (IOException e) {
        RedProtect.get().logger.severe("Error during save database file for world " + world + ": ");
        CoreUtil.printJarVersion();
        e.printStackTrace();
    }
}
 
Example 5
Source File: ConfigManager.java    From BlueMap with MIT License 4 votes vote down vote up
private ConfigurationNode loadOrCreate(File configFile, URL defaultConfig, URL defaultValues, boolean usePlaceholders, boolean generateEmptyConfig) throws IOException {
	
	ConfigurationNode configNode;
	if (!configFile.exists()) {
		configFile.getParentFile().mkdirs();
		
		if (defaultConfig != null) {
			//load content of default config
			String content;
			try (BufferedReader reader = new BufferedReader(new InputStreamReader(defaultConfig.openStream(), StandardCharsets.UTF_8))){
				content = reader.lines().collect(Collectors.joining("\n"));
			}

			//replace placeholders if enabled
			if (usePlaceholders) {
				for (Placeholder placeholder : CONFIG_PLACEHOLDERS) {
					content = placeholder.apply(content);
				}
			}

			//create the config file
			Files.write(configFile.toPath(), content.getBytes(StandardCharsets.UTF_8));
			
			//load
			configNode = getLoader(configFile).load();
		} else {
			//create empty config
			ConfigurationLoader<? extends ConfigurationNode> loader = getLoader(configFile);
			configNode = loader.createEmptyNode();
			
			//save to create file
			if (generateEmptyConfig) loader.save(configNode);
		}
	} else {
		//load config
		configNode = getLoader(configFile).load();
	}
	
	//populate missing values with default values
	if (defaultValues != null) {
		ConfigurationNode defaultValuesNode = getLoader(defaultValues).load();
		configNode.mergeValuesFrom(defaultValuesNode);
	}
	
	return configNode;
}
 
Example 6
Source File: ConfigurationVersionUtil.java    From AntiVPN with MIT License 4 votes vote down vote up
public static void conformVersion(ConfigurationLoader<ConfigurationNode> loader, ConfigurationNode config, File fileOnDisk) throws IOException {
    double oldVersion = config.getNode("version").getDouble(1.0d);

    if (config.getNode("version").getDouble(1.0d) == 1.0d) {
        to20(config);
    }
    if (config.getNode("version").getDouble() == 2.0d) {
        to21(config);
    }
    if (config.getNode("version").getDouble() == 2.1d) {
        to22(config);
    }
    if (config.getNode("version").getDouble() == 2.2d) {
        to23(config);
    }
    if (config.getNode("version").getDouble() == 2.3d) {
        to33(config);
    }
    if (config.getNode("version").getDouble() == 3.3d) {
        to34(config);
    }
    if (config.getNode("version").getDouble() == 3.4d) {
        to35(config);
    }
    if (config.getNode("version").getDouble() == 3.5d) {
        to36(config);
    }
    if (config.getNode("version").getDouble() == 3.6d) {
        to37(config);
    }
    if (config.getNode("version").getDouble() == 3.7d) {
        to38(config);
    }
    if (config.getNode("version").getDouble() == 3.8d) {
        to39(config);
    }
    if (config.getNode("version").getDouble() == 3.9d) {
        to49(config);
    }
    if (config.getNode("version").getDouble() == 4.9d) {
        to411(config);
    }
    if (config.getNode("version").getDouble() == 4.11d) {
        to412(config);
    }
    if (config.getNode("version").getDouble() == 4.12d) {
        to413(config);
    }

    if (config.getNode("version").getDouble() != oldVersion) {
        File backupFile = new File(fileOnDisk.getParent(), fileOnDisk.getName() + ".bak");
        if (backupFile.exists()) {
            java.nio.file.Files.delete(backupFile.toPath());
        }

        Files.copy(fileOnDisk, backupFile);
        loader.save(config);
    }
}
 
Example 7
Source File: UCConfig.java    From UltimateChat with GNU General Public License v3.0 4 votes vote down vote up
private void loadChannels() throws IOException {
    File chfolder = new File(UChat.get().configDir(), "channels");

    if (!chfolder.exists()) {
        chfolder.mkdir();
        UChat.get().getLogger().info("Created folder: " + chfolder.getPath());
    }

    //--------------------------------------- Load Aliases -----------------------------------//

    if (UChat.get().getChannels() == null) {
        UChat.get().setChannels(new HashMap<>());
    }

    File[] listOfFiles = chfolder.listFiles();

    CommentedConfigurationNode channel;
    ConfigurationLoader<CommentedConfigurationNode> channelManager;

    if (listOfFiles.length == 0) {
        //create default channels
        File g = new File(chfolder, "global.conf");
        channelManager = HoconConfigurationLoader.builder().setFile(g).build();
        channel = channelManager.load();
        channel.getNode("name").setValue("Global");
        channel.getNode("alias").setValue("g");
        channel.getNode("color").setValue("&2");
        channel.getNode("jedis").setValue(true);
        channelManager.save(channel);

        File l = new File(chfolder, "local.conf");
        channelManager = HoconConfigurationLoader.builder().setFile(l).build();
        channel = channelManager.load();
        channel.getNode("name").setValue("Local");
        channel.getNode("alias").setValue("l");
        channel.getNode("across-worlds").setValue(false);
        channel.getNode("distance").setValue(40);
        channel.getNode("color").setValue("&e");
        channelManager.save(channel);

        File ad = new File(chfolder, "admin.conf");
        channelManager = HoconConfigurationLoader.builder().setFile(ad).build();
        channel = channelManager.load();
        channel.getNode("name").setValue("Admin");
        channel.getNode("alias").setValue("ad");
        channel.getNode("color").setValue("&b");
        channel.getNode("jedis").setValue(true);
        channelManager.save(channel);

        listOfFiles = chfolder.listFiles();
    }

    for (File file : listOfFiles) {
        if (file.getName().endsWith(".conf")) {
            channelManager = HoconConfigurationLoader.builder().setFile(file).build();
            channel = channelManager.load();

            Map<String, Object> chProps = new HashMap<>();
            channel.getChildrenMap().forEach((key, value) -> {
                if (value.hasMapChildren()) {
                    String rkey = key.toString();
                    for (Entry<Object, ? extends CommentedConfigurationNode> vl : value.getChildrenMap().entrySet()) {
                        chProps.put(rkey + "." + vl.getKey(), vl.getValue().getValue());
                    }
                } else {
                    chProps.put(key.toString(), value.getValue());
                }
            });

            UCChannel ch = new UCChannel(chProps);
            addChannel(ch);
        }
    }
}
 
Example 8
Source File: UCConfig.java    From UltimateChat with GNU General Public License v3.0 4 votes vote down vote up
public void addChannel(UCChannel ch) throws IOException {
    CommentedConfigurationNode chFile;
    ConfigurationLoader<CommentedConfigurationNode> channelManager;
    File defch = new File(UChat.get().configDir(), "channels" + File.separator + ch.getName().toLowerCase() + ".conf");

    channelManager = HoconConfigurationLoader.builder().setFile(defch).build();
    chFile = channelManager.load();

    chFile.getNode("across-worlds").setComment(""
            + "###################################################\n"
            + "############## Channel Configuration ##############\n"
            + "###################################################\n"
            + "\n"
            + "This is the channel configuration.\n"
            + "You can change and copy this file to create as many channels you want.\n"
            + "This is the default options:\n"
            + "\n"
            + "name: Global - The name of channel.\n"
            + "alias: g - The alias to use the channel\n"
            + "across-worlds: true - Send messages of this channel to all worlds?\n"
            + "distance: 0 - If across worlds is false, distance to receive this messages.\n"
            + "color: &b - The color of channel\n"
            + "tag-builder: ch-tags,world,clan-tag,marry-tag,group-prefix,nickname,group-suffix,message - Tags of this channel\n"
            + "need-focus: false - Player can use the alias or need to use '/ch g' to use this channel?\n"
            + "canLock: true - Change if the player can use /<channel> to lock on channel.\n"
            + "receivers-message: true - Send chat messages like if no player near to receive the message?\n"
            + "cost: 0.0 - Cost to player use this channel.\n"
            + "use-this-builder: false - Use this tag builder or use the 'config.yml' tag-builder?\n"
            + "\n"
            + "channelAlias - Use this channel as a command alias.\n"
            + "  enable: true - Enable this execute a command alias?\n"
            + "  sendAs: player - Send the command alias as 'player' or 'console'?\n"
            + "  cmd: '' - Command to send on every message send by this channel.\n"
            + "available-worlds - Worlds and only this world where this chat can be used and messages sent/received.\n"
            + "discord:\n"
            + "  mode: NONE - The options are NONE, SEND, LISTEN, BOTH. If enabled and token code set and the channel ID matches with one discord channel, will react according the choosen mode.\n"
            + "  hover: &3Discord Channel: &a{dd-channel}\n"
            + "  format-to-mc: {ch-color}[{ch-alias}]&b{dd-rolecolor}[{dd-rolename}]{sender}&r: \n"
            + "  format-to-dd: :thought_balloon: **{sender}**: {message} \n"
            + "  allow-server-cmds: false - Use this channel to send commands from discord > minecraft.\n"
            + "  channelID: '' - The IDs of your Discord Channels. Enable debug on your discord to get the channel ID.\n"
            + "  Note: You can add more than one discord id, just separate by \",\" like: 13246579865498,3216587898754\n");

    ch.getProperties().forEach((key, value) -> chFile.getNode((Object[]) key.toString().split("\\.")).setValue(value));
    channelManager.save(chFile);

    if (UChat.get().getChannel(ch.getName()) != null) {
        ch.setMembers(UChat.get().getChannel(ch.getName()).getMembers());
        UChat.get().getChannels().remove(Arrays.asList(ch.getName().toLowerCase(), ch.getAlias().toLowerCase()));
    }
    UChat.get().getChannels().put(Arrays.asList(ch.getName().toLowerCase(), ch.getAlias().toLowerCase()), ch);
}