diff options
| author | shedaniel <daniel@shedaniel.me> | 2021-08-03 18:29:21 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2021-08-03 18:29:21 +0800 |
| commit | 606ebaf9145b32f63cdeb330f16496df6a68ec2f (patch) | |
| tree | d7e9d91a8a3b3913a368af74cc1d5077727909e1 | |
| parent | b866becfb620c02a74fb990915001e154b5d2b0b (diff) | |
| download | RoughlyEnoughItems-606ebaf9145b32f63cdeb330f16496df6a68ec2f.tar.gz RoughlyEnoughItems-606ebaf9145b32f63cdeb330f16496df6a68ec2f.tar.bz2 RoughlyEnoughItems-606ebaf9145b32f63cdeb330f16496df6a68ec2f.zip | |
Introduce CategoryVisibilityPredicate, update JEI api
5 files changed, 137 insertions, 4 deletions
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 6061e28b0..35817b419 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 @@ -24,6 +24,7 @@ package me.shedaniel.rei.api.client.registry.category; import me.shedaniel.rei.api.client.plugins.REIClientPlugin; +import me.shedaniel.rei.api.client.registry.category.visibility.CategoryVisibilityPredicate; import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; @@ -47,6 +48,11 @@ import java.util.stream.StreamSupport; * Registry for registering new categories for displays. * Relies on {@link CategoryIdentifier}, and is reset per plugin reload. * + * <p>Plugins may also determine the visibility of the categories dynamically via + * {@link CategoryVisibilityPredicate}, these predicates are preferred comparing to + * removing the categories from the registry. + * + * @see CategoryVisibilityPredicate * @see REIClientPlugin#registerCategories(CategoryRegistry) */ @Environment(EnvType.CLIENT) @@ -107,6 +113,38 @@ public interface CategoryRegistry extends Reloadable<REIClientPlugin>, Iterable< int size(); + /** + * Registers a category visibility predicate + * + * @param predicate the predicate to be registered + */ + void registerVisibilityPredicate(CategoryVisibilityPredicate predicate); + + /** + * Tests the category against all visibility predicates to determine whether it is visible + * + * @param category the category to test against + * @return whether the category is visible + */ + boolean isCategoryVisible(DisplayCategory<?> category); + + /** + * Tests the category against all visibility predicates to determine whether it is invisible + * + * @param category the category to test against + * @return whether the category is invisible + */ + default boolean isCategoryInvisible(DisplayCategory<?> category) { + return !isCategoryVisible(category); + } + + /** + * Returns an unmodifiable list of visibility predicates. + * + * @return an unmodifiable list of visibility predicates. + */ + List<CategoryVisibilityPredicate> getVisibilityPredicates(); + default <D extends Display> void addWorkstations(CategoryIdentifier<D> category, EntryIngredient... stations) { configure(category, config -> config.addWorkstations(stations)); } diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/category/visibility/CategoryVisibilityPredicate.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/category/visibility/CategoryVisibilityPredicate.java new file mode 100644 index 000000000..ee40f8c71 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/category/visibility/CategoryVisibilityPredicate.java @@ -0,0 +1,59 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 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.registry.category.visibility; + +import dev.architectury.event.EventResult; +import me.shedaniel.rei.api.client.registry.display.DisplayCategory; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.world.InteractionResult; + +@Environment(EnvType.CLIENT) +public interface CategoryVisibilityPredicate extends Comparable<CategoryVisibilityPredicate> { + /** + * Gets the priority of the handler, the higher the priority, the earlier this is called. + * + * @return the priority + * @return the priority + */ + default double getPriority() { + return 0.0; + } + + /** + * Handles the visibility of the category. + * {@link InteractionResult#PASS} to pass the handling to another handler + * {@link InteractionResult#SUCCESS} to always display it + * {@link InteractionResult#FAIL} to never display it + * + * @param category the category of the display + * @return the visibility + */ + EventResult handleCategory(DisplayCategory<?> category); + + @Override + default int compareTo(CategoryVisibilityPredicate o) { + return Double.compare(getPriority(), o.getPriority()); + } +} diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/category/CategoryRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/category/CategoryRegistryImpl.java index c9fd3b925..1e03b2ce7 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/category/CategoryRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/category/CategoryRegistryImpl.java @@ -23,9 +23,12 @@ package me.shedaniel.rei.impl.client.registry.category; +import dev.architectury.event.EventResult; +import me.shedaniel.rei.RoughlyEnoughItemsCore; import me.shedaniel.rei.api.client.plugins.REIClientPlugin; import me.shedaniel.rei.api.client.registry.category.ButtonArea; import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; +import me.shedaniel.rei.api.client.registry.category.visibility.CategoryVisibilityPredicate; import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; @@ -39,6 +42,7 @@ import java.util.function.Consumer; public class CategoryRegistryImpl implements CategoryRegistry { private final Map<CategoryIdentifier<?>, Configuration<?>> categories = new LinkedHashMap<>(); private final Map<CategoryIdentifier<?>, List<Consumer<CategoryConfiguration<?>>>> listeners = new HashMap<>(); + private final List<CategoryVisibilityPredicate> visibilityPredicates = new ArrayList<>(); @Override public void acceptPlugin(REIClientPlugin plugin) { @@ -90,6 +94,33 @@ public class CategoryRegistryImpl implements CategoryRegistry { return categories.size(); } + @Override + public void registerVisibilityPredicate(CategoryVisibilityPredicate predicate) { + visibilityPredicates.add(predicate); + visibilityPredicates.sort(Comparator.reverseOrder()); + } + + @Override + public boolean isCategoryVisible(DisplayCategory<?> category) { + for (CategoryVisibilityPredicate predicate : visibilityPredicates) { + try { + EventResult result = predicate.handleCategory(category); + if (result.interruptsFurtherEvaluation()) { + return result.isEmpty() || result.isTrue(); + } + } catch (Throwable throwable) { + RoughlyEnoughItemsCore.LOGGER.error("Failed to check if the category is visible!", throwable); + } + } + + return true; + } + + @Override + public List<CategoryVisibilityPredicate> getVisibilityPredicates() { + return Collections.unmodifiableList(visibilityPredicates); + } + private static class Configuration<T extends Display> implements CategoryConfiguration<T> { private final DisplayCategory<T> category; private final List<EntryIngredient> workstations = Collections.synchronizedList(new ArrayList<>()); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java index 5d5c70789..4d57df4e6 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java @@ -23,6 +23,7 @@ package me.shedaniel.rei.impl.client.registry.display; +import com.google.common.base.Preconditions; import dev.architectury.event.EventResult; import me.shedaniel.rei.RoughlyEnoughItemsCore; import me.shedaniel.rei.api.client.plugins.REIClientPlugin; @@ -119,6 +120,7 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi @Override public boolean isDisplayVisible(Display display) { DisplayCategory<Display> category = (DisplayCategory<Display>) CategoryRegistry.getInstance().get(display.getCategoryIdentifier()).getCategory(); + Preconditions.checkNotNull(category, "Failed to resolve category: " + display.getCategoryIdentifier()); for (DisplayVisibilityPredicate predicate : visibilityPredicates) { try { EventResult result = predicate.handleDisplay(category, display); @@ -126,7 +128,7 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi return result.isEmpty() || result.isTrue(); } } catch (Throwable throwable) { - RoughlyEnoughItemsCore.LOGGER.error("Failed to check if the recipe is visible!", throwable); + RoughlyEnoughItemsCore.LOGGER.error("Failed to check if the display is visible!", throwable); } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java index a75e75698..46ba3eff0 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java @@ -67,6 +67,7 @@ public class ViewsImpl implements Views { Map<DisplayCategory<?>, List<Display>> result = Maps.newLinkedHashMap(); for (CategoryRegistry.CategoryConfiguration<?> categoryConfiguration : CategoryRegistry.getInstance()) { DisplayCategory<?> category = categoryConfiguration.getCategory(); + if (CategoryRegistry.getInstance().isCategoryInvisible(category)) continue; CategoryIdentifier<?> categoryId = categoryConfiguration.getCategoryIdentifier(); List<Display> allRecipesFromCategory = displayRegistry.get((CategoryIdentifier<Display>) categoryId); @@ -126,15 +127,17 @@ public class ViewsImpl implements Views { for (Map.Entry<CategoryIdentifier<?>, List<DynamicDisplayGenerator<?>>> entry : displayRegistry.getCategoryDisplayGenerators().entrySet()) { CategoryIdentifier<?> categoryId = entry.getKey(); + DisplayCategory<?> category = CategoryRegistry.getInstance().get(categoryId).getCategory(); + if (CategoryRegistry.getInstance().isCategoryInvisible(category)) continue; Set<Display> set = new LinkedHashSet<>(); generatorsCount += entry.getValue().size(); - + for (DynamicDisplayGenerator<Display> generator : (List<DynamicDisplayGenerator<Display>>) (List<? extends DynamicDisplayGenerator<?>>) entry.getValue()) { generateLiveDisplays(displayRegistry, generator, builder, set::add); } - + if (!set.isEmpty()) { - CollectionUtils.getOrPutEmptyList(result, CategoryRegistry.getInstance().get(categoryId).getCategory()).addAll(set); + CollectionUtils.getOrPutEmptyList(result, category).addAll(set); } } |
