More IBaritoneInventory api methods

This commit is contained in:
Brady
2023-07-05 10:13:38 -07:00
parent 57c3e369b0
commit 600d5e8a67
4 changed files with 41 additions and 15 deletions

View File

@@ -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<Pair<InventorySlot, ItemStack>> inventorySlots();
Pair<InventorySlot, ItemStack> offhand();
Stream<Pair<InventorySlot, ItemStack>> armorSlots();
ItemStack itemAt(InventorySlot slot);
}

View File

@@ -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() {

View File

@@ -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);
}
}

View File

@@ -45,18 +45,30 @@ public final class BaritoneInventory implements IBaritoneInventory {
@Override
public Stream<Pair<InventorySlot, ItemStack>> 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<Pair<InventorySlot, ItemStack>> 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<InventorySlot, ItemStack> offhand() {
return new Pair<>(InventorySlot.offhand(), ctx.player().inventory.offHandInventory.get(0));
}
@Override
public Stream<Pair<InventorySlot, ItemStack>> 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<InventorySlot, ItemStack> itemSlotPairAt(InventorySlot slot) {
return new Pair<>(slot, this.itemAt(slot));
}
}