Skip to content

Commit

Permalink
[MC1.16] [Client] Performance improvements for grid view (#2705)
Browse files Browse the repository at this point in the history
* Make Crafting Manager AND search terms, not OR them

Plus refactor getFilters to avoid passing raw lists of filters around
and instead use GridFilter objects that combine conditions through OR
and AND.

* Don't make all items vanish if query ends in `|`

* Split out Comparator and Predicate generation for GridView into 2 separate functions

* Move common code up into BaseGridView; remove identical subclasses

This also follows the Law of Demeter a litle better.

* Avoid sorting entire grid view on single item update

Instead, use binary search and insert.

* Ensure crafting stack is removed/inserted to complement original stack
  • Loading branch information
ScoreUnder authored Jan 2, 2021
1 parent 6b8daf1 commit 74ab430
Show file tree
Hide file tree
Showing 20 changed files with 285 additions and 289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void initSlots(@Nullable Map<String, Integer> data) {
int y = 19 + 18 - screenInfoProvider.getCurrentOffset() * 18;
int x = 8;

List<Predicate<IGridStack>> filters = GridFilterParser.getFilters(null, screenInfoProvider.getSearchFieldText(), Collections.emptyList());
Predicate<IGridStack> filters = GridFilterParser.getFilters(null, screenInfoProvider.getSearchFieldText(), Collections.emptyList());

for (Map.Entry<String, Integer> category : containerData.entrySet()) {
IItemHandlerModifiable dummy;
Expand Down Expand Up @@ -124,11 +124,9 @@ public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate
for (ItemStack output : pattern.getOutputs()) {
ItemGridStack outputConverted = new ItemGridStack(output);

for (Predicate<IGridStack> filter : filters) {
if (filter.test(outputConverted)) {
visible = true;
break;
}
if (filters.test(outputConverted)) {
visible = true;
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public static void encode(GridFluidDeltaMessage message, PacketBuffer buf) {
public static void handle(GridFluidDeltaMessage message, Supplier<NetworkEvent.Context> ctx) {
BaseScreen.executeLater(GridScreen.class, grid -> {
message.clientDeltas.forEach(p -> grid.getView().postChange(p.getLeft(), p.getRight()));

grid.getView().sort();
});

ctx.get().setPacketHandled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.refinedmods.refinedstorage.screen.BaseScreen;
import com.refinedmods.refinedstorage.screen.grid.GridScreen;
import com.refinedmods.refinedstorage.screen.grid.stack.IGridStack;
import com.refinedmods.refinedstorage.screen.grid.view.FluidGridView;
import com.refinedmods.refinedstorage.screen.grid.view.GridViewImpl;
import com.refinedmods.refinedstorage.util.StackUtils;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fluids.FluidStack;
Expand Down Expand Up @@ -68,7 +68,7 @@ public static void encode(GridFluidUpdateMessage message, PacketBuffer buf) {

public static void handle(GridFluidUpdateMessage message, Supplier<NetworkEvent.Context> ctx) {
BaseScreen.executeLater(GridScreen.class, grid -> {
grid.setView(new FluidGridView(grid, GridScreen.getDefaultSorter(), GridScreen.getSorters()));
grid.setView(new GridViewImpl(grid, GridScreen.getDefaultSorter(), GridScreen.getSorters()));
grid.getView().setCanCraft(message.canCraft);
grid.getView().setStacks(message.stacks);
grid.getView().sort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public static void encode(GridItemDeltaMessage message, PacketBuffer buf) {
public static void handle(GridItemDeltaMessage message, Supplier<NetworkEvent.Context> ctx) {
BaseScreen.executeLater(GridScreen.class, grid -> {
message.clientDeltas.forEach(p -> grid.getView().postChange(p.getLeft(), p.getRight()));

grid.getView().sort();
});

ctx.get().setPacketHandled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.refinedmods.refinedstorage.screen.BaseScreen;
import com.refinedmods.refinedstorage.screen.grid.GridScreen;
import com.refinedmods.refinedstorage.screen.grid.stack.IGridStack;
import com.refinedmods.refinedstorage.screen.grid.view.ItemGridView;
import com.refinedmods.refinedstorage.screen.grid.view.GridViewImpl;
import com.refinedmods.refinedstorage.util.StackUtils;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
Expand Down Expand Up @@ -68,7 +68,7 @@ public static void encode(GridItemUpdateMessage message, PacketBuffer buf) {

public static void handle(GridItemUpdateMessage message, Supplier<NetworkEvent.Context> ctx) {
BaseScreen.executeLater(GridScreen.class, grid -> {
grid.setView(new ItemGridView(grid, GridScreen.getDefaultSorter(), GridScreen.getSorters()));
grid.setView(new GridViewImpl(grid, GridScreen.getDefaultSorter(), GridScreen.getSorters()));
grid.getView().setCanCraft(message.canCraft);
grid.getView().setStacks(message.stacks);
grid.getView().sort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public static void encode(PortableGridFluidDeltaMessage message, PacketBuffer bu
public static void handle(PortableGridFluidDeltaMessage message, Supplier<NetworkEvent.Context> ctx) {
BaseScreen.executeLater(GridScreen.class, grid -> {
message.clientDeltas.forEach(p -> grid.getView().postChange(p.getLeft(), p.getRight()));

grid.getView().sort();
});

ctx.get().setPacketHandled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.refinedmods.refinedstorage.screen.BaseScreen;
import com.refinedmods.refinedstorage.screen.grid.GridScreen;
import com.refinedmods.refinedstorage.screen.grid.stack.IGridStack;
import com.refinedmods.refinedstorage.screen.grid.view.FluidGridView;
import com.refinedmods.refinedstorage.screen.grid.view.GridViewImpl;
import com.refinedmods.refinedstorage.tile.grid.portable.IPortableGrid;
import com.refinedmods.refinedstorage.util.StackUtils;
import net.minecraft.network.PacketBuffer;
Expand Down Expand Up @@ -52,7 +52,7 @@ public static void encode(PortableGridFluidUpdateMessage message, PacketBuffer b

public static void handle(PortableGridFluidUpdateMessage message, Supplier<NetworkEvent.Context> ctx) {
BaseScreen.executeLater(GridScreen.class, grid -> {
grid.setView(new FluidGridView(grid, GridScreen.getDefaultSorter(), GridScreen.getSorters()));
grid.setView(new GridViewImpl(grid, GridScreen.getDefaultSorter(), GridScreen.getSorters()));
grid.getView().setStacks(message.stacks);
grid.getView().sort();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public static void encode(PortableGridItemDeltaMessage message, PacketBuffer buf
public static void handle(PortableGridItemDeltaMessage message, Supplier<NetworkEvent.Context> ctx) {
BaseScreen.executeLater(GridScreen.class, grid -> {
message.clientDeltas.forEach(p -> grid.getView().postChange(p.getLeft(), p.getRight()));

grid.getView().sort();
});

ctx.get().setPacketHandled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.refinedmods.refinedstorage.screen.BaseScreen;
import com.refinedmods.refinedstorage.screen.grid.GridScreen;
import com.refinedmods.refinedstorage.screen.grid.stack.IGridStack;
import com.refinedmods.refinedstorage.screen.grid.view.ItemGridView;
import com.refinedmods.refinedstorage.screen.grid.view.GridViewImpl;
import com.refinedmods.refinedstorage.tile.grid.portable.IPortableGrid;
import com.refinedmods.refinedstorage.util.StackUtils;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -52,7 +52,7 @@ public static void encode(PortableGridItemUpdateMessage message, PacketBuffer bu

public static void handle(PortableGridItemUpdateMessage message, Supplier<NetworkEvent.Context> ctx) {
BaseScreen.executeLater(GridScreen.class, grid -> {
grid.setView(new ItemGridView(grid, GridScreen.getDefaultSorter(), GridScreen.getSorters()));
grid.setView(new GridViewImpl(grid, GridScreen.getDefaultSorter(), GridScreen.getSorters()));
grid.getView().setStacks(message.stacks);
grid.getView().sort();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
import com.refinedmods.refinedstorage.screen.grid.sorting.*;
import com.refinedmods.refinedstorage.screen.grid.stack.IGridStack;
import com.refinedmods.refinedstorage.screen.grid.stack.ItemGridStack;
import com.refinedmods.refinedstorage.screen.grid.view.FluidGridView;
import com.refinedmods.refinedstorage.screen.grid.view.GridViewImpl;
import com.refinedmods.refinedstorage.screen.grid.view.IGridView;
import com.refinedmods.refinedstorage.screen.grid.view.ItemGridView;
import com.refinedmods.refinedstorage.screen.widget.CheckboxWidget;
import com.refinedmods.refinedstorage.screen.widget.ScrollbarWidget;
import com.refinedmods.refinedstorage.screen.widget.SearchWidget;
Expand Down Expand Up @@ -70,7 +69,7 @@ public GridScreen(GridContainer container, IGrid grid, PlayerInventory inventory
super(container, 227, 0, inventory, title);

this.grid = grid;
this.view = grid.getGridType() == GridType.FLUID ? new FluidGridView(this, getDefaultSorter(), getSorters()) : new ItemGridView(this, getDefaultSorter(), getSorters());
this.view = new GridViewImpl(this, getDefaultSorter(), getSorters());
this.wasConnected = this.grid.isGridActive();
this.tabs = new TabListWidget<>(this, new ElementDrawers<>(this), grid::getTabs, grid::getTotalTabPages, grid::getTabPage, grid::getTabSelected, IGrid.TABS_PER_PAGE);
this.tabs.addListener(new TabListWidget.ITabListListener() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.refinedmods.refinedstorage.screen.grid.filtering;

import com.refinedmods.refinedstorage.screen.grid.stack.IGridStack;

import java.util.List;
import java.util.function.Predicate;

public class AndGridFilter implements Predicate<IGridStack> {
private final List<Predicate<IGridStack>> andPartFilters;

private AndGridFilter(List<Predicate<IGridStack>> andPartFilters) {
this.andPartFilters = andPartFilters;
}

@Override
public boolean test(IGridStack gridStack) {
for (Predicate<IGridStack> part : andPartFilters) {
if (!part.test(gridStack)) {
return false;
}
}

return true;
}

public static Predicate<IGridStack> of(List<Predicate<IGridStack>> filters) {
if (filters.isEmpty()) {
return t -> true;
}
if (filters.size() == 1) {
return filters.get(0);
}
return new AndGridFilter(filters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ public final class GridFilterParser {
private GridFilterParser() {
}

public static List<Predicate<IGridStack>> getFilters(@Nullable IGrid grid, String query, List<IFilter> filters) {
public static Predicate<IGridStack> getFilters(@Nullable IGrid grid, String query, List<IFilter> filters) {
List<Predicate<IGridStack>> gridFilters;

String[] orParts = query.split("\\|");

if (orParts.length == 1) {
gridFilters = getFilters(query);
gridFilters = getFilters(orParts[0]);
} else {
List<List<Predicate<IGridStack>>> orPartFilters = new LinkedList<>();
List<Predicate<IGridStack>> orPartFilters = new LinkedList<>();

for (String orPart : orParts) {
orPartFilters.add(getFilters(orPart));
orPartFilters.add(AndGridFilter.of(getFilters(orPart)));
}

gridFilters = new LinkedList<>();
gridFilters.add(new OrGridFilter(orPartFilters));
gridFilters.add(OrGridFilter.of(orPartFilters));
}

if (grid != null) {
Expand All @@ -43,7 +43,7 @@ public static List<Predicate<IGridStack>> getFilters(@Nullable IGrid grid, Strin
gridFilters.add(new FilterGridFilter(filters));
}

return gridFilters;
return AndGridFilter.of(gridFilters);
}

private static List<Predicate<IGridStack>> getFilters(String query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@
import java.util.function.Predicate;

public class OrGridFilter implements Predicate<IGridStack> {
private final List<List<Predicate<IGridStack>>> orPartFilters;
private final List<Predicate<IGridStack>> orPartFilters;

public OrGridFilter(List<List<Predicate<IGridStack>>> orPartFilters) {
private OrGridFilter(List<Predicate<IGridStack>> orPartFilters) {
this.orPartFilters = orPartFilters;
}

@Override
public boolean test(IGridStack gridStack) {
for (List<Predicate<IGridStack>> orPart : orPartFilters) {
for (Predicate<IGridStack> part : orPart) {
if (part.test(gridStack)) {
return true;
}
for (Predicate<IGridStack> part : orPartFilters) {
if (part.test(gridStack)) {
return true;
}
}

return false;
}

public static Predicate<IGridStack> of(List<Predicate<IGridStack>> filters) {
if (filters.isEmpty()) {
return t -> false;
}
if (filters.size() == 1) {
return filters.get(0);
}
return new OrGridFilter(filters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,16 @@ public List<ITextComponent> getTooltip(boolean bypassCache) {
@Override
public int getQuantity() {
// The isCraftable check is needed so sorting is applied correctly
return isCraftable() ? 0 : stack.getAmount();
return isCraftable() || zeroed ? 0 : stack.getAmount();
}

@Override
public void setQuantity(int amount) {
if (amount <= 0) {
setZeroed(true);
} else {
stack.setAmount(amount);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public interface IGridStack {

int getQuantity();

void setQuantity(int amount);

String getFormattedFullQuantity();

void draw(MatrixStack matrixStack, BaseScreen<?> screen, int x, int y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,16 @@ public List<ITextComponent> getTooltip(boolean bypassCache) {
@Override
public int getQuantity() {
// The isCraftable check is needed so sorting is applied correctly
return isCraftable() ? 0 : stack.getCount();
return isCraftable() || zeroed ? 0 : stack.getCount();
}

@Override
public void setQuantity(int amount) {
if (amount <= 0) {
setZeroed(true);
} else {
stack.setCount(amount);
}
}

@Override
Expand Down
Loading

0 comments on commit 74ab430

Please sign in to comment.