aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java25
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java32
2 files changed, 55 insertions, 2 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java b/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java
index 1e9ee3c..6e7db9c 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java
@@ -7,11 +7,14 @@ import net.minecraft.screen.slot.Slot;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
+import java.util.function.Predicate;
+
public class ValidatedSlot extends Slot {
private static final Logger LOGGER = LogManager.getLogger();
private final int slotNumber;
private boolean insertingAllowed = true;
private boolean takingAllowed = true;
+ private Predicate<ItemStack> filter;
public ValidatedSlot(Inventory inventory, int index, int x, int y) {
super(inventory, index, x, y);
@@ -21,7 +24,7 @@ public class ValidatedSlot extends Slot {
@Override
public boolean canInsert(ItemStack stack) {
- return insertingAllowed && inventory.isValid(slotNumber, stack);
+ return insertingAllowed && inventory.isValid(slotNumber, stack) && filter.test(stack);
}
@Override
@@ -88,4 +91,24 @@ public class ValidatedSlot extends Slot {
public void setTakingAllowed(boolean takingAllowed) {
this.takingAllowed = takingAllowed;
}
+
+ /**
+ * Gets the item stack filter of this slot.
+ *
+ * @return the item filter
+ * @since 2.0.0
+ */
+ public Predicate<ItemStack> getFilter() {
+ return filter;
+ }
+
+ /**
+ * Sets the item stack filter of this slot.
+ *
+ * @param filter the new item filter
+ * @since 2.0.0
+ */
+ public void setFilter(Predicate<ItemStack> filter) {
+ this.filter = filter;
+ }
} \ No newline at end of file
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java
index ec1bf9c..d21ee98 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java
@@ -2,6 +2,7 @@ package io.github.cottonmc.cotton.gui.widget;
import java.util.ArrayList;
import java.util.List;
+import java.util.function.Predicate;
import io.github.cottonmc.cotton.gui.GuiDescription;
import io.github.cottonmc.cotton.gui.ValidatedSlot;
@@ -11,12 +12,14 @@ import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.inventory.Inventory;
+import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.SlotActionType;
import javax.annotation.Nullable;
public class WItemSlot extends WWidget {
+ private static final Predicate<ItemStack> DEFAULT_FILTER = stack -> true;
private final List<ValidatedSlot> peers = new ArrayList<>();
@Nullable
@Environment(EnvType.CLIENT)
@@ -29,6 +32,7 @@ public class WItemSlot extends WWidget {
private boolean insertingAllowed = true;
private boolean takingAllowed = true;
private int focusedSlot = -1;
+ private Predicate<ItemStack> filter = DEFAULT_FILTER;
public WItemSlot(Inventory inventory, int startIndex, int slotsWide, int slotsHigh, boolean big) {
this.inventory = inventory;
@@ -197,6 +201,7 @@ public class WItemSlot extends WWidget {
ValidatedSlot slot = createSlotPeer(inventory, index, this.getAbsoluteX() + (x * 18) + 1, this.getAbsoluteY() + (y * 18) + 1);
slot.setInsertingAllowed(insertingAllowed);
slot.setTakingAllowed(takingAllowed);
+ slot.setFilter(filter);
peers.add(slot);
c.addSlotPeer(slot);
index++;
@@ -251,7 +256,32 @@ public class WItemSlot extends WWidget {
public void setBackgroundPainter(@Nullable BackgroundPainter painter) {
this.backgroundPainter = painter;
}
-
+
+ /**
+ * Gets the item filter of this item slot.
+ *
+ * @return the item filter
+ * @since 2.0.0
+ */
+ public Predicate<ItemStack> getFilter() {
+ return filter;
+ }
+
+ /**
+ * Sets the item filter of this item slot.
+ *
+ * @param filter the new item filter
+ * @return this item slot
+ * @since 2.0.0
+ */
+ public WItemSlot setFilter(Predicate<ItemStack> filter) {
+ this.filter = filter;
+ for (ValidatedSlot peer : peers) {
+ peer.setFilter(filter);
+ }
+ return this;
+ }
+
@Environment(EnvType.CLIENT)
@Override
public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) {