add int & sub options
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package com.kasetoatz.dumbassconfig.options;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.kasetoatz.dumbassconfig.ui.ConfigScreen;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.widget.ButtonWidget;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SubOption extends AbstractButtonOption<JsonObject>
|
||||
{
|
||||
private final ArrayList<AbstractOption<?>> options = new ArrayList<>();
|
||||
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
private ConfigScreen ui;
|
||||
|
||||
private SubOption(String text, String key)
|
||||
{
|
||||
super(text, key, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Text getButtonText()
|
||||
{
|
||||
return Text.literal("Open");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(ButtonWidget button)
|
||||
{
|
||||
this.ui.setParent(MinecraftClient.getInstance().currentScreen);
|
||||
MinecraftClient.getInstance().setScreen(this.ui);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject getValue()
|
||||
{
|
||||
JsonObject data = new JsonObject();
|
||||
for (AbstractOption<?> option : this.options)
|
||||
{
|
||||
data.add(option.getKey(), this.gson.toJsonTree(option.getValue()));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromJson(JsonElement element)
|
||||
{
|
||||
JsonObject data = element.getAsJsonObject();
|
||||
for (AbstractOption<?> option : this.options)
|
||||
{
|
||||
option.fromJson(data.get(option.getKey()));
|
||||
}
|
||||
}
|
||||
|
||||
public static Builder builder(String text, String key)
|
||||
{
|
||||
return new Builder(text, key);
|
||||
}
|
||||
|
||||
public static class Builder
|
||||
{
|
||||
private final SubOption option;
|
||||
|
||||
private Builder(String text, String key)
|
||||
{
|
||||
this.option = new SubOption(text, key);
|
||||
}
|
||||
|
||||
public Builder withOption(AbstractOption<?> option)
|
||||
{
|
||||
this.option.options.add(option);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SubOption build()
|
||||
{
|
||||
this.option.ui = new ConfigScreen(this.option.getText(), this.option.options);
|
||||
return this.option;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user