Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
0.0.2 alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed Nov 25, 2022
1 parent 88f78f7 commit 6202f88
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 103 deletions.
73 changes: 71 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,74 @@
# `< Plume 🪶 Config >`

## License
### `Fabric` `Minecraft 1.18.2` `...` `0.0.1 α`

This mod is under the MIT license.
## `📖 Introduction`

**Plume Config is a lightweight Configuration Library for Minecraft Mods.**

If you are looking for an easy way to create, write and read configuration files for your mod, this is the library for you. Just a few simple steps will get you up and running in no time.

Plume Config is using `.properties` files to store the configuration. **This is a simple and easy to read format from Java.** This means that you can easily edit the configuration files with any text editor.

## `📦 Installation`

**Plume Config is currently in `alpha.`**
You can download alpha releases from GitHub for now.

If you meet the requirements, feel free to build the library yourself.

## `📝 Configuration`

### `🧑‍💻 Example Config Content`

```properties
# | Category Booleans |

option.test.boolean=false
# Default: false


# | Category Numbers |

option.test.color=FF123456
# Default: FFFFFFFF

option.test.int=0
# Default: 0


# | Category String Values |

option.test.string=abc
# Default: abc

option.test.double=0.0
# Default: 0.0
```

> **Note:**
>
> The `#` character is used to comment out lines. This means that the line will be ignored by the library. You can define the comments in your codes to add descriptions to your configuration.
>
> The `=` character is used to separate the key from the value. The key must be unique to identify the value. The value can be a boolean, a number, a string or a hex-string stored integer color (custom value types will be allowed in future).
> **Please refer to our wiki page for example codes.**
### `📁 Structure`

The configuration files are stored in the `config` folder of your Minecraft instance. The folder structure is as follows:

config
├── <modid>
│ ├── <config1>.properties
│ └── <config2>.properties
└── <another_modid>
└── <another_config>.properties

Folder names and file names are defined by the mod developer, and there could be multiple configuration files under one folder. Plume Config will create the folders and files if they do not exist.

**Please use your modid as the folder name in case of repetition with another mod.**

## `📜 License`

This mod is available under the `MIT license.`
Binary file added artwork/banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added artwork/data/banner.pxd
Binary file not shown.
Binary file added artwork/data/icon.pxd
Binary file not shown.
Binary file added artwork/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.14.6

# Mod Properties
mod_version = 0.0.1-alpha
mod_version = 0.0.2-alpha
maven_group = net.krlite
archives_base_name = plumeconfig

Expand Down
16 changes: 6 additions & 10 deletions src/main/java/net/krlite/plumeconfig/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,30 @@ public class Formatter {
public static final String END_LINE = "\n\n";
public static final String END_CATEGORY = "\n\n\n";

public static String newLine(String content) {
return END + content;
}

public static String formatCategory(String category) {
return "# | " + category + " |";
}

public static <T> String formatOption(String name, String key, T value, T defaultValue, String comment) {
return formatName(name)
+ newLine(formatContent(key, value))
+ newLine(formatDefaultValue(defaultValue))
+ formatContent(key, value)
+ formatDefaultValue(defaultValue)
+ formatComment(comment);
}

private static String formatName(String name) {
return (name != null ? "# " + name : "");
return (name != null ? "# " + name + END : "");
}

private static <T> String formatContent(String key, T value) {
return key + "=" + value.toString();
return key + "=" + value.toString() + END;
}

private static <T> String formatDefaultValue(T defaultValue) {
return "# Default: " + defaultValue.toString();
return "# Default: " + defaultValue.toString() + END;
}

private static String formatComment(String comment) {
return (comment != null ? newLine("# " + comment) : "");
return (comment != null ? "# " + comment + END_LINE : END);
}
}
25 changes: 17 additions & 8 deletions src/main/java/net/krlite/plumeconfig/PlumeConfigMod.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
package net.krlite.plumeconfig;

import net.fabricmc.api.ModInitializer;
import net.krlite.plumeconfig.test.TestConfig;
import net.krlite.plumeconfig.example.ExampleConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;

