Search in sources :

Example 11 with ConfigurationNode

use of org.spongepowered.configurate.ConfigurationNode in project SpongeCommon by SpongePowered.

the class ConfigurateDataViewTest method testEmptyDataToNode.

@Test
void testEmptyDataToNode() {
    final DataContainer source = DataContainer.createNew();
    final ConfigurationNode destination = ConfigurateTranslator.instance().translate(source);
    assertTrue(destination.empty());
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) BasicConfigurationNode(org.spongepowered.configurate.BasicConfigurationNode) Test(org.junit.jupiter.api.Test)

Example 12 with ConfigurationNode

use of org.spongepowered.configurate.ConfigurationNode in project SpongeCommon by SpongePowered.

the class HoconDataFormat method writeTo.

private static void writeTo(final Callable<BufferedWriter> sink, final DataView data) throws IOException {
    final HoconConfigurationLoader loader = HoconConfigurationLoader.builder().sink(sink).build();
    final ConfigurationNode node = loader.createNode();
    ConfigurateTranslator.instance().translateDataToNode(node, data);
    try {
        loader.save(node);
    } catch (final ConfigurateException ex) {
        throw new IOException(ex);
    }
}
Also used : HoconConfigurationLoader(org.spongepowered.configurate.hocon.HoconConfigurationLoader) CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) ConfigurateException(org.spongepowered.configurate.ConfigurateException) IOException(java.io.IOException)

Example 13 with ConfigurationNode

use of org.spongepowered.configurate.ConfigurationNode in project SpongeCommon by SpongePowered.

the class FileMovingConfigurationTransformation method apply.

@Override
public void apply(@NonNull final ConfigurationNode oldConfig) throws ConfigurateException {
    final ConfigurationNode newConfig = this.destinationLoader.load();
    boolean acted = false;
    for (final NodePath path : this.paths) {
        final ConfigurationNode source = oldConfig.node(path);
        if (!source.virtual()) {
            acted = true;
            if (this.override) {
                newConfig.node(path).from(source);
            } else {
                newConfig.node(path).mergeFrom(source);
            }
            source.raw(null);
        }
    }
    if (acted) {
        this.destinationLoader.save(newConfig);
    }
}
Also used : ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) NodePath(org.spongepowered.configurate.NodePath)

Example 14 with ConfigurationNode

use of org.spongepowered.configurate.ConfigurationNode in project SpongeCommon by SpongePowered.

the class DuplicateRemovalVisitor method enterScalarNode.

@Override
@SuppressWarnings("UnnecessaryUnboxing")
public void enterScalarNode(final ConfigurationNode node, final AtomicReference<ConfigurationNode> parent) {
    final ConfigurationNode parentNode = this.popParent(parent);
    // ignore list values
    if (this.isListElement(node)) {
        return;
    }
    // ignore the version key that's a child of the root node
    if (Objects.equals(node.key(), "version") && node.parent().parent() == null) {
        return;
    }
    // if the node already exists in the parent config, remove it
    if (Objects.equals(node.raw(), parentNode.raw())) {
        node.raw(null);
        return;
    }
    // Fix double bug
    final Double nodeVal = Scalars.DOUBLE.tryDeserialize(node.raw());
    if (nodeVal != null) {
        final Double parentVal = Scalars.DOUBLE.tryDeserialize(parentNode.raw());
        if (parentVal == null && nodeVal.doubleValue() == 0 || (parentVal != null && nodeVal.doubleValue() == parentVal.doubleValue())) {
            node.raw(null);
        }
    }
}
Also used : ConfigurationNode(org.spongepowered.configurate.ConfigurationNode)

Example 15 with ConfigurationNode

use of org.spongepowered.configurate.ConfigurationNode in project SpongeCommon by SpongePowered.

the class ConfigurateDataViewTest method testNumber.

@Test
void testNumber() throws IOException {
    final DataContainer container = DataContainer.createNew().set(DataQuery.of("double"), 1.0);
    final ConfigurationNode node = ConfigurateTranslator.instance().translate(container);
    assertEquals(1.0, node.node("double").raw());
    final DataContainer dc = ConfigurateTranslator.instance().translate(node);
    assertEquals(container, dc);
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) BasicConfigurationNode(org.spongepowered.configurate.BasicConfigurationNode) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigurationNode (org.spongepowered.configurate.ConfigurationNode)24 CommentedConfigurationNode (org.spongepowered.configurate.CommentedConfigurationNode)20 Test (org.junit.jupiter.api.Test)19 BasicConfigurationNode (org.spongepowered.configurate.BasicConfigurationNode)19 DataContainer (org.spongepowered.api.data.persistence.DataContainer)7 DataView (org.spongepowered.api.data.persistence.DataView)2 ConfigurateException (org.spongepowered.configurate.ConfigurateException)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ResourceKey (org.spongepowered.api.ResourceKey)1 RespawnLocation (org.spongepowered.api.util.RespawnLocation)1 NodePath (org.spongepowered.configurate.NodePath)1 HoconConfigurationLoader (org.spongepowered.configurate.hocon.HoconConfigurationLoader)1