aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorLuiz Feikes <33578169+Luligabi1@users.noreply.github.com>2023-09-09 06:25:12 -0300
committerGitHub <noreply@github.com>2023-09-09 12:25:12 +0300
commit4a63d4ee6c118c006ec5b09c1565cbd75b318a38 (patch)
tree7b5fe0e4b87064b7e3cc4e2bcf70aa708b920dae /src/main
parente7a36835ff95ac045bbeac156a105c16b4d23df2 (diff)
downloadLibGui-4a63d4ee6c118c006ec5b09c1565cbd75b318a38.tar.gz
LibGui-4a63d4ee6c118c006ec5b09c1565cbd75b318a38.tar.bz2
LibGui-4a63d4ee6c118c006ec5b09c1565cbd75b318a38.zip
Add input/output filter + weaken WPlayerInvPanel's components' protection (#209)
Co-authored-by: Juuz <6596629+Juuxel@users.noreply.github.com>
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java53
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java75
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java32
3 files changed, 136 insertions, 24 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 6d1475c..31cb59f 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java
@@ -15,7 +15,7 @@ import java.util.function.Predicate;
public class ValidatedSlot extends Slot {
/**
- * The default {@linkplain #setFilter(Predicate) item filter} that allows all items.
+ * The default {@linkplain #setInputFilter(Predicate) item filter} that allows all items.
*
* @since 5.1.1
*/
@@ -24,7 +24,8 @@ public class ValidatedSlot extends Slot {
private final int slotNumber;
private boolean insertingAllowed = true;
private boolean takingAllowed = true;
- private Predicate<ItemStack> filter = DEFAULT_ITEM_FILTER;
+ private Predicate<ItemStack> inputFilter = DEFAULT_ITEM_FILTER;
+ private Predicate<ItemStack> outputFilter = DEFAULT_ITEM_FILTER;
protected final Multimap<WItemSlot, WItemSlot.ChangeListener> listeners = HashMultimap.create();
private boolean visible = true;
@@ -36,12 +37,12 @@ public class ValidatedSlot extends Slot {
@Override
public boolean canInsert(ItemStack stack) {
- return insertingAllowed && inventory.isValid(slotNumber, stack) && filter.test(stack);
+ return insertingAllowed && inventory.isValid(slotNumber, stack) && inputFilter.test(stack);
}
@Override
public boolean canTakeItems(PlayerEntity player) {
- return takingAllowed && inventory.canPlayerUse(player);
+ return takingAllowed && inventory.canPlayerUse(player) && outputFilter.test(getStack());
}
@Override
@@ -116,23 +117,63 @@ public class ValidatedSlot extends Slot {
}
/**
+ * Gets the item stack input filter of this slot.
+ *
+ * @return the item input filter
+ */
+ public Predicate<ItemStack> getInputFilter() {
+ return inputFilter;
+ }
+
+ /**
+ * Sets the item stack input filter of this slot.
+ *
+ * @param inputFilter the new item input filter
+ */
+ public void setInputFilter(Predicate<ItemStack> inputFilter) {
+ this.inputFilter = inputFilter;
+ }
+
+ /**
+ * Gets the item stack output filter of this slot.
+ *
+ * @return the item output filter
+ */
+ public Predicate<ItemStack> getOutputFilter() {
+ return outputFilter;
+ }
+
+ /**
+ * Sets the item stack output filter of this slot.
+ *
+ * @param outputFilter the new item output filter
+ */
+ public void setOutputFilter(Predicate<ItemStack> outputFilter) {
+ this.outputFilter = outputFilter;
+ }
+
+ /**
* Gets the item stack filter of this slot.
*
* @return the item filter
+ * @deprecated Replaced by {@link #getInputFilter()}
* @since 2.0.0
*/
+ @Deprecated
public Predicate<ItemStack> getFilter() {
- return filter;
+ return inputFilter;
}
/**
* Sets the item stack filter of this slot.
*
* @param filter the new item filter
+ * @deprecated Replaced by {@link #setInputFilter(Predicate)}
* @since 2.0.0
*/
+ @Deprecated
public void setFilter(Predicate<ItemStack> filter) {
- this.filter = filter;
+ setInputFilter(filter);
}
/**
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 3ef3684..11542a4 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
@@ -46,13 +46,16 @@ import java.util.stream.Stream;
* between the player and the widget.
*
* <h2>Filters</h2>
- * Item slots can have filters that check whether a player is allowed to insert an item or not.
- * The filter can be set with {@link #setFilter(Predicate)}. For example:
+ * Item slots can have filters that check whether a player is allowed to insert or take out an item or not.
+ * The filters can be set with {@link #setInputFilter(Predicate)} and {@link #setOutputFilter(Predicate)}. For example:
*
* <pre>
* {@code
- * // Only sand in this slot!
- * slot.setFilter(stack -> stack.getItem() == Items.SAND);
+ * // Only sand can be placed on this slot
+ * slot.setInputFilter(stack -> stack.isOf(Items.SAND));
+ *
+ * // Everything except glass can be taken out of this slot
+ * slot.setOutputFilter(stack -> !stack.isOf(Items.GLASS));
* }
* </pre>
*
@@ -94,7 +97,8 @@ public class WItemSlot extends WWidget {
private boolean takingAllowed = true;
private int focusedSlot = -1;
private int hoveredSlot = -1;
- private Predicate<ItemStack> filter = ValidatedSlot.DEFAULT_ITEM_FILTER;
+ private Predicate<ItemStack> inputFilter = ValidatedSlot.DEFAULT_ITEM_FILTER;
+ private Predicate<ItemStack> outputFilter = ValidatedSlot.DEFAULT_ITEM_FILTER;
private final Set<ChangeListener> listeners = new HashSet<>();
private final FocusModel<Integer> focusModel = new FocusModel<>() {
@Override
@@ -338,7 +342,8 @@ 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);
+ slot.setInputFilter(inputFilter);
+ slot.setOutputFilter(outputFilter);
for (ChangeListener listener : listeners) {
slot.addChangeListener(this, listener);
}
@@ -401,13 +406,61 @@ public class WItemSlot extends WWidget {
}
/**
+ * Gets the item stack input filter of this slot.
+ *
+ * @return the item input filter
+ */
+ public Predicate<ItemStack> getInputFilter() {
+ return inputFilter;
+ }
+
+ /**
+ * Sets the item input filter of this item slot.
+ *
+ * @param inputFilter the new item input filter
+ * @return this item slot
+ */
+ public WItemSlot setInputFilter(Predicate<ItemStack> inputFilter) {
+ this.inputFilter = inputFilter;
+ for (ValidatedSlot peer : peers) {
+ peer.setInputFilter(inputFilter);
+ }
+ return this;
+ }
+
+ /**
+ * Gets the item stack output filter of this slot.
+ *
+ * @return the item output filter
+ */
+ public Predicate<ItemStack> getOutputFilter() {
+ return outputFilter;
+ }
+
+ /**
+ * Sets the item output filter of this item slot.
+ *
+ * @param outputFilter the new item output filter
+ * @return this item slot
+ */
+ public WItemSlot setOutputFilter(Predicate<ItemStack> outputFilter) {
+ this.outputFilter = outputFilter;
+ for (ValidatedSlot peer : peers) {
+ peer.setOutputFilter(outputFilter);
+ }
+ return this;
+ }
+
+ /**
* Gets the item filter of this item slot.
*
* @return the item filter
+ * @deprecated Replaced by {@link #getInputFilter()}
* @since 2.0.0
*/
+ @Deprecated
public Predicate<ItemStack> getFilter() {
- return filter;
+ return inputFilter;
}
/**
@@ -415,14 +468,12 @@ public class WItemSlot extends WWidget {
*
* @param filter the new item filter
* @return this item slot
+ * @deprecated Replaced by {@link #setInputFilter(Predicate)}
* @since 2.0.0
*/
+ @Deprecated
public WItemSlot setFilter(Predicate<ItemStack> filter) {
- this.filter = filter;
- for (ValidatedSlot peer : peers) {
- peer.setFilter(filter);
- }
- return this;
+ return setInputFilter(filter);
}
@Environment(EnvType.CLIENT)
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java
index d0fd001..9411164 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java
@@ -6,18 +6,38 @@ import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.text.Text;
import io.github.cottonmc.cotton.gui.GuiDescription;
+import io.github.cottonmc.cotton.gui.SyncedGuiDescription;
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
import io.github.cottonmc.cotton.gui.impl.client.NarrationMessages;
import org.jetbrains.annotations.Nullable;
/**
* A player inventory widget that has a visually separate hotbar.
+ *
+ * @see SyncedGuiDescription#createPlayerInventoryPanel()
*/
public class WPlayerInvPanel extends WPlainPanel {
- private final WItemSlot inv;
- private final WItemSlot hotbar;
+ /**
+ * A 9 by 3 {@link WItemSlot} that represents the player's inventory.
+ *
+ * @see PlayerInventory
+ */
+ protected final WItemSlot inventory;
+ /**
+ * A 9 by 1 {@link WItemSlot} that represents the player's hotbar.
+ *
+ * @see PlayerInventory
+ */
+ protected final WItemSlot hotbar;
+ /**
+ * The label seen above {@link WPlayerInvPanel#inventory}.
+ *
+ * <p> In vanilla and {@link WPlayerInvPanel#WPlayerInvPanel(PlayerInventory)}, this label is always 'Inventory'
+ *
+ * @see #createInventoryLabel(PlayerInventory)
+ */
@Nullable
- private final WWidget label;
+ protected final WWidget label;
/**
* Constructs a player inventory panel with a label.
@@ -55,14 +75,14 @@ public class WPlayerInvPanel extends WPlainPanel {
y += label.getHeight();
}
- inv = WItemSlot.ofPlayerStorage(playerInventory);
+ inventory = WItemSlot.ofPlayerStorage(playerInventory);
hotbar = new WItemSlot(playerInventory, 0, 9, 1, false) {
@Override
protected Text getNarrationName() {
return NarrationMessages.Vanilla.HOTBAR;
}
};
- this.add(inv, 0, y);
+ this.add(inventory, 0, y);
this.add(hotbar, 0, y + 58);
}
@@ -94,7 +114,7 @@ public class WPlayerInvPanel extends WPlainPanel {
@Override
public WPanel setBackgroundPainter(BackgroundPainter painter) {
super.setBackgroundPainter(null);
- inv.setBackgroundPainter(painter);
+ inventory.setBackgroundPainter(painter);
hotbar.setBackgroundPainter(painter);
return this;
}