From d4d1d97c8d77569ff3a786ddf9b75ccea5a6a75c Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 25 Dec 2022 00:28:13 +0800 Subject: [PATCH] Close #1246 --- .../impl/client/config/ConfigManagerImpl.java | 4 +- .../impl/client/config/ConfigObjectImpl.java | 6 +- .../client/config/FavoritesConfigManager.java | 91 +++++++++++++++++++ 3 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/client/config/FavoritesConfigManager.java diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerImpl.java index ce3e6159c..98e00fed1 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerImpl.java @@ -149,9 +149,10 @@ public ConfigManagerImpl() { , (field) -> field.getType() == Map.class, ConfigObjectImpl.UseFilteringCategoriesScreen.class); InternalLogger.getInstance().info("Config loaded"); saveConfig(); + FavoritesConfigManager.getInstance().syncFrom(this); } - private static Jankson buildJankson(Jankson.Builder builder) { + public static Jankson buildJankson(Jankson.Builder builder) { // ResourceLocation builder.registerSerializer(ResourceLocation.class, (location, marshaller) -> { return new JsonPrimitive(location == null ? null : location.toString()); @@ -329,6 +330,7 @@ public void saveConfig() { return InteractionResult.PASS; }); AutoConfig.getConfigHolder(ConfigObjectImpl.class).save(); + FavoritesConfigManager.getInstance().saveConfig(); InternalLogger.getInstance().debug("Config saved"); } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigObjectImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigObjectImpl.java index 905f101d3..ae0d601de 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigObjectImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigObjectImpl.java @@ -408,15 +408,15 @@ public List getFavoriteEntries() { } public List getConfigFavoriteEntries() { - return basics.favorites; + return FavoritesConfigManager.getInstance().getConfig().favorites; } public List getHiddenFavoriteEntries() { - return basics.hiddenFavorites; + return FavoritesConfigManager.getInstance().getConfig().hiddenFavorites; } public List getDisplayHistory() { - return basics.displayHistory; + return FavoritesConfigManager.getInstance().getConfig().displays; } @Override diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/FavoritesConfigManager.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/FavoritesConfigManager.java new file mode 100644 index 000000000..b4a73f007 --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/FavoritesConfigManager.java @@ -0,0 +1,91 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * 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 me.shedaniel.rei.impl.client.config; + +import me.shedaniel.autoconfig.AutoConfig; +import me.shedaniel.autoconfig.ConfigData; +import me.shedaniel.autoconfig.annotation.Config; +import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer; +import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.Jankson; +import me.shedaniel.rei.api.client.favorites.FavoriteEntry; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.InteractionResult; +import org.jetbrains.annotations.ApiStatus; + +import java.util.ArrayList; +import java.util.List; + +import static me.shedaniel.rei.impl.client.config.ConfigManagerImpl.buildJankson; + +@ApiStatus.Internal +public class FavoritesConfigManager { + private static final FavoritesConfigManager INSTANCE = new FavoritesConfigManager(); + private FavoritesConfigObject object; + + public FavoritesConfigManager() { + AutoConfig.register(FavoritesConfigObject.class, (definition, configClass) -> new JanksonConfigSerializer<>(definition, configClass, buildJankson(Jankson.builder()))); + } + + public static FavoritesConfigManager getInstance() { + return INSTANCE; + } + + public void saveConfig() { + AutoConfig.getConfigHolder(FavoritesConfigObject.class).registerLoadListener((configHolder, configObject) -> { + object = configObject; + return InteractionResult.PASS; + }); + AutoConfig.getConfigHolder(FavoritesConfigObject.class).save(); + } + + public FavoritesConfigObject getConfig() { + if (object == null) { + object = AutoConfig.getConfigHolder(FavoritesConfigObject.class).getConfig(); + } + return object; + } + + public void syncFrom(ConfigManagerImpl manager) { + ConfigObjectImpl.Basics basics = manager.getConfig().basics; + FavoritesConfigObject config = this.getConfig(); + config.favorites.addAll(basics.favorites); + config.hiddenFavorites.addAll(basics.hiddenFavorites); + config.displays.addAll(basics.displayHistory); + basics.favorites.clear(); + basics.hiddenFavorites.clear(); + basics.displayHistory.clear(); + manager.saveConfig(); + this.saveConfig(); + } + + @Config(name = "roughlyenoughitems/favorites") + @Environment(EnvType.CLIENT) + public static final class FavoritesConfigObject implements ConfigData { + public List favorites = new ArrayList<>(); + public List hiddenFavorites = new ArrayList<>(); + public List displays = new ArrayList<>(); + } +}