From 8d77ad86b203de62aa08a5ee62f91e27fd0fbd00 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 30 Aug 2020 21:29:58 +0800 Subject: Fix brewing potions not appearing and allow scrolling in the tabs for villager like recipe viewing screen Signed-off-by: shedaniel --- .../java/me/shedaniel/rei/api/ClientHelper.java | 2 + .../main/java/me/shedaniel/rei/api/REIHelper.java | 2 + .../java/me/shedaniel/rei/api/RecipeDisplay.java | 3 +- .../java/me/shedaniel/rei/api/RecipeHelper.java | 12 ++++- .../rei/plugin/brewing/DefaultBrewingDisplay.java | 2 +- .../shedaniel/rei/gui/ContainerScreenOverlay.java | 21 +++------ .../rei/gui/VillagerRecipeViewingScreen.java | 21 ++++++--- .../shedaniel/rei/gui/widget/EntryListWidget.java | 9 ++-- .../rei/gui/widget/FavoritesListWidget.java | 4 +- .../me/shedaniel/rei/impl/ClientHelperImpl.java | 54 +++++++++++----------- .../me/shedaniel/rei/impl/EntryRegistryImpl.java | 36 +++++++++++---- .../me/shedaniel/rei/impl/RecipeHelperImpl.java | 51 ++++++++++---------- .../java/me/shedaniel/rei/impl/ScreenHelper.java | 12 +++-- .../rei/impl/filtering/FilteringContextImpl.java | 2 +- .../roughlyenoughitems-runtime.accessWidener | 1 + gradle.properties | 2 +- 16 files changed, 134 insertions(+), 100 deletions(-) diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClientHelper.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClientHelper.java index e6b67ff3c..73f768e8d 100644 --- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClientHelper.java +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClientHelper.java @@ -65,6 +65,8 @@ public interface ClientHelper { */ void setCheating(boolean cheating); + @Deprecated + @ApiStatus.ScheduledForRemoval List getInventoryItemsTypes(); /** diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/REIHelper.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/REIHelper.java index 126358513..6a42c90dd 100644 --- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/REIHelper.java +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/REIHelper.java @@ -65,6 +65,8 @@ public interface REIHelper { @NotNull @ApiStatus.Internal + @Deprecated + @ApiStatus.ScheduledForRemoval List getInventoryStacks(); void queueTooltip(@Nullable Tooltip tooltip); diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeDisplay.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeDisplay.java index 44e655da4..ff6722e74 100644 --- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeDisplay.java +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeDisplay.java @@ -42,9 +42,10 @@ public interface RecipeDisplay { /** * @return a list of outputs + * @deprecated Use {@link RecipeDisplay#getResultingEntries()} */ @Deprecated - @ApiStatus.ScheduledForRemoval + @ApiStatus.ScheduledForRemoval(inVersion = "1.17") @NotNull default List getOutputEntries() { return Collections.emptyList(); diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeHelper.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeHelper.java index 79bff2d4c..74a869474 100644 --- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeHelper.java +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeHelper.java @@ -81,7 +81,17 @@ public interface RecipeHelper { * @param inventoryItems the materials * @return the list of craftable entries */ - List findCraftableEntriesByItems(List inventoryItems); + List findCraftableEntriesByItems(Iterable inventoryItems); + + /** + * Gets all craftable items from materials. + * + * @param inventoryItems the materials + * @return the list of craftable entries + */ + default List findCraftableEntriesByItems(List inventoryItems) { + return findCraftableEntriesByItems((Iterable) inventoryItems); + } /** * Registers a category diff --git a/RoughlyEnoughItems-default-plugin/src/main/java/me/shedaniel/rei/plugin/brewing/DefaultBrewingDisplay.java b/RoughlyEnoughItems-default-plugin/src/main/java/me/shedaniel/rei/plugin/brewing/DefaultBrewingDisplay.java index 1a882ad3e..69537a9b6 100644 --- a/RoughlyEnoughItems-default-plugin/src/main/java/me/shedaniel/rei/plugin/brewing/DefaultBrewingDisplay.java +++ b/RoughlyEnoughItems-default-plugin/src/main/java/me/shedaniel/rei/plugin/brewing/DefaultBrewingDisplay.java @@ -80,7 +80,7 @@ public class DefaultBrewingDisplay implements RecipeDisplay { for (int i = 0; i < slot * 2; i++) stack.add(EntryStack.empty()); for (int i = 0; i < 6 - slot * 2; i++) - stack.addAll(getOutputEntries()); + stack.add(output); return stack; } diff --git a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/ContainerScreenOverlay.java b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/ContainerScreenOverlay.java index d97953d72..1cb9b885f 100644 --- a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/ContainerScreenOverlay.java +++ b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/ContainerScreenOverlay.java @@ -74,10 +74,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Arrays; -import java.util.Comparator; -import java.util.LinkedList; -import java.util.List; +import java.util.*; @ApiStatus.Internal public class ContainerScreenOverlay extends WidgetWithBounds implements REIOverlay { @@ -498,7 +495,6 @@ public class ContainerScreenOverlay extends WidgetWithBounds implements REIOverl @Override public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { - List currentStacks = ClientHelper.getInstance().getInventoryItemsTypes(); if (shouldReInit) { ENTRY_LIST_WIDGET.updateSearch(ScreenHelper.getSearchField().getText(), true); init(); @@ -510,9 +506,12 @@ public class ContainerScreenOverlay extends WidgetWithBounds implements REIOverl } } } - if (ConfigManager.getInstance().isCraftableOnlyEnabled() && ((currentStacks.size() != ScreenHelper.inventoryStacks.size()) || !hasSameListContent(new LinkedList<>(ScreenHelper.inventoryStacks), currentStacks))) { - ScreenHelper.inventoryStacks = currentStacks; - ENTRY_LIST_WIDGET.updateSearch(ScreenHelper.getSearchField().getText(), true); + if (ConfigManager.getInstance().isCraftableOnlyEnabled()) { + Set currentStacks = ClientHelperImpl.getInstance()._getInventoryItemsTypes(); + if (!currentStacks.equals(ScreenHelper.inventoryStacks)) { + ScreenHelper.inventoryStacks = currentStacks; + ENTRY_LIST_WIDGET.updateSearch(ScreenHelper.getSearchField().getText(), true); + } } if (OverlaySearchField.isSearching) { matrices.pushPose(); @@ -596,12 +595,6 @@ public class ContainerScreenOverlay extends WidgetWithBounds implements REIOverl ScreenHelper.drawHoveringWidget(matrices, mouseX, mouseY, renderTooltipCallback, tooltipWidth, tooltipHeight, 0); } - private boolean hasSameListContent(List list1, List list2) { - list1.sort(Comparator.comparing(Object::toString)); - list2.sort(Comparator.comparing(Object::toString)); - return CollectionUtils.mapAndJoinToString(list1, Object::toString, "").equals(CollectionUtils.mapAndJoinToString(list2, Object::toString, "")); - } - public void addTooltip(@Nullable Tooltip tooltip) { if (tooltip != null) TOOLTIPS.add(tooltip); diff --git a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java index b8a18d4b2..183bea49d 100644 --- a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java +++ b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java @@ -290,10 +290,10 @@ public class VillagerRecipeViewingScreen extends Screen implements RecipeScreen } @Override - public boolean mouseScrolled(double double_1, double double_2, double double_3) { + public boolean mouseScrolled(double mouseX, double mouseY, double amount) { double height = scrolling.getMaxScrollHeight(); - if (scrollListBounds.contains(double_1, double_2) && height > scrollListBounds.height - 2) { - scrolling.offset(ClothConfigInitializer.getScrollStep() * -double_3, true); + if (scrollListBounds.contains(mouseX, mouseY) && height > scrollListBounds.height - 2) { + scrolling.offset(ClothConfigInitializer.getScrollStep() * -amount, true); if (scrollBarAlphaFuture == 0) scrollBarAlphaFuture = 1f; if (System.currentTimeMillis() - scrollBarAlphaFutureTime > 300f) @@ -301,10 +301,19 @@ public class VillagerRecipeViewingScreen extends Screen implements RecipeScreen return true; } for (GuiEventListener listener : children()) - if (listener.mouseScrolled(double_1, double_2, double_3)) + if (listener.mouseScrolled(mouseX, mouseY, amount)) return true; + int tabSize = ConfigObject.getInstance().isUsingCompactTabs() ? 24 : 28; + if (mouseX >= bounds.x && mouseX <= bounds.getMaxX() && mouseY >= bounds.y - tabSize && mouseY < bounds.y) { + if (amount < 0) selectedCategoryIndex++; + else if (amount > 0) selectedCategoryIndex--; + if (selectedCategoryIndex < 0) selectedCategoryIndex = categories.size() - 1; + else if (selectedCategoryIndex >= categories.size()) selectedCategoryIndex = 0; + ClientHelperImpl.getInstance().openRecipeViewingScreen(categoryMap, categories.get(selectedCategoryIndex).getIdentifier(), ingredientStackToNotice, resultStackToNotice); + return true; + } if (bounds.contains(PointHelper.ofMouse())) { - if (double_3 < 0 && categoryMap.get(categories.get(selectedCategoryIndex)).size() > 1) { + if (amount < 0 && categoryMap.get(categories.get(selectedCategoryIndex)).size() > 1) { selectedRecipeIndex++; if (selectedRecipeIndex >= categoryMap.get(categories.get(selectedCategoryIndex)).size()) selectedRecipeIndex = 0; @@ -318,7 +327,7 @@ public class VillagerRecipeViewingScreen extends Screen implements RecipeScreen return true; } } - return super.mouseScrolled(double_1, double_2, double_3); + return super.mouseScrolled(mouseX, mouseY, amount); } @Override diff --git a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/widget/EntryListWidget.java b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/widget/EntryListWidget.java index efd6be18b..abad5ef0c 100644 --- a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/widget/EntryListWidget.java +++ b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/widget/EntryListWidget.java @@ -23,6 +23,7 @@ package me.shedaniel.rei.gui.widget; +import com.google.common.base.Stopwatch; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.mojang.blaze3d.vertex.PoseStack; @@ -429,7 +430,7 @@ public class EntryListWidget extends WidgetWithBounds { } public void updateSearch(String searchTerm, boolean ignoreLastSearch) { - long started = System.nanoTime(); + Stopwatch stopwatch = Stopwatch.createStarted(); if (ignoreLastSearch || this.lastSearchTerm == null || !this.lastSearchTerm.equals(searchTerm)) { this.lastSearchTerm = searchTerm; this.lastSearchArguments = SearchArgument.processSearchTerm(searchTerm); @@ -437,7 +438,7 @@ public class EntryListWidget extends WidgetWithBounds { boolean checkCraftable = ConfigManager.getInstance().isCraftableOnlyEnabled() && !ScreenHelper.inventoryStacks.isEmpty(); Set workingItems = checkCraftable ? Sets.newHashSet() : null; if (checkCraftable) - workingItems.addAll(CollectionUtils.map(RecipeHelper.getInstance().findCraftableEntriesByItems(CollectionUtils.map(ScreenHelper.inventoryStacks, EntryStack::create)), EntryStack::hashIgnoreAmount)); + workingItems.addAll(CollectionUtils.map(RecipeHelper.getInstance().findCraftableEntriesByItems(ScreenHelper.inventoryStacks), EntryStack::hashIgnoreAmount)); List stacks = EntryRegistry.getInstance().getPreFilteredList(); if (stacks instanceof CopyOnWriteArrayList && !stacks.isEmpty()) { if (ConfigObject.getInstance().shouldAsyncSearch()) { @@ -488,10 +489,8 @@ public class EntryListWidget extends WidgetWithBounds { FavoritesListWidget favoritesListWidget = ContainerScreenOverlay.getFavoritesListWidget(); if (favoritesListWidget != null) favoritesListWidget.updateSearch(this, searchTerm); - long ended = System.nanoTime(); - long time = ended - started; if (ConfigObject.getInstance().doDebugSearchTimeRequired()) - RoughlyEnoughItemsCore.LOGGER.info("Search Used: %.2fms", time * 1e-6); + RoughlyEnoughItemsCore.LOGGER.info("Search Used: %s", stopwatch.stop().toString()); updateEntriesPosition(); } diff --git a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/widget/FavoritesListWidget.java b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/widget/FavoritesListWidget.java index f94faef62..a09d01e96 100644 --- a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/widget/FavoritesListWidget.java +++ b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/widget/FavoritesListWidget.java @@ -201,7 +201,7 @@ public class FavoritesListWidget extends WidgetWithBounds { boolean checkCraftable = ConfigManager.getInstance().isCraftableOnlyEnabled() && !ScreenHelper.inventoryStacks.isEmpty(); Set workingItems = checkCraftable ? Sets.newHashSet() : null; if (checkCraftable) - workingItems.addAll(CollectionUtils.map(RecipeHelper.getInstance().findCraftableEntriesByItems(CollectionUtils.map(ScreenHelper.inventoryStacks, EntryStack::create)), EntryStack::hashIgnoreAmount)); + workingItems.addAll(CollectionUtils.map(RecipeHelper.getInstance().findCraftableEntriesByItems(ScreenHelper.inventoryStacks), EntryStack::hashIgnoreAmount)); for (EntryStack stack : ConfigObject.getInstance().getFavorites()) { if (listWidget.canLastSearchTermsBeAppliedTo(stack)) { if (checkCraftable && !workingItems.contains(stack.hashIgnoreAmount())) @@ -222,7 +222,7 @@ public class FavoritesListWidget extends WidgetWithBounds { boolean checkCraftable = ConfigManager.getInstance().isCraftableOnlyEnabled() && !ScreenHelper.inventoryStacks.isEmpty(); Set workingItems = checkCraftable ? Sets.newHashSet() : null; if (checkCraftable) - workingItems.addAll(CollectionUtils.map(RecipeHelper.getInstance().findCraftableEntriesByItems(CollectionUtils.map(ScreenHelper.inventoryStacks, EntryStack::create)), EntryStack::hashIgnoreAmount)); + workingItems.addAll(CollectionUtils.map(RecipeHelper.getInstance().findCraftableEntriesByItems(ScreenHelper.inventoryStacks), EntryStack::hashIgnoreAmount)); for (EntryStack stack : ConfigObject.getInstance().getFavorites()) { if (checkCraftable && !workingItems.contains(stack.hashIgnoreAmount())) continue; diff --git a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/ClientHelperImpl.java b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/ClientHelperImpl.java index 34735d0c2..b07898abe 100644 --- a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/ClientHelperImpl.java +++ b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/ClientHelperImpl.java @@ -65,6 +65,8 @@ import org.jetbrains.annotations.Nullable; import java.time.LocalDateTime; import java.util.*; import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static me.shedaniel.rei.impl.Internals.attachInstance; @@ -206,10 +208,15 @@ public class ClientHelperImpl implements ClientHelper, ClientModInitializer { @Override public List getInventoryItemsTypes() { - List inventoryStacks = new ArrayList<>(Minecraft.getInstance().player.inventory.items); - inventoryStacks.addAll(Minecraft.getInstance().player.inventory.armor); - inventoryStacks.addAll(Minecraft.getInstance().player.inventory.offhand); - return inventoryStacks; + return Minecraft.getInstance().player.inventory.compartments.stream().flatMap(Collection::stream).collect(Collectors.toList()); + } + + @ApiStatus.Internal + public Set _getInventoryItemsTypes() { + return Minecraft.getInstance().player.inventory.compartments.stream() + .flatMap(Collection::stream) + .map(EntryStack::create) + .collect(Collectors.toSet()); } @ApiStatus.ScheduledForRemoval @@ -269,7 +276,20 @@ public class ClientHelperImpl implements ClientHelper, ClientModInitializer { modNameCache.put("global", "Global"); } - public static final class ViewSearchBuilder implements ClientHelper.ViewSearchBuilder { + private static abstract class AbstractViewSearchBuilder implements ClientHelper.ViewSearchBuilder { + @Override + public ClientHelper.ViewSearchBuilder fillPreferredOpenedCategory() { + if (getPreferredOpenedCategory() == null) { + Screen currentScreen = Minecraft.getInstance().screen; + if (currentScreen instanceof RecipeScreen) { + setPreferredOpenedCategory(((RecipeScreen) currentScreen).getCurrentCategory()); + } + } + return this; + } + } + + public static final class ViewSearchBuilder extends AbstractViewSearchBuilder { @NotNull private final Set categories = new HashSet<>(); @NotNull private final List recipesFor = new ArrayList<>(); @NotNull private final List usagesFor = new ArrayList<>(); @@ -332,17 +352,6 @@ public class ClientHelperImpl implements ClientHelper, ClientModInitializer { return this.preferredOpenedCategory; } - @Override - public ClientHelper.ViewSearchBuilder fillPreferredOpenedCategory() { - if (getPreferredOpenedCategory() == null) { - Screen currentScreen = Minecraft.getInstance().screen; - if (currentScreen instanceof RecipeScreen) { - setPreferredOpenedCategory(((RecipeScreen) currentScreen).getCurrentCategory()); - } - } - return this; - } - @Override public ClientHelper.ViewSearchBuilder setInputNotice(@Nullable EntryStack stack) { this.inputNotice = stack; @@ -374,7 +383,7 @@ public class ClientHelperImpl implements ClientHelper, ClientModInitializer { } } - public static final class LegacyWrapperViewSearchBuilder implements ClientHelper.ViewSearchBuilder { + public static final class LegacyWrapperViewSearchBuilder extends AbstractViewSearchBuilder { @NotNull private final Map, List> map; @Nullable private ResourceLocation preferredOpenedCategory = null; @Nullable private EntryStack inputNotice; @@ -431,17 +440,6 @@ public class ClientHelperImpl implements ClientHelper, ClientModInitializer { return this.preferredOpenedCategory; } - @Override - public ClientHelper.ViewSearchBuilder fillPreferredOpenedCategory() { - if (getPreferredOpenedCategory() == null) { - Screen currentScreen = Minecraft.getInstance().screen; - if (currentScreen instanceof RecipeScreen) { - setPreferredOpenedCategory(((RecipeScreen) currentScreen).getCurrentCategory()); - } - } - return this; - } - @Override public ClientHelper.ViewSearchBuilder setInputNotice(@Nullable EntryStack stack) { this.inputNotice = stack; diff --git a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/EntryRegistryImpl.java b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/EntryRegistryImpl.java index 3bbc9d773..637acb134 100644 --- a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/EntryRegistryImpl.java +++ b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/EntryRegistryImpl.java @@ -23,13 +23,14 @@ package me.shedaniel.rei.impl; +import com.google.common.base.Stopwatch; import com.google.common.collect.Lists; -import com.google.common.collect.Sets; import me.shedaniel.rei.RoughlyEnoughItemsCore; import me.shedaniel.rei.api.ConfigObject; import me.shedaniel.rei.api.EntryRegistry; import me.shedaniel.rei.api.EntryStack; import me.shedaniel.rei.impl.filtering.FilteringContextImpl; +import me.shedaniel.rei.impl.filtering.FilteringContextType; import me.shedaniel.rei.impl.filtering.FilteringRule; import me.shedaniel.rei.utils.CollectionUtils; import net.fabricmc.api.EnvType; @@ -43,6 +44,7 @@ import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.function.Predicate; +import java.util.stream.Collectors; import java.util.stream.Stream; @ApiStatus.Internal @@ -86,21 +88,37 @@ public class EntryRegistryImpl implements EntryRegistry { @Override @ApiStatus.Experimental public void refilter() { - long started = System.currentTimeMillis(); + Stopwatch stopwatch = Stopwatch.createStarted(); FilteringContextImpl context = new FilteringContextImpl(entries); List> rules = ((ConfigObjectImpl) ConfigObject.getInstance()).getFilteringRules(); + Stopwatch innerStopwatch = Stopwatch.createStarted(); for (int i = rules.size() - 1; i >= 0; i--) { - context.handleResult(rules.get(i).processFilteredStacks(context)); + innerStopwatch.reset().start(); + FilteringRule rule = rules.get(i); + context.handleResult(rule.processFilteredStacks(context)); + RoughlyEnoughItemsCore.LOGGER.debug("Refiltered rule [%s] in %s.", FilteringRule.REGISTRY.getKey(rule).toString(), innerStopwatch.stop().toString()); } - Set set = CollectionUtils.mapParallel(entries, AmountIgnoredEntryStackWrapper::new, Sets::newLinkedHashSet); - set.removeAll(CollectionUtils.mapParallel(context.getHiddenStacks(), AmountIgnoredEntryStackWrapper::new, Sets::newHashSet)); - preFilteredList.clear(); - preFilteredList.addAll(CollectionUtils.mapParallel(set, AmountIgnoredEntryStackWrapper::unwrap)); + Set hiddenStacks = context.stacks.get(FilteringContextType.HIDDEN); + if (hiddenStacks.isEmpty()) { + preFilteredList.clear(); + preFilteredList.addAll(entries); + } else { + preFilteredList.clear(); + preFilteredList.addAll(entries.parallelStream() + .map(AmountIgnoredEntryStackWrapper::new) + .filter(not(hiddenStacks::contains)) + .map(AmountIgnoredEntryStackWrapper::unwrap) + .collect(Collectors.toList())); + } - long time = System.currentTimeMillis() - started; - RoughlyEnoughItemsCore.LOGGER.info("Refiltered %d entries with %d rules in %dms.", entries.size() - preFilteredList.size(), rules.size(), time); + RoughlyEnoughItemsCore.LOGGER.debug("Refiltered %d entries with %d rules in %s.", entries.size() - preFilteredList.size(), rules.size(), stopwatch.stop().toString()); + } + + static Predicate not(Predicate target) { + Objects.requireNonNull(target); + return (Predicate) target.negate(); } public void reset() { diff --git a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/RecipeHelperImpl.java b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/RecipeHelperImpl.java index 7e3f57868..2e254962e 100644 --- a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/RecipeHelperImpl.java +++ b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/RecipeHelperImpl.java @@ -23,6 +23,7 @@ package me.shedaniel.rei.impl; +import com.google.common.base.Stopwatch; import com.google.common.collect.*; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.RoughlyEnoughItemsCore; @@ -42,6 +43,7 @@ import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeManager; +import org.apache.commons.lang3.tuple.MutablePair; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -77,7 +79,7 @@ public class RecipeHelperImpl implements RecipeHelper { private boolean arePluginsLoading = false; @Override - public List findCraftableEntriesByItems(List inventoryItems) { + public List findCraftableEntriesByItems(Iterable inventoryItems) { List craftables = new ArrayList<>(); for (List value : recipeDisplays.values()) for (RecipeDisplay recipeDisplay : Lists.newArrayList(value)) { @@ -149,7 +151,7 @@ public class RecipeHelperImpl implements RecipeHelper { @Override public Map, List> buildMapFor(ClientHelper.ViewSearchBuilder builder) { - long start = Util.getNanos(); + Stopwatch stopwatch = Stopwatch.createStarted(); Set categories = builder.getCategories(); List recipesFor = builder.getRecipesFor(); List usagesFor = builder.getUsagesFor(); @@ -244,9 +246,8 @@ public class RecipeHelperImpl implements RecipeHelper { } } - long end = Util.getNanos(); - String message = String.format("Built Recipe View in %dμs for %d categories, %d recipes for, %d usages for and %d live recipe generators.", - (end - start) / 1000, categories.size(), recipesFor.size(), usagesFor.size(), liveRecipeGenerators.size()); + String message = String.format("Built Recipe View in %s for %d categories, %d recipes for, %d usages for and %d live recipe generators.", + stopwatch.stop().toString(), categories.size(), recipesFor.size(), usagesFor.size(), liveRecipeGenerators.size()); if (ConfigObject.getInstance().doDebugSearchTimeRequired()) { RoughlyEnoughItemsCore.LOGGER.info(message); } else { @@ -305,24 +306,20 @@ public class RecipeHelperImpl implements RecipeHelper { autoCraftAreaSupplierMap.put(category, rectangle); } - private void startSection(Object[] sectionData, String section) { - sectionData[0] = Util.getNanos(); - sectionData[2] = section; + private void startSection(MutablePair sectionData, String section) { + sectionData.setRight(section); RoughlyEnoughItemsCore.LOGGER.debug("Reloading Section: \"%s\"", section); + sectionData.getLeft().start(); } - private void endSection(Object[] sectionData) { - sectionData[1] = Util.getNanos(); - long time = (long) sectionData[1] - (long) sectionData[0]; - String section = (String) sectionData[2]; - if (time >= 1000000) { - RoughlyEnoughItemsCore.LOGGER.debug("Reloading Section: \"%s\" done in %.2fms", section, time / 1000000.0F); - } else { - RoughlyEnoughItemsCore.LOGGER.debug("Reloading Section: \"%s\" done in %.2fμs", section, time / 1000.0F); - } + private void endSection(MutablePair sectionData) { + sectionData.getLeft().stop(); + String section = sectionData.getRight(); + RoughlyEnoughItemsCore.LOGGER.debug("Reloading Section: \"%s\" done in %s", section, sectionData.getLeft().toString()); + sectionData.getLeft().reset(); } - private void pluginSection(Object[] sectionData, String sectionName, List list, Consumer consumer) { + private void pluginSection(MutablePair sectionData, String sectionName, List list, Consumer consumer) { for (REIPluginV0 plugin : list) { try { startSection(sectionData, sectionName + " for " + plugin.getPluginIdentifier().toString()); @@ -345,7 +342,7 @@ public class RecipeHelperImpl implements RecipeHelper { public void recipesLoaded(RecipeManager recipeManager) { long startTime = Util.getMillis(); - Object[] sectionData = {0L, 0L, ""}; + MutablePair sectionData = new MutablePair<>(Stopwatch.createUnstarted(), ""); startSection(sectionData, "reset-data"); arePluginsLoading = true; @@ -401,14 +398,16 @@ public class RecipeHelperImpl implements RecipeHelper { startSection(sectionData, "recipe-functions"); if (!recipeFunctions.isEmpty()) { @SuppressWarnings("rawtypes") List allSortedRecipes = getAllSortedRecipes(); - Collections.reverse(allSortedRecipes); - for (RecipeFunction recipeFunction : recipeFunctions) { - try { - for (Recipe recipe : CollectionUtils.filter(allSortedRecipes, recipe -> recipeFunction.recipeFilter.test(recipe))) { - registerDisplay(recipeFunction.category, (RecipeDisplay) recipeFunction.mappingFunction.apply(recipe), 0); + for (int i = allSortedRecipes.size() - 1; i >= 0; i--) { + Recipe recipe = allSortedRecipes.get(i); + for (RecipeFunction recipeFunction : recipeFunctions) { + try { + if (recipeFunction.recipeFilter.test(recipe)) { + registerDisplay(recipeFunction.category, (RecipeDisplay) recipeFunction.mappingFunction.apply(recipe), 0); + } + } catch (Throwable e) { + RoughlyEnoughItemsCore.LOGGER.error("Failed to add recipes!", e); } - } catch (Throwable e) { - RoughlyEnoughItemsCore.LOGGER.error("Failed to add recipes!", e); } } } diff --git a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/ScreenHelper.java b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/ScreenHelper.java index 1352d7656..b5ef8a29e 100644 --- a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/ScreenHelper.java +++ b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/ScreenHelper.java @@ -58,9 +58,8 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Optional; +import java.util.*; +import java.util.stream.Collectors; import static me.shedaniel.rei.impl.Internals.attachInstance; @@ -71,7 +70,7 @@ public class ScreenHelper implements ClientModInitializer, REIHelper { private static final ResourceLocation DISPLAY_TEXTURE_DARK = new ResourceLocation("roughlyenoughitems", "textures/gui/display_dark.png"); private OverlaySearchField searchField; @ApiStatus.Internal - public static List inventoryStacks = Lists.newArrayList(); + public static Set inventoryStacks = Sets.newHashSet(); private static ContainerScreenOverlay overlay; private static AbstractContainerScreen previousContainerScreen = null; private static LinkedHashSet lastRecipeScreen = Sets.newLinkedHashSetWithExpectedSize(5); @@ -101,7 +100,10 @@ public class ScreenHelper implements ClientModInitializer, REIHelper { @Override public @NotNull List getInventoryStacks() { - return inventoryStacks; + return inventoryStacks.stream() + .map(EntryStack::getItemStack) + .filter(Objects::nonNull) + .collect(Collectors.toList()); } @Nullable diff --git a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/filtering/FilteringContextImpl.java b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/filtering/FilteringContextImpl.java index dae8e1a09..9ba5acb4c 100644 --- a/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/filtering/FilteringContextImpl.java +++ b/RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/filtering/FilteringContextImpl.java @@ -43,7 +43,7 @@ import java.util.concurrent.TimeoutException; @Environment(EnvType.CLIENT) public class FilteringContextImpl implements FilteringContext { - private final Map> stacks; + public final Map> stacks; private final Map> cachedStacks; public FilteringContextImpl(List allStacks) { diff --git a/RoughlyEnoughItems-runtime/src/main/resources/roughlyenoughitems-runtime.accessWidener b/RoughlyEnoughItems-runtime/src/main/resources/roughlyenoughitems-runtime.accessWidener index 72bd0ae97..be435d05c 100644 --- a/RoughlyEnoughItems-runtime/src/main/resources/roughlyenoughitems-runtime.accessWidener +++ b/RoughlyEnoughItems-runtime/src/main/resources/roughlyenoughitems-runtime.accessWidener @@ -11,3 +11,4 @@ accessible field net/minecraft/client/gui/screens/inventory/AbstractContainerScr accessible field net/minecraft/client/gui/components/ImageButton resourceLocation Lnet/minecraft/resources/ResourceLocation; accessible method net/minecraft/client/gui/GuiComponent innerBlit (Lcom/mojang/math/Matrix4f;IIIIIFFFF)V accessible field net/minecraft/world/item/CreativeModeTab langId Ljava/lang/String; +accessible field net/minecraft/world/entity/player/Inventory compartments Ljava/util/List; diff --git a/gradle.properties b/gradle.properties index 628d75614..b29f7844c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ org.gradle.jvmargs=-Xmx3G -mod_version=5.2.9 +mod_version=5.2.10 supported_version=1.16.2 minecraft_version=1.16.2-rc1 yarn_version=1.16.2-rc1+build.4+legacy.20w09a+build.8 -- cgit