added builder

This commit is contained in:
2025-11-02 20:52:25 +01:00
parent f4f4337b39
commit f9c0411e59
9 changed files with 246 additions and 27 deletions

View File

@@ -1,6 +1,9 @@
package com.kasetoatz.fastghast;
import com.kasetoatz.fastghast.configscreen.ConfigScreen;
import com.kasetoatz.fastghast.configscreen.DumbassConfig;
import com.kasetoatz.fastghast.configscreen.options.BoolOption;
import com.kasetoatz.fastghast.configscreen.options.FloatOption;
import com.kasetoatz.fastghast.configscreen.options.Validator;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import net.fabricmc.api.ModInitializer;
@@ -30,6 +33,9 @@ public class Fastghast implements ModInitializer, ModMenuApi
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory()
{
return ConfigScreen::new;
return DumbassConfig.builder()
.withOption(new BoolOption("Test Boolean Option", "test_bool_opt", false))
.withOption(new FloatOption("Test Float Option", "test_float_opt", 1.F, value -> new Validator(value > 0.F, "Number must be binger than 0.")))
::build;
}
}

View File

@@ -8,11 +8,11 @@ import net.minecraft.client.gui.widget.ElementListWidget;
import java.util.List;
public class Option extends ElementListWidget.Entry<Option>
public class ConfigEntry extends ElementListWidget.Entry<ConfigEntry>
{
private final List<ClickableWidget> widgets;
public Option(ClickableWidget... widgets)
public ConfigEntry(ClickableWidget... widgets)
{
this.widgets = List.of(widgets);
}

View File

@@ -1,15 +1,15 @@
package com.kasetoatz.fastghast.configscreen;
import com.kasetoatz.fastghast.configscreen.options.Option;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.widget.*;
import net.minecraft.text.Text;
import java.util.List;
public class OptionList extends ElementListWidget<Option>
public class ConfigEntryList extends ElementListWidget<ConfigEntry>
{
public OptionList(MinecraftClient client, int width, int height, int y, int itemHeight)
public ConfigEntryList(MinecraftClient client, int width, int height, int y, int itemHeight)
{
super(client, width, height, y, itemHeight);
}
@@ -29,10 +29,10 @@ public class OptionList extends ElementListWidget<Option>
@Override
public void renderWidget(DrawContext context, int mouseX, int mouseY, float delta)
{
List<Option> options = this.children();
List<ConfigEntry> options = this.children();
for (int i = 0; i < options.size(); i++)
{
Option option = options.get(i);
ConfigEntry option = options.get(i);
if (option.getY() + option.getHeight() < this.getY())
{
continue;
@@ -46,11 +46,8 @@ public class OptionList extends ElementListWidget<Option>
super.renderWidget(context, mouseX, mouseY, delta);
}
public void add()
public void add(Option<?> option)
{
this.addEntry(new Option(
new TextWidget(20, 0, 200, 40, Text.of("TEST TEXT"), client.textRenderer),
ButtonWidget.builder(Text.of("TEST BUTTON"), button -> System.out.println("test button pressed")).dimensions(this.getWidth() - 100, 0, 80, 40).build())
);
this.addEntry(option.toEntry(this.client.textRenderer, this.getWidth()));
}
}

View File

@@ -1,43 +1,45 @@
package com.kasetoatz.fastghast.configscreen;
import com.kasetoatz.fastghast.configscreen.options.Option;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextWidget;
import net.minecraft.client.gui.widget.ThreePartsLayoutWidget;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.Text;
import java.util.ArrayList;
import java.util.List;
public class ConfigScreen extends Screen
{
private final Screen parent;
private final List<Option<?>> options;
private final ThreePartsLayoutWidget layout = new ThreePartsLayoutWidget(this);
private OptionList body;
private ConfigEntryList body;
public ConfigScreen(Screen parent)
public ConfigScreen(Screen parent, Text title, List<Option<?>> options)
{
super(Text.of("TEST SCREEN"));
super(title);
this.parent = parent;
this.options = options;
}
@Override
protected void init()
{
this.layout.addHeader(this.title, this.textRenderer);
this.body = new OptionList(this.client, this.layout.getWidth(), this.layout.getContentHeight(), this.layout.getHeaderHeight(), 50);
this.addItems();
this.body = new ConfigEntryList(this.client, this.layout.getWidth(), this.layout.getContentHeight(), this.layout.getHeaderHeight(), 50);
for (Option<?> option : this.options)
{
this.body.add(option);
}
this.layout.addBody(body);
this.layout.addFooter(ButtonWidget.builder(ScreenTexts.DONE, button -> this.close()).width(200).build());
this.layout.forEachChild(this::addDrawableChild);
this.refreshWidgetPositions();
}
private void addItems()
{
for (int i = 0; i < 20; i++)
{
this.body.add();
}
}
@Override
protected void refreshWidgetPositions()
{

View File

@@ -0,0 +1,35 @@
package com.kasetoatz.fastghast.configscreen;
import com.kasetoatz.fastghast.configscreen.options.Option;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import java.util.ArrayList;
public class DumbassConfig
{
public static Builder builder()
{
return new Builder();
}
public static class Builder
{
private final ArrayList<Option<?>> options = new ArrayList<>();
private Builder()
{
}
public Builder withOption(Option<?> option)
{
this.options.add(option);
return this;
}
public ConfigScreen build(Screen parent)
{
return new ConfigScreen(parent, Text.literal("TEST SCREEN"), this.options);
}
}
}

View File

@@ -0,0 +1,40 @@
package com.kasetoatz.fastghast.configscreen.options;
import com.kasetoatz.fastghast.configscreen.ConfigEntry;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextWidget;
import net.minecraft.text.Text;
public class BoolOption extends Option<Boolean>
{
public BoolOption(String text, String key, boolean defaultValue)
{
super(text, key, defaultValue);
}
private Text getButtonText()
{
return this.getValue() ? Text.literal("True").withColor(0x00FF00) : Text.literal("False").withColor(0xFF0000);
}
private void onClick(ButtonWidget button)
{
this.onChange(!this.getValue());
button.setMessage(this.getButtonText());
}
@Override
public ConfigEntry toEntry(TextRenderer textRenderer, int maxWidth)
{
return new ConfigEntry(
new TextWidget(20, 0, 200, 40, this.getText(), textRenderer),
ButtonWidget.builder(getButtonText(), this::onClick).dimensions(maxWidth - 100, 0, 80, 40).build()
);
}
@Override
public void onError(String message)
{
}
}

View File

@@ -0,0 +1,52 @@
package com.kasetoatz.fastghast.configscreen.options;
import com.kasetoatz.fastghast.configscreen.ConfigEntry;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.gui.widget.TextWidget;
import net.minecraft.text.Text;
import java.util.function.Function;
public class FloatOption extends Option<Float>
{
public FloatOption(String text, String key, Float defaultValue, Function<Float, Validator> validator)
{
super(text, key, defaultValue, validator);
}
private boolean predicate(String text)
{
if (text.isEmpty() || text.equals(".") || text.equals("-"))
{
return true;
}
try
{
Float.parseFloat(text);
return true;
}
catch (NumberFormatException exc)
{
return false;
}
}
@Override
public ConfigEntry toEntry(TextRenderer textRenderer, int maxWidth)
{
TextFieldWidget textField = new TextFieldWidget(textRenderer, maxWidth - 100, 0, 80, 40, this.getText());
textField.setText(this.getValue().toString());
textField.setTextPredicate(this::predicate);
return new ConfigEntry(
new TextWidget(20, 0, 200, 40, this.getText(), textRenderer),
textField
);
}
@Override
public void onError(String message)
{
}
}

View File

@@ -0,0 +1,64 @@
package com.kasetoatz.fastghast.configscreen.options;
import com.kasetoatz.fastghast.configscreen.ConfigEntry;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.text.Text;
import java.util.function.Function;
public abstract class Option<T>
{
private final Text text;
private final String key;
private final T defaultValue;
private final Function<T, Validator> validator;
private T value;
public Option(String text, String key, T defaultValue, Function<T, Validator> validator)
{
this.text = Text.of(text);
this.key = key;
this.defaultValue = defaultValue;
this.validator = validator;
this.value = defaultValue;
}
public Option(String text, String key, T defaultValue)
{
this(text, key, defaultValue, value -> new Validator(true, ""));
}
public Text getText()
{
return this.text;
}
public String getKey()
{
return this.key;
}
public T getDefaultValue()
{
return this.defaultValue;
}
public T getValue()
{
return this.value;
}
public void onChange(T value)
{
Validator validator = this.validator.apply(value);
if (!validator.isValid())
{
onError(validator.getErrorMessage());
return;
}
this.value = value;
}
public abstract ConfigEntry toEntry(TextRenderer textRenderer, int maxWidth);
public abstract void onError(String message);
}

View File

@@ -0,0 +1,23 @@
package com.kasetoatz.fastghast.configscreen.options;
public class Validator
{
private final boolean valid;
private final String errorMessage;
public Validator(boolean condition, String errorMessage)
{
this.valid = condition;
this.errorMessage = errorMessage;
}
public boolean isValid()
{
return this.valid;
}
public String getErrorMessage()
{
return this.errorMessage;
}
}