Skip to content

Value-getter return default value if the actual value is explicitly set to 0 #206

@TBlueF

Description

@TBlueF

Version

dependencies {
    implementation 'org.spongepowered:configurate-hocon:4.1.1'
}

Description

Given:
A (hocon) configuration-file node has its value explicitly set to 0.
It got loaded using a HoconConfigurationLoader .
The value is retrieved as a float using a getter with a default-fallback argument (e.g. node.getFloat(1f))

Then:
That getter returns the default/fallback-value. (e.g. 1f)

Expected result:
That getter returns the explicitly set value: 0f

Extra info:
This only happens with float values.
This only happens if the node got loaded from a config-file.
I only tested the HOCON-loader.

Code Example

public static void main(String[] args) throws IOException {
	Path testFile = Path.of("testFile.conf").toAbsolutePath();
	Files.createDirectories(testFile.getParent());

	HoconConfigurationLoader loader = HoconConfigurationLoader
			.builder()
			.path(testFile)
			.build();

	ConfigurationNode originalNode = loader.createNode();
	originalNode.node("test").set(0f);

	loader.save(originalNode);

	System.out.println("## Original Node:");
	testNode(originalNode);

	ConfigurationNode loadedNode = loader.load();

	System.out.println("\n## Loaded Node:");
	testNode(loadedNode);
}

public static void testNode(ConfigurationNode node) {
	float result = node.node("test").getFloat();
	float resultWithDefault = node.node("test").getFloat(1f); // <- this returns 1f instead of 0f if the node got loaded

	System.out.println("Result: " + result);
	System.out.println("Result with default: " + resultWithDefault);
}

The console output of the above code:

## Original Node:
Result: 0.0
Result with default: 0.0

## Loaded Node:
Result: 0.0
Result with default: 1.0             <-- should be 0.0

The contents of the testFile.conf (after execution):

test=0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions