option to only move inventory when stationary

This commit is contained in:
Leijurv
2023-02-28 23:44:33 -08:00
parent 174bdd2307
commit f1ead19b6e
5 changed files with 141 additions and 8 deletions

View File

@@ -69,6 +69,16 @@ public final class Settings {
*/
public final Setting<Boolean> allowInventory = new Setting<>(false);
/**
* Wait this many ticks between InventoryBehavior moving inventory items
*/
public final Setting<Integer> ticksBetweenInventoryMoves = new Setting<>(1);
/**
* Come to a halt before doing any inventory moves. Intended for anticheat such as 2b2t
*/
public final Setting<Boolean> inventoryMoveOnlyIfStationary = new Setting<>(false);
/**
* Disable baritone's auto-tool at runtime, but still assume that another mod will provide auto tool functionality
* <p>

View File

@@ -80,6 +80,7 @@ public class Baritone implements IBaritone {
private ExploreProcess exploreProcess;
private BackfillProcess backfillProcess;
private FarmProcess farmProcess;
private InventoryPauserProcess inventoryPauserProcess;
private PathingControlManager pathingControlManager;
private SelectionManager selectionManager;
@@ -115,6 +116,7 @@ public class Baritone implements IBaritone {
this.pathingControlManager.registerProcess(exploreProcess = new ExploreProcess(this));
this.pathingControlManager.registerProcess(backfillProcess = new BackfillProcess(this));
this.pathingControlManager.registerProcess(farmProcess = new FarmProcess(this));
this.pathingControlManager.registerProcess(inventoryPauserProcess = new InventoryPauserProcess(this));
}
this.worldProvider = new WorldProvider();
@@ -183,6 +185,10 @@ public class Baritone implements IBaritone {
return this.farmProcess;
}
public InventoryPauserProcess getInventoryPauserProcess() {
return this.inventoryPauserProcess;
}
@Override
public PathingBehavior getPathingBehavior() {
return this.pathingBehavior;

View File

@@ -19,6 +19,7 @@ package baritone.behavior;
import baritone.Baritone;
import baritone.api.event.events.TickEvent;
import baritone.api.utils.Helper;
import baritone.utils.ToolSet;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
@@ -34,7 +35,10 @@ import java.util.OptionalInt;
import java.util.Random;
import java.util.function.Predicate;
public final class InventoryBehavior extends Behavior {
public final class InventoryBehavior extends Behavior implements Helper {
int ticksSinceLastInventoryMove;
int[] lastTickRequestedMove; // not everything asks every tick, so remember the request while coming to a halt
public InventoryBehavior(Baritone baritone) {
super(baritone);
@@ -52,20 +56,28 @@ public final class InventoryBehavior extends Behavior {
// we have a crafting table or a chest or something open
return;
}
ticksSinceLastInventoryMove++;
if (firstValidThrowaway() >= 9) { // aka there are none on the hotbar, but there are some in main inventory
swapWithHotBar(firstValidThrowaway(), 8);
requestSwapWithHotBar(firstValidThrowaway(), 8);
}
int pick = bestToolAgainst(Blocks.STONE, ItemPickaxe.class);
if (pick >= 9) {
swapWithHotBar(pick, 0);
requestSwapWithHotBar(pick, 0);
}
if (lastTickRequestedMove != null) {
logDebug("Remembering to move " + lastTickRequestedMove[0] + " " + lastTickRequestedMove[1] + " from a previous tick");
requestSwapWithHotBar(lastTickRequestedMove[0], lastTickRequestedMove[1]);
}
}
public void attemptToPutOnHotbar(int inMainInvy, Predicate<Integer> disallowedHotbar) {
public boolean attemptToPutOnHotbar(int inMainInvy, Predicate<Integer> disallowedHotbar) {
OptionalInt destination = getTempHotbarSlot(disallowedHotbar);
if (destination.isPresent()) {
swapWithHotBar(inMainInvy, destination.getAsInt());
if (!requestSwapWithHotBar(inMainInvy, destination.getAsInt())) {
return false;
}
}
return true;
}
public OptionalInt getTempHotbarSlot(Predicate<Integer> disallowedHotbar) {
@@ -89,8 +101,20 @@ public final class InventoryBehavior extends Behavior {
return OptionalInt.of(candidates.get(new Random().nextInt(candidates.size())));
}
private void swapWithHotBar(int inInventory, int inHotbar) {
private boolean requestSwapWithHotBar(int inInventory, int inHotbar) {
lastTickRequestedMove = new int[]{inInventory, inHotbar};
if (ticksSinceLastInventoryMove < Baritone.settings().ticksBetweenInventoryMoves.value) {
logDebug("Inventory move requested but delaying " + ticksSinceLastInventoryMove + " " + Baritone.settings().ticksBetweenInventoryMoves.value);
return false;
}
if (Baritone.settings().inventoryMoveOnlyIfStationary.value && !baritone.getInventoryPauserProcess().stationaryForInventoryMove()) {
logDebug("Inventory move requested but delaying until stationary");
return false;
}
ctx.playerController().windowClick(ctx.player().inventoryContainer.windowId, inInventory < 9 ? inInventory + 36 : inInventory, inHotbar, ClickType.SWAP, ctx.player());
ticksSinceLastInventoryMove = 0;
lastTickRequestedMove = null;
return true;
}
private int firstValidThrowaway() { // TODO offhand idk
@@ -192,8 +216,8 @@ public final class InventoryBehavior extends Behavior {
if (allowInventory) {
for (int i = 9; i < 36; i++) {
if (desired.test(inv.get(i))) {
swapWithHotBar(i, 7);
if (select) {
requestSwapWithHotBar(i, 7);
p.inventory.currentItem = 7;
}
return true;

View File

@@ -562,7 +562,10 @@ public final class BuilderProcess extends BaritoneProcessHelper implements IBuil
for (int i = 9; i < 36; i++) {
for (IBlockState desired : noValidHotbarOption) {
if (valid(approxPlaceable.get(i), desired, true)) {
baritone.getInventoryBehavior().attemptToPutOnHotbar(i, usefulSlots::contains);
if (!baritone.getInventoryBehavior().attemptToPutOnHotbar(i, usefulSlots::contains)) {
// awaiting inventory move, so pause
return new PathingCommand(null, PathingCommandType.REQUEST_PAUSE);
}
break outer;
}
}

View File

@@ -0,0 +1,90 @@
/*
* 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.process;
import baritone.Baritone;
import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType;
import baritone.utils.BaritoneProcessHelper;
public class InventoryPauserProcess extends BaritoneProcessHelper {
boolean pauseRequestedLastTick;
boolean safeToCancelLastTick;
int ticksOfStationary;
public InventoryPauserProcess(Baritone baritone) {
super(baritone);
}
@Override
public boolean isActive() {
if (mc.player == null || mc.world == null) {
return false;
}
return true;
}
private double motion() {
return Math.sqrt(mc.player.motionX * mc.player.motionX + mc.player.motionZ * mc.player.motionZ);
}
private boolean stationaryNow() {
return motion() < 0.00001;
}
public boolean stationaryForInventoryMove() {
pauseRequestedLastTick = true;
return safeToCancelLastTick && ticksOfStationary > 1;
}
@Override
public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
//logDebug(pauseRequestedLastTick + " " + safeToCancelLastTick + " " + ticksOfStationary);
safeToCancelLastTick = isSafeToCancel;
if (pauseRequestedLastTick) {
pauseRequestedLastTick = false;
if (stationaryNow()) {
ticksOfStationary++;
}
return new PathingCommand(null, PathingCommandType.REQUEST_PAUSE);
}
ticksOfStationary = 0;
return new PathingCommand(null, PathingCommandType.DEFER);
}
@Override
public void onLostControl() {
}
@Override
public String displayName0() {
return "inventory pauser";
}
@Override
public double priority() {
return 5.1; // slightly higher than backfill
}
@Override
public boolean isTemporary() {
return true;
}
}