generated from KessokuTeaTime/Example-Mod
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
4 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/java/band/kessokuteatime/nightautoconfig/config/ExampleConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/band/kessokuteatime/nightautoconfig/serializer/ConfigType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/band/kessokuteatime/nightautoconfig/serializer/NightConfigSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
4 changes: 0 additions & 4 deletions
4
src/main/java/band/kessokuteatime/nightautoconfig/serializers/JsonNightConfigSerializer.java
This file was deleted.
Oops, something went wrong.