Compare commits

...

3 Commits

Author SHA1 Message Date
PrototypeTrousers 3ae251f098 Revert "Merge remote-tracking branch 'LasmGratel/async-itemrepo' into AE2-Omnifactory"
This reverts commit 86a42e970f.
2023-01-09 01:03:55 -03:00
PrototypeTrousers 82a906886f Revert "delay repo view sorting to once per tick"
This reverts commit d9588441b0.
2023-01-09 01:03:51 -03:00
PrototypeTrousers 5b2c6658c0 Revert "synchronizedList must be in a synchronized block when iterating"
This reverts commit 1f12fc5e91.
2023-01-09 01:03:51 -03:00
7 changed files with 101 additions and 171 deletions
@@ -39,7 +39,7 @@ import appeng.api.storage.IStorageChannel;
* - For fluids: AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class).createList() * - For fluids: AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class).createList()
* - Replace with the corresponding {@link IStorageChannel} type for non native channels * - Replace with the corresponding {@link IStorageChannel} type for non native channels
*/ */
public interface IItemList<T extends IAEStack<T>> extends IItemContainer<T>, Iterable<T>, Cloneable public interface IItemList<T extends IAEStack<T>> extends IItemContainer<T>, Iterable<T>
{ {
/** /**
@@ -85,9 +85,4 @@ public interface IItemList<T extends IAEStack<T>> extends IItemContainer<T>, Ite
* resets stack sizes to 0. * resets stack sizes to 0.
*/ */
void resetStatus(); void resetStatus();
/**
* create a copy of this list.
*/
IItemList<T> clone();
} }
@@ -103,7 +103,6 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi
private int currentMouseY = 0; private int currentMouseY = 0;
private boolean delayedUpdate; private boolean delayedUpdate;
private boolean updateView = true;
protected int jeiOffset = Loader.isModLoaded("jei") ? 24 : 0; protected int jeiOffset = Loader.isModLoaded("jei") ? 24 : 0;
public GuiMEMonitorable(final InventoryPlayer inventoryPlayer, final ITerminalHost te) { public GuiMEMonitorable(final InventoryPlayer inventoryPlayer, final ITerminalHost te) {
@@ -162,7 +161,8 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi
} }
if (!this.delayedUpdate) { if (!this.delayedUpdate) {
this.updateView = true; this.repo.updateView();
this.setScrollBar();
} }
} }
@@ -319,7 +319,8 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi
this.searchField.setText(memoryText); this.searchField.setText(memoryText);
this.searchField.selectAll(); this.searchField.selectAll();
this.repo.setSearchString(memoryText); this.repo.setSearchString(memoryText);
this.updateView = true; this.repo.updateView();
this.setScrollBar();
} }
craftingGridOffsetX = Integer.MAX_VALUE; craftingGridOffsetX = Integer.MAX_VALUE;
@@ -380,7 +381,8 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi
if (btn == 1 && this.searchField.isMouseIn(xCoord, yCoord)) { if (btn == 1 && this.searchField.isMouseIn(xCoord, yCoord)) {
this.searchField.setText(""); this.searchField.setText("");
this.repo.setSearchString(""); this.repo.setSearchString("");
this.updateView = true; this.repo.updateView();
this.setScrollBar();
} }
super.mouseClicked(xCoord, yCoord, btn); super.mouseClicked(xCoord, yCoord, btn);
@@ -474,7 +476,8 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi
if (this.searchField.textboxKeyTyped(character, key)) { if (this.searchField.textboxKeyTyped(character, key)) {
this.repo.setSearchString(this.searchField.getText()); this.repo.setSearchString(this.searchField.getText());
this.updateView = true; this.repo.updateView();
this.setScrollBar();
// tell forge the key event is handled and should not be sent out // tell forge the key event is handled and should not be sent out
this.keyHandled = mouseInGui; this.keyHandled = mouseInGui;
} else { } else {
@@ -501,7 +504,7 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi
this.delayedUpdate = false; this.delayedUpdate = false;
} }
} }
if (!this.delayedUpdate && updateView) { if (!this.delayedUpdate) {
this.repo.updateView(); this.repo.updateView();
this.setScrollBar(); this.setScrollBar();
} }
@@ -537,7 +540,7 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi
this.ViewBox.set(this.configSrc.getSetting(Settings.VIEW_MODE)); this.ViewBox.set(this.configSrc.getSetting(Settings.VIEW_MODE));
} }
this.updateView = true; this.repo.updateView();
} }
int getReservedSpace() { int getReservedSpace() {
@@ -55,8 +55,6 @@ public class GuiNetworkStatus extends AEBaseGui implements ISortSource {
private GuiImgButton units; private GuiImgButton units;
private int tooltip = -1; private int tooltip = -1;
private boolean updateView = true;
public GuiNetworkStatus(final InventoryPlayer inventoryPlayer, final INetworkTool te) { public GuiNetworkStatus(final InventoryPlayer inventoryPlayer, final INetworkTool te) {
super(new ContainerNetworkStatus(inventoryPlayer, te)); super(new ContainerNetworkStatus(inventoryPlayer, te));
final GuiScrollbar scrollbar = new GuiScrollbar(); final GuiScrollbar scrollbar = new GuiScrollbar();
@@ -208,17 +206,9 @@ public class GuiNetworkStatus extends AEBaseGui implements ISortSource {
for (final IAEItemStack is : list) { for (final IAEItemStack is : list) {
this.repo.postUpdate(is); this.repo.postUpdate(is);
} }
this.updateView = true;
}
@Override
public void updateScreen() {
if (updateView) {
this.repo.updateView(); this.repo.updateView();
this.setScrollBar(); this.setScrollBar();
updateView = false;
}
super.updateScreen();
} }
private void setScrollBar() { private void setScrollBar() {
+27 -65
View File
@@ -39,22 +39,19 @@ import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class ItemRepo { public class ItemRepo {
private final IItemList<IAEItemStack> list = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList(); private final IItemList<IAEItemStack> list = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
private final List<IAEItemStack> view; private final ArrayList<IAEItemStack> view = new ArrayList<>();
private List<IAEItemStack> asyncUpdatedView;
private boolean updated;
private final IScrollSource src; private final IScrollSource src;
private final ISortSource sortSrc; private final ISortSource sortSrc;
private int rowSize = 9; private int rowSize = 9;
private volatile String searchString = ""; private String searchString = "";
private IPartitionList<IAEItemStack> myPartitionList; private IPartitionList<IAEItemStack> myPartitionList;
private String innerSearch = ""; private String innerSearch = "";
private boolean hasPower; private boolean hasPower;
@@ -62,14 +59,11 @@ public class ItemRepo {
public ItemRepo(final IScrollSource src, final ISortSource sortSrc) { public ItemRepo(final IScrollSource src, final ISortSource sortSrc) {
this.src = src; this.src = src;
this.sortSrc = sortSrc; this.sortSrc = sortSrc;
this.view = Collections.synchronizedList(new ArrayList<>());
this.asyncUpdatedView = Collections.synchronizedList(new ArrayList<>());
list.forEach(this.view::add);
} }
public IAEItemStack getReferenceItem(int idx) { public IAEItemStack getReferenceItem(int idx) {
idx += this.src.getCurrentScroll() * this.rowSize; idx += this.src.getCurrentScroll() * this.rowSize;
if (idx >= this.view.size()) { if (idx >= this.view.size()) {
return null; return null;
} }
@@ -101,51 +95,42 @@ public class ItemRepo {
this.updateView(); this.updateView();
} }
private CompletableFuture<Void> searchTask = null;
public void updateView() { public void updateView() {
if (searchTask != null) {
return;
}
if (updated) {
this.view.clear(); this.view.clear();
this.view.addAll(asyncUpdatedView);
this.asyncUpdatedView.clear(); this.view.ensureCapacity(this.list.size());
this.updated = false;
final Enum viewMode = this.sortSrc.getSortDisplay();
final Enum searchMode = AEConfig.instance().getConfigManager().getSetting(Settings.SEARCH_MODE);
final boolean needsZeroCopy = viewMode == ViewItems.CRAFTABLE;
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);
} }
// Since sortSrc is final, so we can safely call it inside lambda final boolean terminalSearchToolTips = AEConfig.instance().getConfigManager().getSetting(Settings.SEARCH_TOOLTIPS) != YesNo.NO;
searchTask = CompletableFuture.supplyAsync(() -> {
IItemList<IAEItemStack> list = this.list.clone();
List<IAEItemStack> view = new ArrayList<>(list.size());
Enum viewMode = this.sortSrc.getSortDisplay();
boolean needsZeroCopy = viewMode == ViewItems.CRAFTABLE;
boolean terminalSearchToolTips = AEConfig.instance().getConfigManager().getSetting(Settings.SEARCH_TOOLTIPS) != YesNo.NO;
boolean searchMod = false; boolean searchMod = false;
String innerSearch = searchString.toLowerCase(); this.innerSearch = searchString.toLowerCase();
if (innerSearch.startsWith("@")) { if (this.innerSearch.startsWith("@")) {
searchMod = true; searchMod = true;
innerSearch = innerSearch.substring(1); this.innerSearch = this.innerSearch.substring(1);
} }
Pattern m = null; Pattern m = null;
try { try {
m = Pattern.compile(innerSearch, Pattern.CASE_INSENSITIVE); m = Pattern.compile(this.innerSearch, Pattern.CASE_INSENSITIVE);
} catch (final Throwable ignore) { } catch (final Throwable ignore) {
try { try {
m = Pattern.compile(Pattern.quote(innerSearch), Pattern.CASE_INSENSITIVE); m = Pattern.compile(Pattern.quote(this.innerSearch), Pattern.CASE_INSENSITIVE);
} catch (final Throwable __) { } catch (final Throwable __) {
return Collections.<IAEItemStack>emptyList(); return;
} }
} }
boolean notDone = false;
for (IAEItemStack is : list) { for (IAEItemStack is : this.list) {
if (this.myPartitionList != null) { if (this.myPartitionList != null) {
if (!this.myPartitionList.isListed(is)) { if (!this.myPartitionList.isListed(is)) {
continue; continue;
@@ -192,7 +177,7 @@ public class ItemRepo {
is.setStackSize(0); is.setStackSize(0);
} }
view.add(is); this.view.add(is);
} }
} }
@@ -203,26 +188,18 @@ public class ItemRepo {
ItemSorters.init(); ItemSorters.init();
if (SortBy == SortOrder.MOD) { if (SortBy == SortOrder.MOD) {
view.sort(ItemSorters.CONFIG_BASED_SORT_BY_MOD); Collections.sort(this.view, ItemSorters.CONFIG_BASED_SORT_BY_MOD);
} else if (SortBy == SortOrder.AMOUNT) { } else if (SortBy == SortOrder.AMOUNT) {
view.sort(ItemSorters.CONFIG_BASED_SORT_BY_SIZE); Collections.sort(this.view, ItemSorters.CONFIG_BASED_SORT_BY_SIZE);
} else if (SortBy == SortOrder.INVTWEAKS) { } else if (SortBy == SortOrder.INVTWEAKS) {
if (InventoryBogoSortModule.isLoaded()) { if (InventoryBogoSortModule.isLoaded()) {
view.sort(InventoryBogoSortModule.COMPARATOR); Collections.sort(this.view, InventoryBogoSortModule.COMPARATOR);
} else { } else {
view.sort(ItemSorters.CONFIG_BASED_SORT_BY_INV_TWEAKS); Collections.sort(this.view, ItemSorters.CONFIG_BASED_SORT_BY_INV_TWEAKS);
} }
} else { } else {
view.sort(ItemSorters.CONFIG_BASED_SORT_BY_NAME); Collections.sort(this.view, ItemSorters.CONFIG_BASED_SORT_BY_NAME);
} }
return view;
}).thenAcceptAsync(view -> {
this.updated = true;
this.asyncUpdatedView.addAll(view);
}).thenRunAsync(() -> {
this.searchTask = null; // Prevent redundant cancellation
});
} }
private void updateJEI(String filter) { private void updateJEI(String filter) {
@@ -234,10 +211,6 @@ public class ItemRepo {
} }
public void clear() { public void clear() {
if (searchTask != null) {
searchTask.cancel(true);
searchTask = null;
}
this.list.resetStatus(); this.list.resetStatus();
} }
@@ -263,17 +236,6 @@ public class ItemRepo {
public void setSearchString(@Nonnull final String searchString) { public void setSearchString(@Nonnull final String searchString) {
this.searchString = searchString; this.searchString = searchString;
if (searchTask != null) {
searchTask.cancel(true);
searchTask = null;
}
// Passive JEI auto search
final Enum<?> searchMode = AEConfig.instance().getConfigManager().getSetting(Settings.SEARCH_MODE);
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);
}
} }
public IItemList<IAEItemStack> getList() { public IItemList<IAEItemStack> getList() {
@@ -161,13 +161,6 @@ public final class FluidList implements IItemList<IAEFluidStack> {
} }
} }
@Override
public FluidList clone() {
FluidList list = new FluidList();
list.records.putAll(records);
return list;
}
private IAEFluidStack getFluidRecord(final IAEFluidStack fluid) { private IAEFluidStack getFluidRecord(final IAEFluidStack fluid) {
return this.records.get(fluid); return this.records.get(fluid);
} }
@@ -94,9 +94,4 @@ public class ItemListIgnoreCrafting<T extends IAEStack<T>> implements IItemList<
public void resetStatus() { public void resetStatus() {
this.target.resetStatus(); this.target.resetStatus();
} }
@Override
public ItemListIgnoreCrafting<T> clone() {
return new ItemListIgnoreCrafting<>(target);
}
} }
@@ -140,14 +140,6 @@ public final class ItemList implements IItemList<IAEItemStack> {
} }
} }
@Override
public ItemList clone() {
ItemList list = new ItemList();
list.records.putAll(records);
list.version.set(version.get());
return list;
}
private ItemVariantList getOrCreateRecord(Item item) { private ItemVariantList getOrCreateRecord(Item item) {
return this.records.computeIfAbsent(item, this::makeRecordMap); return this.records.computeIfAbsent(item, this::makeRecordMap);
} }