Release version
This commit is contained in:
133
src/main/java/com/kasetoatz/dacl/DumbassConfig.java
Normal file
133
src/main/java/com/kasetoatz/dacl/DumbassConfig.java
Normal file
@@ -0,0 +1,133 @@
|
||||
package com.kasetoatz.dacl;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.kasetoatz.dacl.options.AbstractInputOption;
|
||||
import com.kasetoatz.dacl.options.AbstractOption;
|
||||
import com.kasetoatz.dacl.ui.ConfigScreen;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.crash.CrashException;
|
||||
import net.minecraft.util.crash.CrashReport;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class DumbassConfig
|
||||
{
|
||||
public static int TEXT_LEFT = 20;
|
||||
public static int TEXT_WIDTH = 200;
|
||||
public static int TEXT_HEIGHT = 40;
|
||||
public static int OPTION_RIGHT = 100;
|
||||
public static int OPTION_WIDTH = 80;
|
||||
public static int OPTION_HEIGHT = 40;
|
||||
public static int ENTRY_HEIGHT = 50;
|
||||
public static int DONE_BUTTON_WIDTH = 200;
|
||||
public static int BOOLEAN_TRUE_COLOR = 0x00FF00;
|
||||
public static int BOOLEAN_FALSE_COLOR = 0xFF0000;
|
||||
public static int VALID_INPUT_COLOR = 0xFFFFFFFF;
|
||||
public static int INVALID_INPUT_COLOR = 0xFFFF0000;
|
||||
public static int BACKGROUND_COLOR_1 = 0x50555555;
|
||||
public static int BACKGROUND_COLOR_2 = 0x50333333;
|
||||
|
||||
private final ArrayList<AbstractOption<?>> options = new ArrayList<>();
|
||||
private ConfigScreen ui;
|
||||
private final Path file;
|
||||
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
private DumbassConfig(String filename)
|
||||
{
|
||||
this.file = FabricLoader.getInstance().getConfigDir().resolve(filename.endsWith(".json") ? filename : filename + ".json");
|
||||
}
|
||||
|
||||
public Screen getUI(Screen parent)
|
||||
{
|
||||
this.ui.setParent(parent);
|
||||
return this.ui;
|
||||
}
|
||||
|
||||
private void load()
|
||||
{
|
||||
if (!Files.exists(this.file))
|
||||
{
|
||||
this.save();
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
String json = Files.readString(this.file);
|
||||
JsonObject data = this.gson.fromJson(json, JsonObject.class);
|
||||
for (AbstractOption<?> option : this.options)
|
||||
{
|
||||
if (data.has(option.getKey()))
|
||||
{
|
||||
JsonElement value = data.get(option.getKey());
|
||||
if (option instanceof AbstractInputOption<?> inputOption && !inputOption.isValid(value))
|
||||
{
|
||||
option.reset();
|
||||
continue;
|
||||
}
|
||||
option.fromJson(data.get(option.getKey()));
|
||||
}
|
||||
}
|
||||
this.save();
|
||||
}
|
||||
catch (IOException exc)
|
||||
{
|
||||
throw new CrashException(CrashReport.create(exc, "Loading config file."));
|
||||
}
|
||||
}
|
||||
|
||||
public void save()
|
||||
{
|
||||
try
|
||||
{
|
||||
JsonObject data = new JsonObject();
|
||||
for (AbstractOption<?> option : this.options)
|
||||
{
|
||||
data.add(option.getKey(), this.gson.toJsonTree(option.getValue()));
|
||||
}
|
||||
Files.writeString(this.file, this.gson.toJson(data));
|
||||
}
|
||||
catch (IOException exc)
|
||||
{
|
||||
throw new CrashException(CrashReport.create(exc, "Saving config file."));
|
||||
}
|
||||
}
|
||||
|
||||
public static Builder builder(Text uiTitle, String filename)
|
||||
{
|
||||
return new Builder(uiTitle, filename);
|
||||
}
|
||||
|
||||
public static class Builder
|
||||
{
|
||||
private final DumbassConfig config;
|
||||
private final Text uiTitle;
|
||||
|
||||
private Builder(Text uiTitle, String filename)
|
||||
{
|
||||
this.config = new DumbassConfig(filename);
|
||||
this.uiTitle = uiTitle;
|
||||
}
|
||||
|
||||
public Builder withOption(AbstractOption<?> option)
|
||||
{
|
||||
this.config.options.add(option);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DumbassConfig build()
|
||||
{
|
||||
this.config.load();
|
||||
this.config.ui = new ConfigScreen(uiTitle, this.config.options);
|
||||
this.config.ui.setSaveCallback(this.config::save);
|
||||
return this.config;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user