diff --git a/src/main/java/org/cyclops/integratedterminals/core/terminalstorage/TerminalStorageTabIngredientComponentClient.java b/src/main/java/org/cyclops/integratedterminals/core/terminalstorage/TerminalStorageTabIngredientComponentClient.java index eb94b4e44..6ea65f2b6 100644 --- a/src/main/java/org/cyclops/integratedterminals/core/terminalstorage/TerminalStorageTabIngredientComponentClient.java +++ b/src/main/java/org/cyclops/integratedterminals/core/terminalstorage/TerminalStorageTabIngredientComponentClient.java @@ -34,7 +34,7 @@ import org.cyclops.cyclopscore.helper.StringHelpers; import org.cyclops.cyclopscore.ingredient.collection.IIngredientCollapsedCollectionMutable; import org.cyclops.cyclopscore.ingredient.collection.IngredientArrayList; -import org.cyclops.cyclopscore.ingredient.collection.IngredientCollectionPrototypeMap; +import org.cyclops.cyclopscore.ingredient.collection.IngredientCollectionHelpers; import org.cyclops.cyclopscore.ingredient.collection.diff.IngredientCollectionDiff; import org.cyclops.cyclopscore.ingredient.collection.diff.IngredientCollectionDiffHelpers; import org.cyclops.integrateddynamics.api.ingredient.IIngredientComponentStorageObservable; @@ -98,7 +98,7 @@ public class TerminalStorageTabIngredientComponentClient protected final ContainerTerminalStorageBase container; private final List> buttons; - private final Int2ObjectMap>> ingredientsViews; + private final Int2ObjectMap> ingredientsUnsortedViews; private final Int2ObjectMap>> filteredIngredientsViews; private final Int2ObjectMap>> craftingOptions; @@ -142,7 +142,7 @@ public TerminalStorageTabIngredientComponentClient(ContainerTerminalStorageBase NeoForge.EVENT_BUS.post(event); this.buttons = event.getButtons(); - this.ingredientsViews = new Int2ObjectOpenHashMap<>(); + this.ingredientsUnsortedViews = new Int2ObjectOpenHashMap<>(); this.filteredIngredientsViews = new Int2ObjectOpenHashMap<>(); this.craftingOptions = new Int2ObjectOpenHashMap<>(); @@ -238,11 +238,11 @@ public void setInstanceFilter(int channel, String filter) { container.getGuiState().setSearch(getTabSettingsName().toString(), channel, filter.toLowerCase(Locale.ENGLISH)); } - public List> getRawUnfilteredIngredientsView(int channel) { - List> ingredientsView = ingredientsViews.get(channel); + public IIngredientCollapsedCollectionMutable getRawUnfilteredIngredientsView(int channel) { + IIngredientCollapsedCollectionMutable ingredientsView = ingredientsUnsortedViews.get(channel); if (ingredientsView == null) { - ingredientsView = Lists.newArrayList(); - ingredientsViews.put(channel, ingredientsView); + ingredientsView = IngredientCollectionHelpers.createCollapsedCollection(getIngredientComponent()); + ingredientsUnsortedViews.put(channel, ingredientsView); } return ingredientsView; } @@ -252,24 +252,27 @@ public Collection> getCraftingOptions(in return craftingOptions.get(channel); } - public List> getUnfilteredIngredientsView(int channel) { + public List> createUnfilteredIngredientsView(int channel) { + // Convert raw ingredients view to list + List> enrichedIngredients = Lists.newArrayList(); + for (T persistedIngredient : getRawUnfilteredIngredientsView(channel)) { + enrichedIngredients.add(new InstanceWithMetadata<>(persistedIngredient, null)); + } + + // Add all crafting option outputs Collection> craftingOptions = getCraftingOptions(channel); - if (craftingOptions == null) { - return getRawUnfilteredIngredientsView(channel); - } else { - List> enrichedIngredients = Lists.newArrayList(); - enrichedIngredients.addAll(getRawUnfilteredIngredientsView(channel)); - // Add all crafting option outputs + if (craftingOptions != null) { for (HandlerWrappedTerminalCraftingOption craftingOption : craftingOptions) { for (T output : getUniqueCraftingOptionOutputs(craftingOption.getCraftingOption())) { enrichedIngredients.add(new InstanceWithMetadata<>(output, craftingOption)); } } - return enrichedIngredients; } + + return enrichedIngredients; } - protected Collection getUniqueCraftingOptionOutputs(ITerminalCraftingOption craftingOption) { + public Collection getUniqueCraftingOptionOutputs(ITerminalCraftingOption craftingOption) { TreeSet uniqueOutputs = Sets.newTreeSet(ingredientComponent.getMatcher()); Iterator it = craftingOption.getOutputs(); while (it.hasNext()) { @@ -281,7 +284,7 @@ protected Collection getUniqueCraftingOptionOutputs(ITerminalCraftingOption> getFilteredIngredientsView(int channel) { List> ingredientsView = filteredIngredientsViews.get(channel); if (ingredientsView == null) { - ingredientsView = getUnfilteredIngredientsView(channel); + ingredientsView = createUnfilteredIngredientsView(channel); // Filter ingredientsView = Lists.newArrayList( @@ -399,21 +402,12 @@ public synchronized void onChange(int channel, IIngredientComponentStorageObserv totalQuantities.put(channel, newQuantity); // Apply diff - List> rawPersistedIngredients = getRawUnfilteredIngredientsView(channel); - IIngredientCollapsedCollectionMutable persistedIngredients = new IngredientCollectionPrototypeMap<>(ingredientComponent); - rawPersistedIngredients - .stream() - .map(InstanceWithMetadata::getInstance) - .forEach(persistedIngredients::add); + IIngredientCollapsedCollectionMutable rawPersistedIngredients = getRawUnfilteredIngredientsView(channel); IngredientCollectionDiff diff = new IngredientCollectionDiff<>( changeType == IIngredientComponentStorageObservable.Change.ADDITION ? ingredients : null, changeType == IIngredientComponentStorageObservable.Change.DELETION ? ingredients : null, false); - IngredientCollectionDiffHelpers.applyDiff(ingredientComponent, diff, persistedIngredients); - rawPersistedIngredients.clear(); - for (T persistedIngredient : persistedIngredients) { - rawPersistedIngredients.add(new InstanceWithMetadata<>(persistedIngredient, null)); - } + IngredientCollectionDiffHelpers.applyDiff(ingredientComponent, diff, rawPersistedIngredients); // Persist changes resetFilteredIngredientsViews(channel);