diff --git a/src/main/java/ru/bclib/config/Config.java b/src/main/java/ru/bclib/config/Config.java index 1eecd70c..ba2c8afb 100644 --- a/src/main/java/ru/bclib/config/Config.java +++ b/src/main/java/ru/bclib/config/Config.java @@ -11,11 +11,13 @@ import ru.bclib.config.ConfigKeeper.Entry; import ru.bclib.config.ConfigKeeper.FloatEntry; import ru.bclib.config.ConfigKeeper.IntegerEntry; import ru.bclib.config.ConfigKeeper.RangeEntry; +import ru.bclib.config.ConfigKeeper.StringArrayEntry; import ru.bclib.config.ConfigKeeper.StringEntry; -import ru.bclib.config.NamedPathConfig.ConfigToken; import java.io.File; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; public abstract class Config { @@ -210,4 +212,31 @@ public abstract class Config { } return false; } + + protected List getStringArray(ConfigKey key, List defaultValue) { + List str = keeper.getValue(key, StringArrayEntry.class); + if (str == null) { + StringArrayEntry entry = keeper.registerEntry(key, new StringArrayEntry(defaultValue)); + return entry.getValue(); + } + return str != null ? str : defaultValue; + } + + protected List getStringArray(ConfigKey key) { + List str = keeper.getValue(key, StringArrayEntry.class); + return str != null ? str : new ArrayList<>(0); + } + + protected boolean setStringArray(ConfigKey key, List value) { + try { + StringArrayEntry entry = keeper.getEntry(key, StringArrayEntry.class); + if (entry == null) return false; + entry.setValue(value); + return true; + } + catch (NullPointerException ex) { + BCLib.LOGGER.catching(ex); + } + return false; + } } diff --git a/src/main/java/ru/bclib/config/ConfigKeeper.java b/src/main/java/ru/bclib/config/ConfigKeeper.java index 57e5406d..f3e6263e 100644 --- a/src/main/java/ru/bclib/config/ConfigKeeper.java +++ b/src/main/java/ru/bclib/config/ConfigKeeper.java @@ -2,6 +2,7 @@ package ru.bclib.config; import com.google.common.collect.Maps; import com.google.common.reflect.TypeToken; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import net.minecraft.util.GsonHelper; @@ -14,6 +15,8 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.OutputStream; import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.function.Supplier; @@ -293,7 +296,51 @@ public final class ConfigKeeper { public void toJson(String value) { this.location.addProperty(key, value); } + } + + public static abstract class ArrayEntry extends Entry> { + public ArrayEntry(List defaultValue) { + super(defaultValue); + } + protected abstract T getValue(JsonElement element); + protected abstract void add(JsonArray array, T element); + + private JsonArray toArray(List input){ + final JsonArray array = new JsonArray(); + input.forEach(s -> add(array, s)); + return array; + } + + @Override + public List fromJson() { + final JsonArray resArray = GsonHelper.getAsJsonArray(location, key, toArray(defaultValue)); + final List res = new ArrayList<>(resArray.size()); + resArray.forEach(e -> res.add(getValue(e))); + + return res; + } + + @Override + public void toJson(List value) { + this.location.add(key, toArray(value)); + } + } + + public static class StringArrayEntry extends ArrayEntry { + + public StringArrayEntry(List defaultValue) { + super(defaultValue); + } + + @Override + protected String getValue(JsonElement el){ + return el.getAsString(); + } + + protected void add(JsonArray array, String el){ + array.add(el); + } } public static class EnumEntry> extends Entry {