From 85ab4c61276f249f27b42a794167f2831ca7656a Mon Sep 17 00:00:00 2001 From: shedaniel Date: Fri, 28 Oct 2022 20:03:11 +0800 Subject: Add filtering rules to the API (#1169) --- .../client/entry/filtering/FilteringContext.java | 56 ++++++++ .../client/entry/filtering/FilteringResult.java | 43 ++++++ .../entry/filtering/FilteringResultFactory.java | 29 ++++ .../api/client/entry/filtering/FilteringRule.java | 66 +++++++++ .../client/entry/filtering/FilteringRuleType.java | 148 +++++++++++++++++++++ .../entry/filtering/FilteringRuleTypeRegistry.java | 77 +++++++++++ .../entry/filtering/base/BasicFilteringRule.java | 44 ++++++ .../rei/api/client/plugins/REIClientPlugin.java | 10 ++ .../me/shedaniel/rei/impl/ClientInternals.java | 6 + 9 files changed, 479 insertions(+) create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringContext.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringResult.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringResultFactory.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRule.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRuleType.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRuleTypeRegistry.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/base/BasicFilteringRule.java (limited to 'api/src/main/java/me/shedaniel') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringContext.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringContext.java new file mode 100644 index 000000000..d46709af9 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringContext.java @@ -0,0 +1,56 @@ +/* + * 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.api.client.entry.filtering; + +import me.shedaniel.rei.api.common.entry.EntryStack; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import org.jetbrains.annotations.ApiStatus; + +import java.util.Collection; + +@ApiStatus.Experimental +@Environment(EnvType.CLIENT) +public interface FilteringContext { + /** + * Returns the list of stacks that are previously marked as shown from other filtering rules. + * + * @return the list of stacks that are previously marked as shown from other filtering rules. + */ + Collection> getShownStacks(); + + /** + * Returns the list of stacks that have not been processed by any filtering rules. + * + * @return the list of stacks that have not been processed by any filtering rules. + */ + Collection> getUnsetStacks(); + + /** + * Returns the list of stacks that are previously marked as hidden from other filtering rules. + * + * @return the list of stacks that are previously marked as hidden from other filtering rules. + */ + Collection> getHiddenStacks(); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringResult.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringResult.java new file mode 100644 index 000000000..4d4392ae9 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringResult.java @@ -0,0 +1,43 @@ +/* + * 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.api.client.entry.filtering; + +import me.shedaniel.rei.api.common.entry.EntryStack; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import org.jetbrains.annotations.ApiStatus; + +import java.util.Collection; + +@Environment(EnvType.CLIENT) +@ApiStatus.Experimental +public interface FilteringResult { + FilteringResult hide(EntryStack stack); + + FilteringResult hide(Collection> stacks); + + FilteringResult show(EntryStack stack); + + FilteringResult show(Collection> stacks); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringResultFactory.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringResultFactory.java new file mode 100644 index 000000000..fa391362a --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringResultFactory.java @@ -0,0 +1,29 @@ +/* + * 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.api.client.entry.filtering; + +@FunctionalInterface +public interface FilteringResultFactory { + FilteringResult create(); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRule.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRule.java new file mode 100644 index 000000000..1f3cb5b50 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRule.java @@ -0,0 +1,66 @@ +/* + * 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.api.client.entry.filtering; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import org.jetbrains.annotations.ApiStatus; + +/** + * A filtering rule type that is used to filter the entries on the entry panel, + * dictate what shows up in slots, or hide the entire display if all ingredients are filtered. + * + * @param the type of the cache + */ +@ApiStatus.Experimental +@Environment(EnvType.CLIENT) +public interface FilteringRule { + /** + * Returns the type of this filtering rule. + * + * @return the type of this filtering rule + */ + FilteringRuleType> getType(); + + /** + * Prepares the cache for this filtering rule. + * + * @param async whether the cache should be prepared asynchronously + * @return the cache + */ + default Cache prepareCache(boolean async) { + return null; + } + + /** + * Processes the specified entry with the specified cache. + * + * @param context the context of this filtering + * @param resultFactory the result factory + * @param cache the cache + * @param async whether the stacks should be processed asynchronously + * @return the result of the processing + */ + FilteringResult processFilteredStacks(FilteringContext context, FilteringResultFactory resultFactory, Cache cache, boolean async); +} \ No newline at end of file diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRuleType.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRuleType.java new file mode 100644 index 000000000..7891ebbc5 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRuleType.java @@ -0,0 +1,148 @@ +/* + * 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.api.client.entry.filtering; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +import java.util.Objects; +import java.util.function.Function; + +/** + * A type of filtering rule. A filtering rule will filter the entries on the entry panel, + * dictate what shows up in slots, or hide the entire display if all ingredients are filtered. + * + * @param the type of the filtering rule + */ +@ApiStatus.Experimental +@Environment(EnvType.CLIENT) +public interface FilteringRuleType> { + /** + * Serializes the specified filtering rule to a compound tag. + * + * @param rule the filtering rule + * @param tag the compound tag + * @return the serialized compound tag + */ + static CompoundTag save(FilteringRule rule, CompoundTag tag) { + tag.putString("id", rule.getType().getId().toString()); + tag.put("rule", ((FilteringRuleType>) rule.getType()).saveTo(rule, new CompoundTag())); + return tag; + } + + /** + * Serializes the specified filtering rule to a compound tag. + * + * @param rule the filtering rule + * @param tag the compound tag + * @return the serialized compound tag + */ + CompoundTag saveTo(T rule, CompoundTag tag); + + /** + * Deserializes the specified compound tag to a filtering rule. + * + * @param tag the compound tag + * @return the deserialized filtering rule + */ + @Nullable + static FilteringRule read(CompoundTag tag) { + FilteringRuleType type = FilteringRuleTypeRegistry.getInstance().get(ResourceLocation.tryParse(tag.getString("id"))); + if (type == null) return null; + return type.readFrom(tag.getCompound("rule")); + } + + /** + * Deserializes the specified compound tag to a filtering rule. + * + * @param tag the compound tag + * @return the deserialized filtering rule + */ + T readFrom(CompoundTag tag); + + /** + * Returns a function to create the configuration screen for this filtering rule type. + * The parent of the newly created screen is passed as the argument of the function. + *

+ * If the function returns {@code null}, the filtering rule will not be configurable + * graphically. + * + * @param rule the filtering rule + * @return the screen function, or {@code null} if the filtering rule is not configurable + */ + @Nullable + default Function createEntryScreen(T rule) { + return null; + } + + /** + * Returns the name of the filtering rule. + * + * @param rule the filtering rule + * @return the name of the filtering rule + */ + default Component getTitle(T rule) { + return Component.nullToEmpty(getId().toString()); + } + + /** + * Returns the description of the filtering rule. + * + * @param rule the filtering rule + * @return the description of the filtering rule + */ + default Component getSubtitle(T rule) { + return Component.nullToEmpty(null); + } + + /** + * Returns the id of the filtering rule type. + * + * @return the id of the filtering rule type + */ + default ResourceLocation getId() { + return Objects.requireNonNull(FilteringRuleTypeRegistry.getInstance().getId(this), "Id of " + this); + } + + /** + * Constructs a new filtering rule of this type. + * + * @return the new filtering rule + */ + T createNew(); + + /** + * Returns whether this filtering rule type is always enforced, + * and only one filtering rule of this type can be present in a filtering rule list. + * + * @return whether this filtering rule type is always enforced + */ + boolean isSingular(); +} \ No newline at end of file diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRuleTypeRegistry.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRuleTypeRegistry.java new file mode 100644 index 000000000..16638a828 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/FilteringRuleTypeRegistry.java @@ -0,0 +1,77 @@ +/* + * 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.api.client.entry.filtering; + +import me.shedaniel.rei.api.client.entry.filtering.base.BasicFilteringRule; +import me.shedaniel.rei.impl.ClientInternals; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +@ApiStatus.Experimental +@Environment(EnvType.CLIENT) +public interface FilteringRuleTypeRegistry extends List> { + static FilteringRuleTypeRegistry getInstance() { + return ClientInternals.getFilteringRuleTypeRegistry(); + } + + /** + * Returns the filtering rule type with the specified id, or {@code null} if none exists. + * + * @param id the id of the filtering rule type + * @return the filtering rule type with the specified id, or {@code null} if none exists + */ + @Nullable + FilteringRuleType get(ResourceLocation id); + + /** + * Returns the id of the specified filtering rule type, or {@code null} if none exists. + * + * @param rule the filtering rule type + * @return the id of the specified filtering rule type, or {@code null} if none exists + */ + @Nullable + ResourceLocation getId(FilteringRuleType rule); + + /** + * Registers the specified filtering rule type. + * + * @param id the id of the filtering rule type + * @param rule the filtering rule type + */ + void register(ResourceLocation id, FilteringRuleType rule); + + /** + * Returns the basic filtering rule that can be used to filter entries, + * without registering a new filtering rule type, for external + * plugins. + * + * @return the base filtering rule + */ + BasicFilteringRule basic(); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/base/BasicFilteringRule.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/base/BasicFilteringRule.java new file mode 100644 index 000000000..26b2986c2 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/filtering/base/BasicFilteringRule.java @@ -0,0 +1,44 @@ +/* + * 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.api.client.entry.filtering.base; + +import me.shedaniel.rei.api.client.entry.filtering.FilteringResult; +import me.shedaniel.rei.api.client.entry.filtering.FilteringRule; +import me.shedaniel.rei.api.client.plugins.REIClientPlugin; +import me.shedaniel.rei.api.common.registry.Reloadable; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import org.jetbrains.annotations.ApiStatus; + +/** + * The basic filtering rule that can be used to filter entries, + * without registering a new filtering rule type, for external + * plugins. + * + * @param the cache type + */ +@ApiStatus.Experimental +@Environment(EnvType.CLIENT) +public interface BasicFilteringRule extends Reloadable, FilteringRule, FilteringResult { +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java b/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java index deeca9ff3..c8045aff6 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java @@ -24,6 +24,7 @@ package me.shedaniel.rei.api.client.plugins; import me.shedaniel.rei.api.client.config.addon.ConfigAddonRegistry; +import me.shedaniel.rei.api.client.entry.filtering.base.BasicFilteringRule; import me.shedaniel.rei.api.client.entry.renderer.EntryRendererRegistry; import me.shedaniel.rei.api.client.favorites.FavoriteEntryType; import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; @@ -97,6 +98,15 @@ public interface REIClientPlugin extends REIPlugin { default void registerEntries(EntryRegistry registry) { } + /** + * Registers basic entry filtering. + * + * @param rule the filtering rule + */ + @ApiStatus.OverrideOnly + default void registerBasicEntryFiltering(BasicFilteringRule rule) { + } + /** * Registers entries to collapse on the entry panel. * diff --git a/api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java b/api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java index 36456399b..2f4a8f45c 100644 --- a/api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java +++ b/api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java @@ -28,6 +28,7 @@ import com.mojang.serialization.DataResult; import me.shedaniel.math.Point; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.client.ClientHelper; +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.gui.DrawableConsumer; @@ -67,6 +68,7 @@ public final class ClientInternals { private static Supplier viewSearchBuilder = ClientInternals::throwNotSetup; private static Supplier> clientPluginManager = ClientInternals::throwNotSetup; private static Supplier> emptyEntryRenderer = ClientInternals::throwNotSetup; + private static Supplier filteringRuleTypeRegistry = ClientInternals::throwNotSetup; private static BiFunction>, Supplier, FavoriteEntry> delegateFavoriteEntry = (supplier, toJson) -> throwNotSetup(); private static Function> favoriteEntryFromJson = (object) -> throwNotSetup(); private static Function clickAreaHandlerResult = (result) -> throwNotSetup(); @@ -167,6 +169,10 @@ public final class ClientInternals { return missingTooltip.apply(stacks); } + public static FilteringRuleTypeRegistry getFilteringRuleTypeRegistry() { + return filteringRuleTypeRegistry.get(); + } + @Environment(EnvType.CLIENT) public interface WidgetsProvider { boolean isRenderingPanel(Panel panel); -- cgit From 8d1c2c2e5e6e513715101f5eabc39dff8fb4a35c Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 30 Oct 2022 16:29:29 +0800 Subject: Fix SearchFilteringRule --- .../rei/api/common/util/CollectionUtils.java | 69 +++++++++++++++++++++- 1 file changed, 67 insertions(+), 2 deletions(-) (limited to 'api/src/main/java/me/shedaniel') diff --git a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java index c80e0dc25..67894fb27 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java @@ -23,6 +23,7 @@ package me.shedaniel.rei.api.common.util; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.google.common.collect.UnmodifiableIterator; @@ -361,7 +362,7 @@ public class CollectionUtils { } public static Iterable> partition(List list, int size) { - return () -> new UnmodifiableIterator>() { + return () -> new UnmodifiableIterator<>() { int i = 0; int partitionSize = Mth.ceil(list.size() / (float) size); @@ -374,7 +375,7 @@ public class CollectionUtils { public List next() { int cursor = i++ * size; int realSize = Math.min(list.size() - cursor, size); - return new AbstractList() { + return new AbstractList<>() { @Override public T get(int index) { if (index < 0 || index >= realSize) @@ -414,6 +415,70 @@ public class CollectionUtils { }; } + public static Iterable> partitionIterator(Iterator iterator, int iteratorSize, int size) { + return partitionCollection(new AbstractCollection<>() { + + @Override + public Iterator iterator() { + return iterator; + } + + @Override + public int size() { + return iteratorSize; + } + }, size); + } + + public static Iterable> partitionCollection(Collection collection, int size) { + if (collection instanceof List) { + return Iterables.transform(partition((List) collection, size), List::iterator); + } + + return () -> new Iterator<>() { + int i = 0; + int partitionSize = Mth.ceil(collection.size() / (float) size); + int advanced = 0; + Iterator iterator = collection.iterator(); + + @Override + public boolean hasNext() { + return i < partitionSize; + } + + @Override + public Iterator next() { + int cursor = i++ * size; + int realSize = Math.min(collection.size() - cursor, size); + + if (advanced < cursor) { + for (int j = 0; j < cursor - advanced; j++) { + if (iterator.hasNext()) { + iterator.next(); + } else { + advanced = cursor; + return Collections.emptyIterator(); + } + } + advanced = cursor; + } + + return new Iterator<>() { + @Override + public boolean hasNext() { + return iterator.hasNext() && advanced < cursor + realSize; + } + + @Override + public T next() { + advanced++; + return iterator.next(); + } + }; + } + }; + } + public static Ingredient toIngredient(ItemStack stack) { return Ingredient.of(Stream.of(stack)); } -- cgit From 8ec269c48ac4576d7fcda9373c2e10afc599f303 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sat, 5 Nov 2022 01:15:41 +0800 Subject: Fix #1187 --- .../me/shedaniel/rei/api/client/config/ConfigObject.java | 5 +++++ .../api/client/registry/category/CategoryRegistry.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) (limited to 'api/src/main/java/me/shedaniel') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java b/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java index c994980cd..a932071db 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java @@ -28,6 +28,7 @@ import me.shedaniel.rei.api.client.REIRuntime; import me.shedaniel.rei.api.client.config.entry.EntryStackProvider; import me.shedaniel.rei.api.client.favorites.FavoriteEntry; import me.shedaniel.rei.api.client.gui.config.*; +import me.shedaniel.rei.api.common.category.CategoryIdentifier; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.resources.ResourceLocation; @@ -35,6 +36,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.List; +import java.util.Map; @Environment(EnvType.CLIENT) public interface ConfigObject { @@ -310,6 +312,9 @@ public interface ConfigObject { @ApiStatus.Experimental boolean shouldFilterDisplays(); + @ApiStatus.Experimental + Map, Boolean> getFilteringQuickCraftCategories(); + @ApiStatus.Experimental boolean shouldAsyncSearch(); diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/category/CategoryRegistry.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/category/CategoryRegistry.java index f209cf11b..4bdcab8ac 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/registry/category/CategoryRegistry.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/category/CategoryRegistry.java @@ -269,6 +269,22 @@ public interface CategoryRegistry extends Reloadable, Iterable< */ void setPlusButtonArea(ButtonArea area); + /** + * Returns whether the category is available for quick crafting by default. + * + * @return whether the category is available for quick crafting by default + */ + @ApiStatus.Experimental + boolean isQuickCraftingEnabledByDefault(); + + /** + * Sets whether the category is available for quick crafting by default. + * + * @param enabled whether the category is available for quick crafting by default + */ + @ApiStatus.Experimental + void setQuickCraftingEnabledByDefault(boolean enabled); + /** * Returns the optional plus button area provider * -- cgit