diff --git a/src/api/java/baritone/api/utils/IBaritoneInventory.java b/src/api/java/baritone/api/utils/IBaritoneInventory.java index a30fd97d0..b5790ecf8 100644 --- a/src/api/java/baritone/api/utils/IBaritoneInventory.java +++ b/src/api/java/baritone/api/utils/IBaritoneInventory.java @@ -27,8 +27,9 @@ import java.util.stream.Stream; public interface IBaritoneInventory { /** - * Returns a stream containing all the player's regular inventory slots and items. In the order of hotbar, offhand, - * then main inventory, for a total of 37 slots. This explicitly does not contain the armor slots or crafting grid. + * Returns a stream containing all the player's regular inventory slots and items. The elements of the stream are in + * the order of hotbar, offhand, then main inventory, for a total of 37 slots. This explicitly does not contain the + * armor slots or crafting grid, which may otherwise be accessed with {@link #armorSlots()} and/or {@link #itemAt}. * * @return All the player's inventory slots and items */ @@ -39,4 +40,8 @@ public interface IBaritoneInventory { Stream> inventorySlots(); Pair offhand(); + + Stream> armorSlots(); + + ItemStack itemAt(InventorySlot slot); } diff --git a/src/api/java/baritone/api/utils/InventorySlot.java b/src/api/java/baritone/api/utils/InventorySlot.java index 87c730f43..441563155 100644 --- a/src/api/java/baritone/api/utils/InventorySlot.java +++ b/src/api/java/baritone/api/utils/InventorySlot.java @@ -80,6 +80,13 @@ public final class InventorySlot { return this.type; } + /** + * Returns the index of this slot in {@code mainInventory}. If this slot does not correspond to an index into + * {@code mainInventory}, then an {@link IllegalArgumentException} is thrown. + * + * @return The index of this slot in the player's {@code mainInventory} + * @throws IllegalArgumentException if type is not {@link Type#HOTBAR} or {@link Type#INVENTORY} + */ public int getInventoryIndex() { switch (this.getType()) { case HOTBAR: @@ -91,18 +98,20 @@ public final class InventorySlot { } } - public static InventorySlot hotbar(final int index) { - if (index < 0 || index >= 9) { - throw new IllegalArgumentException(); + public static InventorySlot inventory(final int index) { + if (index >= 0 && index < 9) { + return SLOTS[index + 36]; // HOTBAR + } else if (index >= 9 && index < 36) { + return SLOTS[index]; // INVENTORY } - return SLOTS[index + 36]; + throw new IllegalArgumentException(); } - public static InventorySlot inventory(final int index) { - if (index < 9 || index >= 36) { + public static InventorySlot armor(final int index) { + if (index < 0 || index >= 4) { throw new IllegalArgumentException(); } - return SLOTS[index]; + return SLOTS[index + 5]; } public static InventorySlot offhand() { diff --git a/src/main/java/baritone/behavior/InventoryBehavior.java b/src/main/java/baritone/behavior/InventoryBehavior.java index 6a484ea87..898c2d0f6 100644 --- a/src/main/java/baritone/behavior/InventoryBehavior.java +++ b/src/main/java/baritone/behavior/InventoryBehavior.java @@ -86,9 +86,9 @@ public final class InventoryBehavior extends Behavior implements Helper { return; } - final InventorySlot pick = bestToolAgainst(Blocks.STONE, ItemPickaxe.class); + final InventorySlot pick = this.bestToolAgainst(Blocks.STONE, ItemPickaxe.class); if (pick != null && pick.getType() == InventorySlot.Type.INVENTORY) { - requestSwapWithHotBar(pick.getInventoryIndex(), 0); + this.requestSwapWithHotBar(pick.getInventoryIndex(), 0); } } diff --git a/src/main/java/baritone/utils/player/BaritoneInventory.java b/src/main/java/baritone/utils/player/BaritoneInventory.java index 58f1e992d..fb042864f 100644 --- a/src/main/java/baritone/utils/player/BaritoneInventory.java +++ b/src/main/java/baritone/utils/player/BaritoneInventory.java @@ -45,18 +45,30 @@ public final class BaritoneInventory implements IBaritoneInventory { @Override public Stream> hotbarSlots() { - return IntStream.range(0, 9).mapToObj(i -> - new Pair<>(InventorySlot.hotbar(i), ctx.player().inventory.mainInventory.get(i))); + return IntStream.range(0, 9).mapToObj(InventorySlot::inventory).map(this::itemSlotPairAt); } @Override public Stream> inventorySlots() { - return IntStream.range(9, 36).mapToObj(i -> - new Pair<>(InventorySlot.inventory(i), ctx.player().inventory.mainInventory.get(i))); + return IntStream.range(9, 36).mapToObj(InventorySlot::inventory).map(this::itemSlotPairAt); } @Override public Pair offhand() { return new Pair<>(InventorySlot.offhand(), ctx.player().inventory.offHandInventory.get(0)); } + + @Override + public Stream> armorSlots() { + return IntStream.range(0, 4).mapToObj(InventorySlot::armor).map(this::itemSlotPairAt); + } + + @Override + public ItemStack itemAt(InventorySlot slot) { + return ctx.player().inventoryContainer.getSlot(slot.getSlotId()).getStack(); + } + + private Pair itemSlotPairAt(InventorySlot slot) { + return new Pair<>(slot, this.itemAt(slot)); + } }