Skip to content

Commit

Permalink
maybe it works.
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed May 2, 2024
1 parent ae4f4ef commit d42c264
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package band.kessokuteatime.nightautoconfig.config;

import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;

@Config(name = "example")
public class ExampleConfig implements ConfigData {
public int exampleInt = 10;
public double exampleDouble = 3.14159;
public float exampleFloat = 2.71828F;
public boolean exampleBoolean = true;
public String exampleString = "Hello, World!";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package band.kessokuteatime.nightautoconfig.serializer;

import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.util.Utils;

import java.nio.file.Path;

public enum ConfigType {
JSON("json"), YAML("yaml"), TOML("toml"), HOCON("conf");

private final String suffix;

ConfigType(String suffix) {
this.suffix = suffix;
}

public String suffix() {
return suffix;
}

public Path getConfigPath(Config definition) {
return Utils.getConfigFolder().resolve(definition.name() + suffix());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package band.kessokuteatime.nightautoconfig.serializer;

import com.electronwill.nightconfig.core.conversion.ObjectConverter;
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.electronwill.nightconfig.core.file.CommentedFileConfigBuilder;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.serializer.ConfigSerializer;
import me.shedaniel.autoconfig.util.Utils;

import java.util.function.UnaryOperator;

public class NightConfigSerializer<T extends ConfigData> implements ConfigSerializer<T> {
protected final Config definition;
protected final Class<T> configClass;
protected final ConfigType type;
protected final CommentedFileConfigBuilder builder;

public NightConfigSerializer(Config definition, Class<T> configClass, ConfigType type, CommentedFileConfigBuilder builder) {
this.definition = definition;
this.configClass = configClass;
this.type = type;
this.builder = builder;
}

public NightConfigSerializer(Config definition, Class<T> configClass, ConfigType type, UnaryOperator<CommentedFileConfigBuilder> builder) {
this(definition, configClass, type, builder.apply(CommentedFileConfig.builder(type.getConfigPath(definition))));
}

public NightConfigSerializer(Config definition, Class<T> configClass, ConfigType type) {
this(definition, configClass, type, UnaryOperator.identity());
}

@Override
public void serialize(T t) throws SerializationException {
CommentedFileConfig config = new ObjectConverter().toConfig(t, builder::build);
config.save();
config.close();
}

@Override
public T deserialize() throws SerializationException {
CommentedFileConfig config = builder.build();
config.load();
return new ObjectConverter().toObject(config, this::createDefault);
}

@Override
public T createDefault() {
return Utils.constructUnsafely(configClass);
}
}

This file was deleted.

0 comments on commit d42c264

Please sign in to comment.