diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/java/dev/isxander/yacl/api/YetAnotherConfigLib.java | 15 | ||||
-rw-r--r-- | src/main/java/dev/isxander/yacl/api/utils/Dimension.java (renamed from src/client/java/dev/isxander/yacl/api/utils/Dimension.java) | 0 | ||||
-rw-r--r-- | src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java (renamed from src/client/java/dev/isxander/yacl/api/utils/MutableDimension.java) | 0 | ||||
-rw-r--r-- | src/main/java/dev/isxander/yacl/config/ConfigInstance.java | 2 | ||||
-rw-r--r-- | src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java | 5 | ||||
-rw-r--r-- | src/main/java/dev/isxander/yacl/config/NbtConfigInstance.java | 13 | ||||
-rw-r--r-- | src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java (renamed from src/client/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java) | 0 | ||||
-rw-r--r-- | src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java (renamed from src/client/java/dev/isxander/yacl/impl/utils/YACLConstants.java) | 0 | ||||
-rw-r--r-- | src/testmod/java/dev/isxander/yacl/test/GuiTest.java | 115 |
9 files changed, 83 insertions, 67 deletions
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 <T> YetAnotherConfigLib create(ConfigInstance<T> configInstance, ConfigBackedBuilder<T> builder) { + return builder.build(configInstance.getDefaults(), configInstance.getConfig(), createBuilder().save(configInstance::save)).build(); + } + class Builder { private Text title; private final List<ConfigCategory> categories = new ArrayList<>(); @@ -133,4 +143,9 @@ public interface YetAnotherConfigLib { return new YetAnotherConfigLibImpl(title, ImmutableList.copyOf(categories), saveFunction, initConsumer); } } + + @FunctionalInterface + interface ConfigBackedBuilder<T> { + 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/main/java/dev/isxander/yacl/api/utils/Dimension.java index 0de0a58..0de0a58 100644 --- a/src/client/java/dev/isxander/yacl/api/utils/Dimension.java +++ b/src/main/java/dev/isxander/yacl/api/utils/Dimension.java diff --git a/src/client/java/dev/isxander/yacl/api/utils/MutableDimension.java b/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java index eff0186..eff0186 100644 --- a/src/client/java/dev/isxander/yacl/api/utils/MutableDimension.java +++ b/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java 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 <T> config data type */ public abstract class ConfigInstance<T> { - protected final static Logger logger = LoggerFactory.getLogger("YetAnotherConfigLib"); - private final Class<T> 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<T> extends ConfigInstance<T> { @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<T> extends ConfigInstance<T> { 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<T> extends ConfigInstance<T> { @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<T> extends ConfigInstance<T> { 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<T> extends ConfigInstance<T> { 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/client/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java b/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java index 6c7508d..6c7508d 100644 --- a/src/client/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java +++ b/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java diff --git a/src/client/java/dev/isxander/yacl/impl/utils/YACLConstants.java b/src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java index 3d382d4..3d382d4 100644 --- a/src/client/java/dev/isxander/yacl/impl/utils/YACLConstants.java +++ b/src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java 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()) |