Skip to content

Commit

Permalink
Merge pull request #348 from refinedmods/feat/GH-77/crafting-matrix-s…
Browse files Browse the repository at this point in the history
…lot-filter

feat: add crafting matrix item filter
  • Loading branch information
raoulvdberge authored Mar 4, 2023
2 parents 1ce1460 + 1967c8b commit 672f3f2
Show file tree
Hide file tree
Showing 19 changed files with 153 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
The keybinding is only available on Forge.
- A config option to clear items from the Crafting Grid crafting matrix to the player or network inventory.
- Support for collapsable entries for REI.
- Pressing CTRL + SHIFT on the crafting result slot filters the Grid view based on the items in the crafting matrix.
The reason for this is that you can quickly see how much you have left in the storage network.

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public interface GridView {

/**
* @param predicate the filter
* @return the previous filtering predicate
*/
void setFilterAndSort(Predicate<GridResource> predicate);
Predicate<GridResource> setFilterAndSort(Predicate<GridResource> predicate);

/**
* Preventing sorting means that the changes will still arrive at the backing list and view list, but,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ public void setSortingType(final GridSortingType sortingType) {
}

@Override
public void setFilterAndSort(final Predicate<GridResource> predicate) {
public Predicate<GridResource> setFilterAndSort(final Predicate<GridResource> predicate) {
final Predicate<GridResource> previousPredicate = filter;
this.filter = predicate;
sort();
return previousPredicate;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.Comparator;
import java.util.Optional;
import java.util.function.Predicate;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -24,8 +25,8 @@ class GridViewImplTest {
void setUp() {
viewBuilder = new GridViewBuilderImpl(
resourceAmount -> Optional.of(new GridResourceImpl(resourceAmount)),
(view) -> Comparator.comparing(GridResource::getName),
(view) -> Comparator.comparing(GridResource::getAmount)
view -> Comparator.comparing(GridResource::getName),
view -> Comparator.comparing(GridResource::getAmount)
);
}

Expand Down Expand Up @@ -149,6 +150,29 @@ void shouldInsertNewResource() {
verify(listener, times(1)).run();
}

@Test
void shouldSetFilterAndSort() {
// Arrange
final GridView view = viewBuilder
.withResource("A", 10, null)
.withResource("B", 10, null)
.build();

final Predicate<GridResource> filterA = resource -> resource.getName().equals("A");
final Predicate<GridResource> filterB = resource -> resource.getName().equals("B");

// Act
final Predicate<GridResource> previousFilter1 = view.setFilterAndSort(filterA);
final Predicate<GridResource> previousFilter2 = view.setFilterAndSort(filterB);

// Assert
assertThat(previousFilter1).isNotNull();
assertThat(previousFilter2).isEqualTo(filterA);
assertThat(view.getViewList()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new GridResourceImpl("B", 10)
);
}

@Test
void shouldNotInsertNewResourceWhenFilteringProhibitsIt() {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.refinedmods.refinedstorage2.platform.common.containermenu.grid;

import com.refinedmods.refinedstorage2.api.grid.view.GridResource;
import com.refinedmods.refinedstorage2.api.resource.list.ResourceList;
import com.refinedmods.refinedstorage2.platform.api.resource.ItemResource;
import com.refinedmods.refinedstorage2.platform.common.block.entity.grid.CraftingGridBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.content.Menus;
import com.refinedmods.refinedstorage2.platform.common.internal.grid.view.ItemGridResource;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import javax.annotation.Nullable;

import net.minecraft.network.FriendlyByteBuf;
Expand All @@ -21,9 +27,12 @@ public class CraftingGridContainerMenu extends AbstractGridContainerMenu {

private final Player player;
private final CraftingGridSource source;
private final List<Slot> craftingMatrixSlots = new ArrayList<>();

@Nullable
private Consumer<Boolean> activenessListener;
@Nullable
private Predicate<GridResource> filterBeforeFilteringBasedOnCraftingMatrixItems;

public CraftingGridContainerMenu(final int syncId, final Inventory playerInventory, final FriendlyByteBuf buf) {
super(Menus.INSTANCE.getCraftingGrid(), syncId, playerInventory, buf);
Expand Down Expand Up @@ -72,13 +81,14 @@ public ItemStack quickMoveStack(final Player actor, final int slotIndex) {
@Override
public void addSlots(final int playerInventoryY) {
super.addSlots(playerInventoryY);
craftingMatrixSlots.clear();
for (int y = 0; y < 3; ++y) {
for (int x = 0; x < 3; ++x) {
final int slotX = 26 + ((x % 3) * 18);
final int slotY = playerInventoryY
- Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_FIRST_CRAFTING_MATRIX_SLOT
+ ((y % 3) * 18);
addSlot(new Slot(source.getCraftingMatrix(), x + y * 3, slotX, slotY));
craftingMatrixSlots.add(addSlot(new Slot(source.getCraftingMatrix(), x + y * 3, slotX, slotY)));
}
}
addSlot(new CraftingGridResultSlot(
Expand All @@ -89,6 +99,10 @@ public void addSlots(final int playerInventoryY) {
));
}

public List<Slot> getCraftingMatrixSlots() {
return craftingMatrixSlots;
}

public void clear(final boolean toPlayerInventory) {
source.clearMatrix(player, toPlayerInventory);
}
Expand All @@ -113,4 +127,32 @@ private void addContainerToList(final Container container, final ResourceList<Ob
public void transferRecipe(final List<List<ItemResource>> recipe) {
source.transferRecipe(player, recipe);
}

public void filterBasedOnCraftingMatrixItems() {
final Set<ItemResource> craftingMatrixItems = getCraftingMatrixItems();
filterBeforeFilteringBasedOnCraftingMatrixItems = getView().setFilterAndSort(
gridResource -> gridResource instanceof ItemGridResource itemGridResource
&& craftingMatrixItems.contains(itemGridResource.getResource())
);
}

private Set<ItemResource> getCraftingMatrixItems() {
final Set<ItemResource> craftingMatrixItems = new HashSet<>();
for (int i = 0; i < source.getCraftingMatrix().getContainerSize(); ++i) {
final ItemStack craftingMatrixStack = source.getCraftingMatrix().getItem(i);
if (craftingMatrixStack.isEmpty()) {
continue;
}
craftingMatrixItems.add(ItemResource.ofItemStack(craftingMatrixStack));
}
return craftingMatrixItems;
}

public void stopFilteringBasedOnCraftingMatrixItems() {
if (filterBeforeFilteringBasedOnCraftingMatrixItems == null) {
return;
}
getView().setFilterAndSort(filterBeforeFilteringBasedOnCraftingMatrixItems);
filterBeforeFilteringBasedOnCraftingMatrixItems = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
Expand Down Expand Up @@ -62,7 +61,7 @@ public T getDefault() {
}

public Collection<T> values() {
return map.values().stream().map(Supplier::get).collect(Collectors.toList());
return map.values().stream().map(Supplier::get).toList();
}

private record ColoredSorter<T>(DyeColor defaultColor) implements Comparator<Map.Entry<DyeColor, T>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public Optional<Object> convertToIngredient(final Object resource) {
}
if (resource instanceof FluidGridResource fluidGridResource) {
return Optional.of(fluidHelper.create(
fluidGridResource.getFluidResource().fluid(),
fluidGridResource.getResource().fluid(),
fluidHelper.bucketVolume(),
fluidGridResource.getFluidResource().tag()
fluidGridResource.getResource().tag()
));
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ protected AbstractGridResource(final ResourceAmount<T> resourceAmount,
this.attributes = attributes;
}

public T getResource() {
return resourceAmount.getResource();
}

@Override
public Optional<TrackedResource> getTrackedResource(final GridView view) {
return view.getTrackedResource(resourceAmount.getResource());
return view.getTrackedResource(getResource());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Optional<GridResource> apply(final ResourceAmount<?> resourceAmount) {

private String getTooltip(final ItemStack itemStack) {
return itemStack
.getTooltipLines(null, TooltipFlag.Default.ADVANCED)
.getTooltipLines(null, TooltipFlag.ADVANCED)
.stream()
.map(Component::getString)
.collect(Collectors.joining("\n"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ public FluidGridResource(final ResourceAmount<FluidResource> resourceAmount,
this.fluidResource = resourceAmount.getResource();
}

public FluidResource getFluidResource() {
return fluidResource;
}

@Override
public int getRegistryId() {
return id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public abstract class AbstractGridScreen<T extends AbstractGridContainerMenu> ex
private static final List<String> SEARCH_FIELD_HISTORY = new ArrayList<>();

protected final int bottomHeight;
@Nullable
protected GridSearchBoxWidget searchField;

@Nullable
private ScrollbarWidget scrollbar;
@Nullable
private GridSearchBoxWidget searchField;
private int totalRows;
private int visibleRows;
private int gridSlotNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.containermenu.grid.CraftingGridContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.containermenu.grid.CraftingGridResultSlot;
import com.refinedmods.refinedstorage2.platform.common.content.KeyMappings;

import javax.annotation.Nullable;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.ChatFormatting;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.gui.components.ImageButton;
Expand All @@ -14,6 +16,7 @@
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;

import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createIdentifier;

Expand All @@ -24,6 +27,8 @@ public class CraftingGridScreen extends AbstractGridScreen<CraftingGridContainer
@Nullable
private ImageButton clearToNetworkButton;

private boolean filteringBasedOnCraftingMatrixItems;

public CraftingGridScreen(final CraftingGridContainerMenu menu, final Inventory inventory, final Component title) {
super(menu, inventory, title, 156);
this.inventoryLabelY = 134;
Expand All @@ -46,6 +51,48 @@ protected void init() {
addRenderableWidget(createClearButton(clearToInventoryButtonX, clearButtonY, 249, true));
}

@Override
protected void renderBg(final PoseStack poseStack, final float delta, final int mouseX, final int mouseY) {
super.renderBg(poseStack, delta, mouseX, mouseY);
if (filteringBasedOnCraftingMatrixItems) {
renderCraftingMatrixFilteringHighlights(poseStack);
}
}

private void renderCraftingMatrixFilteringHighlights(final PoseStack poseStack) {
for (final Slot slot : getMenu().getCraftingMatrixSlots()) {
if (!slot.hasItem()) {
continue;
}
renderCraftingMatrixFilteringHighlight(poseStack, slot);
}
}

private void renderCraftingMatrixFilteringHighlight(final PoseStack poseStack, final Slot slot) {
blit(poseStack, leftPos + slot.x - 1, topPos + slot.y - 1, 224, 238, 18, 18);
}

@Override
protected void containerTick() {
super.containerTick();
final boolean mayFilterOnCraftingMatrixItems = hoveredSlot instanceof CraftingGridResultSlot
&& hasShiftDown()
&& hasControlDown();
if (mayFilterOnCraftingMatrixItems && !filteringBasedOnCraftingMatrixItems) {
filteringBasedOnCraftingMatrixItems = true;
getMenu().filterBasedOnCraftingMatrixItems();
if (searchField != null) {
searchField.setEditable(false);
}
} else if (!mayFilterOnCraftingMatrixItems && filteringBasedOnCraftingMatrixItems) {
getMenu().stopFilteringBasedOnCraftingMatrixItems();
filteringBasedOnCraftingMatrixItems = false;
if (searchField != null) {
searchField.setEditable(true);
}
}
}

private void setClearToNetworkButtonActive(final boolean active) {
if (clearToNetworkButton == null) {
return;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ private TransferHandlerRenderer createMissingItemsRenderer(final MissingIngredie
return (matrices, mouseX, mouseY, delta, widgets, bounds, display) -> {
int index = 0;
for (final Widget widget : widgets) {
if (widget instanceof Slot slot && slot.getNoticeMark() == Slot.INPUT) {
if (missingIngredients.isMissing(index++)) {
renderMissingOverlay(matrices, slot);
}
if (widget instanceof Slot slot
&& slot.getNoticeMark() == Slot.INPUT
&& missingIngredients.isMissing(index++)) {
renderMissingItemOverlay(matrices, slot);
}
}
};
}

private void renderMissingOverlay(final PoseStack poseStack, final Slot slot) {
private void renderMissingItemOverlay(final PoseStack poseStack, final Slot slot) {
poseStack.pushPose();
poseStack.translate(0, 0, 400);
final Rectangle innerBounds = slot.getInnerBounds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public Optional<Object> convertToIngredient(final Object resource) {
}
if (resource instanceof FluidGridResource fluidGridResource) {
final FluidStack fluidStack = FluidStack.create(
fluidGridResource.getFluidResource().fluid(),
fluidGridResource.getResource().fluid(),
FluidStack.bucketAmount(),
fluidGridResource.getFluidResource().tag()
fluidGridResource.getResource().tag()
);
return Optional.of(EntryStacks.of(fluidStack));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public List<Component> getTooltip(final FluidResource fluidResource) {
return FluidVariantRendering.getTooltip(
getFluidVariantFromCache(fluidResource),
Minecraft.getInstance().options.advancedItemTooltips
? TooltipFlag.Default.ADVANCED
: TooltipFlag.Default.NORMAL
? TooltipFlag.ADVANCED
: TooltipFlag.NORMAL
);
}
}
Loading

0 comments on commit 672f3f2

Please sign in to comment.