Utilize ToolSet for hotbar management's `bestToolAgainst`

This commit is contained in:
Brady 2023-07-06 18:40:34 -07:00
parent dbf5058066
commit 6a6d0642be
No known key found for this signature in database
GPG Key ID: 73A788379A197567
3 changed files with 11 additions and 34 deletions

View File

@ -90,6 +90,10 @@ public final class InventoryBehavior extends Behavior implements Helper {
} }
} }
private InventorySlot bestToolAgainst(final Block against, final Class<? extends ItemTool> cla$$) {
return new ToolSet(ctx).getBestSlot(against.getDefaultState(), Baritone.settings().preferSilkTouch.value, stack -> cla$$.isInstance(stack.getItem()));
}
public boolean attemptToPutOnHotbar(int inMainInvy, IntPredicate disallowedHotbar) { public boolean attemptToPutOnHotbar(int inMainInvy, IntPredicate disallowedHotbar) {
OptionalInt destination = getTempHotbarSlot(disallowedHotbar); OptionalInt destination = getTempHotbarSlot(disallowedHotbar);
if (destination.isPresent()) { if (destination.isPresent()) {
@ -137,25 +141,6 @@ public final class InventoryBehavior extends Behavior implements Helper {
return true; return true;
} }
private InventorySlot bestToolAgainst(final Block against, final Class<? extends ItemTool> cla$$) {
// TODO: Replace with ToolSet.getBestSlot
return this.findBestSlotMatching(
Comparator.comparingDouble(stack -> ToolSet.calculateSpeedVsBlock(stack, against.getDefaultState())),
stack -> {
if (stack.isEmpty()) {
return false;
}
if (Baritone.settings().itemSaver.value
&& stack.getItemDamage() + Baritone.settings().itemSaverThreshold.value >= stack.getMaxDamage()
&& stack.getMaxDamage() > 1
) {
return false;
}
return cla$$.isInstance(stack.getItem());
}
);
}
public boolean hasGenericThrowaway() { public boolean hasGenericThrowaway() {
return this.canSelectItem(this::isThrowawayItem); return this.canSelectItem(this::isThrowawayItem);
} }

View File

@ -581,20 +581,9 @@ public interface MovementHelper extends ActionCosts, Helper {
* @param state The blockstate to mine * @param state The blockstate to mine
*/ */
static void switchToBestToolFor(IPlayerContext ctx, IBlockState state) { static void switchToBestToolFor(IPlayerContext ctx, IBlockState state) {
switchToBestToolFor(ctx, state, new ToolSet(ctx), Baritone.settings().preferSilkTouch.value);
}
/**
* AutoTool for a specific block with precomputed ToolSet data
*
* @param ctx The player context
* @param state The blockstate to mine
* @param ts Previously calculated ToolSet
*/
static void switchToBestToolFor(IPlayerContext ctx, IBlockState state, ToolSet ts, boolean preferSilkTouch) {
if (ToolSet.isAutoTool()) { if (ToolSet.isAutoTool()) {
// TODO: Submit through InventoryBehavior, instead of executing the strategy here // TODO: Submit through InventoryBehavior, instead of executing the strategy here
final InventorySlot slot = ts.getBestSlot(state, preferSilkTouch); final InventorySlot slot = new ToolSet(ctx).getBestSlot(state, Baritone.settings().preferSilkTouch.value, null);
final InventoryBehavior.SelectionStrategy strategy = ((Baritone) ctx.baritone()).getInventoryBehavior().resolveSelectionStrategy(slot); final InventoryBehavior.SelectionStrategy strategy = ((Baritone) ctx.baritone()).getInventoryBehavior().resolveSelectionStrategy(slot);
if (strategy != null) { if (strategy != null) {
strategy.run(); strategy.run();

View File

@ -33,6 +33,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword; import net.minecraft.item.ItemSword;
import java.util.Comparator; import java.util.Comparator;
import java.util.function.Predicate;
import java.util.function.ToDoubleFunction; import java.util.function.ToDoubleFunction;
/** /**
@ -86,7 +87,7 @@ public final class ToolSet {
*/ */
private double getBestDestructionSpeed(IBlockState state) { private double getBestDestructionSpeed(IBlockState state) {
final ItemStack stack = isAutoTool() final ItemStack stack = isAutoTool()
? ctx.inventory().itemAt(this.getBestSlot(state, false)) ? ctx.inventory().itemAt(this.getBestSlot(state, false, null))
: ctx.player().getHeldItemMainhand(); : ctx.player().getHeldItemMainhand();
return calculateSpeedVsBlock(stack, state) * avoidanceMultiplier(state.getBlock()); return calculateSpeedVsBlock(stack, state) * avoidanceMultiplier(state.getBlock());
} }
@ -113,9 +114,11 @@ public final class ToolSet {
* *
* @param state the blockstate to be mined * @param state the blockstate to be mined
* @param preferSilkTouch whether to prefer silk touch tools * @param preferSilkTouch whether to prefer silk touch tools
* @param extra An additional filter to apply on top of the default, setting-based ones, may be {@code null}
* @return An int containing the index in the tools array that worked best * @return An int containing the index in the tools array that worked best
*/ */
public InventorySlot getBestSlot(IBlockState state, boolean preferSilkTouch) { public InventorySlot getBestSlot(final IBlockState state, final boolean preferSilkTouch,
final Predicate<? super ItemStack> extra) {
final Comparator<ItemStack> compare = Comparator final Comparator<ItemStack> compare = Comparator
// Prioritize mining speed over everything // Prioritize mining speed over everything
.<ItemStack>comparingDouble(stack -> calculateSpeedVsBlock(stack, state)) .<ItemStack>comparingDouble(stack -> calculateSpeedVsBlock(stack, state))
@ -136,7 +139,7 @@ public final class ToolSet {
) { ) {
return false; return false;
} }
return true; return extra == null || extra.test(stack);
} }
); );
} }