Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve InventoryBehavior #4037

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/api/java/baritone/api/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ public final class Settings {
*/
public final Setting<Boolean> allowInventory = new Setting<>(false);

/**
* Allow Baritone to automatically put useful items (such as tools and throwaway blocks) on the hotbar while
* pathing. This can reduce delays when retrieving items due settings like {@link #ticksBetweenInventoryMoves} and
* {@link #inventoryMoveOnlyIfStationary}. Requires {@link #allowInventory}.
*/
public final Setting<Boolean> allowHotbarManagement = new Setting<>(false);
ZeroMemes marked this conversation as resolved.
Show resolved Hide resolved

/**
* Wait this many ticks between InventoryBehavior moving inventory items
*/
Expand Down
47 changes: 47 additions & 0 deletions src/api/java/baritone/api/utils/IBaritoneInventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/

package baritone.api.utils;

import net.minecraft.item.ItemStack;

import java.util.stream.Stream;

/**
* @author Brady
*/
public interface IBaritoneInventory {

/**
* 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
*/
Stream<Pair<InventorySlot, ItemStack>> allSlots();

Stream<Pair<InventorySlot, ItemStack>> hotbarSlots();

Stream<Pair<InventorySlot, ItemStack>> inventorySlots();

Pair<InventorySlot, ItemStack> offhand();

Stream<Pair<InventorySlot, ItemStack>> armorSlots();

ItemStack itemAt(InventorySlot slot);
}
5 changes: 5 additions & 0 deletions src/api/java/baritone/api/utils/IPlayerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package baritone.api.utils;

import baritone.api.IBaritone;
import baritone.api.cache.IWorldData;
import net.minecraft.block.BlockSlab;
import net.minecraft.client.Minecraft;
Expand All @@ -34,12 +35,16 @@
*/
public interface IPlayerContext {

IBaritone baritone();

Minecraft minecraft();

EntityPlayerSP player();

IPlayerController playerController();

IBaritoneInventory inventory();

World world();

IWorldData worldData();
Expand Down
129 changes: 129 additions & 0 deletions src/api/java/baritone/api/utils/InventorySlot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/

package baritone.api.utils;

import java.util.function.ObjIntConsumer;

/**
* @author Brady
*/
public final class InventorySlot {

/**
* Maps directly to the slot ids of the player's inventory container.
* <table width="100%">
* <tr>
* <th width="10%">Index</th>
* <th>Description</th>
* </tr>
* <tr><td>0</td><td>crafting output</td></tr>
* <tr><td>1-4</td><td>crafting grid</td></tr>
* <tr><td>5-8</td><td>armor</td></tr>
* <tr><td>9-35</td><td>inventory (index 9-35)</td></tr>
* <tr><td>36-44</td><td>hotbar (index 0-8)</td></tr>
* <tr><td>45</td><td>off-hand</td></tr>
* </table>
*/
private static final InventorySlot[] SLOTS = new InventorySlot[46];

static {
final ObjIntConsumer<Type> populate = new ObjIntConsumer<Type>() {
private int index;

@Override
public void accept(Type type, int count) {
for (int i = 0; i < count; i++) {
SLOTS[this.index] = new InventorySlot(this.index, type);
this.index++;
}
}
};
populate.accept(Type.CRAFTING_OUTPUT, 1);
populate.accept(Type.CRAFTING_GRID, 4);
populate.accept(Type.ARMOR, 4);
populate.accept(Type.INVENTORY, 27);
populate.accept(Type.HOTBAR, 9);
populate.accept(Type.OFFHAND, 1);
}

private final int slotId;
private final Type type;

private InventorySlot(int slotId, Type type) {
this.slotId = slotId;
this.type = type;
}

/**
* @return The ID of this slot, as used by {@code ContainerPlayer}
*/
public int getSlotId() {
return this.slotId;
}

public Type getType() {
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:
return this.slotId - 36;
case INVENTORY:
return this.slotId;
default:
throw new IllegalStateException("Slot type must be either HOTBAR or INVENTORY");
}
}

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
}
throw new IllegalArgumentException();
}

public static InventorySlot armor(final int index) {
if (index < 0 || index >= 4) {
throw new IllegalArgumentException();
}
return SLOTS[index + 5];
}

public static InventorySlot offhand() {
return SLOTS[45];
}

public enum Type {
CRAFTING_OUTPUT,
CRAFTING_GRID,
ARMOR,
INVENTORY,
HOTBAR,
OFFHAND
}
}
59 changes: 59 additions & 0 deletions src/api/java/baritone/api/utils/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/

package baritone.api.utils;

import java.util.Objects;

/**
* @author Brady
*/
public final class Pair<A, B> {

private final A a;
private final B b;

public Pair(A a, B b) {
this.a = a;
this.b = b;
}

public A first() {
return this.a;
}

public B second() {
return this.b;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || o.getClass() != Pair.class) {
return false;
}
Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(this.a, pair.a) && Objects.equals(this.b, pair.b);
}

@Override
public int hashCode() {
return 31 * Objects.hashCode(this.a) + Objects.hashCode(this.b);
}
}
36 changes: 36 additions & 0 deletions src/main/java/baritone/PerformanceCritical.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/

package baritone;

import baritone.pathing.movement.CalculationContext;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation that should be used on methods which are performance critical (i.e. called millions of times per second
* by the pathfinder) and should be modified with care. Particularly useful for methods for which this fact is not
* obvious, such as those which don't have a {@link CalculationContext} parameter.
*
* @author Brady
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface PerformanceCritical {}
Loading