Skip to content

Commit

Permalink
Persist favoritesFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
plusls committed Feb 12, 2022
1 parent e155213 commit 290541f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/main/java/com/plusls/MasaGadget/config/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ public static void loadFromFile() {
ConfigUtils.readConfigBase(root, "malilib", Malilib.OPTIONS);
try {
JsonObject obj = Objects.requireNonNull(JsonUtils.getNestedObject(root, "malilib", true));
JsonObject favoriteObj = Objects.requireNonNull(JsonUtils.getNestedObject(obj, "favorite", true));
JsonObject favoriteObj = Objects.requireNonNull(JsonUtils.getNestedObject(obj, "favorites", true));
for (Map.Entry<String, JsonElement> favoriteEntry : favoriteObj.entrySet()) {
Malilib.FAVORITES.put(favoriteEntry.getKey(), favoriteEntry.getValue().getAsBoolean());
}
Malilib.favoritesFilter = JsonUtils.getBooleanOrDefault(obj, "favoritesFilter", false);
} catch (ClassCastException | IllegalStateException ignored) {

}
Expand Down Expand Up @@ -81,7 +82,8 @@ public static void saveToFile() {
favoriteObj.add(favoriteEntry.getKey(), new JsonPrimitive(true));
}
}
obj.add("favorite", favoriteObj);
obj.add("favorites", favoriteObj);
obj.add("favoritesFilter", new JsonPrimitive(Malilib.favoritesFilter));
ConfigUtils.writeConfigBase(root, "minihud", Minihud.OPTIONS);
ConfigUtils.writeConfigBase(root, "tweakeroo", Tweakeroo.OPTIONS);
root.add("configVersion", new JsonPrimitive(CONFIG_VERSION));
Expand Down Expand Up @@ -187,7 +189,7 @@ public static class Malilib {
SHOW_ORIGINAL_CONFIG_NAME
);
public static final List<IConfigBase> GUI_OPTIONS = new LinkedList<>(OPTIONS);
public static boolean favorites;
public static boolean favoritesFilter;

static {
GUI_OPTIONS.removeIf(iConfigBase -> iConfigBase == FAST_SWITCH_MASA_CONFIG_GUI && !ModInfo.isModLoaded(ModInfo.MODMENU_MOD_ID));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.plusls.MasaGadget.mixin.malilib.favorites;

import com.plusls.MasaGadget.config.Configs;
import com.plusls.MasaGadget.gui.GuiConfigs;
import com.plusls.MasaGadget.gui.MasaGadgetIcons;
import com.plusls.MasaGadget.gui.WidgetIconToggleButton;
import fi.dy.masa.malilib.gui.GuiConfigsBase;
Expand All @@ -21,14 +20,16 @@
@Mixin(value = GuiConfigsBase.class, remap = false)
public abstract class MixinGuiConfigBase extends GuiListBase<GuiConfigsBase.ConfigOptionWrapper, WidgetConfigOption, WidgetListConfigOptions> implements IKeybindConfigGui {
private final WidgetIconToggleButton favoritesButton = new WidgetIconToggleButton(GuiUtils.getScaledWindowWidth() - 175, 13,
MasaGadgetIcons.FAVORITE, Configs.Malilib.favorites,
MasaGadgetIcons.FAVORITE, Configs.Malilib.favoritesFilter,
status -> {
Configs.Malilib.favorites = status;
Configs.Malilib.favoritesFilter = status;
WidgetListConfigOptions widgetListConfigOptions = this.getListWidget();
if (widgetListConfigOptions != null) {
widgetListConfigOptions.getScrollbar().setValue(0);
widgetListConfigOptions.refreshEntries();
}
Configs.saveToFile();
Configs.loadFromFile();
},
status -> status ? I18n.translate("masa_gadget_mod.message.showAllOptions") : I18n.translate("masa_gadget_mod.message.showFavorite"),
widgetIconToggleButton -> Configs.Malilib.FAVORITES_SUPPORT.getBooleanValue());
Expand All @@ -43,7 +44,7 @@ public void postInitGui(CallbackInfo ci) {
}

@Dynamic
@Inject(method = "resize", at = @At(value = "HEAD"))
@Inject(method = "resize", at = @At(value = "RETURN"))
public void favoritesResize(MinecraftClient mc, int width, int height, CallbackInfo callbackInfo) {
favoritesButton.setX(GuiUtils.getScaledWindowWidth() - 175);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MixinWidgetListConfigOptions(int x, int y, int width, int height, int con

@Inject(method = "getEntryStringsForFilter*", at = @At(value = "HEAD"), cancellable = true)
private void filterFavorites(GuiConfigsBase.ConfigOptionWrapper entry, CallbackInfoReturnable<List<String>> cir) {
if (Configs.Malilib.FAVORITES_SUPPORT.getBooleanValue() && Configs.Malilib.favorites) {
if (Configs.Malilib.FAVORITES_SUPPORT.getBooleanValue() && Configs.Malilib.favoritesFilter) {
IConfigBase config = entry.getConfig();
if (config == null || !Configs.Malilib.FAVORITES.getOrDefault(config.getName(), false)) {
cir.setReturnValue(ImmutableList.of(""));
Expand All @@ -34,7 +34,7 @@ private void filterFavorites(GuiConfigsBase.ConfigOptionWrapper entry, CallbackI

@Override
protected void addNonFilteredContents(Collection<GuiConfigsBase.ConfigOptionWrapper> placements) {
if (Configs.Malilib.FAVORITES_SUPPORT.getBooleanValue() && Configs.Malilib.favorites) {
if (Configs.Malilib.FAVORITES_SUPPORT.getBooleanValue() && Configs.Malilib.favoritesFilter) {
for (GuiConfigsBase.ConfigOptionWrapper configWrapper : placements) {
IConfigBase config = configWrapper.getConfig();
if (config != null && Configs.Malilib.FAVORITES.getOrDefault(config.getName(), false)) {
Expand Down

0 comments on commit 290541f

Please sign in to comment.