diff --git a/src/main/java/refinedstorage/gui/GuiGrid.java b/src/main/java/refinedstorage/gui/GuiGrid.java index 27ece4711..bd1163403 100755 --- a/src/main/java/refinedstorage/gui/GuiGrid.java +++ b/src/main/java/refinedstorage/gui/GuiGrid.java @@ -28,6 +28,8 @@ public class GuiGrid extends GuiBase { private ContainerGrid container; private TileGrid grid; + private List items = new ArrayList(); + private GuiTextField searchField; private int hoveringSlotId; @@ -60,6 +62,56 @@ public class GuiGrid extends GuiBase { @Override public void update(int x, int y) { + items.clear(); + + if (grid.isConnected()) { + items.addAll(grid.getController().getItems()); + + if (!searchField.getText().trim().isEmpty()) { + Iterator t = items.iterator(); + + while (t.hasNext()) { + StorageItem item = t.next(); + + if (!item.toItemStack().getDisplayName().toLowerCase().contains(searchField.getText().toLowerCase())) { + t.remove(); + } + } + } + + Collections.sort(items, new Comparator() { + @Override + public int compare(StorageItem o1, StorageItem o2) { + if (o1 != null && o2 != null) { + if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_ASCENDING) { + return o2.toItemStack().getDisplayName().compareTo(o1.toItemStack().getDisplayName()); + } else if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_DESCENDING) { + return o1.toItemStack().getDisplayName().compareTo(o2.toItemStack().getDisplayName()); + } + } + + return 0; + } + }); + + if (grid.getSortingType() == TileGrid.SORTING_TYPE_QUANTITY) { + Collections.sort(items, new Comparator() { + @Override + public int compare(StorageItem o1, StorageItem o2) { + if (o1 != null && o2 != null) { + if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_ASCENDING) { + return Integer.valueOf(o2.getQuantity()).compareTo(o1.getQuantity()); + } else if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_DESCENDING) { + return Integer.valueOf(o1.getQuantity()).compareTo(o2.getQuantity()); + } + } + + return 0; + } + }); + } + } + scrollbar.setCanScroll(getRows() > getVisibleRows()); } @@ -72,12 +124,12 @@ public class GuiGrid extends GuiBase { return 0; } - int max = (int) Math.ceil((float) getItems().size() / (float) 9); + int max = (int) Math.ceil((float) items.size() / (float) 9); return max < 0 ? 0 : max; } - private boolean isHoveringOverValidSlot(List items) { + private boolean isHoveringOverValidSlot() { return grid.isConnected() && isHoveringOverSlot() && hoveringSlotId < items.size(); } @@ -124,8 +176,6 @@ public class GuiGrid extends GuiBase { int x = 8; int y = 20; - List items = getItems(); - hoveringSlotId = -1; int slot = getOffset() * 9; @@ -189,7 +239,7 @@ public class GuiGrid extends GuiBase { } } - if (isHoveringOverValidSlot(items)) { + if (isHoveringOverValidSlot()) { drawTooltip(mouseX, mouseY, items.get(hoveringSlotId).toItemStack()); } @@ -198,64 +248,6 @@ public class GuiGrid extends GuiBase { } } - public List getItems() { - List items = new ArrayList(); - - if (!grid.isConnected()) { - return items; - } - - synchronized (grid.getController()) { - items.addAll(grid.getController().getItems()); - - if (!searchField.getText().trim().isEmpty()) { - Iterator t = items.iterator(); - - while (t.hasNext()) { - StorageItem item = t.next(); - - if (!item.toItemStack().getDisplayName().toLowerCase().contains(searchField.getText().toLowerCase())) { - t.remove(); - } - } - } - - Collections.sort(items, new Comparator() { - @Override - public int compare(StorageItem o1, StorageItem o2) { - if (o1 != null && o2 != null) { - if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_ASCENDING) { - return o2.toItemStack().getDisplayName().compareTo(o1.toItemStack().getDisplayName()); - } else if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_DESCENDING) { - return o1.toItemStack().getDisplayName().compareTo(o2.toItemStack().getDisplayName()); - } - } - - return 0; - } - }); - - if (grid.getSortingType() == TileGrid.SORTING_TYPE_QUANTITY) { - Collections.sort(items, new Comparator() { - @Override - public int compare(StorageItem o1, StorageItem o2) { - if (o1 != null && o2 != null) { - if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_ASCENDING) { - return Integer.valueOf(o2.getQuantity()).compareTo(o1.getQuantity()); - } else if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_DESCENDING) { - return Integer.valueOf(o1.getQuantity()).compareTo(o2.getQuantity()); - } - } - - return 0; - } - }); - } - - return items; - } - } - @Override public void mouseClicked(int mouseX, int mouseY, int clickedButton) throws IOException { super.mouseClicked(mouseX, mouseY, clickedButton); @@ -267,7 +259,7 @@ public class GuiGrid extends GuiBase { if (isHoveringOverSlot() && container.getPlayer().inventory.getItemStack() != null) { RefinedStorage.NETWORK.sendToServer(new MessageStoragePush(controller.getPos().getX(), controller.getPos().getY(), controller.getPos().getZ(), -1, clickedButton == 1)); - } else if (isHoveringOverValidSlot(getItems()) && container.getPlayer().inventory.getItemStack() == null) { + } else if (isHoveringOverValidSlot() && container.getPlayer().inventory.getItemStack() == null) { boolean half = clickedButton == 1; boolean shift = GuiScreen.isShiftKeyDown(); boolean one = clickedButton == 2;