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 2 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
33 changes: 33 additions & 0 deletions src/main/java/baritone/PerformanceCritical.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 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.
*
* @author Brady
*/
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
ZeroMemes marked this conversation as resolved.
Show resolved Hide resolved
@Retention(RetentionPolicy.SOURCE)
public @interface PerformanceCritical {}
24 changes: 17 additions & 7 deletions src/main/java/baritone/behavior/InventoryBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.ArrayList;
import java.util.OptionalInt;
import java.util.Random;
import java.util.function.IntPredicate;
import java.util.function.Predicate;

public final class InventoryBehavior extends Behavior implements Helper {
Expand Down Expand Up @@ -70,7 +71,7 @@ public void onTick(TickEvent event) {
}
}

public boolean attemptToPutOnHotbar(int inMainInvy, Predicate<Integer> disallowedHotbar) {
public boolean attemptToPutOnHotbar(int inMainInvy, IntPredicate disallowedHotbar) {
OptionalInt destination = getTempHotbarSlot(disallowedHotbar);
if (destination.isPresent()) {
if (!requestSwapWithHotBar(inMainInvy, destination.getAsInt())) {
Expand All @@ -80,7 +81,7 @@ public boolean attemptToPutOnHotbar(int inMainInvy, Predicate<Integer> disallowe
return true;
}

public OptionalInt getTempHotbarSlot(Predicate<Integer> disallowedHotbar) {
public OptionalInt getTempHotbarSlot(IntPredicate disallowedHotbar) {
// we're using 0 and 8 for pickaxe and throwaway
ArrayList<Integer> candidates = new ArrayList<>();
for (int i = 1; i < 8; i++) {
Expand Down Expand Up @@ -152,7 +153,7 @@ private int bestToolAgainst(Block against, Class<? extends ItemTool> cla$$) {

public boolean hasGenericThrowaway() {
for (Item item : Baritone.settings().acceptableThrowawayItems.value) {
if (throwaway(false, stack -> item.equals(stack.getItem()))) {
if (this.canSelectItem(stack -> item.equals(stack.getItem()))) {
return true;
}
}
Expand All @@ -175,11 +176,15 @@ public boolean selectThrowawayForLocation(boolean select, int x, int y, int z) {
return false;
}

public boolean throwaway(boolean select, Predicate<? super ItemStack> desired) {
return throwaway(select, desired, Baritone.settings().allowInventory.value);
public boolean canSelectItem(Predicate<? super ItemStack> desired) {
return this.throwaway(false, desired);
}

public boolean trySelectItem(Predicate<? super ItemStack> desired) {
return this.throwaway(true, desired);
}

public boolean throwaway(boolean select, Predicate<? super ItemStack> desired, boolean allowInventory) {
public boolean throwaway(boolean select, Predicate<? super ItemStack> desired) {
EntityPlayerSP p = ctx.player();
NonNullList<ItemStack> inv = p.inventory.mainInventory;
for (int i = 0; i < 9; i++) {
Expand All @@ -196,6 +201,7 @@ public boolean throwaway(boolean select, Predicate<? super ItemStack> desired, b
return true;
}
}

if (desired.test(p.inventory.offHandInventory.get(0))) {
// main hand takes precedence over off hand
// that means that if we have block A selected in main hand and block B in off hand, right clicking places block B
Expand All @@ -213,7 +219,7 @@ public boolean throwaway(boolean select, Predicate<? super ItemStack> desired, b
}
}

if (allowInventory) {
if (this.canAccessInventory()) {
for (int i = 9; i < 36; i++) {
if (desired.test(inv.get(i))) {
if (select) {
Expand All @@ -227,4 +233,8 @@ public boolean throwaway(boolean select, Predicate<? super ItemStack> desired, b

return false;
}

public boolean canAccessInventory() {
return Baritone.settings().allowInventory.value;
}
}
2 changes: 1 addition & 1 deletion src/main/java/baritone/process/BuilderProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public int lengthZ() {
return new PathingCommand(null, PathingCommandType.CANCEL_AND_SET_GOAL);
}

if (Baritone.settings().allowInventory.value) {
if (baritone.getInventoryBehavior().canAccessInventory()) {
ArrayList<Integer> usefulSlots = new ArrayList<>();
List<IBlockState> noValidHotbarOption = new ArrayList<>();
outer:
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/baritone/process/FarmProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
for (BlockPos pos : both) {
boolean soulsand = openSoulsand.contains(pos);
Optional<Rotation> rot = RotationUtils.reachableOffset(ctx, pos, new Vec3d(pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5), ctx.playerController().getBlockReachDistance(), false);
if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().throwaway(true, soulsand ? this::isNetherWart : this::isPlantable)) {
if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().trySelectItem(soulsand ? this::isNetherWart : this::isPlantable)) {
RayTraceResult result = RayTraceUtils.rayTraceTowards(ctx.player(), rot.get(), ctx.playerController().getBlockReachDistance());
if (result.typeOfHit == RayTraceResult.Type.BLOCK && result.sideHit == EnumFacing.UP) {
baritone.getLookBehavior().updateTarget(rot.get(), true);
Expand All @@ -287,7 +287,7 @@ public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
}
Vec3d faceCenter = new Vec3d(pos).add(0.5, 0.5, 0.5).add(new Vec3d(dir.getDirectionVec()).scale(0.5));
Optional<Rotation> rot = RotationUtils.reachableOffset(ctx, pos, faceCenter, ctx.playerController().getBlockReachDistance(), false);
if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().throwaway(true, this::isCocoa)) {
if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().trySelectItem(this::isCocoa)) {
RayTraceResult result = RayTraceUtils.rayTraceTowards(ctx.player(), rot.get(), ctx.playerController().getBlockReachDistance());
if (result.typeOfHit == RayTraceResult.Type.BLOCK && result.sideHit == dir) {
baritone.getLookBehavior().updateTarget(rot.get(), true);
Expand All @@ -301,7 +301,7 @@ public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
}
for (BlockPos pos : bonemealable) {
Optional<Rotation> rot = RotationUtils.reachable(ctx, pos);
if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().throwaway(true, this::isBoneMeal)) {
if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().trySelectItem(this::isBoneMeal)) {
baritone.getLookBehavior().updateTarget(rot.get(), true);
if (ctx.isLookingAt(pos)) {
baritone.getInputOverrideHandler().setInputForceState(Input.CLICK_RIGHT, true);
Expand All @@ -323,17 +323,17 @@ public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
for (BlockPos pos : toBreak) {
goalz.add(new BuilderProcess.GoalBreak(pos));
}
if (baritone.getInventoryBehavior().throwaway(false, this::isPlantable)) {
if (baritone.getInventoryBehavior().canSelectItem(this::isPlantable)) {
for (BlockPos pos : openFarmland) {
goalz.add(new GoalBlock(pos.up()));
}
}
if (baritone.getInventoryBehavior().throwaway(false, this::isNetherWart)) {
if (baritone.getInventoryBehavior().canSelectItem(this::isNetherWart)) {
for (BlockPos pos : openSoulsand) {
goalz.add(new GoalBlock(pos.up()));
}
}
if (baritone.getInventoryBehavior().throwaway(false, this::isCocoa)) {
if (baritone.getInventoryBehavior().canSelectItem(this::isCocoa)) {
for (BlockPos pos : openLog) {
for (EnumFacing direction : EnumFacing.Plane.HORIZONTAL) {
if (ctx.world().getBlockState(pos.offset(direction)).getBlock() instanceof BlockAir) {
Expand All @@ -342,7 +342,7 @@ public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
}
}
}
if (baritone.getInventoryBehavior().throwaway(false, this::isBoneMeal)) {
if (baritone.getInventoryBehavior().canSelectItem(this::isBoneMeal)) {
for (BlockPos pos : bonemealable) {
goalz.add(new GoalBlock(pos));
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/baritone/utils/ToolSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package baritone.utils;

import baritone.Baritone;
import baritone.PerformanceCritical;
import baritone.utils.accessor.IItemTool;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
Expand Down Expand Up @@ -72,6 +73,7 @@ public ToolSet(EntityPlayerSP player) {
* @param state the blockstate to be mined
* @return the speed of how fast we'll mine it. 1/(time in ticks)
*/
@PerformanceCritical
public double getStrVsBlock(IBlockState state) {
return breakStrengthCache.computeIfAbsent(state.getBlock(), backendCalculation);
}
Expand Down Expand Up @@ -104,7 +106,6 @@ public boolean hasSilkTouch(ItemStack stack) {
* @param b the blockstate to be mined
* @return An int containing the index in the tools array that worked best
*/

public int getBestSlot(Block b, boolean preferSilkTouch) {
return getBestSlot(b, preferSilkTouch, false);
}
Expand Down