From 7a462ea318a433a1520a5a1a5804980552c0d0a7 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Sat, 16 May 2020 17:54:22 +0300 Subject: Split item slot inserting and taking into different flags --- .../github/cottonmc/cotton/gui/ValidatedSlot.java | 62 +++++++++++++++++--- .../cottonmc/cotton/gui/widget/WItemSlot.java | 68 +++++++++++++++++++--- 2 files changed, 116 insertions(+), 14 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 cce4cfe..2c6b5b2 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java @@ -7,8 +7,9 @@ import net.minecraft.screen.slot.Slot; public class ValidatedSlot extends Slot { private final int slotNumber; - private boolean modifiable = true; - + private boolean insertingAllowed = true; + private boolean takingAllowed = true; + public ValidatedSlot(Inventory inventoryIn, int index, int xPosition, int yPosition) { super(inventoryIn, index, xPosition, yPosition); if (inventoryIn==null) throw new IllegalArgumentException("Can't make an itemslot from a null inventory!"); @@ -17,12 +18,12 @@ public class ValidatedSlot extends Slot { @Override public boolean canInsert(ItemStack stack) { - return modifiable && inventory.isValid(slotNumber, stack); + return insertingAllowed && inventory.isValid(slotNumber, stack); } @Override public boolean canTakeItems(PlayerEntity player) { - return modifiable && inventory.canPlayerUse(player); + return takingAllowed && inventory.canPlayerUse(player); } @Override @@ -44,18 +45,65 @@ public class ValidatedSlot extends Slot { /** * Returns true if the item in this slot can be modified by players. * - * @return true if this slot is modifiable + * @return true if items can be inserted into or taken from this slot widget, false otherwise * @since 1.8.0 + * @deprecated Replaced with {@link #isInsertingAllowed()} and {@link #isTakingAllowed()}. */ + @Deprecated public boolean isModifiable() { - return modifiable; + return insertingAllowed || takingAllowed; } + /** + * @deprecated Replaced with {@link #setInsertingAllowed(boolean)} and {@link #setTakingAllowed(boolean)}. + */ + @Deprecated public void setModifiable(boolean modifiable) { - this.modifiable = modifiable; + this.insertingAllowed = modifiable; + this.takingAllowed = modifiable; } public int getInventoryIndex() { return slotNumber; } + + /** + * Returns whether items can be inserted into this slot. + * + * @return true if items can be inserted, false otherwise + * @since 1.10.0 + */ + public boolean isInsertingAllowed() { + return insertingAllowed; + } + + /** + * Sets whether inserting items into this slot is allowed. + * + * @param insertingAllowed true if items can be inserted, false otherwise + * @since 1.10.0 + */ + public void setInsertingAllowed(boolean insertingAllowed) { + this.insertingAllowed = insertingAllowed; + } + + /** + * Returns whether items can be taken from this slot. + * + * @return true if items can be taken, false otherwise + * @since 1.10.0 + */ + public boolean isTakingAllowed() { + return takingAllowed; + } + + /** + * Sets whether taking items from this slot is allowed. + * + * @param takingAllowed true if items can be taken, false otherwise + * @since 1.10.0 + */ + public void setTakingAllowed(boolean takingAllowed) { + this.takingAllowed = takingAllowed; + } } \ 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 8d0cf94..7d11881 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 @@ -20,8 +20,9 @@ public class WItemSlot extends WWidget { private int slotsWide = 1; private int slotsHigh = 1; private boolean big = false; - private boolean modifiable = true; - + private boolean insertingAllowed = true; + private boolean takingAllowed = true; + public WItemSlot(Inventory inventory, int startIndex, int slotsWide, int slotsHigh, boolean big, boolean ltr) { this.inventory = inventory; this.startIndex = startIndex; @@ -88,17 +89,69 @@ public class WItemSlot extends WWidget { /** * Returns true if the contents of this {@code WItemSlot} can be modified by players. * - * @return true if this slot is modifiable + * @return true if items can be inserted into or taken from this slot widget, false otherwise * @since 1.8.0 */ public boolean isModifiable() { - return modifiable; + return takingAllowed || insertingAllowed; } public WItemSlot setModifiable(boolean modifiable) { - this.modifiable = modifiable; + this.insertingAllowed = modifiable; + this.takingAllowed = modifiable; + for (ValidatedSlot peer : peers) { + peer.setInsertingAllowed(modifiable); + peer.setTakingAllowed(modifiable); + } + return this; + } + + /** + * Returns whether items can be inserted into this slot. + * + * @return true if items can be inserted, false otherwise + * @since 1.10.0 + */ + public boolean isInsertingAllowed() { + return insertingAllowed; + } + + /** + * Sets whether inserting items into this slot is allowed. + * + * @param insertingAllowed true if items can be inserted, false otherwise + * @return this slot widget + * @since 1.10.0 + */ + public WItemSlot setInsertingAllowed(boolean insertingAllowed) { + this.insertingAllowed = insertingAllowed; + for (ValidatedSlot peer : peers) { + peer.setInsertingAllowed(insertingAllowed); + } + return this; + } + + /** + * Returns whether items can be taken from this slot. + * + * @return true if items can be taken, false otherwise + * @since 1.10.0 + */ + public boolean isTakingAllowed() { + return takingAllowed; + } + + /** + * Sets whether taking items from this slot is allowed. + * + * @param takingAllowed true if items can be taken, false otherwise + * @return this slot widget + * @since 1.10.0 + */ + public WItemSlot setTakingAllowed(boolean takingAllowed) { + this.takingAllowed = takingAllowed; for (ValidatedSlot peer : peers) { - peer.setModifiable(modifiable); + peer.setTakingAllowed(takingAllowed); } return this; } @@ -112,7 +165,8 @@ public class WItemSlot extends WWidget { for (int y = 0; y < slotsHigh; y++) { for (int x = 0; x < slotsWide; x++) { ValidatedSlot slot = createSlotPeer(inventory, index, this.getAbsoluteX() + (x * 18), this.getAbsoluteY() + (y * 18)); - slot.setModifiable(modifiable); + slot.setInsertingAllowed(insertingAllowed); + slot.setTakingAllowed(takingAllowed); peers.add(slot); c.addSlotPeer(slot); index++; -- cgit