diff --git a/src/main/java/codechicken/nei/FavoriteRecipes.java b/src/main/java/codechicken/nei/FavoriteRecipes.java index 8f588f94..9e3f9195 100644 --- a/src/main/java/codechicken/nei/FavoriteRecipes.java +++ b/src/main/java/codechicken/nei/FavoriteRecipes.java @@ -183,6 +183,10 @@ public class FavoriteRecipes { return null; } + public static int size() { + return items.size() + fluids.size(); + } + public static boolean contains(ItemStack stack) { return getFavorite(stack) != null; } diff --git a/src/main/java/codechicken/nei/ItemCraftablesPanel.java b/src/main/java/codechicken/nei/ItemCraftablesPanel.java index 562401d9..110921b9 100644 --- a/src/main/java/codechicken/nei/ItemCraftablesPanel.java +++ b/src/main/java/codechicken/nei/ItemCraftablesPanel.java @@ -2,10 +2,11 @@ package codechicken.nei; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; import java.util.stream.IntStream; import net.minecraft.client.gui.inventory.GuiContainer; @@ -90,6 +91,7 @@ public class ItemCraftablesPanel private GuiContainer lastGuiContainer = null; private ItemStackAmount lastPlayerInventory = null; private long lastPlayerInventorySync = 0; + private int lastFavoritesCount = 0; public ItemCraftablesPanel() { this.grid = new ItemsGrid<>() { @@ -106,28 +108,37 @@ public class ItemCraftablesPanel public List getMask() { if (this.gridMask == null) { - int maxSlotIndex = this.rows * this.columns; - final AtomicInteger slotIndex = new AtomicInteger(0); - final AtomicInteger itemIndex = new AtomicInteger(0); + final int maxSlotIndex = this.rows * this.columns; final List gridMask = new ArrayList<>(); this.realItems.clear(); if (!ItemCraftablesPanel.this.availableRecipes.isEmpty()) { - ItemCraftablesPanel.this.availableRecipes.entrySet().stream() - .sorted(Map.Entry.comparingByKey(ItemSorter.instance)).forEach(entry -> { - if (!isInvalidSlot(slotIndex.get())) { - this.realItems.add(entry.getKey()); - gridMask.add( - new CraftablesGridSlot( - slotIndex.get(), - itemIndex.getAndIncrement(), - entry.getKey(), - entry.getValue())); - } - slotIndex.incrementAndGet(); - }); + final List> entries = ItemCraftablesPanel.this.availableRecipes + .entrySet().stream() + .sorted( + Map.Entry.comparingByKey( + Comparator.comparing(FavoriteRecipes::contains).reversed() + .thenComparing(ItemSorter.instance))) + .limit(maxSlotIndex).collect(Collectors.toList()); + int slotIndex = 0; + int itemIndex = 0; - this.gridMask = gridMask.subList(0, Math.min(gridMask.size(), maxSlotIndex)); + for (Map.Entry entry : entries) { + if (!isInvalidSlot(slotIndex)) { + this.realItems.add(entry.getKey()); + gridMask.add( + new CraftablesGridSlot( + slotIndex, + itemIndex++, + entry.getKey(), + entry.getValue())); + } + if (slotIndex++ >= maxSlotIndex) { + break; + } + } + + this.gridMask = gridMask; } else { this.gridMask = Collections.emptyList(); } @@ -197,11 +208,16 @@ public class ItemCraftablesPanel this.lastPlayerInventorySync = System.currentTimeMillis(); if (firstGui != null && !ItemCraftablesPanel.guiBlacklist.contains(firstGui.getClass().getName())) { + final boolean showOnlyFavorites = NEIClientConfig + .getBooleanSetting("inventory.craftables.favoritesOnly"); final ItemStackAmount inv = AutoCraftingManager.getInventoryItems(firstGui); - if (this.lastGuiContainer != (this.lastGuiContainer = firstGui) + if (this.lastGuiContainer != firstGui + || showOnlyFavorites && this.lastFavoritesCount != FavoriteRecipes.size() || !inv.equals(this.lastPlayerInventory)) { this.lastPlayerInventory = inv; + this.lastGuiContainer = firstGui; + this.lastFavoritesCount = FavoriteRecipes.size(); this.availableRecipes = generateCraftables(firstGui); this.grid.onGridChanged(); } @@ -256,12 +272,14 @@ public class ItemCraftablesPanel private Map filterHandlerRecipes(IRecipeHandler handler, GuiContainer firstGui, List invStacks) { final Map availableRecipes = new HashMap<>(); + final boolean showOnlyFavorites = NEIClientConfig.getBooleanSetting("inventory.craftables.favoritesOnly"); IntStream.range(0, handler.numRecipes()).parallel().forEach(recipeIndex -> { if (existsIngredients(firstGui, handler.getIngredientStacks(recipeIndex), invStacks)) { final Recipe recipe = Recipe.of(handler, recipeIndex); - if (!recipe.getResults().isEmpty()) { + if (!recipe.getResults().isEmpty() + && (!showOnlyFavorites || FavoriteRecipes.getFavorite(recipe.getRecipeId()) != null)) { synchronized (availableRecipes) { availableRecipes.put(recipe.getResult(), recipe.getRecipeId()); } diff --git a/src/main/java/codechicken/nei/NEIClientConfig.java b/src/main/java/codechicken/nei/NEIClientConfig.java index fd33eaeb..a6e51d42 100644 --- a/src/main/java/codechicken/nei/NEIClientConfig.java +++ b/src/main/java/codechicken/nei/NEIClientConfig.java @@ -292,6 +292,10 @@ public class NEIClientConfig { tag.getTag("inventory.craftables.useRows").setComment("Rows used in craftables areas").getIntValue(2); API.addOption(new OptionIntegerField("inventory.craftables.useRows", 1, 5)); + tag.getTag("inventory.craftables.favoritesOnly").setComment("Show Only Favorites Recipes") + .getBooleanValue(false); + API.addOption(new OptionToggleButton("inventory.craftables.favoritesOnly", true)); + tag.getTag("inventory.collapsibleItems.enabled").getBooleanValue(true); API.addOption(new OptionToggleButton("inventory.collapsibleItems.enabled", true) { diff --git a/src/main/resources/assets/nei/lang/en_US.lang b/src/main/resources/assets/nei/lang/en_US.lang index 3da46163..8a1db45b 100644 --- a/src/main/resources/assets/nei/lang/en_US.lang +++ b/src/main/resources/assets/nei/lang/en_US.lang @@ -418,6 +418,9 @@ nei.options.inventory.craftables.enabled.true=Enabled nei.options.inventory.craftables.enabled.false=Disabled nei.options.inventory.craftables.color=Color nei.options.inventory.craftables.useRows=Rows +nei.options.inventory.craftables.favoritesOnly=Show Favorites Only +nei.options.inventory.craftables.favoritesOnly.true=Yes +nei.options.inventory.craftables.favoritesOnly.false=No nei.options.inventory.subsets=Subsets Widget nei.options.inventory.subsets.enabled=Subsets Visibility nei.options.inventory.subsets.enabled.true=Enabled