Skip to content

Commit

Permalink
Add loading from configurate node
Browse files Browse the repository at this point in the history
  • Loading branch information
virustotalop committed Feb 9, 2024
1 parent 72e5a71 commit 903876c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repositories {
maven { url 'https://jitpack.io' }
}
compile 'com.github.clubobsidian:wrappy:4.0.0'
compile 'com.github.clubobsidian:wrappy:4.1.0'
```

### Maven
Expand All @@ -45,7 +45,7 @@ compile 'com.github.clubobsidian:wrappy:4.0.0'
<dependency>
<groupId>com.github.clubobsidian</groupId>
<artifactId>wrappy</artifactId>
<version>4.0.0</version>
<version>4.1.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "com.github.clubobsidian"
version = "4.0.0"
version = "4.1.0"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(11))
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/clubobsidian/wrappy/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.clubobsidian.wrappy.util.HashUtil;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
import org.spongepowered.configurate.jackson.JacksonConfigurationLoader;
import org.spongepowered.configurate.loader.AbstractConfigurationLoader;
Expand Down Expand Up @@ -107,6 +108,12 @@ public static Configuration load(ConfigurationLoader<?> loader) {
return modified ? config : null;
}

public static Configuration load(ConfigurationNode node) {
Configuration config = new Configuration();
config.node = node;
return config;
}

private static boolean modifyNode(Configuration config, ConfigurationLoader<?> loader) {
try {
config.loader = loader;
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/com/clubobsidian/wrappy/ConfigurationSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ public class ConfigurationSection {
protected ConfigurationLoader<?> loader;

public boolean save() {
if (!this.loader.canSave()) {
return false;
}
try {
this.loader.save(this.node);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
if (this.loader != null && this.loader.canSave()) {
try {
this.loader.save(this.node);
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}

public ConfigurationNode getNode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class TestConfigurateLoading {
private static File testFile = new File("test.yml");

@Test
public void loadConfigurate() {
public void loadConfigurateLoader() {
ConfigurationLoader<?> loader = YamlConfigurationLoader
.builder()
.nodeStyle(NodeStyle.BLOCK)
Expand All @@ -41,4 +41,17 @@ public void loadConfigurate() {
Configuration config = Configuration.load(loader);
assertEquals("value", config.getString("key"));
}

@Test
public void loadConfigurateNode() {
ConfigurationLoader<?> loader = YamlConfigurationLoader
.builder()
.nodeStyle(NodeStyle.BLOCK)
.indent(2)
.path(testFile.toPath())
.build();
Configuration config = Configuration.load(loader);
Configuration copied = Configuration.load(config.getNode());
assertEquals("value", copied.getString("key"));
}
}

0 comments on commit 903876c

Please sign in to comment.