Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for yacl #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ repositories {
maven("https://repo.hypixel.net/repository/Hypixel/")
maven("https://repo.cookies.codes/releases")
maven("https://pkgs.dev.azure.com/djtheredstoner/DevAuth/_packaging/public/maven/v1")
maven("https://maven.isxander.dev/releases")
}

fabricApi {
Expand Down Expand Up @@ -49,6 +50,8 @@ dependencies {
modImplementation("net.fabricmc:fabric-loader:${project.properties["loader_version"]}")
modImplementation("net.fabricmc.fabric-api:fabric-api:${project.properties["fabric_version"]}")
modRuntimeOnly("me.djtheredstoner:DevAuth-fabric:1.2.1")

modImplementation("dev.isxander:yet-another-config-lib:3.6.1+1.21.2-fabric")
}

tasks.withType<Jar>().configureEach {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/codes/cookies/mod/CookiesMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import codes.cookies.mod.commands.system.CommandManager;
import codes.cookies.mod.config.ConfigManager;
import codes.cookies.mod.config.screen.ConfigScreen;
import codes.cookies.mod.config.system.yacl.YaclConfigReader;
import codes.cookies.mod.data.cookiesmoddata.CookieDataManager;
import codes.cookies.mod.data.profile.ProfileStorage;
import codes.cookies.mod.events.EventLoader;
Expand All @@ -22,6 +23,7 @@
import codes.cookies.mod.services.mining.CrystalStatusService;
import codes.cookies.mod.utils.UpdateChecker;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

Expand All @@ -30,6 +32,7 @@
import codes.cookies.mod.utils.skyblock.LocationUtils;
import codes.cookies.mod.utils.skyblock.MayorUtils;
import codes.cookies.mod.utils.skyblock.playerlist.PlayerListUtils;
import dev.isxander.yacl3.api.YetAnotherConfigLib;
import lombok.Getter;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
Expand Down Expand Up @@ -60,6 +63,16 @@ public class CookiesMod implements ClientModInitializer {
* Opens the config screen.
*/
public static void openConfig() {
if (ConfigManager.getConfigReader() instanceof YaclConfigReader yaclConfigReader) {
final CompletableFuture<YetAnotherConfigLib.Builder> finalConfig = yaclConfigReader.getFinalConfig();
if (finalConfig.isDone()) {
final YetAnotherConfigLib join = finalConfig.join().build();
final Screen screen = join.generateScreen(MinecraftClient.getInstance().currentScreen);
openScreen(screen);
return;
}
}

openScreen(new ConfigScreen(ConfigManager.getConfigReader()));
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/codes/cookies/mod/config/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codes.cookies.mod.config;

import codes.cookies.mod.config.system.yacl.YaclConfigReader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
Expand Down Expand Up @@ -62,7 +63,7 @@ public static boolean isLoaded() {
public static void processConfig() {
config = new CookiesConfig();
reload();
configReader = new ConfigReader();
configReader = new YaclConfigReader();
ConfigProcessor.processConfig(config, configReader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* Config reader to correctly parse a config from its source.
*/
public class ConfigReader {
public abstract class ConfigReader {
private static final Logger logger = LoggerFactory.getLogger(ConfigReader.class);

@Getter
Expand Down Expand Up @@ -70,19 +70,19 @@ public void endParent() {
*
* @param foldable The foldable to open.
*/
public void beginFoldable(Foldable foldable) {
//noinspection rawtypes

ProcessedOption processedOption = new ProcessedOption<>(new FoldableOption(
foldable,
foldableId.incrementAndGet()
));
if (!this.foldableStack.isEmpty()) {
public FoldableOption beginFoldable(Foldable foldable) {
final FoldableOption foldableOption = new FoldableOption(
foldable,
foldableId.incrementAndGet()
);
ProcessedOption<?, FoldableOption> processedOption = new ProcessedOption<>(foldableOption);
if (!this.foldableStack.isEmpty()) {
processedOption.setFoldable(this.foldableStack.peek());
}
this.currentCategory.addOption(processedOption);
this.foldableStack.push(this.foldableId.get());
}
return foldableOption;
}

/**
* Ends a foldable.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package codes.cookies.mod.config.system.yacl;

import java.awt.*;
import java.util.Stack;
import java.util.concurrent.CompletableFuture;

import codes.cookies.mod.config.system.Category;
import codes.cookies.mod.config.system.Config;
import codes.cookies.mod.config.system.Foldable;
import codes.cookies.mod.config.system.Option;
import codes.cookies.mod.config.system.options.BooleanOption;
import codes.cookies.mod.config.system.options.ButtonOption;
import codes.cookies.mod.config.system.options.ColorOption;
import codes.cookies.mod.config.system.options.EnumCycleOption;
import codes.cookies.mod.config.system.options.FoldableOption;
import codes.cookies.mod.config.system.options.SliderOption;
import codes.cookies.mod.config.system.options.StringInputOption;
import codes.cookies.mod.config.system.options.TextDisplayOption;
import codes.cookies.mod.config.system.parsed.ConfigReader;
import codes.cookies.mod.config.system.parsed.ProcessedOption;
import dev.isxander.yacl3.api.ConfigCategory;
import dev.isxander.yacl3.api.LabelOption;
import dev.isxander.yacl3.api.Option.Builder;
import dev.isxander.yacl3.api.OptionDescription;
import dev.isxander.yacl3.api.OptionGroup;
import dev.isxander.yacl3.api.YetAnotherConfigLib;
import dev.isxander.yacl3.api.controller.ColorControllerBuilder;
import dev.isxander.yacl3.api.controller.DoubleSliderControllerBuilder;
import dev.isxander.yacl3.api.controller.EnumControllerBuilder;
import dev.isxander.yacl3.api.controller.FloatSliderControllerBuilder;
import dev.isxander.yacl3.api.controller.IntegerSliderControllerBuilder;
import dev.isxander.yacl3.api.controller.LongSliderControllerBuilder;
import dev.isxander.yacl3.api.controller.StringControllerBuilder;
import dev.isxander.yacl3.api.controller.TickBoxControllerBuilder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import net.minecraft.text.Text;

@Slf4j
public class YaclConfigReader extends ConfigReader {

@Getter
final CompletableFuture<YetAnotherConfigLib.Builder> finalConfig = new CompletableFuture<>();
final Stack<OptionGroup.Builder> optionGroups = new Stack<>();
YetAnotherConfigLib.Builder builder;
ConfigCategory.Builder categoryBuilder;

@Override
public void beginConfig(Config<?> config) {
super.beginConfig(config);

builder = YetAnotherConfigLib.createBuilder();
builder.title(Text.literal("Cookies Mod"));
}

@Override
public void beginCategory(Category category) {
super.beginCategory(category);
categoryBuilder = ConfigCategory.createBuilder();
categoryBuilder.name(category.getName());
categoryBuilder.tooltip(category.getDescription());
}

@Override
public void endCategory() {
super.endCategory();
builder.category(categoryBuilder.build());
categoryBuilder = null;
}

private void addOption(dev.isxander.yacl3.api.Option<?> builder) {
if (!optionGroups.isEmpty()) {
final OptionGroup.Builder peek = optionGroups.peek();
peek.option(builder);
} else {
categoryBuilder.option(builder);
}
}

/**
* GOD THIS IS HORRIBLE QWQ
*/
@SuppressWarnings("unchecked")
private <T extends Number> void configureSliderOption(SliderOption<T> option) {

if (option.getValue() instanceof Double) {
final SliderOption<Double> option1 = (SliderOption<Double>) option;
final Builder<Double> basicBuilder = createBasicBuilder(option1);
basicBuilder.controller(doubleOption -> DoubleSliderControllerBuilder.create(doubleOption)
.range(option1.getMin(), option1.getMax()));
addOption(basicBuilder.build());
} else if (option.getValue() instanceof Integer) {
final SliderOption<Integer> option1 = (SliderOption<Integer>) option;
final Builder<Integer> basicBuilder = createBasicBuilder(option1);
basicBuilder.controller(doubleOption -> IntegerSliderControllerBuilder.create(doubleOption)
.range(option1.getMin(), option1.getMax()));
addOption(basicBuilder.build());
} else if (option.getValue() instanceof Float) {
final SliderOption<Float> option1 = (SliderOption<Float>) option;
final Builder<Float> basicBuilder = createBasicBuilder(option1);
basicBuilder.controller(doubleOption -> FloatSliderControllerBuilder.create(doubleOption)
.range(option1.getMin(), option1.getMax()));
addOption(basicBuilder.build());
} else if (option.getValue() instanceof Long) {
final SliderOption<Long> option1 = (SliderOption<Long>) option;
final Builder<Long> basicBuilder = createBasicBuilder(option1);
basicBuilder.controller(doubleOption -> LongSliderControllerBuilder.create(doubleOption)
.range(option1.getMin(), option1.getMax()));
addOption(basicBuilder.build());
}

}

private <T> Builder<T> createBasicBuilder(Option<T, ?> option) {
final var yaclOption = dev.isxander.yacl3.api.Option.<T>createBuilder();
yaclOption.name(option.getName());
yaclOption.description(OptionDescription.createBuilder().text(option.getDescription()).build());
yaclOption.binding(option.getValue(), option::getValue, option::setValue);
return yaclOption;
}

private <T extends Enum<T>> void addEnumCycle(EnumCycleOption<T> option) {
final Builder<T> basicBuilder = createBasicBuilder(option);
basicBuilder.controller(opt -> EnumControllerBuilder.create(opt)
.enumClass(option.getValue().getDeclaringClass())
.formatValue(option.getTextSupplier()::supplyText));
addOption(basicBuilder.build());
}

@Override
public <T, O extends Option<T, O>> ProcessedOption<T, O> processOption(Option<T, O> option, String fieldName) {
if (option instanceof ButtonOption buttonOption) {
addOption(dev.isxander.yacl3.api.ButtonOption.createBuilder()
.name(buttonOption.getName())
.description(OptionDescription.createBuilder().text(option.getDescription()).build())
.action((yaclScreen, buttonOption1) -> buttonOption.getValue().run()).build());
return super.processOption(option, fieldName);
} else if (option instanceof TextDisplayOption textDisplayOption) {
addOption(LabelOption.create(textDisplayOption.getName()));
return super.processOption(option, fieldName);
}

final Builder<T> basicBuilder = createBasicBuilder(option);

switch (option) {
case BooleanOption booleanOption -> {
((Builder<Boolean>) basicBuilder).controller(TickBoxControllerBuilder::create);
}
case SliderOption<?> sliderOption -> {
configureSliderOption(sliderOption);
return super.processOption(option, fieldName);
}
case ColorOption colorOption -> {
((Builder<Color>) basicBuilder).controller(opt -> ColorControllerBuilder.create(opt)
.allowAlpha(colorOption.isAllowAlpha()));
}
case StringInputOption stringInputOption -> {
((Builder<String>) basicBuilder).controller(StringControllerBuilder::create);
}
case EnumCycleOption<? extends Enum<?>> enumCycleOption -> {
addEnumCycle(enumCycleOption);
return super.processOption(option, fieldName);
}
default -> {
System.err.println("Unhandled option: " + fieldName + ": " + option);
}
}

addOption(basicBuilder.build());

return super.processOption(option, fieldName);
}

@Override
public FoldableOption beginFoldable(Foldable foldable) {
final FoldableOption foldableOption = super.beginFoldable(foldable);
final OptionGroup.Builder categoryBuilder = OptionGroup.createBuilder();
categoryBuilder.name(foldableOption.getName());
categoryBuilder.description(OptionDescription.createBuilder().text(foldableOption.getDescription()).build());
categoryBuilder.collapsed(true);
optionGroups.push(categoryBuilder);

return foldableOption;
}

@Override
public void endFoldable() {
super.endFoldable();
final OptionGroup.Builder pop = optionGroups.pop();
categoryBuilder.group(pop.build());
}

@Override
public void endConfig() {
super.endConfig();
finalConfig.complete(builder);
}
}
Loading