Merge branch 'item-repo-sort' into AE2-Omnifactory

This commit is contained in:
PrototypeTrousers
2023-07-03 20:22:06 -03:00
2 changed files with 124 additions and 73 deletions
@@ -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;
+124 -70
View File
@@ -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<IAEItemStack> list = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
private final ArrayList<IAEItemStack> view = new ArrayList<>();
private List<IAEItemStack> 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<IAEItemStack> c = getComparator(sortBy);
for (IAEItemStack is : this.list) {
addIAE(is, viewMode);
}
view.sort(c);
}
}
private static Comparator<IAEItemStack> getComparator(Enum sortBy) {
Comparator<IAEItemStack> 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,52 @@ 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<String> 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;
}
} else {
Collections.sort(this.view, ItemSorters.CONFIG_BASED_SORT_BY_NAME);
}
if (terminalSearchToolTips && !foundMatchingItemStack) {
final List<String> 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);
}
}