diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-10-28 20:03:11 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2023-05-29 21:17:52 +0800 |
| commit | e7bae71713b93ce5cdf200e4e76c4ede875150ec (patch) | |
| tree | 80a99a56f8a7cfbd6a65e107361e828c85578970 /runtime/src/main/java | |
| parent | 00593a71de5082f3ff6a57e4c819c72f78735e5f (diff) | |
| download | RoughlyEnoughItems-e7bae71713b93ce5cdf200e4e76c4ede875150ec.tar.gz RoughlyEnoughItems-e7bae71713b93ce5cdf200e4e76c4ede875150ec.tar.bz2 RoughlyEnoughItems-e7bae71713b93ce5cdf200e4e76c4ede875150ec.zip | |
Add filtering rules to the API (#1169)
Diffstat (limited to 'runtime/src/main/java')
23 files changed, 633 insertions, 531 deletions
diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java index d2469c67e..6ea8997fc 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java @@ -35,6 +35,7 @@ import me.shedaniel.architectury.platform.Platform; import me.shedaniel.math.Point; import me.shedaniel.rei.api.client.REIRuntime; import me.shedaniel.rei.api.client.config.ConfigObject; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRuleTypeRegistry; import me.shedaniel.rei.api.client.entry.renderer.EntryRenderer; import me.shedaniel.rei.api.client.favorites.FavoriteEntry; import me.shedaniel.rei.api.client.favorites.FavoriteEntryType; @@ -59,6 +60,7 @@ import me.shedaniel.rei.impl.ClientInternals; import me.shedaniel.rei.impl.client.REIRuntimeImpl; import me.shedaniel.rei.impl.client.config.ConfigManagerImpl; import me.shedaniel.rei.impl.client.config.addon.ConfigAddonRegistryImpl; +import me.shedaniel.rei.impl.client.entry.filtering.rules.FilteringRuleTypeRegistryImpl; import me.shedaniel.rei.impl.client.entry.renderer.EntryRendererRegistryImpl; import me.shedaniel.rei.impl.client.favorites.DelegatingFavoriteEntryProviderImpl; import me.shedaniel.rei.impl.client.favorites.FavoriteEntryTypeRegistryImpl; @@ -203,6 +205,7 @@ public class RoughlyEnoughItemsCoreClient { return categories.stream(); } }, "clickAreaHandlerResult"); + ClientInternals.attachInstanceSupplier(new FilteringRuleTypeRegistryImpl(), "filteringRuleTypeRegistry"); ClientInternals.attachInstanceSupplier(new PluginManagerImpl<>( REIClientPlugin.class, view -> view.then(PluginView.getInstance()), @@ -213,6 +216,7 @@ public class RoughlyEnoughItemsCoreClient { new ConfigManagerImpl(), new EntryRegistryImpl(), new CollapsibleEntryRegistryImpl(), + FilteringRuleTypeRegistry.getInstance().basic(), new CategoryRegistryImpl(), new DisplayRegistryImpl(), new ScreenRegistryImpl(), 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 7a105d0a6..41db7c7ed 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 @@ -46,11 +46,13 @@ import me.shedaniel.clothconfig2.gui.AbstractConfigScreen; import me.shedaniel.clothconfig2.gui.GlobalizedClothConfigScreen; import me.shedaniel.clothconfig2.gui.entries.KeyCodeEntry; import me.shedaniel.clothconfig2.gui.entries.TextListEntry; -import me.shedaniel.rei.RoughlyEnoughItemsCore; import me.shedaniel.rei.api.client.REIRuntime; import me.shedaniel.rei.api.client.config.ConfigManager; import me.shedaniel.rei.api.client.config.addon.ConfigAddonRegistry; import me.shedaniel.rei.api.client.config.entry.EntryStackProvider; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRule; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRuleType; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRuleTypeRegistry; import me.shedaniel.rei.api.client.favorites.FavoriteEntry; import me.shedaniel.rei.api.client.gui.config.CheatingMode; import me.shedaniel.rei.api.client.gui.config.DisplayScreenType; @@ -63,8 +65,6 @@ import me.shedaniel.rei.api.common.util.ImmutableTextComponent; import me.shedaniel.rei.impl.client.REIRuntimeImpl; import me.shedaniel.rei.impl.client.config.addon.ConfigAddonRegistryImpl; import me.shedaniel.rei.impl.client.config.entries.*; -import me.shedaniel.rei.impl.client.entry.filtering.FilteringRule; -import me.shedaniel.rei.impl.client.entry.filtering.rules.ManualFilteringRule; import me.shedaniel.rei.impl.client.gui.ScreenOverlayImpl; import me.shedaniel.rei.impl.client.gui.credits.CreditsScreen; import me.shedaniel.rei.impl.client.gui.performance.entry.PerformanceEntry; @@ -234,7 +234,7 @@ public class ConfigManagerImpl implements ConfigManager { // FilteringRule builder.registerSerializer(FilteringRule.class, (value, marshaller) -> { try { - return marshaller.serialize(FilteringRule.save(value, new CompoundTag())); + return marshaller.serialize(FilteringRuleType.save(value, new CompoundTag())); } catch (Exception e) { e.printStackTrace(); return JsonNull.INSTANCE; @@ -242,7 +242,7 @@ public class ConfigManagerImpl implements ConfigManager { }); builder.registerDeserializer(Tag.class, FilteringRule.class, (value, marshaller) -> { try { - return FilteringRule.read((CompoundTag) value); + return FilteringRuleType.read((CompoundTag) value); } catch (Exception e) { e.printStackTrace(); return null; @@ -250,7 +250,7 @@ public class ConfigManagerImpl implements ConfigManager { }); builder.registerDeserializer(String.class, FilteringRule.class, (value, marshaller) -> { try { - return FilteringRule.read(TagParser.parseTag(value)); + return FilteringRuleType.read(TagParser.parseTag(value)); } catch (Exception e) { e.printStackTrace(); return null; @@ -292,8 +292,10 @@ public class ConfigManagerImpl implements ConfigManager { @Override public void saveConfig() { - if (getConfig().getFilteringRules().stream().noneMatch(filteringRule -> filteringRule instanceof ManualFilteringRule)) { - getConfig().getFilteringRules().add(new ManualFilteringRule()); + for (FilteringRuleType<?> type : FilteringRuleTypeRegistry.getInstance()) { + if (type.isSingular() && getConfig().getFilteringRules().stream().noneMatch(filteringRule -> filteringRule.getType().equals(type))) { + getConfig().getFilteringRules().add(type.createNew()); + } } AutoConfig.getConfigHolder(ConfigObjectImpl.class).registerLoadListener((configHolder, configObject) -> { object = configObject; 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 13b9d06dd..b92f89b4a 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 @@ -33,6 +33,7 @@ import me.shedaniel.clothconfig2.api.Modifier; import me.shedaniel.clothconfig2.api.ModifierKeyCode; import me.shedaniel.rei.api.client.config.ConfigObject; import me.shedaniel.rei.api.client.config.entry.EntryStackProvider; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRule; import me.shedaniel.rei.api.client.favorites.FavoriteEntry; import me.shedaniel.rei.api.client.gui.config.*; import me.shedaniel.rei.api.common.entry.EntryStack; diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringAddRuleScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringAddRuleScreen.java index 212e46c38..950e0c5e0 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringAddRuleScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringAddRuleScreen.java @@ -25,8 +25,9 @@ package me.shedaniel.rei.impl.client.config.entries; import com.mojang.blaze3d.vertex.PoseStack; import me.shedaniel.clothconfig2.gui.widget.DynamicElementListWidget; -import me.shedaniel.rei.impl.client.entry.filtering.FilteringRule; -import me.shedaniel.rei.impl.client.entry.filtering.rules.ManualFilteringRule; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRule; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRuleType; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRuleTypeRegistry; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.components.Button; import net.minecraft.client.gui.components.events.GuiEventListener; @@ -41,7 +42,7 @@ import net.minecraft.resources.ResourceLocation; import java.util.Collections; import java.util.List; import java.util.Objects; -import java.util.function.BiFunction; +import java.util.function.Function; public class FilteringAddRuleScreen extends Screen { private final FilteringEntry entry; @@ -64,8 +65,8 @@ public class FilteringAddRuleScreen extends Screen { })); } rulesList = addWidget(new RulesList(minecraft, width, height, 30, height, BACKGROUND_LOCATION)); - for (FilteringRule<?> rule : FilteringRule.REGISTRY) { - if (!(rule instanceof ManualFilteringRule)) + for (FilteringRuleType<?> rule : FilteringRuleTypeRegistry.getInstance()) { + if (!rule.isSingular()) rulesList.addItem(new DefaultRuleEntry(parent, entry, rule.createNew(), null)); } rulesList.selectItem(rulesList.children().get(0)); @@ -146,14 +147,14 @@ public class FilteringAddRuleScreen extends Screen { public static class DefaultRuleEntry extends RuleEntry { private final Button addButton; - private final BiFunction<FilteringEntry, Screen, Screen> screenFunction; + private final Function<Screen, Screen> screenFunction; - public DefaultRuleEntry(Screen parent, FilteringEntry entry, FilteringRule<?> rule, BiFunction<FilteringEntry, Screen, Screen> screenFunction) { + public DefaultRuleEntry(Screen parent, FilteringEntry entry, FilteringRule<?> rule, Function<Screen, Screen> screenFunction) { super(rule); - this.screenFunction = (screenFunction == null ? rule.createEntryScreen().orElse(null) : screenFunction); + this.screenFunction = (screenFunction == null ? ((FilteringRuleType<FilteringRule<?>>) rule.getType()).createEntryScreen(rule) : screenFunction); addButton = new Button(0, 0, 20, 20, Component.nullToEmpty(" + "), button -> { entry.edited = true; - Minecraft.getInstance().setScreen(this.screenFunction.apply(entry, parent)); + Minecraft.getInstance().setScreen(this.screenFunction.apply(parent)); entry.rules.add(0, rule); }); addButton.active = this.screenFunction != null; @@ -163,7 +164,7 @@ public class FilteringAddRuleScreen extends Screen { public void render(PoseStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isHovered, float delta) { Minecraft client = Minecraft.getInstance(); { - Component title = getRule().getTitle(); + Component title = ((FilteringRuleType<FilteringRule<?>>) getRule().getType()).getTitle(getRule()); int i = client.font.width(title); if (i > entryWidth - 28) { FormattedText titleTrimmed = FormattedText.composite(client.font.substrByWidth(title, entryWidth - 28 - client.font.width("...")), FormattedText.of("...")); @@ -173,7 +174,7 @@ public class FilteringAddRuleScreen extends Screen { } } { - Component subtitle = getRule().getSubtitle(); + Component subtitle = ((FilteringRuleType<FilteringRule<?>>) getRule().getType()).getSubtitle(getRule()); int i = client.font.width(subtitle); if (i > entryWidth - 28) { FormattedText subtitleTrimmed = FormattedText.composite(client.font.substrByWidth(subtitle, entryWidth - 28 - client.font.width("...")), FormattedText.of("...")); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringEntry.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringEntry.java index 54544f9cf..6aea3afd5 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringEntry.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringEntry.java @@ -28,9 +28,9 @@ import com.google.common.collect.Lists; import com.mojang.blaze3d.platform.Window; import com.mojang.blaze3d.vertex.PoseStack; import me.shedaniel.clothconfig2.api.AbstractConfigListEntry; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRule; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.util.EntryStacks; -import me.shedaniel.rei.impl.client.entry.filtering.FilteringRule; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.chat.NarratorChatListener; import net.minecraft.client.gui.components.AbstractWidget; diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringRuleOptionsScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringRuleOptionsScreen.java index 391032391..9d4b0757c 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringRuleOptionsScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringRuleOptionsScreen.java @@ -27,7 +27,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; import me.shedaniel.clothconfig2.gui.widget.DynamicElementListWidget; import me.shedaniel.math.Rectangle; -import me.shedaniel.rei.impl.client.entry.filtering.FilteringRule; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRule; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.components.Button; import net.minecraft.client.gui.components.EditBox; @@ -50,14 +50,12 @@ import java.util.function.Function; import java.util.function.Supplier; public abstract class FilteringRuleOptionsScreen<T extends FilteringRule<?>> extends Screen { - private final FilteringEntry entry; private RulesList rulesList; Screen parent; public T rule; - public FilteringRuleOptionsScreen(FilteringEntry entry, T rule, Screen screen) { + public FilteringRuleOptionsScreen(T rule, Screen screen) { super(new TranslatableComponent("config.roughlyenoughitems.filteringRulesScreen")); - this.entry = entry; this.rule = rule; this.parent = screen; } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringRulesScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringRulesScreen.java index ef439a005..300ba851a 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringRulesScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/entries/FilteringRulesScreen.java @@ -25,7 +25,8 @@ package me.shedaniel.rei.impl.client.config.entries; import com.mojang.blaze3d.vertex.PoseStack; import me.shedaniel.clothconfig2.gui.widget.DynamicElementListWidget; -import me.shedaniel.rei.impl.client.entry.filtering.FilteringRule; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRule; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRuleType; import me.shedaniel.rei.impl.client.entry.filtering.rules.ManualFilteringRule; import me.shedaniel.rei.impl.client.gui.InternalTextures; import net.minecraft.client.Minecraft; @@ -44,7 +45,7 @@ import net.minecraft.sounds.SoundEvents; import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.function.BiFunction; +import java.util.function.Function; public class FilteringRulesScreen extends Screen { private final FilteringEntry entry; @@ -78,7 +79,7 @@ public class FilteringRulesScreen extends Screen { for (int i = entry.rules.size() - 1; i >= 0; i--) { FilteringRule<?> rule = entry.rules.get(i); if (rule instanceof ManualFilteringRule) - rulesList.addItem(new DefaultRuleEntry(rule, entry, (entry, screen) -> { + rulesList.addItem(new DefaultRuleEntry(rule, entry, (screen) -> { entry.filteringScreen.parent = screen; return entry.filteringScreen; })); @@ -178,14 +179,14 @@ public class FilteringRulesScreen extends Screen { public static class DefaultRuleEntry extends RuleEntry { private final Button configureButton; private final Button deleteButton; - private final BiFunction<FilteringEntry, Screen, Screen> screenFunction; + private final Function<Screen, Screen> screenFunction; - public DefaultRuleEntry(FilteringRule<?> rule, FilteringEntry entry, BiFunction<FilteringEntry, Screen, Screen> screenFunction) { + public DefaultRuleEntry(FilteringRule<?> rule, FilteringEntry entry, Function<Screen, Screen> screenFunction) { super(rule); - this.screenFunction = (screenFunction == null ? rule.createEntryScreen().orElse(null) : screenFunction); + this.screenFunction = (screenFunction == null ? ((FilteringRuleType<FilteringRule<?>>) rule.getType()).createEntryScreen(rule) : screenFunction); configureButton = new Button(0, 0, 20, 20, Component.nullToEmpty(null), button -> { entry.edited = true; - Minecraft.getInstance().setScreen(this.screenFunction.apply(entry, Minecraft.getInstance().screen)); + Minecraft.getInstance().setScreen(this.screenFunction.apply(Minecraft.getInstance().screen)); }) { @Override protected void renderBg(PoseStack matrices, Minecraft client, int mouseX, int mouseY) { @@ -204,14 +205,14 @@ public class FilteringRulesScreen extends Screen { }); } configureButton.active = this.screenFunction != null; - deleteButton.active = !(rule instanceof ManualFilteringRule); + deleteButton.active = !rule.getType().isSingular(); } @Override public void render(PoseStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isHovered, float delta) { Minecraft client = Minecraft.getInstance(); { - Component title = getRule().getTitle(); + Component title = ((FilteringRuleType<FilteringRule<?>>) getRule().getType()).getTitle(getRule()); int i = client.font.width(title); if (i > entryWidth - 28) { FormattedText titleTrimmed = FormattedText.composite(client.font.substrByWidth(title, entryWidth - 28 - client.font.width("...")), FormattedText.of("...")); @@ -221,7 +222,7 @@ public class FilteringRulesScreen extends Screen { } } { - Component subtitle = getRule().getSubtitle(); + Component subtitle = ((FilteringRuleType<FilteringRule<?>>) getRule().getType()).getSubtitle(getRule()); int i = client.font.width(subtitle); if (i > entryWidth - 28) { FormattedText subtitleTrimmed = FormattedText.composite(client.font.substrByWidth(subtitle, entryWidth - 28 - client.font.width("...")), FormattedText.of("...")); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/entry/filtering/FilteringCache.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/entry/filtering/FilteringCache.java deleted file mode 100644 index 388652210..000000000 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/entry/filtering/FilteringCache.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.entry.filtering; - -import org.jetbrains.annotations.Nullable; - -public interface FilteringCache { - @Nullable - Object getCache(FilteringRule<?> rule); -} diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/entry/filtering/FilteringContext.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/entry/filtering/FilteringContext.java deleted file mode 100644 index 59b2ce647..000000000 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/entry/filtering/FilteringContext.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.entry.filtering; - -import me.shedaniel.rei.api.common.entry.EntryStack; -import org.jetbrains.annotations.ApiStatus; - -import java.util.Collection; - -@ApiStatus.Internal -@ApiStatus.Experimental -public interface FilteringContext { - Collection<EntryStack<?>> getStacks(FilteringContextType type); - - default Collection<EntryStack<?>> getShownStacks() { - return getStacks(FilteringContextType.SHOWN); - } - - default Collection<EntryStack<?>> getUnsetStacks() { - return getStacks(FilteringContextType.DEFAULT); - } - - default Collection<EntryStack<?>> getHiddenStacks() { - return getStacks(FilteringContextType.HIDDEN); - } -} diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/entry/filtering/FilteringContextImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/entry/filtering/FilteringContextImpl.java index e46f8660b..d0e26a9fc 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/entry/filtering/FilteringContextImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/entry/filtering/FilteringContextImpl.java @@ -23,19 +23,18 @@ package me.shedaniel.rei.impl.client.entry.filtering; +import com.google.common.collect.Iterators; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import me.shedaniel.rei.api.client.entry.filtering.FilteringContext; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.impl.common.util.HashedEntryStackWrapper; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -45,7 +44,6 @@ import java.util.concurrent.TimeoutException; public class FilteringContextImpl implements FilteringContext { private final boolean async; public final Map<FilteringContextType, Set<HashedEntryStackWrapper>> stacks; - private final Map<FilteringContextType, Collection<EntryStack<?>>> cachedStacks; public FilteringContextImpl(Collection<EntryStack<?>> allStacks) { this(true, allStacks); @@ -54,40 +52,55 @@ public class FilteringContextImpl implements FilteringContext { public FilteringContextImpl(boolean async, Collection<EntryStack<?>> allStacks) { this.async = async; this.stacks = Maps.newHashMap(); - this.cachedStacks = Maps.newHashMap(); for (FilteringContextType type : FilteringContextType.values()) { this.stacks.computeIfAbsent(type, t -> Sets.newHashSet()); } this.stacks.get(FilteringContextType.DEFAULT).addAll(async ? CollectionUtils.mapParallel(allStacks, HashedEntryStackWrapper::new) : CollectionUtils.map(allStacks, HashedEntryStackWrapper::new)); - fillCache(); } public FilteringContextImpl(Map<FilteringContextType, Set<HashedEntryStackWrapper>> stacks) { this.async = false; this.stacks = stacks; - this.cachedStacks = Maps.newHashMap(); for (FilteringContextType type : FilteringContextType.values()) { this.stacks.computeIfAbsent(type, t -> Sets.newHashSet()); } - fillCache(); } - private void fillCache() { - this.cachedStacks.clear(); - for (FilteringContextType type : FilteringContextType.values()) { - this.cachedStacks.put(type, CollectionUtils.map(stacks.get(type), HashedEntryStackWrapper::unwrap)); - } + @Override + public Collection<EntryStack<?>> getHiddenStacks() { + return getPublicFacing(FilteringContextType.HIDDEN); + } + + @Override + public Collection<EntryStack<?>> getShownStacks() { + return getPublicFacing(FilteringContextType.SHOWN); } @Override - public Collection<EntryStack<?>> getStacks(FilteringContextType type) { - return cachedStacks.get(type); + public Collection<EntryStack<?>> getUnsetStacks() { + return getPublicFacing(FilteringContextType.DEFAULT); } - public void handleResult(FilteringResult result) { - Collection<HashedEntryStackWrapper> hiddenStacks = result.getHiddenStacks(); - Collection<HashedEntryStackWrapper> shownStacks = result.getShownStacks(); + private Collection<EntryStack<?>> getPublicFacing(FilteringContextType type) { + Set<HashedEntryStackWrapper> wrappers = this.stacks.get(type); + if (wrappers == null || wrappers.isEmpty()) return List.of(); + return new AbstractSet<>() { + @Override + public Iterator<EntryStack<?>> iterator() { + return Iterators.transform(wrappers.iterator(), HashedEntryStackWrapper::unwrap); + } + + @Override + public int size() { |
