Java Code Examples for ninja.leaping.configurate.commented.CommentedConfigurationNode#setValue()

The following examples show how to use ninja.leaping.configurate.commented.CommentedConfigurationNode#setValue() . 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: VirtualChestPluginUpdate.java    From VirtualChest with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void saveConfig(CommentedConfigurationNode node)
{
    if (this.checkPre)
    {
        node.setValue("prerelease");
    }
    else if (this.check)
    {
        node.setValue("release");
    }
    else
    {
        node.setValue("disabled");
    }
    node.setComment(this.plugin.getTranslation().take("virtualchest.config.checkUpdate.comment").toPlain());
}
 
Example 2
Source File: ConfigCompleter.java    From UltimateCore with MIT License 6 votes vote down vote up
private static boolean complete(CommentedConfigurationNode assetnode, CommentedConfigurationNode sourcenode) {
    boolean changed = false;
    //Children of sourcenode && assetnode
    Map<Object, ? extends CommentedConfigurationNode> sourcenodes = sourcenode.getChildrenMap();
    Map<Object, ? extends CommentedConfigurationNode> assetnodes = assetnode.getChildrenMap();

    //For each child of assetnode
    for (Object name : new ArrayList<>(assetnode.getChildrenMap().keySet())) {
        //The current child of assetnode which is being tested
        CommentedConfigurationNode node = assetnode.getNode(name);
        //If it doesnt contain the current key
        if (!sourcenodes.containsKey(name)) {
            CommentedConfigurationNode newnode = sourcenode.getNode(name);
            newnode.setValue(node.getValue());
            newnode.setComment(node.getComment().orElse(null));
            changed = true;
        }
    }
    return changed;
}
 
Example 3
Source File: PlayerStorageData.java    From GriefDefender with MIT License 5 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
public PlayerStorageData(Path path) {

    try {
        if (Files.notExists(path.getParent())) {
            Files.createDirectories(path.getParent());
        }
        if (Files.notExists(path)) {
            Files.createFile(path);
        }

        this.loader = HoconConfigurationLoader.builder().setPath(path).build();
        this.configMapper = (ObjectMapper.BoundInstance) ObjectMapper.forClass(PlayerDataConfig.class).bindToNew();
        this.root = this.loader.load(ConfigurationOptions.defaults());
        CommentedConfigurationNode rootNode = this.root.getNode(GriefDefenderPlugin.MOD_ID);
        // Check if server is using existing Sponge GP data
        if (rootNode.isVirtual()) {
            // check GriefPrevention
            CommentedConfigurationNode gpRootNode = this.root.getNode("GriefPrevention");
            if (!gpRootNode.isVirtual()) {
                rootNode.setValue(gpRootNode.getValue());
                gpRootNode.setValue(null);
            }
        }
        this.configBase = this.configMapper.populate(rootNode);
        save();
    } catch (Exception e) {
        GriefDefenderPlugin.getInstance().getLogger().error("Failed to initialize configuration", e);
    }
}
 
Example 4
Source File: Config.java    From ChatUI with MIT License 5 votes vote down vote up
public static void loadConfig() {
    try {
        config = confLoader.load();
    } catch (IOException e) {
        logger.error("Error while loading config", e);
        config = confLoader.createEmptyNode();
    }
    CommentedConfigurationNode playerSettings = config.getNode("player-settings");
    if (playerSettings.isVirtual()) {
        playerSettings.setValue(Collections.emptyMap());
    }
}
 
Example 5
Source File: ConfigHandler.java    From Nations with MIT License 5 votes vote down vote up
public static void ensureString(CommentedConfigurationNode node, String def)
{
	if (node.getString() == null)
	{
		node.setValue(def);
	}
}
 
Example 6
Source File: ConfigHandler.java    From Nations with MIT License 5 votes vote down vote up
public static void ensurePositiveNumber(CommentedConfigurationNode node, Number def)
{
	if (!(node.getValue() instanceof Number) || node.getDouble(-1) < 0)
	{
		node.setValue(def);
	}
}
 
