Skip to content

Commit

Permalink
Version 2.1.0 with arrays!
Browse files Browse the repository at this point in the history
  • Loading branch information
OroArmor committed Apr 2, 2021
1 parent e16ea58 commit 6472504
Show file tree
Hide file tree
Showing 14 changed files with 514 additions and 81 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ The details are a string representing a language key that can be used in your la

The second constructor has an `onChange` parameter, which is a Consumer that is run every time this config is changed, which can be used to send data to clients on servers or trigger other events.

You can get the value from a `ConfigItem` with the `get` method.
You can get the value from a `ConfigItem` with the `getValue` method.

#### Array Config Items

`ArrayConfigItem`s are exactly the same as normal config items, but have a couple useful methods to get values based on their index. You cannot nest `ArrayConfigItem`s inside other `ArrayConfigItem`s

### Config Item Groups
`ConfigItemGroup`s are a way to store multiple `ConfigItem`s into one group. `ConfigItemGroup`s can be nested in each other for sub groups. There is one constructor:
Expand Down Expand Up @@ -117,21 +121,23 @@ public class TestConfig extends Config {

public static class ConfigGroupLevel1 extends ConfigItemGroup {
public static final ConfigItem<EnumTest> testEnum = new ConfigItem<>("test_enum", EnumTest.A, "test_enum");
public static final ConfigItem<Boolean> testItem = new ConfigItem<Boolean>("test_boolean", true, "test_boolean");
public static final ConfigItem<Boolean> testItem = new ConfigItem<>("test_boolean", true, "test_boolean");

public static final ArrayConfigItem<Integer> testArray = new ArrayConfigItem<>("test_array", new Integer[]{1, 2, 3}, "test_array");

public ConfigGroupLevel1() {
super(of(new NestedGroup(), testItem, testEnum), "group");
super(of(new NestedGroup(), testItem, testEnum, testArray), "group");
}

public static class NestedGroup extends ConfigItemGroup {
public static final ConfigItem<Integer> nestedItem = new ConfigItem<Integer>("test_int", 0, "test_integer");
public static final ConfigItem<Integer> nestedItem = new ConfigItem<>("test_int", 0, "test_integer");

public NestedGroup() {
super(of(nestedItem, new TripleNested()), "nested");
}

public static class TripleNested extends ConfigItemGroup {
public static final ConfigItem<String> testString = new ConfigItem<String>("test_string", "Default", "test_string");
public static final ConfigItem<String> testString = new ConfigItem<>("test_string", "Default", "test_string");

public TripleNested() {
super(of(testString), "triple");
Expand All @@ -152,7 +158,12 @@ This then creates a config file called `oroarmor_config_testmod.json` in the `/c
}
},
"test_boolean": true,
"test_enum": "A"
"test_enum": "A",
"test_array": [
1,
2,
3
]
}
}
```
Expand Down
144 changes: 144 additions & 0 deletions common/src/main/java/com/oroarmor/config/ArrayConfigItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* MIT License
*
* Copyright (c) 2021 OroArmor (Eli Orona)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.oroarmor.config;

import java.util.Arrays;
import java.util.function.Consumer;

import com.google.gson.JsonElement;
import org.jetbrains.annotations.Nullable;

/**
* {@link ArrayConfigItem} stores an array of the supported types <br>
* The current supported types are booleans, integers, doubles, strings, and
* {@link ConfigItemGroup}
*
* @param <T>
* @author Eli Orona
*/
public class ArrayConfigItem<T> extends ConfigItem<T[]> {

/**
* Creates a new config with the name, defaultValue, and details
*
* @param name The name for the config item
* @param defaultValue The default value in case of a corrupted/missing config
* @param details A translatable string for readability in multiple
* languages
*/
public ArrayConfigItem(String name, T[] defaultValue, String details) {
this(name, defaultValue, details, null);
}

/**
* Creates a new config with the name, defaultValue, details, and an onChange
* consumer
*
* @param name The name for the config item
* @param defaultValue The default value in case of a corrupted/missing config
* @param details A translatable string for readability in multiple
* languages
* @param onChange A {@link Consumer} that is run every time the config item
* is modified
*/
public ArrayConfigItem(String name, T[] defaultValue, String details, @Nullable Consumer<ConfigItem<T[]>> onChange) {
super(name, defaultValue, details, onChange);
if(defaultValue[0] instanceof ArrayConfigItem) {
throw new UnsupportedOperationException("ArrayConfigItems cannot be nested");
}
this.value = Arrays.copyOf(defaultValue, defaultValue.length);
}

/**
* Reads and sets the {@link ConfigItem} from a JSON Element. Will throw an
* error if the type does not match the type of the {@link ConfigItem}
*
* @param element The JSON Element
*/
@SuppressWarnings("unchecked")
public void fromJson(JsonElement element) {
for(int i = 0; i < element.getAsJsonArray().size(); i++) {
T newValue;
JsonElement arrayElement = element.getAsJsonArray().get(i);
switch (this.type) {
case BOOLEAN:
newValue = (T) (Object) arrayElement.getAsBoolean();
break;

case INTEGER:
newValue = (T) (Object) arrayElement.getAsInt();
break;

case DOUBLE:
newValue = (T) (Object) arrayElement.getAsDouble();
break;

case STRING:
newValue = (T) arrayElement.getAsString();
break;

case ENUM:
newValue = (T) Arrays.stream(((T) defaultValue[i]).getClass().getEnumConstants()).filter(val -> val.toString().equals(arrayElement.getAsString())).findFirst().get();
break;

case GROUP:
((ConfigItemGroup) defaultValue[i]).fromJson(arrayElement.getAsJsonObject());

default:
return;
}
value[i] = newValue;
}
if (value != null) {
setValue(value);
}
}

/**
* @param position The position in the array for the value
* @return The default value of the {@link ConfigItem}
*/
public T getDefaultValue(int position) {
return this.defaultValue[position];
}

/**
* @param position The position in the array for the value
* @return The current value of the {@link ConfigItem}
*/
public T getValue(int position) {
return this.value[position];
}

/**
* Sets the value in {@code position} the {@link ArrayConfigItem}
*
* @param value The value to set
* @param position The position in the array for the value
*/
public void setValue(T value, int position) {
this.value[position] = value;
}
}
5 changes: 4 additions & 1 deletion common/src/main/java/com/oroarmor/config/ConfigItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,11 @@ public static Type getTypeFrom(Object value) {
return ENUM;
}

if (value != null && value.getClass().isArray()) {
return getTypeFrom(((Object[]) value)[0]);
}

return null;
}

}
}
40 changes: 39 additions & 1 deletion common/src/main/java/com/oroarmor/config/ConfigItemGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Map.Entry;
import java.util.StringJoiner;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

Expand Down Expand Up @@ -111,6 +112,40 @@ private void parseConfig(ConfigItem<?> c, JsonObject object) {
}
}

/**
* Turns an array config into a json property
*
* @param c The array config item
* @param object the json object
*/
private void parseArrayConfig(ArrayConfigItem<?> c, JsonObject object) {
JsonArray array = new JsonArray();

for (int i = 0; i < c.getValue().length; i++) {
switch (c.getType()) {
case BOOLEAN:
array.add((Boolean) c.getValue(i));
break;
case DOUBLE:
case INTEGER:
array.add((Number) c.getValue(i));
break;
case STRING:
array.add((String) c.getValue(i));
break;
case ENUM:
array.add(c.getValue(i).toString());
break;
case GROUP:
array.add(((ConfigItemGroup) c.getValue(i)).toJson());
default:
break;
}
}

object.add(c.getName(), array);
}

/**
* Converts the config items into json
*
Expand All @@ -120,7 +155,10 @@ public JsonObject toJson() {
JsonObject object = new JsonObject();

for (ConfigItem<?> c : configs) {
parseConfig(c, object);
if (!(c instanceof ArrayConfigItem))
parseConfig(c, object);
else
parseArrayConfig((ArrayConfigItem<?>) c, object);
}

return object;
Expand Down
Loading

0 comments on commit 6472504

Please sign in to comment.