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 /api/src/main/java | |
| parent | b866becfb620c02a74fb990915001e154b5d2b0b (diff) | |
| download | RoughlyEnoughItems-606ebaf9145b32f63cdeb330f16496df6a68ec2f.tar.gz RoughlyEnoughItems-606ebaf9145b32f63cdeb330f16496df6a68ec2f.tar.bz2 RoughlyEnoughItems-606ebaf9145b32f63cdeb330f16496df6a68ec2f.zip | |
Introduce CategoryVisibilityPredicate, update JEI api
Diffstat (limited to 'api/src/main/java')
2 files changed, 97 insertions, 0 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()); + } +} |
