From d163b9128d760e53e34fd6c08dbf782fa3d50c51 Mon Sep 17 00:00:00 2001 From: isXander Date: Sun, 27 Nov 2022 18:17:36 +0000 Subject: split sourcesets --- src/main/java/dev/isxander/yacl/api/Binding.java | 64 ---- .../java/dev/isxander/yacl/api/ButtonOption.java | 123 -------- .../java/dev/isxander/yacl/api/ConfigCategory.java | 158 ---------- .../java/dev/isxander/yacl/api/Controller.java | 28 -- .../java/dev/isxander/yacl/api/NameableEnum.java | 10 - src/main/java/dev/isxander/yacl/api/Option.java | 336 --------------------- .../java/dev/isxander/yacl/api/OptionFlag.java | 27 -- .../java/dev/isxander/yacl/api/OptionGroup.java | 141 --------- .../dev/isxander/yacl/api/PlaceholderCategory.java | 94 ------ .../dev/isxander/yacl/api/YetAnotherConfigLib.java | 136 --------- .../dev/isxander/yacl/api/utils/Dimension.java | 33 -- .../isxander/yacl/api/utils/MutableDimension.java | 11 - .../dev/isxander/yacl/api/utils/OptionUtils.java | 37 --- 13 files changed, 1198 deletions(-) delete mode 100644 src/main/java/dev/isxander/yacl/api/Binding.java delete mode 100644 src/main/java/dev/isxander/yacl/api/ButtonOption.java delete mode 100644 src/main/java/dev/isxander/yacl/api/ConfigCategory.java delete mode 100644 src/main/java/dev/isxander/yacl/api/Controller.java delete mode 100644 src/main/java/dev/isxander/yacl/api/NameableEnum.java delete mode 100644 src/main/java/dev/isxander/yacl/api/Option.java delete mode 100644 src/main/java/dev/isxander/yacl/api/OptionFlag.java delete mode 100644 src/main/java/dev/isxander/yacl/api/OptionGroup.java delete mode 100644 src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java delete mode 100644 src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java delete mode 100644 src/main/java/dev/isxander/yacl/api/utils/Dimension.java delete mode 100644 src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java delete mode 100644 src/main/java/dev/isxander/yacl/api/utils/OptionUtils.java (limited to 'src/main/java/dev/isxander/yacl/api') diff --git a/src/main/java/dev/isxander/yacl/api/Binding.java b/src/main/java/dev/isxander/yacl/api/Binding.java deleted file mode 100644 index 395beb2..0000000 --- a/src/main/java/dev/isxander/yacl/api/Binding.java +++ /dev/null @@ -1,64 +0,0 @@ -package dev.isxander.yacl.api; - -import dev.isxander.yacl.impl.GenericBindingImpl; -import dev.isxander.yacl.mixin.SimpleOptionAccessor; -import net.minecraft.client.option.SimpleOption; -import org.apache.commons.lang3.Validate; - -import java.util.function.Consumer; -import java.util.function.Supplier; - -/** - * Controls modifying the bound option. - * Provides the default value, a setter and a getter. - */ -public interface Binding { - void setValue(T value); - - T getValue(); - - T defaultValue(); - - /** - * Creates a generic binding. - * - * @param def default value of the option, used to reset - * @param getter should return the current value of the option - * @param setter should set the option to the supplied value - */ - static Binding generic(T def, Supplier getter, Consumer setter) { - Validate.notNull(def, "`def` must not be null"); - Validate.notNull(getter, "`getter` must not be null"); - Validate.notNull(setter, "`setter` must not be null"); - - return new GenericBindingImpl<>(def, getter, setter); - } - - /** - * Creates a {@link Binding} for Minecraft's {@link SimpleOption} - */ - static Binding minecraft(SimpleOption minecraftOption) { - Validate.notNull(minecraftOption, "`minecraftOption` must not be null"); - - return new GenericBindingImpl<>( - ((SimpleOptionAccessor) (Object) minecraftOption).getDefaultValue(), - minecraftOption::getValue, - minecraftOption::setValue - ); - } - - /** - * Creates an immutable binding that has no default and cannot be modified. - * - * @param value the value for the binding - */ - static Binding immutable(T value) { - Validate.notNull(value, "`value` must not be null"); - - return new GenericBindingImpl<>( - value, - () -> value, - changed -> {} - ); - } -} diff --git a/src/main/java/dev/isxander/yacl/api/ButtonOption.java b/src/main/java/dev/isxander/yacl/api/ButtonOption.java deleted file mode 100644 index 1124a9a..0000000 --- a/src/main/java/dev/isxander/yacl/api/ButtonOption.java +++ /dev/null @@ -1,123 +0,0 @@ -package dev.isxander.yacl.api; - -import dev.isxander.yacl.gui.YACLScreen; -import dev.isxander.yacl.impl.ButtonOptionImpl; -import net.minecraft.text.MutableText; -import net.minecraft.text.Text; -import org.apache.commons.lang3.Validate; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.List; -import java.util.function.BiConsumer; -import java.util.function.Consumer; -import java.util.function.Function; - -public interface ButtonOption extends Option> { - /** - * Action to be executed upon button press - */ - BiConsumer action(); - - static Builder createBuilder() { - return new Builder(); - } - - class Builder { - private Text name; - private final List tooltipLines = new ArrayList<>(); - private boolean available = true; - private Function>> controlGetter; - private BiConsumer action; - - private Builder() { - - } - - /** - * Sets the name to be used by the option. - * - * @see Option#name() - */ - public Builder name(@NotNull Text name) { - Validate.notNull(name, "`name` cannot be null"); - - this.name = name; - return this; - } - - /** - * Sets the tooltip to be used by the option. - * Can be invoked twice to append more lines. - * No need to wrap the text yourself, the gui does this itself. - * - * @param tooltips text lines - merged with a new-line on {@link Option.Builder#build()}. - */ - public Builder tooltip(@NotNull Text... tooltips) { - Validate.notNull(tooltips, "`tooltips` cannot be empty"); - - tooltipLines.addAll(List.of(tooltips)); - return this; - } - - public Builder action(@NotNull BiConsumer action) { - Validate.notNull(action, "`action` cannot be null"); - - this.action = action; - return this; - } - - /** - * Action to be executed upon button press - * - * @see ButtonOption#action() - */ - @Deprecated - public Builder action(@NotNull Consumer action) { - Validate.notNull(action, "`action` cannot be null"); - - this.action = (screen, button) -> action.accept(screen); - return this; - } - - /** - * Sets if the option can be configured - * - * @see Option#available() - */ - public Builder available(boolean available) { - this.available = available; - return this; - } - - /** - * Sets the controller for the option. - * This is how you interact and change the options. - * - * @see dev.isxander.yacl.gui.controllers - */ - public Builder controller(@NotNull Function>> control) { - Validate.notNull(control, "`control` cannot be null"); - - this.controlGetter = control; - return this; - } - - public ButtonOption build() { - Validate.notNull(name, "`name` must not be null when building `Option`"); - Validate.notNull(controlGetter, "`control` must not be null when building `Option`"); - Validate.notNull(action, "`action` must not be null when building `Option`"); - - MutableText concatenatedTooltip = Text.empty(); - boolean first = true; - for (Text line : tooltipLines) { - if (!first) concatenatedTooltip.append("\n"); - first = false; - - concatenatedTooltip.append(line); - } - - return new ButtonOptionImpl(name, concatenatedTooltip, action, available, controlGetter); - } - } -} diff --git a/src/main/java/dev/isxander/yacl/api/ConfigCategory.java b/src/main/java/dev/isxander/yacl/api/ConfigCategory.java deleted file mode 100644 index 27c3e95..0000000 --- a/src/main/java/dev/isxander/yacl/api/ConfigCategory.java +++ /dev/null @@ -1,158 +0,0 @@ -package dev.isxander.yacl.api; - -import com.google.common.collect.ImmutableList; -import dev.isxander.yacl.impl.ConfigCategoryImpl; -import dev.isxander.yacl.impl.OptionGroupImpl; -import net.minecraft.text.MutableText; -import net.minecraft.text.Text; -import org.apache.commons.lang3.Validate; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.function.Function; - -/** - * Separates {@link Option}s or {@link OptionGroup}s into multiple distinct sections. - * Served to a user as a button in the left column, - * upon pressing, the options list is filled with options contained within this category. - */ -public interface ConfigCategory { - /** - * Name of category, displayed as a button on the left column. - */ - @NotNull Text name(); - - /** - * Gets every {@link OptionGroup} in this category. - */ - @NotNull ImmutableList groups(); - - /** - * Tooltip (or description) of the category. - * Rendered on hover. - */ - @NotNull Text tooltip(); - - /** - * Creates a builder to construct a {@link ConfigCategory} - */ - static Builder createBuilder() { - return new Builder(); - } - - class Builder { - private Text name; - - private final List> rootOptions = new ArrayList<>(); - private final List groups = new ArrayList<>(); - - private final List tooltipLines = new ArrayList<>(); - - private Builder() { - - } - - /** - * Sets name of the category - * - * @see ConfigCategory#name() - */ - public Builder name(@NotNull Text name) { - Validate.notNull(name, "`name` cannot be null"); - - this.name = name; - return this; - } - - /** - * Adds an option to the root group of the category. - * To add to another group, use {@link Builder#group(OptionGroup)}. - * To construct an option, use {@link Option#createBuilder(Class)} - * - * @see ConfigCategory#groups() - * @see OptionGroup#isRoot() - */ - public Builder option(@NotNull Option option) { - Validate.notNull(option, "`option` must not be null"); - - this.rootOptions.add(option); - return this; - } - - /** - * Adds multiple options to the root group of the category. - * To add to another group, use {@link Builder#groups(Collection)}. - * To construct an option, use {@link Option#createBuilder(Class)} - * - * @see ConfigCategory#groups() - * @see OptionGroup#isRoot() - */ - public Builder options(@NotNull Collection> options) { - Validate.notNull(options, "`options` must not be null"); - - this.rootOptions.addAll(options); - return this; - } - - /** - * Adds an option group. - * To add an option to the root group, use {@link Builder#option(Option)} - * To construct a group, use {@link OptionGroup#createBuilder()} - */ - public Builder group(@NotNull OptionGroup group) { - Validate.notNull(group, "`group` must not be null"); - - this.groups.add(group); - return this; - } - - /** - * Adds multiple option groups. - * To add multiple options to the root group, use {@link Builder#options(Collection)} - * To construct a group, use {@link OptionGroup#createBuilder()} - */ - public Builder groups(@NotNull Collection groups) { - Validate.notEmpty(groups, "`groups` must not be empty"); - - this.groups.addAll(groups); - return this; - } - - /** - * Sets the tooltip to be used by the category. - * Can be invoked twice to append more lines. - * No need to wrap the text yourself, the gui does this itself. - * - * @param tooltips text lines - merged with a new-line on {@link Builder#build()}. - */ - public Builder tooltip(@NotNull Text... tooltips) { - Validate.notEmpty(tooltips, "`tooltips` cannot be empty"); - - tooltipLines.addAll(List.of(tooltips)); - return this; - } - - public ConfigCategory build() { - Validate.notNull(name, "`name` must not be null to build `ConfigCategory`"); - - List combinedGroups = new ArrayList<>(); - combinedGroups.add(new OptionGroupImpl(Text.empty(), Text.empty(), ImmutableList.copyOf(rootOptions), false, true)); - combinedGroups.addAll(groups); - - Validate.notEmpty(combinedGroups, "at least one option must be added to build `ConfigCategory`"); - - MutableText concatenatedTooltip = Text.empty(); - boolean first = true; - for (Text line : tooltipLines) { - if (!first) concatenatedTooltip.append("\n"); - first = false; - - concatenatedTooltip.append(line); - } - - return new ConfigCategoryImpl(name, ImmutableList.copyOf(combinedGroups), concatenatedTooltip); - } - } -} diff --git a/src/main/java/dev/isxander/yacl/api/Controller.java b/src/main/java/dev/isxander/yacl/api/Controller.java deleted file mode 100644 index 7bf7e7f..0000000 --- a/src/main/java/dev/isxander/yacl/api/Controller.java +++ /dev/null @@ -1,28 +0,0 @@ -package dev.isxander.yacl.api; - -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.AbstractWidget; -import dev.isxander.yacl.gui.YACLScreen; -import net.minecraft.text.Text; - -/** - * Provides a widget to control the option. - */ -public interface Controller { - /** - * Gets the dedicated {@link Option} for this controller - */ - Option option(); - - /** - * Gets the formatted value based on {@link Option#pendingValue()} - */ - Text formatValue(); - - /** - * Provides a widget to display - * - * @param screen parent screen - */ - AbstractWidget provideWidget(YACLScreen screen, Dimension widgetDimension); -} diff --git a/src/main/java/dev/isxander/yacl/api/NameableEnum.java b/src/main/java/dev/isxander/yacl/api/NameableEnum.java deleted file mode 100644 index 793b230..0000000 --- a/src/main/java/dev/isxander/yacl/api/NameableEnum.java +++ /dev/null @@ -1,10 +0,0 @@ -package dev.isxander.yacl.api; - -import net.minecraft.text.Text; - -/** - * Used for the default value formatter of {@link dev.isxander.yacl.gui.controllers.cycling.EnumController} - */ -public interface NameableEnum { - Text getDisplayName(); -} diff --git a/src/main/java/dev/isxander/yacl/api/Option.java b/src/main/java/dev/isxander/yacl/api/Option.java deleted file mode 100644 index 772c816..0000000 --- a/src/main/java/dev/isxander/yacl/api/Option.java +++ /dev/null @@ -1,336 +0,0 @@ -package dev.isxander.yacl.api; - -import com.google.common.collect.ImmutableSet; -import dev.isxander.yacl.impl.OptionImpl; -import net.minecraft.text.MutableText; -import net.minecraft.text.Text; -import net.minecraft.util.Formatting; -import org.apache.commons.lang3.Validate; -import org.jetbrains.annotations.NotNull; - -import java.util.*; -import java.util.function.BiConsumer; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Stream; - -public interface Option { - /** - * Name of the option - */ - @NotNull Text name(); - - /** - * Tooltip (or description) of the option. - * Rendered on hover. - */ - @NotNull Text tooltip(); - - /** - * Widget provider for a type of option. - * - * @see dev.isxander.yacl.gui.controllers - */ - @NotNull Controller controller(); - - /** - * Binding for the option. - * Controls setting, getting and default value. - * - * @see Binding - */ - @NotNull Binding binding(); - - /** - * If the option can be configured - */ - boolean available(); - - /** - * Sets if the option can be configured after being built - * - * @see Option#available() - */ - void setAvailable(boolean available); - - /** - * Class of the option type. - * Used by some controllers. - */ - @NotNull Class typeClass(); - - /** - * Tasks that needs to be executed upon applying changes. - */ - @NotNull ImmutableSet flags(); - - /** - * Checks if the pending value is not equal to the current set value - */ - boolean changed(); - - /** - * If true, modifying this option recommends a restart. - */ - @Deprecated - boolean requiresRestart(); - - /** - * Value in the GUI, ready to set the actual bound value or be undone. - */ - @NotNull T pendingValue(); - - /** - * Sets the pending value - */ - void requestSet(T value); - - /** - * Applies the pending value to the bound value. - * Cannot be undone. - * - * @return if there were changes to apply {@link Option#changed()} - */ - boolean applyValue(); - - /** - * Sets the pending value to the bound value. - */ - void forgetPendingValue(); - - /** - * Sets the pending value to the default bound value. - */ - void requestSetDefault(); - - /** - * Checks if the current pending value is equal to its default value - */ - boolean isPendingValueDefault(); - - /** - * Adds a listener for when the pending value changes - */ - void addListener(BiConsumer, T> changedListener); - - /** - * Creates a builder to construct an {@link Option} - * - * @param type of the option's value - * @param typeClass used to capture the type - */ - static Builder createBuilder(Class typeClass) { - return new Builder<>(typeClass); - } - - class Builder { - private Text name = Text.literal("Name not specified!").formatted(Formatting.RED); - - private final List> tooltipGetters = new ArrayList<>(); - - private Function, Controller> controlGetter; - - private Binding binding; - - private boolean available = true; - - private boolean instant = false; - - private final Set flags = new HashSet<>(); - - private final Class typeClass; - - private final List, T>> listeners = new ArrayList<>(); - - private Builder(Class typeClass) { - this.typeClass = typeClass; - } - - /** - * Sets the name to be used by the option. - * - * @see Option#name() - */ - public Builder name(@NotNull Text name) { - Validate.notNull(name, "`name` cannot be null"); - - this.name = name; - return this; - } - - /** - * Sets the tooltip to be used by the option. - * No need to wrap the text yourself, the gui does this itself. - * - * @param tooltipGetter function to get tooltip depending on value {@link Builder#build()}. - */ - @SafeVarargs - public final Builder tooltip(@NotNull Function... tooltipGetter) { - Validate.notNull(tooltipGetter, "`tooltipGetter` cannot be null"); - - this.tooltipGetters.addAll(List.of(tooltipGetter)); - return this; - } - - /** - * Sets the tooltip to be used by the option. - * Can be invoked twice to append more lines. - * No need to wrap the text yourself, the gui does this itself. - * - * @param tooltips text lines - merged with a new-line on {@link Builder#build()}. - */ - public Builder tooltip(@NotNull Text... tooltips) { - Validate.notNull(tooltips, "`tooltips` cannot be empty"); - - this.tooltipGetters.addAll(Stream.of(tooltips).map(text -> (Function) t -> text).toList()); - return this; - } - - /** - * Sets the controller for the option. - * This is how you interact and change the options. - * - * @see dev.isxander.yacl.gui.controllers - */ - public Builder controller(@NotNull Function, Controller> control) { - Validate.notNull(control, "`control` cannot be null"); - - this.controlGetter = control; - return this; - } - - /** - * Sets the binding for the option. - * Used for default, getter and setter. - * - * @see Binding - */ - public Builder binding(@NotNull Binding binding) { - Validate.notNull(binding, "`binding` cannot be null"); - - this.binding = binding; - return this; - } - - /** - * Sets the binding for the option. - * Shorthand of {@link Binding#generic(Object, Supplier, Consumer)} - * - * @param def default value of the option, used to reset - * @param getter should return the current value of the option - * @param setter should set the option to the supplied value - * @see Binding - */ - public Builder binding(@NotNull T def, @NotNull Supplier<@NotNull T> getter, @NotNull Consumer<@NotNull T> setter) { - Validate.notNull(def, "`def` must not be null"); - Validate.notNull(getter, "`getter` must not be null"); - Validate.notNull(setter, "`setter` must not be null"); - - this.binding = Binding.generic(def, getter, setter); - return this; - } - - /** - * Sets if the option can be configured - * - * @see Option#available() - */ - public Builder available(boolean available) { - this.available = available; - return this; - } - - /** - * Adds a flag to the option. - * Upon applying changes, all flags are executed. - * {@link Option#flags()} - */ - public Builder flag(@NotNull OptionFlag... flag) { - Validate.notNull(flag, "`flag` must not be null"); - - this.flags.addAll(Arrays.asList(flag)); - return this; - } - - /** - * Adds a flag to the option. - * Upon applying changes, all flags are executed. - * {@link Option#flags()} - */ - public Builder flags(@NotNull Collection flags) { - Validate.notNull(flags, "`flags` must not be null"); - - this.flags.addAll(flags); - return this; - } - - /** - * Instantly invokes the binder's setter when modified in the GUI. - * Prevents the user from undoing the change - *

- * Does not support {@link Option#flags()}! - */ - public Builder instant(boolean instant) { - this.instant = instant; - return this; - } - - /** - * Adds a listener to the option. Invoked upon changing the pending value. - * - * @see Option#addListener(BiConsumer) - */ - public Builder listener(@NotNull BiConsumer, T> listener) { - this.listeners.add(listener); - return this; - } - - /** - * Adds multiple listeners to the option. Invoked upon changing the pending value. - * - * @see Option#addListener(BiConsumer) - */ - public Builder listeners(@NotNull Collection, T>> listeners) { - this.listeners.addAll(listeners); - return this; - } - - /** - * Dictates whether the option should require a restart. - * {@link Option#requiresRestart()} - */ - @Deprecated - public Builder requiresRestart(boolean requiresRestart) { - if (requiresRestart) flag(OptionFlag.GAME_RESTART); - else flags.remove(OptionFlag.GAME_RESTART); - - return this; - } - - public Option build() { - Validate.notNull(controlGetter, "`control` must not be null when building `Option`"); - Validate.notNull(binding, "`binding` must not be null when building `Option`"); - Validate.isTrue(!instant || flags.isEmpty(), "instant application does not support option flags"); - - Function concatenatedTooltipGetter = value -> { - MutableText concatenatedTooltip = Text.empty(); - boolean first = true; - for (Function line : tooltipGetters) { - if (!first) concatenatedTooltip.append("\n"); - first = false; - - concatenatedTooltip.append(line.apply(value)); - } - - return concatenatedTooltip; - }; - - if (instant) { - listeners.add((opt, pendingValue) -> opt.applyValue()); - } - - return new OptionImpl<>(name, concatenatedTooltipGetter, controlGetter, binding, available, ImmutableSet.copyOf(flags), typeClass, listeners); - } - } -} diff --git a/src/main/java/dev/isxander/yacl/api/OptionFlag.java b/src/main/java/dev/isxander/yacl/api/OptionFlag.java deleted file mode 100644 index 203a674..0000000 --- a/src/main/java/dev/isxander/yacl/api/OptionFlag.java +++ /dev/null @@ -1,27 +0,0 @@ -package dev.isxander.yacl.api; - -import dev.isxander.yacl.gui.RequireRestartScreen; -import net.minecraft.client.MinecraftClient; - -import java.util.function.Consumer; - -/** - * Code that is executed upon certain options being applied. - * Each flag is executed only once per save, no matter the amount of options with the flag. - */ -@FunctionalInterface -public interface OptionFlag extends Consumer { - /** - * Warns the user that a game restart is required for the changes to take effect - */ - OptionFlag GAME_RESTART = client -> client.setScreen(new RequireRestartScreen(client.currentScreen)); - - /** - * Reloads chunks upon applying (F3+A) - */ - OptionFlag RELOAD_CHUNKS = client -> client.worldRenderer.reload(); - - OptionFlag WORLD_RENDER_UPDATE = client -> client.worldRenderer.scheduleTerrainUpdate(); - - OptionFlag ASSET_RELOAD = MinecraftClient::reloadResourcesConcurrently; -} diff --git a/src/main/java/dev/isxander/yacl/api/OptionGroup.java b/src/main/java/dev/isxander/yacl/api/OptionGroup.java deleted file mode 100644 index 3364bdf..0000000 --- a/src/main/java/dev/isxander/yacl/api/OptionGroup.java +++ /dev/null @@ -1,141 +0,0 @@ -package dev.isxander.yacl.api; - -import com.google.common.collect.ImmutableList; -import dev.isxander.yacl.impl.OptionGroupImpl; -import net.minecraft.text.MutableText; -import net.minecraft.text.Text; -import org.apache.commons.lang3.Validate; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -/** - * Serves as a separator between multiple chunks of options - * that may be too similar or too few to be placed in a separate {@link ConfigCategory}. - * Or maybe you just want your config to feel less dense. - */ -public interface OptionGroup { - /** - * Name of the option group, displayed as a separator in the option lists. - * Can be empty. - */ - Text name(); - - /** - * Tooltip displayed on hover. - */ - Text tooltip(); - - /** - * List of all options in the group - */ - @NotNull ImmutableList> options(); - - /** - * Dictates if the group should be collapsed by default. - */ - boolean collapsed(); - - /** - * Always false when using the {@link Builder} - * used to not render the separator if true - */ - boolean isRoot(); - - /** - * Creates a builder to construct a {@link OptionGroup} - */ - static Builder createBuilder() { - return new Builder(); - } - - class Builder { - private Text name = Text.empty(); - private final List tooltipLines = new ArrayList<>(); - private final List> options = new ArrayList<>(); - private boolean collapsed = false; - - private Builder() { - - } - - /** - * Sets name of the group, can be {@link Text#empty()} to just separate options, like sodium. - * - * @see OptionGroup#name() - */ - public Builder name(@NotNull Text name) { - Validate.notNull(name, "`name` must not be null"); - - this.name = name; - return this; - } - - /** - * Sets the tooltip to be used by the option group. - * Can be invoked twice to append more lines. - * No need to wrap the text yourself, the gui does this itself. - * - * @param tooltips text lines - merged with a new-line on {@link Builder#build()}. - */ - public Builder tooltip(@NotNull Text... tooltips) { - Validate.notEmpty(tooltips, "`tooltips` cannot be empty"); - - tooltipLines.addAll(List.of(tooltips)); - return this; - } - - /** - * Adds an option to group. - * To construct an option, use {@link Option#createBuilder(Class)} - * - * @see OptionGroup#options() - */ - public Builder option(@NotNull Option option) { - Validate.notNull(option, "`option` must not be null"); - - this.options.add(option); - return this; - } - - /** - * Adds multiple options to group. - * To construct an option, use {@link Option#createBuilder(Class)} - * - * @see OptionGroup#options() - */ - public Builder options(@NotNull Collection> options) { - Validate.notEmpty(options, "`options` must not be empty"); - - this.options.addAll(options); - return this; - } - - /** - * Dictates if the group should be collapsed by default - * - * @see OptionGroup#collapsed() - */ - public Builder collapsed(boolean collapsible) { - this.collapsed = collapsible; - return this; - } - - public OptionGroup build() { - Validate.notEmpty(options, "`options` must not be empty to build `OptionGroup`"); - - MutableText concatenatedTooltip = Text.empty(); - boolean first = true; - for (Text line : tooltipLines) { - if (!first) concatenatedTooltip.append("\n"); - first = false; - - concatenatedTooltip.append(line); - } - - return new OptionGroupImpl(name, concatenatedTooltip, ImmutableList.copyOf(options), collapsed, false); - } - } -} diff --git a/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java b/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java deleted file mode 100644 index de7441c..0000000 --- a/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java +++ /dev/null @@ -1,94 +0,0 @@ -package dev.isxander.yacl.api; - -import dev.isxander.yacl.gui.YACLScreen; -import dev.isxander.yacl.impl.PlaceholderCategoryImpl; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.text.MutableText; -import net.minecraft.text.Text; -import org.apache.commons.lang3.Validate; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.List; -import java.util.function.BiFunction; - -/** - * A placeholder category that actually just opens another screen, - * instead of displaying options - */ -public interface PlaceholderCategory extends ConfigCategory { - /** - * Function to create a screen to open upon changing to this category - */ - BiFunction screen(); - - static Builder createBuilder() { - return new Builder(); - } - - class Builder { - private Text name; - - private final List tooltipLines = new ArrayList<>(); - - private BiFunction screenFunction; - - private Builder() { - - } - - /** - * Sets name of the category - * - * @see ConfigCategory#name() - */ - public Builder name(@NotNull Text name) { - Validate.notNull(name, "`name` cannot be null"); - - this.name = name; - return this; - } - - /** - * Sets the tooltip to be used by the category. - * Can be invoked twice to append more lines. - * No need to wrap the text yourself, the gui does this itself. - * - * @param tooltips text lines - merged with a new-line on {@link Builder#build()}. - */ - public Builder tooltip(@NotNull Text... tooltips) { - Validate.notEmpty(tooltips, "`tooltips` cannot be empty"); - - tooltipLines.addAll(List.of(tooltips)); - return this; - } - - /** - * Screen to open upon selecting this category - * - * @see PlaceholderCategory#screen() - */ - public Builder screen(@NotNull BiFunction screenFunction) { - Validate.notNull(screenFunction, "`screenFunction` cannot be null"); - - this.screenFunction = screenFunction; - return this; - } - - public PlaceholderCategory build() { - Validate.notNull(name, "`name` must not be null to build `ConfigCategory`"); - - MutableText concatenatedTooltip = Text.empty(); - boolean first = true; - for (Text line : tooltipLines) { - if (!first) concatenatedTooltip.append("\n"); - first = false; - - concatenatedTooltip.append(line); - } - - return new PlaceholderCategoryImpl(name, screenFunction, concatenatedTooltip); - } - } -} diff --git a/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java b/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java deleted file mode 100644 index a69ae4e..0000000 --- a/src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java +++ /dev/null @@ -1,136 +0,0 @@ -package dev.isxander.yacl.api; - -import com.google.common.collect.ImmutableList; -import dev.isxander.yacl.gui.YACLScreen; -import dev.isxander.yacl.impl.YetAnotherConfigLibImpl; -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.text.Text; -import org.apache.commons.lang3.Validate; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.function.Consumer; - -/** - * Main class of the mod. - * Contains all data and used to provide a {@link Screen} - */ -public interface YetAnotherConfigLib { - /** - * Title of the GUI. Only used for Minecraft narration. - */ - Text title(); - - /** - * Gets all config categories. - */ - ImmutableList categories(); - - /** - * Ran when changes are saved. Can be used to save config to a file etc. - */ - Runnable saveFunction(); - - /** - * Ran every time the YACL screen initialises. Can be paired with FAPI to add custom widgets. - */ - Consumer initConsumer(); - - /** - * Generates a Screen to display based on this instance. - * - * @param parent parent screen to open once closed - */ - Screen generateScreen(@Nullable Screen parent); - - /** - * Creates a builder to construct YACL - */ - static Builder createBuilder() { - return new Builder(); - } - - class Builder { - private Text title; - private final List categories = new ArrayList<>(); - private Runnable saveFunction = () -> {}; - private Consumer initConsumer = screen -> {}; - - private Builder() { - - } - - /** - * Sets title of GUI for Minecraft narration - * - * @see YetAnotherConfigLib#title() - */ - public Builder title(@NotNull Text title) { - Validate.notNull(title, "`title` cannot be null"); - - this.title = title; - return this; - } - - /** - * Adds a new category. - * To create a category you need to use {@link ConfigCategory#createBuilder()} - * - * @see YetAnotherConfigLib#categories() - */ - public Builder category(@NotNull ConfigCategory category) { - Validate.notNull(category, "`category` cannot be null"); - - this.categories.add(category); - return this; - } - - /** - * Adds multiple categories at once. - * To create a category you need to use {@link ConfigCategory#createBuilder()} - * - * @see YetAnotherConfigLib#categories() - */ - public Builder categories(@NotNull Collection categories) { - Validate.notNull(categories, "`categories` cannot be null"); - - this.categories.addAll(categories); - return this; - } - - /** - * Used to define a save function for when user clicks the Save Changes button - * - * @see YetAnotherConfigLib#saveFunction() - */ - public Builder save(@NotNull Runnable saveFunction) { - Validate.notNull(saveFunction, "`saveFunction` cannot be null"); - - this.saveFunction = saveFunction; - return this; - } - - /** - * Defines a consumer that is accepted every time the YACL screen initialises - * - * @see YetAnotherConfigLib#initConsumer() - */ - public Builder screenInit(@NotNull Consumer initConsumer) { - Validate.notNull(initConsumer, "`initConsumer` cannot be null"); - - this.initConsumer = initConsumer; - return this; - } - - public YetAnotherConfigLib build() { - Validate.notNull(title, "`title must not be null to build `YetAnotherConfigLib`"); - Validate.notEmpty(categories, "`categories` must not be empty to build `YetAnotherConfigLib`"); - Validate.isTrue(!categories.stream().allMatch(category -> category instanceof PlaceholderCategory), "At least one regular category is required to build `YetAnotherConfigLib`"); - - return new YetAnotherConfigLibImpl(title, ImmutableList.copyOf(categories), saveFunction, initConsumer); - } - } -} diff --git a/src/main/java/dev/isxander/yacl/api/utils/Dimension.java b/src/main/java/dev/isxander/yacl/api/utils/Dimension.java deleted file mode 100644 index 0de0a58..0000000 --- a/src/main/java/dev/isxander/yacl/api/utils/Dimension.java +++ /dev/null @@ -1,33 +0,0 @@ -package dev.isxander.yacl.api.utils; - -import dev.isxander.yacl.impl.utils.DimensionIntegerImpl; - -public interface Dimension { - T x(); - T y(); - - T width(); - T height(); - - T xLimit(); - T yLimit(); - - T centerX(); - T centerY(); - - boolean isPointInside(T x, T y); - - MutableDimension clone(); - - Dimension withX(T x); - Dimension withY(T y); - Dimension withWidth(T width); - Dimension withHeight(T height); - - Dimension moved(T x, T y); - Dimension expanded(T width, T height); - - static MutableDimension ofInt(int x, int y, int width, int height) { - return new DimensionIntegerImpl(x, y, width, height); - } -} diff --git a/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java b/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java deleted file mode 100644 index eff0186..0000000 --- a/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java +++ /dev/null @@ -1,11 +0,0 @@ -package dev.isxander.yacl.api.utils; - -public interface MutableDimension extends Dimension { - MutableDimension setX(T x); - MutableDimension setY(T y); - MutableDimension setWidth(T width); - MutableDimension setHeight(T height); - - MutableDimension move(T x, T y); - MutableDimension expand(T width, T height); -} diff --git a/src/main/java/dev/isxander/yacl/api/utils/OptionUtils.java b/src/main/java/dev/isxander/yacl/api/utils/OptionUtils.java deleted file mode 100644 index ab46b5b..0000000 --- a/src/main/java/dev/isxander/yacl/api/utils/OptionUtils.java +++ /dev/null @@ -1,37 +0,0 @@ -package dev.isxander.yacl.api.utils; - -import dev.isxander.yacl.api.ConfigCategory; -import dev.isxander.yacl.api.Option; -import dev.isxander.yacl.api.OptionGroup; -import dev.isxander.yacl.api.YetAnotherConfigLib; - -import java.util.function.Consumer; -import java.util.function.Function; - -public class OptionUtils { - /** - * Consumes all options, ignoring groups and categories. - * When consumer returns true, this function stops iterating. - */ - public static void consumeOptions(YetAnotherConfigLib yacl, Function, Boolean> consumer) { - for (ConfigCategory category : yacl.categories()) { - for (OptionGroup group : category.groups()) { - for (Option option : group.options()) { - if (consumer.apply(option)) return; - } - } - } - } - - /** - * Consumes all options, ignoring groups and categories. - * - * @see OptionUtils#consumeOptions(YetAnotherConfigLib, Function) - */ - public static void forEachOptions(YetAnotherConfigLib yacl, Consumer> consumer) { - consumeOptions(yacl, (opt) -> { - consumer.accept(opt); - return false; - }); - } -} -- cgit From d6a5bf1d333586c267a5b156eca6b576529fce74 Mon Sep 17 00:00:00 2001 From: isXander Date: Sun, 27 Nov 2022 18:43:24 +0000 Subject: replacement of ConfigInstance#buildConfig in YetAnotherConfigLib.java move some things to main that can be and fix testmod --- build.gradle.kts | 1 + .../dev/isxander/yacl/api/YetAnotherConfigLib.java | 15 +++ .../dev/isxander/yacl/api/utils/Dimension.java | 33 ------ .../isxander/yacl/api/utils/MutableDimension.java | 11 -- .../yacl/impl/utils/DimensionIntegerImpl.java | 115 --------------------- .../isxander/yacl/impl/utils/YACLConstants.java | 8 -- .../dev/isxander/yacl/api/utils/Dimension.java | 33 ++++++ .../isxander/yacl/api/utils/MutableDimension.java | 11 ++ .../dev/isxander/yacl/config/ConfigInstance.java | 2 - .../isxander/yacl/config/GsonConfigInstance.java | 5 +- .../isxander/yacl/config/NbtConfigInstance.java | 13 +-- .../yacl/impl/utils/DimensionIntegerImpl.java | 115 +++++++++++++++++++++ .../isxander/yacl/impl/utils/YACLConstants.java | 8 ++ .../java/dev/isxander/yacl/test/GuiTest.java | 115 +++++++++++---------- 14 files changed, 251 insertions(+), 234 deletions(-) delete mode 100644 src/client/java/dev/isxander/yacl/api/utils/Dimension.java delete mode 100644 src/client/java/dev/isxander/yacl/api/utils/MutableDimension.java delete mode 100644 src/client/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java delete mode 100644 src/client/java/dev/isxander/yacl/impl/utils/YACLConstants.java create mode 100644 src/main/java/dev/isxander/yacl/api/utils/Dimension.java create mode 100644 src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java create mode 100644 src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java create mode 100644 src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java (limited to 'src/main/java/dev/isxander/yacl/api') diff --git a/build.gradle.kts b/build.gradle.kts index b30a011..e39c697 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -70,6 +70,7 @@ dependencies { "modClientImplementation"(fabricApi.module("fabric-resource-loader-v0", "0.67.2+1.19.3")) "testmodImplementation"(sourceSets.main.get().output) + "testmodImplementation"(sourceSets["client"].output) } java { diff --git a/src/client/java/dev/isxander/yacl/api/YetAnotherConfigLib.java b/src/client/java/dev/isxander/yacl/api/YetAnotherConfigLib.java index a69ae4e..ae6c060 100644 --- a/src/client/java/dev/isxander/yacl/api/YetAnotherConfigLib.java +++ b/src/client/java/dev/isxander/yacl/api/YetAnotherConfigLib.java @@ -1,6 +1,7 @@ package dev.isxander.yacl.api; import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.config.ConfigInstance; import dev.isxander.yacl.gui.YACLScreen; import dev.isxander.yacl.impl.YetAnotherConfigLibImpl; import net.minecraft.client.gui.screen.Screen; @@ -12,6 +13,7 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.function.BiFunction; import java.util.function.Consumer; /** @@ -53,6 +55,14 @@ public interface YetAnotherConfigLib { return new Builder(); } + /** + * Creates an instance using a {@link ConfigInstance} which autofills the save() builder method. + * This also takes an easy functional interface that provides defaults and config to help build YACL bindings. + */ + static YetAnotherConfigLib create(ConfigInstance configInstance, ConfigBackedBuilder builder) { + return builder.build(configInstance.getDefaults(), configInstance.getConfig(), createBuilder().save(configInstance::save)).build(); + } + class Builder { private Text title; private final List categories = new ArrayList<>(); @@ -133,4 +143,9 @@ public interface YetAnotherConfigLib { return new YetAnotherConfigLibImpl(title, ImmutableList.copyOf(categories), saveFunction, initConsumer); } } + + @FunctionalInterface + interface ConfigBackedBuilder { + YetAnotherConfigLib.Builder build(T defaults, T config, YetAnotherConfigLib.Builder builder); + } } diff --git a/src/client/java/dev/isxander/yacl/api/utils/Dimension.java b/src/client/java/dev/isxander/yacl/api/utils/Dimension.java deleted file mode 100644 index 0de0a58..0000000 --- a/src/client/java/dev/isxander/yacl/api/utils/Dimension.java +++ /dev/null @@ -1,33 +0,0 @@ -package dev.isxander.yacl.api.utils; - -import dev.isxander.yacl.impl.utils.DimensionIntegerImpl; - -public interface Dimension { - T x(); - T y(); - - T width(); - T height(); - - T xLimit(); - T yLimit(); - - T centerX(); - T centerY(); - - boolean isPointInside(T x, T y); - - MutableDimension clone(); - - Dimension withX(T x); - Dimension withY(T y); - Dimension withWidth(T width); - Dimension withHeight(T height); - - Dimension moved(T x, T y); - Dimension expanded(T width, T height); - - static MutableDimension ofInt(int x, int y, int width, int height) { - return new DimensionIntegerImpl(x, y, width, height); - } -} diff --git a/src/client/java/dev/isxander/yacl/api/utils/MutableDimension.java b/src/client/java/dev/isxander/yacl/api/utils/MutableDimension.java deleted file mode 100644 index eff0186..0000000 --- a/src/client/java/dev/isxander/yacl/api/utils/MutableDimension.java +++ /dev/null @@ -1,11 +0,0 @@ -package dev.isxander.yacl.api.utils; - -public interface MutableDimension extends Dimension { - MutableDimension setX(T x); - MutableDimension setY(T y); - MutableDimension setWidth(T width); - MutableDimension setHeight(T height); - - MutableDimension move(T x, T y); - MutableDimension expand(T width, T height); -} diff --git a/src/client/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java b/src/client/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java deleted file mode 100644 index 6c7508d..0000000 --- a/src/client/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java +++ /dev/null @@ -1,115 +0,0 @@ -package dev.isxander.yacl.impl.utils; - -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.api.utils.MutableDimension; - -public class DimensionIntegerImpl implements MutableDimension { - private int x, y; - private int width, height; - - public DimensionIntegerImpl(int x, int y, int width, int height) { - this.x = x; - this.y = y; - this.width = width; - this.height = height; - } - - @Override - public Integer x() { - return x; - } - - @Override - public Integer y() { - return y; - } - - @Override - public Integer width() { - return width; - } - - @Override - public Integer height() { - return height; - } - - @Override - public Integer xLimit() { - return x + width; - } - - @Override - public Integer yLimit() { - return y + height; - } - - @Override - public Integer centerX() { - return x + width / 2; - } - - @Override - public Integer centerY() { - return y + height / 2; - } - - @Override - public boolean isPointInside(Integer x, Integer y) { - return x >= x() && x <= xLimit() && y >= y() && y <= yLimit(); - } - - @Override - public MutableDimension clone() { - return new DimensionIntegerImpl(x, y, width, height); - } - - @Override public MutableDimension setX(Integer x) { this.x = x; return this; } - @Override public MutableDimension setY(Integer y) { this.y = y; return this; } - @Override public MutableDimension setWidth(Integer width) { this.width = width; return this; } - @Override public MutableDimension setHeight(Integer height) { this.height = height; return this; } - - @Override - public Dimension withX(Integer x) { - return clone().setX(x); - } - - @Override - public Dimension withY(Integer y) { - return clone().setY(y); - } - - @Override - public Dimension withWidth(Integer width) { - return clone().setWidth(width); - } - - @Override - public Dimension withHeight(Integer height) { - return clone().setHeight(height); - } - - @Override - public MutableDimension move(Integer x, Integer y) { - this.x += x; - this.y += y; - return this; - } - - @Override - public MutableDimension expand(Integer width, Integer height) { - this.width += width; - this.height += height; - return this; - } - - @Override - public Dimension moved(Integer x, Integer y) { - return clone().move(x, y); - } - - @Override - public Dimension expanded(Integer width, Integer height) { - return clone().expand(width, height); - } -} diff --git a/src/client/java/dev/isxander/yacl/impl/utils/YACLConstants.java b/src/client/java/dev/isxander/yacl/impl/utils/YACLConstants.java deleted file mode 100644 index 3d382d4..0000000 --- a/src/client/java/dev/isxander/yacl/impl/utils/YACLConstants.java +++ /dev/null @@ -1,8 +0,0 @@ -package dev.isxander.yacl.impl.utils; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class YACLConstants { - public static final Logger LOGGER = LoggerFactory.getLogger("YetAnotherConfigLib"); -} diff --git a/src/main/java/dev/isxander/yacl/api/utils/Dimension.java b/src/main/java/dev/isxander/yacl/api/utils/Dimension.java new file mode 100644 index 0000000..0de0a58 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/utils/Dimension.java @@ -0,0 +1,33 @@ +package dev.isxander.yacl.api.utils; + +import dev.isxander.yacl.impl.utils.DimensionIntegerImpl; + +public interface Dimension { + T x(); + T y(); + + T width(); + T height(); + + T xLimit(); + T yLimit(); + + T centerX(); + T centerY(); + + boolean isPointInside(T x, T y); + + MutableDimension clone(); + + Dimension withX(T x); + Dimension withY(T y); + Dimension withWidth(T width); + Dimension withHeight(T height); + + Dimension moved(T x, T y); + Dimension expanded(T width, T height); + + static MutableDimension ofInt(int x, int y, int width, int height) { + return new DimensionIntegerImpl(x, y, width, height); + } +} diff --git a/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java b/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java new file mode 100644 index 0000000..eff0186 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java @@ -0,0 +1,11 @@ +package dev.isxander.yacl.api.utils; + +public interface MutableDimension extends Dimension { + MutableDimension setX(T x); + MutableDimension setY(T y); + MutableDimension setWidth(T width); + MutableDimension setHeight(T height); + + MutableDimension move(T x, T y); + MutableDimension expand(T width, T height); +} diff --git a/src/main/java/dev/isxander/yacl/config/ConfigInstance.java b/src/main/java/dev/isxander/yacl/config/ConfigInstance.java index 18733f3..3ceee6d 100644 --- a/src/main/java/dev/isxander/yacl/config/ConfigInstance.java +++ b/src/main/java/dev/isxander/yacl/config/ConfigInstance.java @@ -16,8 +16,6 @@ import java.lang.reflect.InvocationTargetException; * @param config data type */ public abstract class ConfigInstance { - protected final static Logger logger = LoggerFactory.getLogger("YetAnotherConfigLib"); - private final Class configClass; private final T defaultInstance; private T instance; diff --git a/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java b/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java index 3e075ab..40c2c99 100644 --- a/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java +++ b/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java @@ -1,6 +1,7 @@ package dev.isxander.yacl.config; import com.google.gson.*; +import dev.isxander.yacl.impl.utils.YACLConstants; import net.minecraft.text.Style; import net.minecraft.text.Text; @@ -53,7 +54,7 @@ public class GsonConfigInstance extends ConfigInstance { @Override public void save() { try { - logger.info("Saving {}...", getConfigClass().getSimpleName()); + YACLConstants.LOGGER.info("Saving {}...", getConfigClass().getSimpleName()); Files.writeString(path, gson.toJson(getConfig()), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); } catch (IOException e) { e.printStackTrace(); @@ -68,7 +69,7 @@ public class GsonConfigInstance extends ConfigInstance { return; } - logger.info("Loading {}...", getConfigClass().getSimpleName()); + YACLConstants.LOGGER.info("Loading {}...", getConfigClass().getSimpleName()); setConfig(gson.fromJson(Files.readString(path), getConfigClass())); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/dev/isxander/yacl/config/NbtConfigInstance.java b/src/main/java/dev/isxander/yacl/config/NbtConfigInstance.java index 8f817cb..5749695 100644 --- a/src/main/java/dev/isxander/yacl/config/NbtConfigInstance.java +++ b/src/main/java/dev/isxander/yacl/config/NbtConfigInstance.java @@ -1,5 +1,6 @@ package dev.isxander.yacl.config; +import dev.isxander.yacl.impl.utils.YACLConstants; import net.minecraft.nbt.*; import java.awt.*; @@ -57,13 +58,13 @@ public class NbtConfigInstance extends ConfigInstance { @Override public void save() { - logger.info("Saving {}...", getConfigClass().getSimpleName()); + YACLConstants.LOGGER.info("Saving {}...", getConfigClass().getSimpleName()); NbtCompound nbt; try { nbt = (NbtCompound) serializeObject(getConfig(), nbtSerializerHolder, field -> field.isAnnotationPresent(ConfigEntry.class)); } catch (IllegalAccessException e) { - logger.error("Failed to convert '{}' -> NBT", getConfigClass().getName(), e); + YACLConstants.LOGGER.error("Failed to convert '{}' -> NBT", getConfigClass().getName(), e); return; } @@ -76,7 +77,7 @@ public class NbtConfigInstance extends ConfigInstance { else NbtIo.write(nbt, new DataOutputStream(fos)); } catch (IOException e) { - logger.error("Failed to write NBT to '{}'", path, e); + YACLConstants.LOGGER.error("Failed to write NBT to '{}'", path, e); } } @@ -87,19 +88,19 @@ public class NbtConfigInstance extends ConfigInstance { return; } - logger.info("Loading {}...", getConfigClass().getSimpleName()); + YACLConstants.LOGGER.info("Loading {}...", getConfigClass().getSimpleName()); NbtCompound nbt; try { nbt = compressed ? NbtIo.readCompressed(path.toFile()) : NbtIo.read(path.toFile()); } catch (IOException e) { - logger.error("Failed to read NBT file '{}'", path, e); + YACLConstants.LOGGER.error("Failed to read NBT file '{}'", path, e); return; } try { setConfig(deserializeObject(nbt, getConfigClass(), nbtSerializerHolder, field -> field.isAnnotationPresent(ConfigEntry.class))); } catch (InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) { - logger.error("Failed to convert NBT -> '{}'", getConfigClass().getName(), e); + YACLConstants.LOGGER.error("Failed to convert NBT -> '{}'", getConfigClass().getName(), e); } } diff --git a/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java b/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java new file mode 100644 index 0000000..6c7508d --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java @@ -0,0 +1,115 @@ +package dev.isxander.yacl.impl.utils; + +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.api.utils.MutableDimension; + +public class DimensionIntegerImpl implements MutableDimension { + private int x, y; + private int width, height; + + public DimensionIntegerImpl(int x, int y, int width, int height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + @Override + public Integer x() { + return x; + } + + @Override + public Integer y() { + return y; + } + + @Override + public Integer width() { + return width; + } + + @Override + public Integer height() { + return height; + } + + @Override + public Integer xLimit() { + return x + width; + } + + @Override + public Integer yLimit() { + return y + height; + } + + @Override + public Integer centerX() { + return x + width / 2; + } + + @Override + public Integer centerY() { + return y + height / 2; + } + + @Override + public boolean isPointInside(Integer x, Integer y) { + return x >= x() && x <= xLimit() && y >= y() && y <= yLimit(); + } + + @Override + public MutableDimension clone() { + return new DimensionIntegerImpl(x, y, width, height); + } + + @Override public MutableDimension setX(Integer x) { this.x = x; return this; } + @Override public MutableDimension setY(Integer y) { this.y = y; return this; } + @Override public MutableDimension setWidth(Integer width) { this.width = width; return this; } + @Override public MutableDimension setHeight(Integer height) { this.height = height; return this; } + + @Override + public Dimension withX(Integer x) { + return clone().setX(x); + } + + @Override + public Dimension withY(Integer y) { + return clone().setY(y); + } + + @Override + public Dimension withWidth(Integer width) { + return clone().setWidth(width); + } + + @Override + public Dimension withHeight(Integer height) { + return clone().setHeight(height); + } + + @Override + public MutableDimension move(Integer x, Integer y) { + this.x += x; + this.y += y; + return this; + } + + @Override + public MutableDimension expand(Integer width, Integer height) { + this.width += width; + this.height += height; + return this; + } + + @Override + public Dimension moved(Integer x, Integer y) { + return clone().move(x, y); + } + + @Override + public Dimension expanded(Integer width, Integer height) { + return clone().expand(width, height); + } +} diff --git a/src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java b/src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java new file mode 100644 index 0000000..3d382d4 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java @@ -0,0 +1,8 @@ +package dev.isxander.yacl.impl.utils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class YACLConstants { + public static final Logger LOGGER = LoggerFactory.getLogger("YetAnotherConfigLib"); +} diff --git a/src/testmod/java/dev/isxander/yacl/test/GuiTest.java b/src/testmod/java/dev/isxander/yacl/test/GuiTest.java index 2944d69..7377bc9 100644 --- a/src/testmod/java/dev/isxander/yacl/test/GuiTest.java +++ b/src/testmod/java/dev/isxander/yacl/test/GuiTest.java @@ -2,6 +2,7 @@ package dev.isxander.yacl.test; import dev.isxander.yacl.api.*; import dev.isxander.yacl.gui.RequireRestartScreen; +import dev.isxander.yacl.gui.controllers.*; import dev.isxander.yacl.gui.controllers.cycling.EnumController; import dev.isxander.yacl.gui.controllers.slider.DoubleSliderController; import dev.isxander.yacl.gui.controllers.slider.FloatSliderController; @@ -22,7 +23,7 @@ import java.awt.*; public class GuiTest { public static Screen getModConfigScreenFactory(Screen parent) { - return Entrypoint.getConfig().buildConfig((config, builder) -> builder + return YetAnotherConfigLib.create(Entrypoint.getConfig(), (defaults, config, builder) -> builder .title(Text.of("Test Suites")) .category(ConfigCategory.createBuilder() .name(Text.of("Suites")) @@ -52,7 +53,7 @@ public class GuiTest { } private static Screen getFullTestSuite(Screen parent) { - return Entrypoint.getConfig().buildConfig((config, builder) -> builder + return YetAnotherConfigLib.create(Entrypoint.getConfig(), (defaults, config, builder) -> builder .title(Text.of("Test GUI")) .category(ConfigCategory.createBuilder() .name(Text.of("Control Examples")) @@ -65,9 +66,9 @@ public class GuiTest { .name(Text.of("Boolean Toggle")) .tooltip(value -> Text.of("A simple toggle button that contains the value '" + value + "'")) .binding( - config.getDefaults().booleanToggle, - () -> config.getConfig().booleanToggle, - (value) -> config.getConfig().booleanToggle = value + defaults.booleanToggle, + () -> config.booleanToggle, + (value) -> config.booleanToggle = value ) .controller(BooleanController::new) .flag(OptionFlag.GAME_RESTART) @@ -77,9 +78,9 @@ public class GuiTest { .name(Text.of("Custom Boolean Toggle")) .tooltip(Text.of("You can customize these controllers like this!")) .binding( - config.getDefaults().customBooleanToggle, - () -> config.getConfig().customBooleanToggle, - (value) -> config.getConfig().customBooleanToggle = value + defaults.customBooleanToggle, + () -> config.customBooleanToggle, + (value) -> config.customBooleanToggle = value ) .controller(opt -> new BooleanController(opt, state -> state ? Text.of("Amazing") : Text.of("Not Amazing"), true)) .build()) @@ -87,9 +88,9 @@ public class GuiTest { .name(Text.of("Tick Box aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) .tooltip(Text.of("There are even alternate methods of displaying the same data type!")) .binding( - config.getDefaults().tickbox, - () -> config.getConfig().tickbox, - (value) -> config.getConfig().tickbox = value + defaults.tickbox, + () -> config.tickbox, + (value) -> config.tickbox = value ) .controller(TickBoxController::new) .build()) @@ -100,9 +101,9 @@ public class GuiTest { .name(Text.of("Int Slider that is cut off because the slider")) .instant(true) .binding( - config.getDefaults().intSlider, - () -> config.getConfig().intSlider, - value -> config.getConfig().intSlider = value + defaults.intSlider, + () -> config.intSlider, + value -> config.intSlider = value ) .controller(opt -> new IntegerSliderController(opt, 0, 3, 1)) @@ -110,27 +111,27 @@ public class GuiTest { .option(Option.createBuilder(double.class) .name(Text.of("Double Slider")) .binding( - config.getDefaults().doubleSlider, - () -> config.getConfig().doubleSlider, - (value) -> config.getConfig().doubleSlider = value + defaults.doubleSlider, + () -> config.doubleSlider, + (value) -> config.doubleSlider = value ) .controller(opt -> new DoubleSliderController(opt, 0, 3, 0.05)) .build()) .option(Option.createBuilder(float.class) .name(Text.of("Float Slider")) .binding( - config.getDefaults().floatSlider, - () -> config.getConfig().floatSlider, - (value) -> config.getConfig().floatSlider = value + defaults.floatSlider, + () -> config.floatSlider, + (value) -> config.floatSlider = value ) .controller(opt -> new FloatSliderController(opt, 0, 3, 0.1f)) .build()) .option(Option.createBuilder(long.class) .name(Text.of("Long Slider")) .binding( - config.getDefaults().longSlider, - () -> config.getConfig().longSlider, - (value) -> config.getConfig().longSlider = value + defaults.longSlider, + () -> config.longSlider, + (value) -> config.longSlider = value ) .controller(opt -> new LongSliderController(opt, 0, 1_000_000, 100)) .build()) @@ -140,18 +141,18 @@ public class GuiTest { .option(Option.createBuilder(String.class) .name(Text.of("Text Option")) .binding( - config.getDefaults().textField, - () -> config.getConfig().textField, - value -> config.getConfig().textField = value + defaults.textField, + () -> config.textField, + value -> config.textField = value ) .controller(StringController::new) .build()) .option(Option.createBuilder(Color.class) .name(Text.of("Color Option")) .binding( - config.getDefaults().colorOption, - () -> config.getConfig().colorOption, - value -> config.getConfig().colorOption = value + defaults.colorOption, + () -> config.colorOption, + value -> config.colorOption = value ) .controller(ColorController::new) .build()) @@ -161,9 +162,9 @@ public class GuiTest { .option(Option.createBuilder(ConfigData.Alphabet.class) .name(Text.of("Enum Cycler")) .binding( - config.getDefaults().enumOption, - () -> config.getConfig().enumOption, - (value) -> config.getConfig().enumOption = value + defaults.enumOption, + () -> config.enumOption, + (value) -> config.enumOption = value ) .controller(EnumController::new) .build()) @@ -211,9 +212,9 @@ public class GuiTest { .option(Option.createBuilder(boolean.class) .name(Text.of("Root Test")) .binding( - config.getDefaults().groupTestRoot, - () -> config.getConfig().groupTestRoot, - value -> config.getConfig().groupTestRoot = value + defaults.groupTestRoot, + () -> config.groupTestRoot, + value -> config.groupTestRoot = value ) .controller(TickBoxController::new) .build()) @@ -222,18 +223,18 @@ public class GuiTest { .option(Option.createBuilder(boolean.class) .name(Text.of("First Group Test 1")) .binding( - config.getDefaults().groupTestFirstGroup, - () -> config.getConfig().groupTestFirstGroup, - value -> config.getConfig().groupTestFirstGroup = value + defaults.groupTestFirstGroup, + () -> config.groupTestFirstGroup, + value -> config.groupTestFirstGroup = value ) .controller(TickBoxController::new) .build()) .option(Option.createBuilder(boolean.class) .name(Text.of("First Group Test 2")) .binding( - config.getDefaults().groupTestFirstGroup2, - () -> config.getConfig().groupTestFirstGroup2, - value -> config.getConfig().groupTestFirstGroup2 = value + defaults.groupTestFirstGroup2, + () -> config.groupTestFirstGroup2, + value -> config.groupTestFirstGroup2 = value ) .controller(TickBoxController::new) .build()) @@ -243,9 +244,9 @@ public class GuiTest { .option(Option.createBuilder(boolean.class) .name(Text.of("Second Group Test")) .binding( - config.getDefaults().groupTestSecondGroup, - () -> config.getConfig().groupTestSecondGroup, - value -> config.getConfig().groupTestSecondGroup = value + defaults.groupTestSecondGroup, + () -> config.groupTestSecondGroup, + value -> config.groupTestSecondGroup = value ) .controller(TickBoxController::new) .build()) @@ -256,9 +257,9 @@ public class GuiTest { .option(Option.createBuilder(int.class) .name(Text.of("Int Slider that is cut off because the slider")) .binding( - config.getDefaults().scrollingSlider, - () -> config.getConfig().scrollingSlider, - (value) -> config.getConfig().scrollingSlider = value + defaults.scrollingSlider, + () -> config.scrollingSlider, + (value) -> config.scrollingSlider = value ) .controller(opt -> new IntegerSliderController(opt, 0, 10, 1)) .build()) @@ -330,14 +331,14 @@ public class GuiTest { .build()) .save(() -> { MinecraftClient.getInstance().options.write(); - config.save(); + Entrypoint.getConfig().save(); }) ) .generateScreen(parent); } private static Screen getDisabledTest(Screen parent) { - return Entrypoint.getConfig().buildConfig((config, builder) -> builder + return YetAnotherConfigLib.create(Entrypoint.getConfig(), (defaults, config, builder) -> builder .title(Text.empty()) .category(ConfigCategory.createBuilder() .name(Text.of("Disabled Test")) @@ -370,7 +371,7 @@ public class GuiTest { } private static Screen getWikiBasic(Screen parent) { - return Entrypoint.getConfig().buildConfig((config, builder) -> builder + return YetAnotherConfigLib.create(Entrypoint.getConfig(), (defaults, config, builder) -> builder .title(Text.of("Mod Name")) .category(ConfigCategory.createBuilder() .name(Text.of("My Category")) @@ -379,9 +380,9 @@ public class GuiTest { .name(Text.of("My Boolean Option")) .tooltip(Text.of("This option displays the basic capabilities of YetAnotherConfigLib")) // optional .binding( - config.getDefaults().booleanToggle, // default - () -> config.getConfig().booleanToggle, // getter - newValue -> config.getConfig().booleanToggle = newValue // setter + defaults.booleanToggle, // default + () -> config.booleanToggle, // getter + newValue -> config.booleanToggle = newValue // setter ) .controller(BooleanController::new) .build()) @@ -391,7 +392,7 @@ public class GuiTest { } private static Screen getWikiGroups(Screen parent) { - return Entrypoint.getConfig().buildConfig((config, builder) -> builder + return YetAnotherConfigLib.create(Entrypoint.getConfig(), (defaults, config, builder) -> builder .title(Text.of("Mod Name")) .category(ConfigCategory.createBuilder() .name(Text.of("My Category")) @@ -402,9 +403,9 @@ public class GuiTest { .name(Text.of("My Boolean Option")) .tooltip(Text.of("This option displays the basic capabilities of YetAnotherConfigLib")) // optional .binding( - config.getDefaults().booleanToggle, // default - () -> config.getConfig().booleanToggle, // getter - newValue -> config.getConfig().booleanToggle = newValue // setter + defaults.booleanToggle, // default + () -> config.booleanToggle, // getter + newValue -> config.booleanToggle = newValue // setter ) .controller(BooleanController::new) .build()) -- cgit