All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.hendrixshen.magiclib.malilib.impl.gui.ConfigGui Maven / Gradle / Ivy

There is a newer version: 0.7.398+fe2125a-beta
Show newest version
package top.hendrixshen.magiclib.malilib.impl.gui;

import com.google.common.collect.Lists;
import fi.dy.masa.malilib.config.IConfigBase;
import fi.dy.masa.malilib.gui.GuiConfigsBase;
import fi.dy.masa.malilib.gui.button.ButtonGeneric;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import org.jetbrains.annotations.NotNull;
import top.hendrixshen.magiclib.language.api.I18n;
import top.hendrixshen.magiclib.malilib.impl.ConfigManager;
import top.hendrixshen.magiclib.malilib.impl.ConfigOption;

import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

@Environment(EnvType.CLIENT)
public class ConfigGui extends GuiConfigsBase {
    private String tab;
    private final String identifier;
    private final Supplier titleSupplier;

    private final ConfigManager configManager;

    public ConfigGui(String identifier, String defaultTab, ConfigManager configManager, String title) {
        this(identifier, defaultTab, configManager, () -> title);
    }

    public ConfigGui(String identifier, String defaultTab, ConfigManager configManager, @NotNull Supplier titleSupplier) {
        super(10, 50, identifier, null, titleSupplier.get());
        tab = defaultTab;
        this.identifier = identifier;
        this.configManager = configManager;
        this.titleSupplier = titleSupplier;
    }

    @Override
    public void removed() {
        super.removed();
    }

    @Override
    public void initGui() {
        super.initGui();
        this.clearOptions();
        this.setTitle(this.titleSupplier.get());
        int x = 10;
        int y = 26;

        for (String category : configManager.getCategories()) {
            if (this.configManager.getOptionsByCategory(category).stream().noneMatch(ConfigOption::isEnabled)) {
                continue;
            }

            x += this.createNavigationButton(x, y, category);
        }
    }

    private int createNavigationButton(int x, int y, String category) {
        ButtonGeneric button = new ButtonGeneric(x, y, -1, 20, I18n.get(String.format("%s.gui.button.tab.%s", this.identifier, category)));
        button.setEnabled(!Objects.equals(category, tab) && this.configManager.getOptionsByCategory(category).stream().anyMatch(ConfigOption::isEnabled));

        this.addButton(button, (b, mouseButton) -> {
            tab = category;
            this.reDraw();
        });

        return button.getWidth() + 2;
    }

    public void reDraw() {
        this.reCreateListWidget();
        Objects.requireNonNull(this.getListWidget()).resetScrollbarPosition();
        this.initGui();
    }

    @Override
    public List getConfigs() {
        List configs = Lists.newArrayList();

        for (ConfigOption option : this.configManager.getOptionsByCategory(tab)) {
            if (!option.isEnabled()) {
                continue;
            }

            configs.add(option.getConfig());
        }

        configs.sort((a, b) -> a.getName().compareToIgnoreCase(b.getName()));
        return ConfigOptionWrapper.createFor(configs);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy