Skip to content

Commit

Permalink
change favorites data struct
Browse files Browse the repository at this point in the history
  • Loading branch information
plusls committed Feb 12, 2022
1 parent 290541f commit ba08e00
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
17 changes: 12 additions & 5 deletions src/main/java/com/plusls/MasaGadget/config/Configs.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.plusls.MasaGadget.config;

import com.google.common.collect.ImmutableList;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
Expand Down Expand Up @@ -50,7 +51,9 @@ public static void loadFromFile() {
JsonObject obj = Objects.requireNonNull(JsonUtils.getNestedObject(root, "malilib", 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());
HashSet<String> modFavorites = new HashSet<>();
favoriteEntry.getValue().getAsJsonArray().forEach(jsonElement -> modFavorites.add(jsonElement.getAsString()));
Malilib.FAVORITES.put(favoriteEntry.getKey(), modFavorites);
}
Malilib.favoritesFilter = JsonUtils.getBooleanOrDefault(obj, "favoritesFilter", false);
} catch (ClassCastException | IllegalStateException ignored) {
Expand All @@ -77,9 +80,13 @@ public static void saveToFile() {
ConfigUtils.writeConfigBase(root, "malilib", Malilib.OPTIONS);
JsonObject obj = Objects.requireNonNull(JsonUtils.getNestedObject(root, "malilib", true));
JsonObject favoriteObj = new JsonObject();
for (Map.Entry<String, Boolean> favoriteEntry : Malilib.FAVORITES.entrySet()) {
if (favoriteEntry.getValue()) {
favoriteObj.add(favoriteEntry.getKey(), new JsonPrimitive(true));
for (Map.Entry<String, HashSet<String>> favoriteEntry : Malilib.FAVORITES.entrySet()) {
JsonArray modFavoriteObj = new JsonArray();
if (!favoriteEntry.getValue().isEmpty()) {
for (String modFavoriteConfigName: favoriteEntry.getValue()) {
modFavoriteObj.add(modFavoriteConfigName);
}
favoriteObj.add(favoriteEntry.getKey(), modFavoriteObj);
}
}
obj.add("favorites", favoriteObj);
Expand Down Expand Up @@ -178,7 +185,7 @@ public static class Litematica {
}

public static class Malilib {
public static final HashMap<String, Boolean> FAVORITES = new HashMap<>();
public static final HashMap<String, HashSet<String>> FAVORITES = new HashMap<>();
private static final String PREFIX = String.format("%s.config.malilib", ModInfo.MOD_ID);
public static final ConfigBoolean FAST_SWITCH_MASA_CONFIG_GUI = new TranslatableConfigBoolean(PREFIX, "fastSwitchMasaConfigGui", true);
public static final ConfigBoolean FAVORITES_SUPPORT = new TranslatableConfigBoolean(PREFIX, "favoritesSupport", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
import fi.dy.masa.malilib.gui.widgets.WidgetConfigOption;
import fi.dy.masa.malilib.gui.widgets.WidgetConfigOptionBase;
import fi.dy.masa.malilib.gui.widgets.WidgetListConfigOptionsBase;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.resource.language.I18n;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.HashSet;

@Mixin(value = WidgetConfigOption.class, remap = false)
public abstract class MixinWidgetConfigOption extends WidgetConfigOptionBase<GuiConfigsBase.ConfigOptionWrapper> {
public MixinWidgetConfigOption(int x, int y, int width, int height, WidgetListConfigOptionsBase<?, ?> parent, GuiConfigsBase.ConfigOptionWrapper entry, int listIndex) {
Expand All @@ -32,14 +36,22 @@ private void addFavoritesButton(int x, int y, float zLevel, int labelWidth, int
} else if (type == ConfigType.INTEGER || type == ConfigType.DOUBLE) {
configWidth += 18;
}

Screen screen = MinecraftClient.getInstance().currentScreen;
if (!(screen instanceof GuiConfigsBase)) {
return;
}
String modId = ((GuiConfigsBase)screen).getModId();

this.addWidget(new WidgetIconToggleButton(x + configWidth + 15 +
this.getStringWidth(I18n.translate("malilib.gui.button.reset.caps")),
y + 2, MasaGadgetIcons.FAVORITE, Configs.Malilib.FAVORITES.getOrDefault(config.getName(), false),
this.getStringWidth(I18n.translate("malilib.gui.button.reset.caps")), y + 2,
MasaGadgetIcons.FAVORITE, Configs.Malilib.FAVORITES.computeIfAbsent(modId, k -> new HashSet<>()).contains(config.getName()),
status -> {
HashSet<String> modFavorites = Configs.Malilib.FAVORITES.computeIfAbsent(modId, k -> new HashSet<>());
if (status) {
Configs.Malilib.FAVORITES.put(config.getName(), true);
modFavorites.add(config.getName());
} else {
Configs.Malilib.FAVORITES.put(config.getName(), false);
modFavorites.remove(config.getName());
}
Configs.saveToFile();
Configs.loadFromFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import fi.dy.masa.malilib.gui.widgets.WidgetConfigOption;
import fi.dy.masa.malilib.gui.widgets.WidgetListConfigOptions;
import fi.dy.masa.malilib.gui.widgets.WidgetListConfigOptionsBase;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;

@Mixin(value = WidgetListConfigOptions.class, remap = false)
Expand All @@ -26,7 +29,12 @@ public MixinWidgetListConfigOptions(int x, int y, int width, int height, int con
private void filterFavorites(GuiConfigsBase.ConfigOptionWrapper entry, CallbackInfoReturnable<List<String>> cir) {
if (Configs.Malilib.FAVORITES_SUPPORT.getBooleanValue() && Configs.Malilib.favoritesFilter) {
IConfigBase config = entry.getConfig();
if (config == null || !Configs.Malilib.FAVORITES.getOrDefault(config.getName(), false)) {
Screen screen = MinecraftClient.getInstance().currentScreen;
if (!(screen instanceof GuiConfigsBase)) {
return;
}
String modId = ((GuiConfigsBase)screen).getModId();
if (config == null || !Configs.Malilib.FAVORITES.computeIfAbsent(modId, k -> new HashSet<>()).contains(config.getName())) {
cir.setReturnValue(ImmutableList.of(""));
}
}
Expand All @@ -35,9 +43,14 @@ private void filterFavorites(GuiConfigsBase.ConfigOptionWrapper entry, CallbackI
@Override
protected void addNonFilteredContents(Collection<GuiConfigsBase.ConfigOptionWrapper> placements) {
if (Configs.Malilib.FAVORITES_SUPPORT.getBooleanValue() && Configs.Malilib.favoritesFilter) {
Screen screen = MinecraftClient.getInstance().currentScreen;
if (!(screen instanceof GuiConfigsBase)) {
return;
}
String modId = ((GuiConfigsBase)screen).getModId();
for (GuiConfigsBase.ConfigOptionWrapper configWrapper : placements) {
IConfigBase config = configWrapper.getConfig();
if (config != null && Configs.Malilib.FAVORITES.getOrDefault(config.getName(), false)) {
if (config != null && Configs.Malilib.FAVORITES.computeIfAbsent(modId, k -> new HashSet<>()).contains(config.getName())) {
this.listContents.add(configWrapper);
}
}
Expand Down

0 comments on commit ba08e00

Please sign in to comment.