diff --git a/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java b/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java index 92483b728..5720a83a1 100644 --- a/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java +++ b/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java @@ -319,7 +319,6 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi this.searchField.setText(memoryText); this.searchField.selectAll(); this.repo.setSearchString(memoryText); - this.repo.updateView(); this.setScrollBar(); } @@ -381,7 +380,6 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi if (btn == 1 && this.searchField.isMouseIn(xCoord, yCoord)) { this.searchField.setText(""); this.repo.setSearchString(""); - this.repo.updateView(); this.setScrollBar(); } @@ -476,7 +474,6 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi if (this.searchField.textboxKeyTyped(character, key)) { this.repo.setSearchString(this.searchField.getText()); - this.repo.updateView(); this.setScrollBar(); // tell forge the key event is handled and should not be sent out this.keyHandled = mouseInGui; diff --git a/src/main/java/appeng/client/me/ItemRepo.java b/src/main/java/appeng/client/me/ItemRepo.java index 22a211a6a..9796a7935 100644 --- a/src/main/java/appeng/client/me/ItemRepo.java +++ b/src/main/java/appeng/client/me/ItemRepo.java @@ -37,7 +37,7 @@ import net.minecraft.item.ItemStack; import javax.annotation.Nonnull; import java.util.ArrayList; -import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.regex.Pattern; @@ -45,7 +45,7 @@ import java.util.regex.Pattern; public class ItemRepo { private final IItemList list = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList(); - private final ArrayList view = new ArrayList<>(); + private List view = new ArrayList<>(); private final IScrollSource src; private final ISortSource sortSrc; @@ -56,6 +56,16 @@ public class ItemRepo { private String innerSearch = ""; private boolean hasPower; + private Enum lastView; + private Enum lastSearchMode; + private Enum lastSortBy; + private Enum lastSortDir; + private String lastSearch = ""; + + private boolean resort = true; + private boolean changed = false; + + public ItemRepo(final IScrollSource src, final ISortSource sortSrc) { this.src = src; this.sortSrc = sortSrc; @@ -83,6 +93,8 @@ public class ItemRepo { } else { this.list.add(is); } + + changed = true; } public long getItemCount(final IAEItemStack is) { @@ -96,18 +108,84 @@ public class ItemRepo { } public void updateView() { - this.view.clear(); - - this.view.ensureCapacity(this.list.size()); final Enum viewMode = this.sortSrc.getSortDisplay(); + + if (lastView != viewMode) { + resort = true; + lastView = viewMode; + } + final Enum searchMode = AEConfig.instance().getConfigManager().getSetting(Settings.SEARCH_MODE); - final boolean needsZeroCopy = viewMode == ViewItems.CRAFTABLE; + if (lastSearchMode != searchMode) { + resort = true; + lastSearchMode = searchMode; + } if (searchMode == SearchBoxMode.JEI_AUTOSEARCH || searchMode == SearchBoxMode.JEI_MANUAL_SEARCH || searchMode == SearchBoxMode.JEI_AUTOSEARCH_KEEP || searchMode == SearchBoxMode.JEI_MANUAL_SEARCH_KEEP) { this.updateJEI(this.searchString); } + if (!lastSearch.equals(searchString)) { + resort = true; + lastSearch = searchString; + } + + final Enum sortBy = this.sortSrc.getSortBy(); + final Enum sortDir = this.sortSrc.getSortDir(); + + if (lastSortBy != sortBy) { + resort = true; + lastSortBy = sortBy; + } + + if (lastSortDir != sortDir) { + resort = true; + lastSortDir = sortDir; + } + + if (changed || resort) { + changed = false; + resort = false; + + view = new ArrayList<>(); + + ItemSorters.setDirection((appeng.api.config.SortDir) sortDir); + ItemSorters.init(); + + Comparator c = getComparator(sortBy); + + for (IAEItemStack is : this.list) { + addIAE(is, viewMode); + } + + view.sort(c); + } + } + + private static Comparator getComparator(Enum sortBy) { + Comparator c; + + if (sortBy == SortOrder.MOD) { + c = ItemSorters.CONFIG_BASED_SORT_BY_MOD; + } else if (sortBy == SortOrder.AMOUNT) { + c = ItemSorters.CONFIG_BASED_SORT_BY_SIZE; + } else if (sortBy == SortOrder.INVTWEAKS) { + if (InventoryBogoSortModule.isLoaded()) { + c = InventoryBogoSortModule.COMPARATOR; + } else { + c = ItemSorters.CONFIG_BASED_SORT_BY_INV_TWEAKS; + } + } else { + c = ItemSorters.CONFIG_BASED_SORT_BY_NAME; + } + return c; + } + + private void addIAE(IAEItemStack is, Enum viewMode) { + + final boolean needsZeroCopy = viewMode == ViewItems.CRAFTABLE; + final boolean terminalSearchToolTips = AEConfig.instance().getConfigManager().getSetting(Settings.SEARCH_TOOLTIPS) != YesNo.NO; boolean searchMod = false; @@ -129,76 +207,54 @@ public class ItemRepo { } } - boolean notDone = false; - for (IAEItemStack is : this.list) { - if (this.myPartitionList != null) { - if (!this.myPartitionList.isListed(is)) { - continue; - } - } - - if (viewMode == ViewItems.CRAFTABLE && !is.isCraftable()) { - continue; - } - - if (viewMode == ViewItems.STORED && is.getStackSize() == 0) { - continue; - } - - final String dspName = (searchMod ? Platform.getModId(is) : Platform.getItemDisplayName(is)).toLowerCase(); - boolean foundMatchingItemStack = true; - - for (String term : innerSearch.split(" ")) { - if (term.length() > 1 && (term.startsWith("-") || term.startsWith("!"))) { - term = term.substring(1); - if (dspName.contains(term)) { - foundMatchingItemStack = false; - break; - } - } else if (!dspName.contains(term)) { - foundMatchingItemStack = false; - break; - } - } - - if (terminalSearchToolTips && !foundMatchingItemStack) { - final List tooltip = Platform.getTooltip(is); - for (final String line : tooltip) { - if (m.matcher(line).find()) { - foundMatchingItemStack = true; - break; - } - } - } - - if (foundMatchingItemStack) { - if (needsZeroCopy) { - is = is.copy(); - is.setStackSize(0); - } - - this.view.add(is); + if (this.myPartitionList != null) { + if (!this.myPartitionList.isListed(is)) { + return; } } - final Enum SortBy = this.sortSrc.getSortBy(); - final Enum SortDir = this.sortSrc.getSortDir(); + if (viewMode == ViewItems.CRAFTABLE && !is.isCraftable()) { + return; + } - ItemSorters.setDirection((appeng.api.config.SortDir) SortDir); - ItemSorters.init(); + if (viewMode == ViewItems.STORED && is.getStackSize() == 0) { + return; + } - if (SortBy == SortOrder.MOD) { - Collections.sort(this.view, ItemSorters.CONFIG_BASED_SORT_BY_MOD); - } else if (SortBy == SortOrder.AMOUNT) { - Collections.sort(this.view, ItemSorters.CONFIG_BASED_SORT_BY_SIZE); - } else if (SortBy == SortOrder.INVTWEAKS) { - if (InventoryBogoSortModule.isLoaded()) { - Collections.sort(this.view, InventoryBogoSortModule.COMPARATOR); - } else { - Collections.sort(this.view, ItemSorters.CONFIG_BASED_SORT_BY_INV_TWEAKS); + final String dspName = (searchMod ? Platform.getModId(is) : Platform.getItemDisplayName(is)).toLowerCase(); + boolean foundMatchingItemStack = true; + + for (String term : innerSearch.split(" ")) { + if (term.length() > 1 && (term.startsWith("-") || term.startsWith("!"))) { + term = term.substring(1); + if (dspName.contains(term)) { + foundMatchingItemStack = false; + break; + } + } else if (!dspName.contains(term)) { + foundMatchingItemStack = false; + break; + } + } + + if (terminalSearchToolTips && !foundMatchingItemStack) { + final List tooltip = Platform.getTooltip(is); + for (final String line : tooltip) { + if (m.matcher(line).find()) { + foundMatchingItemStack = true; + break; + } + } + } + + if (foundMatchingItemStack) { + if (needsZeroCopy) { + is = is.copy(); + is.setStackSize(0); + this.view.add(is); + } else if (is.getStackSize() > 0) { + this.view.add(is); } - } else { - Collections.sort(this.view, ItemSorters.CONFIG_BASED_SORT_BY_NAME); } }