Move configs to Kirin

This commit is contained in:
Sollace 2019-07-11 09:41:16 +02:00
parent 26875f25cb
commit b5708a9820
6 changed files with 7 additions and 173 deletions

View file

@ -12,8 +12,8 @@ import com.minelittlepony.common.event.ClientReadyCallback;
import com.minelittlepony.common.event.ScreenInitCallback;
import com.minelittlepony.common.event.SkinFilterCallback;
import com.minelittlepony.common.util.GamePaths;
import com.minelittlepony.common.util.settings.JsonConfig;
import com.minelittlepony.pony.IPonyManager;
import com.minelittlepony.settings.JsonConfig;
import com.minelittlepony.settings.PonyConfig;
import net.fabricmc.api.ClientModInitializer;

View file

@ -3,8 +3,7 @@ package com.minelittlepony.client.render.entities;
import com.google.common.collect.Lists;
import com.minelittlepony.client.MineLittlePony;
import com.minelittlepony.client.PonyRenderManager;
import com.minelittlepony.settings.Config;
import com.minelittlepony.settings.Config.Setting;
import com.minelittlepony.common.util.settings.Config;
import java.util.List;
@ -14,7 +13,7 @@ import net.minecraft.entity.passive.*;
/**
* Central location where new entity renderers are registered and applied.
*/
public enum MobRenderers implements Setting<Boolean> {
public enum MobRenderers implements Config.Setting<Boolean> {
VILLAGERS {
@Override
void register(boolean state, PonyRenderManager pony) {
@ -80,7 +79,7 @@ public enum MobRenderers implements Setting<Boolean> {
@Override
public void set(Boolean value) {
Setting.super.set(value);
Config.Setting.super.set(value);
apply(PonyRenderManager.getInstance());
}

View file

@ -1,104 +0,0 @@
package com.minelittlepony.settings;
import com.minelittlepony.common.client.gui.IField.IChangeCallback;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
/**
* A configuration container that lets you programmatically index values by a key.
*/
public abstract class Config {
protected Map<String, Object> entries = new HashMap<>();
protected void initWith(Setting<?>... settings) {
for (Setting<?> s : settings) {
entries.putIfAbsent(s.name().toLowerCase(), s.getDefault());
}
}
public abstract void save();
/**
* Any value that can be stored in this config file.
*/
public class Value<T> implements Setting<T> {
private final T def;
private final String name;
public Value(String name, T def) {
this.name = name;
this.def = def;
entries.putIfAbsent(name().toLowerCase(), def);
}
@Override
public String name() {
return name;
}
@Nonnull
@Override
public T getDefault() {
return def;
}
@Override
public Config config() {
return Config.this;
}
@Override
public String toString() {
return name();
}
}
/**
* Any settings.
*/
public interface Setting<T> extends IChangeCallback<T> {
String name();
@Nonnull
T getDefault();
Config config();
/**
* Gets the config value associated with this entry.
*/
@Nonnull
@SuppressWarnings("unchecked")
default T get() {
T t = (T)config().entries.computeIfAbsent(name().toLowerCase(), k -> getDefault());
if (t == null) {
t = getDefault();
set(t);
}
return t;
}
/**
* Sets the config value associated with this entry.
*/
default void set(@Nullable T value) {
value = value == null ? getDefault() : value;
config().entries.put(name().toLowerCase(), value);
}
@Override
default T perform(T v) {
set(v);
return v;
}
}
}

View file

@ -1,62 +0,0 @@
package com.minelittlepony.settings;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.function.Supplier;
public class JsonConfig extends Config {
public static <T extends JsonConfig> T of(Path file, Supplier<T> creator) {
return creator.get().load(file);
}
static final Gson gson = new GsonBuilder()
.setPrettyPrinting()
.excludeFieldsWithoutExposeAnnotation()
.create();
private Path configFile;
@Override
public void save() {
try (JsonWriter writer = new JsonWriter(Files.newBufferedWriter(configFile))) {
writer.setIndent(" ");
gson.toJson(entries, HashMap.class, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
protected <T extends JsonConfig> T load(Path file) {
try {
if (Files.exists(file)) {
try (BufferedReader s = Files.newBufferedReader(file)) {
gson.fromJson(s, JsonObject.class).entrySet().forEach(entry -> {
String key = entry.getKey().toLowerCase();
if (entries.containsKey(key)) {
Object value = gson.getAdapter(entries.get(key).getClass()).fromJsonTree(entry.getValue());
entries.put(key, value);
}
});
} catch (IOException ignored) { }
}
configFile = file;
} finally {
save();
}
return (T)this;
}
}

View file

@ -2,6 +2,7 @@ package com.minelittlepony.settings;
import net.minecraft.util.math.MathHelper;
import com.minelittlepony.common.util.settings.JsonConfig;
import com.minelittlepony.pony.meta.Size;
/**

View file

@ -1,12 +1,12 @@
package com.minelittlepony.settings;
import com.minelittlepony.client.MineLittlePony;
import com.minelittlepony.settings.Config.Setting;
import com.minelittlepony.common.util.settings.Config;
/**
* Mod settings.
*/
public enum PonySettings implements Setting<Boolean> {
public enum PonySettings implements Config.Setting<Boolean> {
SIZES,
SNUZZLES,
FILLYCAM,