public class PlumeConfigMod implements ModInitializer {
public static final String MOD_ID = "plumeconfig";
public static final Logger LOGGER = LoggerFactory.getLogger("Plume Config");

@Override
public void onInitialize() {
LOGGER.info("Initializing config...");
TestConfig testConfig = new TestConfig();
PlumeConfigMod.LOGGER.error(String.valueOf(testConfig.testColor.getValue().getRGB()));
LOGGER.info("Try loading config...");
testConfig.load();
/*
*
* This is an example of how to load and write the configs when you need to.
*
* LOGGER.info("Initializing config...");
* ExampleConfig exampleConfig = new ExampleConfig("test", "testConfig"); // Initialize the config class by new an instance of it
*
* LOGGER.info("Try loading config..."); // You can load the config as soon as you initialized the config, if the file or some of the values are missing, they will be written in default values
* exampleConfig.load(); // Remember to use pre-defined load() method instead of read() to load the config
*
*
*
* LOGGER.info("Try writing config..."); // You can write the config whenever you want, it will override the old values in the config file
* exampleConfig.write() // Remember to use pre-defined write() method instead of save() to load the config
*
*/
}

// TODO: Request manipulates the config file (write, load, etc.)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@
import java.io.IOException;
import java.util.HashMap;

public record PlumeConfig(Request request) {
public record Config(Request request) {
public void prepare(@NotNull ConfigBuilder configBuilder) {
if (!request.formattedConfig.isEmpty()) {
request.clear();
}

if (configBuilder.categories.containsKey(ConfigBuilder.ROOT_CATEGORY)) {
request.appendCategory(configBuilder.categories.get(ConfigBuilder.ROOT_CATEGORY));
request.appendCategory(configBuilder.categories.get(ConfigBuilder.ROOT_CATEGORY), true);
}

for (Option<?> option : configBuilder.options.values()) {
request.appendOption(option);

if (configBuilder.categories.containsKey(option.getKey())) {
request.appendCategory(configBuilder.categories.get(option.getKey()));
request.appendCategory(configBuilder.categories.get(option.getKey()), false);
}
}
}

private void create() {
try {
request.create();
write();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -37,7 +37,6 @@ private void create() {
public HashMap<String, String> read() {
try {
request.load();

return request.config;
} catch (IOException e) {
create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.krlite.plumeconfig.config;

import net.krlite.plumeconfig.PlumeConfigMod;
import net.krlite.plumeconfig.option.core.Option;
import org.jetbrains.annotations.NotNull;

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/krlite/plumeconfig/config/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public void create() throws IOException {
Files.createFile(file.toPath());
}

public void appendCategory(String category) {
formattedConfig += Formatter.formatCategory(category) + Formatter.END;
public void appendCategory(String category, boolean isRoot) {
formattedConfig += (isRoot ? "" : Formatter.END) + Formatter.formatCategory(category) + Formatter.END_LINE;
}

public <T extends Option<?>> void appendOption(@NotNull T option) {
formattedConfig += Formatter.END + option.format() + Formatter.END;
formattedConfig += option.format();
}

private void parseConfigEntry(@NotNull String entry) {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/net/krlite/plumeconfig/config/api/PlumeConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.krlite.plumeconfig.config.api;

import net.fabricmc.loader.api.FabricLoader;
import net.krlite.plumeconfig.config.Config;
import net.krlite.plumeconfig.config.Request;

import java.nio.file.Path;

public abstract class PlumeConfig extends PlumeConfigApi{
public PlumeConfig(String fileName) {
request = new Request(FabricLoader.getInstance().getConfigDir(), fileName);
config = new Config(request);
}

public PlumeConfig(String path, String fileName) {
request = new Request(Path.of(FabricLoader.getInstance().getConfigDir().toString(), "/", path), fileName);
config = new Config(request);
}

private final Config config;
private final Request request;

Config config() {
return this.config;
}
}
32 changes: 12 additions & 20 deletions src/main/java/net/krlite/plumeconfig/config/api/PlumeConfigApi.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
package net.krlite.plumeconfig.config.api;

import net.fabricmc.loader.api.FabricLoader;
import net.krlite.plumeconfig.config.Config;
import net.krlite.plumeconfig.config.ConfigBuilder;
import net.krlite.plumeconfig.config.PlumeConfig;
import net.krlite.plumeconfig.config.Request;

import java.nio.file.Path;
import java.util.HashMap;

public abstract class PlumeConfigApi {
private final PlumeConfig plumeConfig;
private final Request request;

public PlumeConfigApi(String fileName) {
request = new Request(FabricLoader.getInstance().getConfigDir(), fileName);
plumeConfig = new PlumeConfig(request);
}

public PlumeConfigApi(String path, String fileName) {
request = new Request(Path.of(FabricLoader.getInstance().getConfigDir().toString(), "/", path), fileName);
plumeConfig = new PlumeConfig(request);
}
abstract class PlumeConfigApi {
abstract Config config();

public abstract void read(HashMap<String, String> config);

public abstract void save(ConfigBuilder configBuilder);

public void load() {
this.read(this.plumeConfig.read());
prepare();
this.read(this.config().read());
write();
}

public void write() {
prepare();
this.config().write();
}

private void prepare() {
ConfigBuilder configBuilder = new ConfigBuilder();
this.save(configBuilder);
this.plumeConfig.prepare(configBuilder);
this.plumeConfig.write();
this.config().prepare(configBuilder);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package net.krlite.plumeconfig.config.json;

import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import net.krlite.plumeconfig.config.PlumeConfig;
import net.krlite.plumeconfig.config.Config;

public interface ConfigSerializer<T extends PlumeConfig> {
public interface ConfigSerializer<T extends Config> {
T serialize(JsonObject json);
JsonObject deSerialize(T config);
}
58 changes: 58 additions & 0 deletions src/main/java/net/krlite/plumeconfig/example/ExampleConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.krlite.plumeconfig.example;

import net.krlite.plumeconfig.config.ConfigBuilder;
import net.krlite.plumeconfig.config.api.PlumeConfig;
import net.krlite.plumeconfig.option.*;

import java.awt.*;
import java.util.HashMap;

public class ExampleConfig extends PlumeConfig {
// The constructor, you can modify the path and file name here or leave them as parameters
public ExampleConfig(String path, String fileName) {
super(path, fileName);
}

// Define your options here using the Option* classes
public OptionBoolean exampleBoolean = new OptionBoolean("The name of the example boolean", "option.example.boolean", false);
public OptionDouble exampleDouble = new OptionDouble(/* The name can be null */ "option.example.double", 0.0 /* The comment can also be null */);
public OptionLong exampleInt = new OptionLong("option.example.int", 0 /* The key and the default value cannot be null */);
public OptionString exampleString = new OptionString("My example string", "option.example.string", "abc", "Hey, this is an example comment!");
public OptionColor exampleColor = new OptionColor("option.example.color", Color.WHITE, "Expected color value: AARRGGBB");

/**
* Use config.parse() method to read from the config file.
*
* @param config A HashMap represents the config file, can be read by the config keys.
*/
public void read(HashMap<String, String> config) {
exampleBoolean.parse(config.get(exampleBoolean.getKey()));
exampleDouble.parse(config.get(exampleDouble.getKey()));
exampleInt.parse(config.get(exampleInt.getKey()));
exampleString.parse(config.get(exampleString.getKey()));
exampleColor.parse(config.get(exampleColor.getKey()));
}

/**
* Put the values you want to append to the config file here.
*
* @param configBuilder A ConfigBuilder object, use configBuilder.accept() to append to the config file.
*/
public void save(ConfigBuilder configBuilder) {
// We can append categories
configBuilder.accept("Category Booleans");
// Append an option (do not do it twice to the same option)
configBuilder.accept(exampleBoolean);

// Another category
configBuilder.accept("Category Numbers");
configBuilder.accept(exampleDouble);
configBuilder.accept(exampleInt);

configBuilder.accept("Category String Values");
configBuilder.accept(exampleString);
configBuilder.accept(exampleColor);

// ...More options and categories
}
}
Loading

0 comments on commit 6202f88

Please sign in to comment.