Skip to content

Commit

Permalink
Add ModMenuConfig class
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Jan 12, 2024
1 parent 2795262 commit 8c1a291
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 40 deletions.
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ fabric.loom.multiProjectOptimisation=true
# Versions
versionMc=1.20.1
versionForge=47.1.3
versionConnectorExtras=1.9.1
versionConnectorExtras=1.9.2
versionYarn=1.20.1+build.1

# Publishing
curseForgeId=913445
Expand Down
10 changes: 9 additions & 1 deletion modmenu-bridge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

val versionMc: String by rootProject
val versionForge: String by rootProject
val versionYarn: String by rootProject

loom {
forge {
Expand All @@ -14,8 +15,15 @@ loom {
}
}

repositories {
maven {
name = "TerraformersMC"
url = uri("https://maven.terraformersmc.com/")
}
}

dependencies {
mappings(loom.officialMojangMappings())
mappings("net.fabricmc:yarn:$versionYarn:v2")
forge(group = "net.minecraftforge", name = "forge", version = "$versionMc-$versionForge")

modImplementation(group = "dev.su5ed.sinytra", name = "fabric-loader", version = "2.3.4+0.14.21+1.20.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
package com.terraformersmc.modmenu.api;

import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screen.Screen;

@FunctionalInterface
public interface ConfigScreenFactory<S extends Screen> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
/*
* MIT License
*
* Copyright (c) 2018-2020 Prospector
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.terraformersmc.modmenu.api;

import com.google.common.collect.ImmutableMap;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.Text;
import net.minecraftforge.client.gui.ModListScreen;

import java.util.Map;
Expand All @@ -46,13 +23,13 @@ static Screen createModsScreen(Screen previous) {
}

/**
* Used for creating a {@link Component} just like what would appear
* Used for creating a {@link Text} just like what would appear
* on a Mod Menu Mods button
*
* @return The text that would be displayed on a Mods button
*/
static Component createModsButtonText() {
return CommonComponents.EMPTY;
static Text createModsButtonText() {
return ScreenTexts.EMPTY;
}

/**
Expand Down Expand Up @@ -90,6 +67,6 @@ default Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {
* provided by a modpack.
*/
default void attachModpackBadges(Consumer<String> consumer) {

return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.terraformersmc.modmenu.config;

import com.google.gson.annotations.SerializedName;
import com.terraformersmc.modmenu.config.option.BooleanConfigOption;
import com.terraformersmc.modmenu.config.option.EnumConfigOption;
import com.terraformersmc.modmenu.config.option.OptionConvertable;
import com.terraformersmc.modmenu.config.option.StringSetConfigOption;
import net.minecraft.client.option.SimpleOption;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;

public class ModMenuConfig {
public static final EnumConfigOption<Sorting> SORTING = new EnumConfigOption<>("sorting", Sorting.ASCENDING);
public static final BooleanConfigOption COUNT_LIBRARIES = new BooleanConfigOption("count_libraries", true);
public static final BooleanConfigOption COMPACT_LIST = new BooleanConfigOption("compact_list", false);
public static final BooleanConfigOption COUNT_CHILDREN = new BooleanConfigOption("count_children", true);
public static final EnumConfigOption<TitleMenuButtonStyle> MODS_BUTTON_STYLE = new EnumConfigOption<>("mods_button_style", TitleMenuButtonStyle.CLASSIC);
public static final EnumConfigOption<GameMenuButtonStyle> GAME_MENU_BUTTON_STYLE = new EnumConfigOption<>("game_menu_button_style", GameMenuButtonStyle.REPLACE_BUGS);
public static final BooleanConfigOption COUNT_HIDDEN_MODS = new BooleanConfigOption("count_hidden_mods", true);
public static final EnumConfigOption<ModCountLocation> MOD_COUNT_LOCATION = new EnumConfigOption<>("mod_count_location", ModCountLocation.TITLE_SCREEN);
public static final BooleanConfigOption HIDE_MOD_LINKS = new BooleanConfigOption("hide_mod_links", false);
public static final BooleanConfigOption SHOW_LIBRARIES = new BooleanConfigOption("show_libraries", false);
public static final BooleanConfigOption HIDE_MOD_LICENSE = new BooleanConfigOption("hide_mod_license", false);
public static final BooleanConfigOption HIDE_BADGES = new BooleanConfigOption("hide_badges", false);
public static final BooleanConfigOption HIDE_MOD_CREDITS = new BooleanConfigOption("hide_mod_credits", false);
public static final BooleanConfigOption EASTER_EGGS = new BooleanConfigOption("easter_eggs", true);
public static final BooleanConfigOption MODIFY_TITLE_SCREEN = new BooleanConfigOption("modify_title_screen", true);
public static final BooleanConfigOption MODIFY_GAME_MENU = new BooleanConfigOption("modify_game_menu", true);
public static final BooleanConfigOption HIDE_CONFIG_BUTTONS = new BooleanConfigOption("hide_config_buttons", false);
public static final BooleanConfigOption RANDOM_JAVA_COLORS = new BooleanConfigOption("random_java_colors", false);
public static final BooleanConfigOption TRANSLATE_NAMES = new BooleanConfigOption("translate_names", true);
public static final BooleanConfigOption TRANSLATE_DESCRIPTIONS = new BooleanConfigOption("translate_descriptions", true);
public static final BooleanConfigOption CONFIG_MODE = new BooleanConfigOption("config_mode", false);
public static final BooleanConfigOption DISABLE_DRAG_AND_DROP = new BooleanConfigOption("disable_drag_and_drop", false);
public static final StringSetConfigOption HIDDEN_MODS = new StringSetConfigOption("hidden_mods", new HashSet<>());
public static final StringSetConfigOption HIDDEN_CONFIGS = new StringSetConfigOption("hidden_configs", new HashSet<>());
public static final StringSetConfigOption DISABLE_UPDATE_CHECKER = new StringSetConfigOption("disable_update_checker", new HashSet<>());
public static final BooleanConfigOption UPDATE_CHECKER = new BooleanConfigOption("update_checker", true);
public static final BooleanConfigOption BUTTON_UPDATE_BADGE = new BooleanConfigOption("button_update_badge", true);
public static final BooleanConfigOption QUICK_CONFIGURE = new BooleanConfigOption("quick_configure", true);

public static SimpleOption<?>[] asOptions() {
ArrayList<SimpleOption<?>> options = new ArrayList<>();
for (Field field : ModMenuConfig.class.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers())
&& Modifier.isFinal(field.getModifiers())
&& OptionConvertable.class.isAssignableFrom(field.getType())
&& !field.getName().equals("HIDE_CONFIG_BUTTONS")
&& !field.getName().equals("MODIFY_TITLE_SCREEN")
&& !field.getName().equals("MODIFY_GAME_MENU")
&& !field.getName().equals("CONFIG_MODE")
&& !field.getName().equals("DISABLE_DRAG_AND_DROP")
) {
try {
options.add(((OptionConvertable) field.get(null)).asOption());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return options.stream().toArray(SimpleOption[]::new);
}

public enum Sorting {
@SerializedName("ascending")
ASCENDING,
@SerializedName("descending")
DESCENDING;

public Comparator<?> getComparator() {
return Comparator.naturalOrder();
}
}

public enum ModCountLocation {
@SerializedName("title_screen")
TITLE_SCREEN(true, false),
@SerializedName("mods_button")
MODS_BUTTON(false, true),
@SerializedName("title_screen_and_mods_button")
TITLE_SCREEN_AND_MODS_BUTTON(true, true),
@SerializedName("none")
NONE(false, false);

private final boolean titleScreen, modsButton;

ModCountLocation(boolean titleScreen, boolean modsButton) {
this.titleScreen = titleScreen;
this.modsButton = modsButton;
}

public boolean isOnTitleScreen() {
return titleScreen;
}

public boolean isOnModsButton() {
return modsButton;
}
}

public enum TitleMenuButtonStyle {
@SerializedName("classic")
CLASSIC(),
@SerializedName("replace_realms")
REPLACE_REALMS(),
@SerializedName("shrink")
SHRINK(),
@SerializedName("icon")
ICON();
}

public enum GameMenuButtonStyle {
@SerializedName("replace_bugs")
REPLACE_BUGS,
@SerializedName("below_bugs")
BELOW_BUGS,
@SerializedName("icon")
ICON;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.terraformersmc.modmenu.config.option;

import net.minecraft.client.option.SimpleOption;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.Text;

public class BooleanConfigOption implements OptionConvertable {
private final String key, translationKey;
private final boolean defaultValue;
private final Text enabledText;
private final Text disabledText;

public BooleanConfigOption(String key, boolean defaultValue, String enabledKey, String disabledKey) {
ConfigOptionStorage.setBoolean(key, defaultValue);
this.key = key;
this.translationKey = "";
this.defaultValue = defaultValue;
this.enabledText = Text.translatable(translationKey + "." + enabledKey);
this.disabledText = Text.translatable(translationKey + "." + disabledKey);
}

public BooleanConfigOption(String key, boolean defaultValue) {
this(key, defaultValue, "true", "false");
}

public String getKey() {
return key;
}

public boolean getValue() {
return ConfigOptionStorage.getBoolean(key);
}

public void setValue(boolean value) {
ConfigOptionStorage.setBoolean(key, value);
}

public void toggleValue() {
ConfigOptionStorage.toggleBoolean(key);
}

public boolean getDefaultValue() {
return defaultValue;
}

public Text getButtonText() {
return ScreenTexts.composeGenericOptionText(Text.translatable(translationKey), getValue() ? enabledText : disabledText);
}

@Override
public SimpleOption<Boolean> asOption() {
if (enabledText != null && disabledText != null) {
return new SimpleOption<>(translationKey, SimpleOption.emptyTooltip(),
(text, value) -> value ? enabledText : disabledText, SimpleOption.BOOLEAN, getValue(),
newValue -> ConfigOptionStorage.setBoolean(key, newValue));
}
return SimpleOption.ofBoolean(translationKey, getValue(), (value) -> ConfigOptionStorage.setBoolean(key, value));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.terraformersmc.modmenu.config.option;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ConfigOptionStorage {
private static final Map<String, Boolean> BOOLEAN_OPTIONS = new HashMap<>();
private static final Map<String, Enum<?>> ENUM_OPTIONS = new HashMap<>();
private static final Map<String, Set<String>> STRING_SET_OPTIONS = new HashMap<>();

public static void setStringSet(String key, Set<String> value) {
STRING_SET_OPTIONS.put(key, value);
}

public static Set<String> getStringSet(String key) {
return STRING_SET_OPTIONS.get(key);
}

public static void setBoolean(String key, boolean value) {
BOOLEAN_OPTIONS.put(key, value);
}

public static void toggleBoolean(String key) {
setBoolean(key, !getBoolean(key));
}

public static boolean getBoolean(String key) {
return BOOLEAN_OPTIONS.get(key);
}

@SuppressWarnings("unchecked")
public static <E extends Enum<E>> E getEnum(String key, Class<E> typeClass) {
return (E) ENUM_OPTIONS.get(key);
}

public static Enum<?> getEnumTypeless(String key, Class<Enum<?>> typeClass) {
return ENUM_OPTIONS.get(key);
}

public static <E extends Enum<E>> void setEnum(String key, E value) {
ENUM_OPTIONS.put(key, value);
}

public static void setEnumTypeless(String key, Enum<?> value) {
ENUM_OPTIONS.put(key, value);
}

public static <E extends Enum<E>> E cycleEnum(String key, Class<E> typeClass) {
return cycleEnum(key, typeClass, 1);
}


public static <E extends Enum<E>> E cycleEnum(String key, Class<E> typeClass, int amount) {
E[] values = typeClass.getEnumConstants();
E currentValue = getEnum(key, typeClass);
E newValue = values[(currentValue.ordinal() + amount) % values.length];
setEnum(key, newValue);
return newValue;
}
}
Loading

0 comments on commit 8c1a291

Please sign in to comment.