Example 7
Source File: ConfigHandler.java    From Nations with MIT License 5 votes vote down vote up
public static void ensureBoolean(CommentedConfigurationNode node, boolean def)
{
	if (!(node.getValue() instanceof Boolean))
	{
		node.setValue(def);
	}
}
 
Example 8
Source File: BloodModule.java    From UltimateCore with MIT License 5 votes vote down vote up
@Override
public void onInit(GameInitializationEvent event) {
    //Config
    TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(BloodEffect.class), new BloodEffect.BloodEffectSerializer());
    config = new RawModuleConfig("blood");
    //Check if all entity types are in the config
    CommentedConfigurationNode node = config.get();
    boolean modified = false;
    //For every entitytype, if it doesnt exist in the config add it
    for (EntityType type : Sponge.getRegistry().getAllOf(CatalogTypes.ENTITY_TYPE)) {
        if (!Living.class.isAssignableFrom(type.getEntityClass())) {
            continue;
        }
        //If entitytype is not in config
        if (node.getNode("types", type.getId(), "enabled").getValue() == null) {
            modified = true;
            CommentedConfigurationNode typenode = node.getNode("types", type.getId());
            try {
                typenode.setValue(TypeToken.of(BloodEffect.class), BloodEffects.DEFAULT);
            } catch (ObjectMappingException e) {
                ErrorLogger.log(e, "Failed to set default blood effect.");
            }
        }
    }
    if (modified) {
        config.save(node);
    }
    //Load blood effects from config
    BloodEffects.reload();
    //Listeners
    Sponge.getEventManager().registerListeners(UltimateCore.get(), new BloodListener());
}
 
Example 9
Source File: GriefDefenderConfig.java    From GriefDefender with MIT License 4 votes vote down vote up
/**
 * Traverses the given {@code root} config node, removing any values which
 * are also present and set to the same value on this configs "parent".
 *
 * @param root The node to process
 */
private void removeDuplicates(CommentedConfigurationNode root) {
    if (this.parent == null) {
        throw new IllegalStateException("parent is null");
    }

    Iterator<ConfigurationNodeWalker.VisitedNode<CommentedConfigurationNode>> it = ConfigurationNodeWalker.DEPTH_FIRST_POST_ORDER.walkWithPath(root);
    while (it.hasNext()) {
        ConfigurationNodeWalker.VisitedNode<CommentedConfigurationNode> next = it.next();
        CommentedConfigurationNode node = next.getNode();

        // remove empty maps
        if (node.hasMapChildren()) {
            if (node.getChildrenMap().isEmpty()) {
                node.setValue(null);
            }
            continue;
        }

        // ignore list values
        if (node.getParent() != null && node.getParent().getValueType() == ValueType.LIST) {
            continue;
        }

        // if the node already exists in the parent config, remove it
        CommentedConfigurationNode parentValue = this.parent.data.getNode(next.getPath().getArray());
        if (Objects.equals(node.getValue(), parentValue.getValue())) {
            node.setValue(null);
        } else {
            // Fix list bug
            if (parentValue.getValue() == null) {
                if (node.getValueType() == ValueType.LIST) {
                    final List<?> nodeList = (List<?>) node.getValue();
                    if (nodeList.isEmpty()) {
                        node.setValue(null);
                    }
                    continue;
                }
            }
            // Fix double bug
            final Double nodeVal = node.getValue(Types::asDouble);
            if (nodeVal != null) {
                Double parentVal = parentValue.getValue(Types::asDouble);
                if (parentVal == null && nodeVal.doubleValue() == 0 || (parentVal != null && nodeVal.doubleValue() == parentVal.doubleValue())) {
                    node.setValue(null);
                    continue;
                }
            }
        }
    }
}
 
Example 10
Source File: GriefDefenderConfig.java    From GriefDefender with MIT License 4 votes vote down vote up
/**
 * Traverses the given {@code root} config node, removing any values which
 * are also present and set to the same value on this configs "parent".
 *
 * @param root The node to process
 */
private void removeDuplicates(CommentedConfigurationNode root) {
    if (this.parent == null) {
        throw new IllegalStateException("parent is null");
    }

    Iterator<ConfigurationNodeWalker.VisitedNode<CommentedConfigurationNode>> it = ConfigurationNodeWalker.DEPTH_FIRST_POST_ORDER.walkWithPath(root);
    while (it.hasNext()) {
        ConfigurationNodeWalker.VisitedNode<CommentedConfigurationNode> next = it.next();
        CommentedConfigurationNode node = next.getNode();

        // remove empty maps
        if (node.hasMapChildren()) {
            if (node.getChildrenMap().isEmpty()) {
                node.setValue(null);
            }
            continue;
        }

        // ignore list values
        if (node.getParent() != null && node.getParent().getValueType() == ValueType.LIST) {
            continue;
        }

        // if the node already exists in the parent config, remove it
        CommentedConfigurationNode parentValue = this.parent.data.getNode(next.getPath().getArray());
        if (Objects.equals(node.getValue(), parentValue.getValue())) {
            node.setValue(null);
        } else {
            // Fix list bug
            if (parentValue.getValue() == null) {
                if (node.getValueType() == ValueType.LIST) {
                    final List<?> nodeList = (List<?>) node.getValue();
                    if (nodeList.isEmpty()) {
                        node.setValue(null);
                    }
                    continue;
                }
            }
            // Fix double bug
            final Double nodeVal = node.getValue(Types::asDouble);
            if (nodeVal != null) {
                Double parentVal = parentValue.getValue(Types::asDouble);
                if (parentVal == null && nodeVal.doubleValue() == 0 || (parentVal != null && nodeVal.doubleValue() == parentVal.doubleValue())) {
                    node.setValue(null);
                    continue;
                }
            }
        }
    }
}
 
Example 11
Source File: GriefPreventionConfig.java    From GriefPrevention with MIT License 4 votes vote down vote up
/**
 * Traverses the given {@code root} config node, removing any values which
 * are also present and set to the same value on this configs "parent".
 *
 * @param root The node to process
 */
private void removeDuplicates(CommentedConfigurationNode root) {
    if (this.parent == null) {
        throw new IllegalStateException("parent is null");
    }

    Iterator<ConfigurationNodeWalker.VisitedNode<CommentedConfigurationNode>> it = ConfigurationNodeWalker.DEPTH_FIRST_POST_ORDER.walkWithPath(root);
    while (it.hasNext()) {
        ConfigurationNodeWalker.VisitedNode<CommentedConfigurationNode> next = it.next();
        CommentedConfigurationNode node = next.getNode();

        // remove empty maps
        if (node.hasMapChildren()) {
            if (node.getChildrenMap().isEmpty()) {
                node.setValue(null);
            }
            continue;
        }

        // ignore list values
        if (node.getParent() != null && node.getParent().getValueType() == ValueType.LIST) {
            continue;
        }

        // if the node already exists in the parent config, remove it
        CommentedConfigurationNode parentValue = this.parent.data.getNode(next.getPath().getArray());
        if (Objects.equals(node.getValue(), parentValue.getValue())) {
            node.setValue(null);
        } else {
            // Fix list bug
            if (parentValue.getValue() == null) {
                if (node.getValueType() == ValueType.LIST) {
                    final List<?> nodeList = (List<?>) node.getValue();
                    if (nodeList.isEmpty()) {
                        node.setValue(null);
                    }
                    continue;
                }
            }
            // Fix double bug
            final Double nodeVal = node.getValue(Types::asDouble);
            if (nodeVal != null) {
                Double parentVal = parentValue.getValue(Types::asDouble);
                if (parentVal == null && nodeVal.doubleValue() == 0 || (parentVal != null && nodeVal.doubleValue() == parentVal.doubleValue())) {
                    node.setValue(null);
                    continue;
                }
            }
        }
    }
}