Skip to content

Commit

Permalink
feat: cache storage actuator search results for better performance
Browse files Browse the repository at this point in the history
Closes #1021
  • Loading branch information
klikli-dev committed Dec 30, 2023
1 parent 9718411 commit ef9e244
Showing 1 changed file with 8 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import java.util.stream.Collectors;

public abstract class StorageControllerGuiBase<T extends StorageControllerContainerBase> extends AbstractContainerScreen<T> implements IStorageControllerGui, IStorageControllerGuiContainer, ContainerListener {
//region Fields
public static final int ORDER_AREA_OFFSET = 48;
protected static final ResourceLocation BACKGROUND = new ResourceLocation(Occultism.MODID,
"textures/gui/storage_controller_droparea.png");
Expand Down Expand Up @@ -101,9 +100,9 @@ public abstract class StorageControllerGuiBase<T extends StorageControllerContai

protected boolean forceFocus;
protected long lastClick;
//endregion Fields
private List<ItemStack> cachedStacksToDisplay;
private String cachedSearchString;

//region Initialization
public StorageControllerGuiBase(T container, Inventory playerInventory, Component name) {
super(container, playerInventory, name);
this.storageControllerContainer = container;
Expand All @@ -126,9 +125,7 @@ public StorageControllerGuiBase(T container, Inventory playerInventory, Componen

OccultismPackets.sendToServer(new MessageRequestStacks());
}
//endregion Initialization

//region Getter / Setter
protected abstract boolean isGuiValid();

protected abstract BlockPos getEntityPosition();
Expand All @@ -141,9 +138,6 @@ public StorageControllerGuiBase(T container, Inventory playerInventory, Componen

public abstract void setSortType(SortType sortType);

//endregion Getter / Setter

//region Overrides
@Override
public Font getFontRenderer() {
return this.font;
Expand Down Expand Up @@ -425,9 +419,7 @@ public boolean charTyped(char typedChar, int keyCode) {

return false;
}
//endregion Overrides

//region Methods
public void initButtons() {
int controlButtonSize = 12;

Expand Down Expand Up @@ -706,11 +698,8 @@ protected void buildPage(List<?> objectsToDisplay) {
protected void sortItemStacks(List<ItemStack> stacksToDisplay) {
stacksToDisplay.sort(new Comparator<ItemStack>() {

//region Fields
final int direction = StorageControllerGuiBase.this.getSortDirection().isDown() ? -1 : 1;
//endregion Fields

//region Overrides
@Override
public int compare(ItemStack a, ItemStack b) {
switch (StorageControllerGuiBase.this.getSortType()) {
Expand All @@ -727,19 +716,24 @@ public int compare(ItemStack a, ItemStack b) {
}
return 0;
}
//endregion Overrides
});
}

protected List<ItemStack> applySearchToItems() {
String searchText = this.searchBar.getValue();

if (!searchText.equals("")) {
if (this.cachedStacksToDisplay != null && this.cachedSearchString != null && this.cachedSearchString.equals(searchText))
return this.cachedStacksToDisplay;

List<ItemStack> stacksToDisplay = new ArrayList<>();
for (ItemStack stack : this.stacks) {
if (this.itemMatchesSearch(stack))
stacksToDisplay.add(stack);
}

this.cachedStacksToDisplay = stacksToDisplay;
this.cachedSearchString = searchText;
return stacksToDisplay;
}
return new ArrayList<>(this.stacks);
Expand Down Expand Up @@ -802,11 +796,8 @@ protected void sortMachines(List<MachineReference> machinesToDisplay) {
ResourceKey<Level> dimensionKey = this.minecraft.player.level.dimension();
machinesToDisplay.sort(new Comparator<MachineReference>() {

//region Fields
final int direction = StorageControllerGuiBase.this.getSortDirection().isDown() ? -1 : 1;
//endregion Fields

//region Overrides
@Override
public int compare(MachineReference a, MachineReference b) {
switch (StorageControllerGuiBase.this.getSortType()) {
Expand All @@ -830,7 +821,6 @@ public int compare(MachineReference a, MachineReference b) {
}
return 0;
}
//endregion Overrides
});
}

Expand Down Expand Up @@ -867,5 +857,4 @@ protected void clearSearch() {
}
}

//endregion Methods
}

0 comments on commit ef9e244

Please sign in to comment.