package net.inyourwalls.customlevelcolour; import net.minecraft.client.gui.components.Button; import net.minecraft.client.gui.components.EditBox; import net.minecraft.client.gui.layouts.CommonLayouts; import net.minecraft.client.gui.layouts.HeaderAndFooterLayout; import net.minecraft.client.gui.layouts.LinearLayout; import net.minecraft.client.gui.screens.Screen; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.TextColor; import net.minecraft.util.ARGB; import java.util.regex.Pattern; public class ConfigScreen extends Screen { private static final Component TITLE = Component.translatable("gui.customlevelcolour.title"); private static final Component OUTLINE_COLOUR_LABEL = Component.translatable("gui.customlevelcolour.outlineColour"); private static final Component TEXT_COLOUR_LABEL = Component.translatable("gui.customlevelcolour.textColour"); private static final Component BACK_BUTTON_LABEL = Component.translatable("gui.back"); private static final Component SAVE_BUTTON_LABEL = Component.translatable("gui.customlevelcolour.saveConfig"); private final Screen previous; private final CustomLevelColourConfig config; private int previewOutlineColour; private int previewTextColour; public ConfigScreen() { this(null); } public ConfigScreen(Screen previous) { super(TITLE); this.previous = previous; this.config = CustomLevelColour.getConfig(); } @Override public void init() { this.previewOutlineColour = this.config.outlineColour; this.previewTextColour = this.config.textColour; Button saveButton = Button.builder(SAVE_BUTTON_LABEL, btn -> { this.config.textColour = this.previewTextColour; this.config.outlineColour = this.previewOutlineColour; CustomLevelColour.saveConfig(); }).build(); Button backButton = Button.builder(BACK_BUTTON_LABEL, btn -> { this.onClose(); }).build(); EditBox textColourInput = new EditBox(this.font, TEXT_COLOUR_LABEL); textColourInput.setValue(String.format("%x", this.previewTextColour)); textColourInput.setResponder(v -> { try { this.previewTextColour = Integer.parseInt(v, 16); textColourInput.setTextColor(ARGB.white(1.0f)); if (!v.matches("[0-9a-fA-F]{6}")) { textColourInput.setTextColor(ARGB.opaque(TextColor.YELLOW.getValue())); } } catch (NumberFormatException ex) { textColourInput.setTextColor(ARGB.opaque(TextColor.RED.getValue())); } }); EditBox outlineColourInput = new EditBox(this.font, OUTLINE_COLOUR_LABEL); outlineColourInput.setValue(String.format("%x", this.previewOutlineColour)); outlineColourInput.setResponder(v -> { try { this.previewOutlineColour = Integer.parseInt(v, 16); textColourInput.setTextColor(ARGB.white(1.0f)); if (!v.matches("[0-9a-fA-F]{6}")) { outlineColourInput.setTextColor(ARGB.opaque(TextColor.YELLOW.getValue())); } } catch (NumberFormatException ex) { outlineColourInput.setTextColor(ARGB.opaque(TextColor.RED.getValue())); } }); HeaderAndFooterLayout root = new HeaderAndFooterLayout(this); root.addTitleHeader(TITLE, this.font); LinearLayout contents = LinearLayout.vertical().spacing(4); contents.addChild(CommonLayouts.labeledElement(this.font, textColourInput, TEXT_COLOUR_LABEL)); contents.addChild(CommonLayouts.labeledElement(this.font, outlineColourInput, OUTLINE_COLOUR_LABEL)); root.addToContents(contents); LinearLayout footer = LinearLayout.horizontal().spacing(4); footer.addChild(backButton); footer.addChild(saveButton); root.addToFooter(footer); root.arrangeElements(); root.visitWidgets(elem -> this.addRenderableWidget(elem)); } @Override public void onClose() { this.minecraft.setScreenAndShow(this.previous); } }