aboutsummaryrefslogtreecommitdiff
path: root/runtime/src/main/java
diff options
context:
space:
mode:
authorshedaniel <daniel@shedaniel.me>2021-08-19 17:50:12 +0800
committershedaniel <daniel@shedaniel.me>2021-08-19 17:50:12 +0800
commite2c4f4c8a8bf2f111850d1ef0f5a9172f858c7e5 (patch)
treecc069cbb89a4202b49168dacde1d4afa3dc8764a /runtime/src/main/java
parentf502fc28131ca63b3f180d2cbd6397611f82750b (diff)
downloadRoughlyEnoughItems-e2c4f4c8a8bf2f111850d1ef0f5a9172f858c7e5.tar.gz
RoughlyEnoughItems-e2c4f4c8a8bf2f111850d1ef0f5a9172f858c7e5.tar.bz2
RoughlyEnoughItems-e2c4f4c8a8bf2f111850d1ef0f5a9172f858c7e5.zip
Introduce CategoryVisibilityPredicate, update JEI api
Diffstat (limited to 'runtime/src/main/java')
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/impl/client/registry/category/CategoryRegistryImpl.java32
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java4
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java9
3 files changed, 41 insertions, 4 deletions
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..0ab51036f 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,13 +23,17 @@
package me.shedaniel.rei.impl.client.registry.category;
+import me.shedaniel.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;
import me.shedaniel.rei.api.common.entry.EntryIngredient;
+import org.apache.commons.lang3.BooleanUtils;
import org.jetbrains.annotations.ApiStatus;
import java.util.*;
@@ -39,6 +43,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 +95,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.value() == null || BooleanUtils.isTrue(result.value());
+ }
+ } 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 934e22908..0af7766f4 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 me.shedaniel.architectury.event.EventResult;
import me.shedaniel.rei.RoughlyEnoughItemsCore;
import me.shedaniel.rei.api.client.plugins.REIClientPlugin;
@@ -120,6 +121,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);
@@ -127,7 +129,7 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi
return !BooleanUtils.isFalse(result.value());
}
} 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);
}
}