Skip to content

Commit e32d57e

Browse files
committed
Add Types
1 parent 613794d commit e32d57e

20 files changed

+347
-318
lines changed

src/main/java/org/visuals/legacy/lightconfig/lib/v1/Config.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
import org.slf4j.Logger;
3333
import org.slf4j.LoggerFactory;
3434
import org.visuals.legacy.lightconfig.lib.v1.field.*;
35-
import org.visuals.legacy.lightconfig.lib.v1.serializer.ConfigSerializer;
36-
import org.visuals.legacy.lightconfig.lib.v1.serializer.JsonSerializer;
35+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigDeserializer;
36+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigSerializer;
37+
import org.visuals.legacy.lightconfig.lib.v1.serialization.Json;
38+
import org.visuals.legacy.lightconfig.lib.v1.type.Type;
3739

3840
import java.nio.file.Files;
3941
import java.nio.file.Path;
@@ -46,18 +48,20 @@ public abstract class Config {
4648
protected final ModContainer modContainer;
4749
protected final Path path;
4850
protected final ConfigSerializer<?> serializer;
51+
protected final ConfigDeserializer<?> deserializer;
4952

50-
public Config(ModContainer modContainer, Path path, ConfigSerializer<?> serializer) {
53+
public Config(ModContainer modContainer, Path path, ConfigSerializer<?> serializer, ConfigDeserializer<?> deserializer) {
5154
this.modContainer = modContainer;
5255
this.path = path;
5356
this.serializer = serializer;
54-
if (!(this.serializer instanceof JsonSerializer)) {
55-
throw new RuntimeException("Only json serialization is currently supported! Please use JsonSerializer!");
57+
this.deserializer = deserializer;
58+
if (this.serializer != Json.SERIALIZER || this.deserializer != Json.DESERIALIZER) {
59+
throw new RuntimeException("Only json serialization is currently supported! Please use Json.SERIALIZER/Json.DESERIALIZER!");
5660
}
5761
}
5862

5963
public Config(ModContainer modContainer, Path path) {
60-
this(modContainer, path, null);
64+
this(modContainer, path, Json.SERIALIZER, Json.DESERIALIZER);
6165
}
6266

6367
public BooleanConfigField booleanFieldOf(final String name, final boolean defaultValue) {
@@ -72,8 +76,8 @@ public StringConfigField stringFieldOf(final String name, final String defaultVa
7276
return field;
7377
}
7478

75-
public <T extends Number> NumericConfigField<T> numericFieldOf(final String name, final T defaultValue) {
76-
final NumericConfigField<T> field = new NumericConfigField<>(this, name, defaultValue);
79+
public <T extends Number> NumericConfigField<T> numericFieldOf(final String name, final Type<T> type, final T defaultValue) {
80+
final NumericConfigField<T> field = new NumericConfigField<>(this, type, name, defaultValue);
7781
this.configFields.add(field);
7882
return field;
7983
}
@@ -94,14 +98,18 @@ public void load() {
9498
boolean success = true;
9599
try {
96100
final String json = Files.readString(this.path);
97-
final JsonObject object = ((JsonElement) this.serializer.deserialize(json)).getAsJsonObject();
101+
final JsonObject object = ((JsonElement) this.deserializer.deserialize(json)).getAsJsonObject();
98102
if (object == null) {
99103
this.logger.warn("Failed to load config! Defaulting to original settings.");
100104
success = false;
101105
} else {
102106
this.configFields.forEach(field -> {
103107
try {
104-
field.load(object);
108+
if (!object.has(field.getName())) {
109+
throw new Exception("Failed to load value for '" + field.getName() + "', object didn't contain a value for it.");
110+
} else {
111+
field.load(object);
112+
}
105113
} catch (Exception exception) {
106114
this.logger.warn(exception.getMessage());
107115
}
@@ -128,7 +136,7 @@ public void save() {
128136
});
129137

130138
try {
131-
Files.write(this.path, ((JsonSerializer) this.serializer).serialize(object));
139+
Files.write(this.path, ((ConfigSerializer<JsonElement>) this.serializer).serialize(object));
132140
} catch (Exception ignored) {
133141
this.logger.warn("Failed to save config!");
134142
return;

src/main/java/org/visuals/legacy/lightconfig/lib/v1/ConfigTranslate.java renamed to src/main/java/org/visuals/legacy/lightconfig/lib/v1/Translations.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828

2929
import java.util.function.BiFunction;
3030

31-
public final class ConfigTranslate {
31+
public final class Translations {
3232
public static final Component RESET = Component.translatable("options.reset");
3333
public static final BiFunction<Component, Component, Component> TEMPLATE = (a, b) -> Component.translatable("options.template", a, b);
3434
public static final Component ON = Component.translatable("options.on");
3535
public static final Component OFF = Component.translatable("options.off");
3636

37-
private ConfigTranslate() {
37+
private Translations() {
3838
}
3939

4040
public static Component tooltip(final String translate) {

src/main/java/org/visuals/legacy/lightconfig/lib/v1/field/AbstractConfigField.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,21 @@
3131
public abstract class AbstractConfigField<T> {
3232
protected final Config config;
3333
protected final String name;
34+
protected T value;
3435
protected final T defaultValue;
3536

3637
public AbstractConfigField(final Config config, final String name, final T defaultValue) {
3738
this.config = config;
3839
this.name = name;
40+
this.value = defaultValue;
3941
this.defaultValue = defaultValue;
4042
}
4143

44+
// TODO/Swap w/ serializer/deserializer
4245
public abstract void load(JsonObject object) throws Exception;
4346

4447
public abstract void save(JsonObject object) throws Exception;
4548

46-
public abstract void restore();
47-
4849
public abstract AbstractWidget createWidget();
4950

5051
public String getTranslationKey() {
@@ -55,7 +56,19 @@ public String getName() {
5556
return name;
5657
}
5758

59+
public T getValue() {
60+
return value;
61+
}
62+
5863
public T getDefaultValue() {
5964
return this.defaultValue;
6065
}
66+
67+
public void setValue(T value) {
68+
this.value = value;
69+
}
70+
71+
public void restore() {
72+
this.setValue(this.getDefaultValue());
73+
}
6174
}

src/main/java/org/visuals/legacy/lightconfig/lib/v1/field/BooleanConfigField.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,54 +24,50 @@
2424

2525
package org.visuals.legacy.lightconfig.lib.v1.field;
2626

27-
import com.google.gson.JsonElement;
2827
import com.google.gson.JsonObject;
29-
import com.google.gson.JsonPrimitive;
3028
import net.minecraft.client.gui.components.Button;
3129
import net.minecraft.client.gui.components.Tooltip;
3230
import net.minecraft.network.chat.Component;
33-
import org.visuals.legacy.lightconfig.lib.v1.ConfigTranslate;
3431
import org.visuals.legacy.lightconfig.lib.v1.Config;
32+
import org.visuals.legacy.lightconfig.lib.v1.Translations;
33+
import org.visuals.legacy.lightconfig.lib.v1.type.Types;
3534

36-
public class BooleanConfigField extends GenericConfigField<Boolean> {
35+
import java.util.function.Function;
36+
37+
public class BooleanConfigField extends AbstractConfigField<Boolean> {
3738
public BooleanConfigField(final Config config, final String name, final boolean defaultValue) {
3839
super(config, name, defaultValue);
3940
}
4041

4142
@Override
4243
public void load(JsonObject object) throws Exception {
43-
if (!object.has(this.name)) {
44-
throw new Exception("Failed to load value for '" + this.name + "', object didn't contain a value for it.");
44+
final Boolean value = Types.BOOLEAN_TYPE.read(object, this.name);
45+
if (value == null) {
46+
throw new Exception("Failed to load value for '" + this.name + "'");
4547
} else {
46-
final JsonElement element = object.get(this.name);
47-
if (!element.isJsonPrimitive() || (element instanceof final JsonPrimitive primitive && !primitive.isBoolean())) {
48-
throw new Exception("Failed to load value for '" + this.name + "', type does not match.");
49-
} else {
50-
this.setValue(element.getAsBoolean());
51-
}
48+
this.setValue(value);
5249
}
5350
}
5451

5552
@Override
5653
public void save(JsonObject object) {
57-
object.addProperty(this.name, this.value);
54+
Types.BOOLEAN_TYPE.write(object, this.name, this.value);
5855
}
5956

6057
@Override
6158
public Button createWidget() {
62-
return this.createWidget(() -> {
63-
});
59+
return this.createWidget(Function::identity);
6460
}
6561

6662
public Button createWidget(final Runnable onClick) {
6763
final String translationKey = this.getTranslationKey();
6864
final Component translate = Component.translatable(translationKey);
69-
return Button.builder(ConfigTranslate.TEMPLATE.apply(translate, ConfigTranslate.toggle(this.isEnabled())), (button) -> {
65+
return Button.builder(Translations.TEMPLATE.apply(translate, Translations.toggle(this.isEnabled())), (button) -> {
7066
this.toggle();
7167
onClick.run();
72-
button.setMessage(ConfigTranslate.TEMPLATE.apply(translate, ConfigTranslate.toggle(this.isEnabled())));
68+
button.setMessage(Translations.TEMPLATE.apply(translate, Translations.toggle(this.isEnabled())));
7369
})
74-
.tooltip(Tooltip.create(ConfigTranslate.tooltip(translationKey)))
70+
.tooltip(Tooltip.create(Translations.tooltip(translationKey)))
7571
.build();
7672
}
7773

src/main/java/org/visuals/legacy/lightconfig/lib/v1/field/EnumConfigField.java

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,57 @@
3131
import net.minecraft.client.gui.components.Button;
3232
import net.minecraft.client.gui.components.Tooltip;
3333
import net.minecraft.network.chat.Component;
34-
import org.visuals.legacy.lightconfig.lib.v1.ConfigTranslate;
34+
import org.jetbrains.annotations.Nullable;
3535
import org.visuals.legacy.lightconfig.lib.v1.Config;
36+
import org.visuals.legacy.lightconfig.lib.v1.Translations;
37+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigDeserializer;
38+
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigSerializer;
39+
import org.visuals.legacy.lightconfig.lib.v1.type.Type;
3640

3741
import java.util.Arrays;
3842
import java.util.Optional;
3943

40-
public class EnumConfigField<T extends Enum<T>> extends GenericConfigField<T> {
44+
public class EnumConfigField<T extends Enum<T>> extends AbstractConfigField<T> {
4145
private final Class<T> enumClazz;
4246

47+
private final Type<T> ENUM_TYPE = new Type<>() {
48+
@Override
49+
public @Nullable T read(JsonObject object, String name) {
50+
final JsonElement element = object.get(name);
51+
if (element == null || !element.isJsonPrimitive() || (element instanceof final JsonPrimitive primitive && !primitive.isString())) {
52+
return null;
53+
} else {
54+
return Arrays.stream(EnumConfigField.this.enumClazz.getEnumConstants())
55+
.filter(cons -> cons.name().equals(element.getAsString()))
56+
.findFirst()
57+
.orElse(null);
58+
}
59+
}
60+
61+
@Override
62+
public void write(JsonObject object, String name, T value) {
63+
object.addProperty(name, value.name());
64+
}
65+
};
66+
4367
public EnumConfigField(Config config, String name, T defaultValue, Class<T> clazz) {
4468
super(config, name, defaultValue);
4569
this.enumClazz = clazz;
4670
}
4771

4872
@Override
4973
public void load(JsonObject object) throws Exception {
50-
if (!object.has(this.name)) {
51-
throw new Exception("Failed to load value for '" + this.name + "', object didn't contain a value for it.");
74+
final T value = ENUM_TYPE.read(object, this.name);
75+
if (value == null) {
76+
throw new Exception("Failed to load value for '" + this.name + "'");
5277
} else {
53-
final JsonElement element = object.get(this.name);
54-
if (!element.isJsonPrimitive() || (element instanceof final JsonPrimitive primitive && !primitive.isString())) {
55-
throw new Exception("Failed to load value for '" + this.name + "', type does not match.");
56-
} else {
57-
Optional<T> opt = Arrays.stream(this.enumClazz.getEnumConstants())
58-
.filter(cons -> cons.name().equals(element.getAsString()))
59-
.findFirst();
60-
this.setValue(opt.orElseThrow(() -> new Exception("Failed to load value for '" + this.name + "', invalid enum value.")));
61-
}
78+
this.setValue(value);
6279
}
6380
}
6481

6582
@Override
6683
public void save(JsonObject object) {
67-
object.addProperty(this.name, this.value.name());
84+
ENUM_TYPE.write(object, this.name, this.value);
6885
}
6986

7087
@Override
@@ -77,11 +94,11 @@ public AbstractWidget createWidget() {
7794
this.setValue(next);
7895
button.setMessage(getDisplayText(translate, translationKey));
7996
})
80-
.tooltip(Tooltip.create(ConfigTranslate.tooltip(translationKey)))
97+
.tooltip(Tooltip.create(Translations.tooltip(translationKey)))
8198
.build();
8299
}
83100

84101
private Component getDisplayText(final Component translate, final String translationKey) {
85-
return ConfigTranslate.TEMPLATE.apply(translate, Component.translatable(translationKey + '.' + this.getValue().name()));
102+
return Translations.TEMPLATE.apply(translate, Component.translatable(translationKey + '.' + this.getValue().name()));
86103
}
87104
}

src/main/java/org/visuals/legacy/lightconfig/lib/v1/field/GenericConfigField.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)