Skip to content
Open
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
9 changes: 8 additions & 1 deletion src/api/java/baritone/api/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,15 @@ public final class Settings {
public final Setting<Integer> ticksBetweenInventoryMoves = new Setting<>(1);

/**
* Come to a halt before doing any inventory moves. Intended for anticheat such as 2b2t
* Come to a halt if inventory opens. Intended for anticheat such as 2b2t
*/
public final Setting<Boolean> stopWhenInventoryOpen = new Setting<>(false);

/**
* Just here so mods that use the API don't break. Does nothing.
*/
@Deprecated
@JavaOnly
public final Setting<Boolean> inventoryMoveOnlyIfStationary = new Setting<>(false);

/**
Expand Down
4 changes: 4 additions & 0 deletions src/api/java/baritone/api/utils/SettingsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public static void readAndApply(Settings settings, String settingsName) {
if ("allowjumpat256".equals(settingName)) {
settingName = "allowjumpatbuildlimit";
}
// TODO also remove soonish
if ("inventoryMoveOnlyIfStationary".equals(settingName)) {
settingName = "stopWhenInventoryOpen";
}
try {
parseAndApply(settings, settingName, settingValue);
} catch (Exception ex) {
Expand Down
62 changes: 36 additions & 26 deletions src/main/java/baritone/behavior/InventoryBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
import baritone.Baritone;
import baritone.api.event.events.TickEvent;
import baritone.api.utils.Helper;
import baritone.pathing.path.PathExecutor;
import baritone.utils.ToolSet;
import net.minecraft.client.gui.screens.inventory.InventoryScreen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList;
import net.minecraft.network.protocol.game.ServerboundContainerClosePacket;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.inventory.ClickType;
import net.minecraft.world.item.BlockItem;
Expand All @@ -45,8 +48,9 @@

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
private int ticksSinceLastInventoryMove = 0;
private int[] lastTickRequestedMove; // not everything asks every tick, so remember the request while coming to a halt
private boolean inventoryOpen = false;

public InventoryBehavior(Baritone baritone) {
super(baritone);
Expand All @@ -60,32 +64,26 @@ public void onTick(TickEvent event) {
if (event.getType() == TickEvent.Type.OUT) {
return;
}
if (ctx.player().containerMenu != ctx.player().inventoryMenu) {
// 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
requestSwapWithHotBar(firstValidThrowaway(), 8);
}
int pick = bestToolAgainst(Blocks.STONE, PickaxeItem.class);
if (pick >= 9) {
requestSwapWithHotBar(pick, 0);
}
if (lastTickRequestedMove != null) {
logDebug("Remembering to move " + lastTickRequestedMove[0] + " " + lastTickRequestedMove[1] + " from a previous tick");
requestSwapWithHotBar(lastTickRequestedMove[0], lastTickRequestedMove[1]);
PathExecutor currentPath = baritone.getPathingBehavior().getCurrent();
if (currentPath != null) {
int throwaway = firstValidThrowaway();
if (throwaway >= 9 && !currentPath.toPlace().isEmpty()) { // aka there are none on the hotbar, but there are some in main inventory
requestSwapWithHotBar(throwaway, 8);
}
int pick = bestToolAgainst(Blocks.STONE, PickaxeItem.class);
if (pick >= 9 && !currentPath.toBreak().isEmpty()) {
requestSwapWithHotBar(pick, 0);
}
if (lastTickRequestedMove != null) {
logDebug("Remembering to move " + lastTickRequestedMove[0] + " " + lastTickRequestedMove[1] + " from a previous tick");
requestSwapWithHotBar(lastTickRequestedMove[0], lastTickRequestedMove[1]);
}
}
}

public boolean attemptToPutOnHotbar(int inMainInvy, Predicate<Integer> disallowedHotbar) {
OptionalInt destination = getTempHotbarSlot(disallowedHotbar);
if (destination.isPresent()) {
if (!requestSwapWithHotBar(inMainInvy, destination.getAsInt())) {
return false;
}
}
return true;
return destination.isEmpty() || requestSwapWithHotBar(inMainInvy, destination.getAsInt());
}

public OptionalInt getTempHotbarSlot(Predicate<Integer> disallowedHotbar) {
Expand All @@ -110,18 +108,31 @@ public OptionalInt getTempHotbarSlot(Predicate<Integer> disallowedHotbar) {
}

private boolean requestSwapWithHotBar(int inInventory, int inHotbar) {
if (ctx.minecraft().screen == null) {
if (!inventoryOpen) {
ctx.player().sendOpenInventory();
inventoryOpen = true;
}
} else if (!(ctx.minecraft().screen instanceof InventoryScreen)) {
return false;
}
lastTickRequestedMove = new int[]{inInventory, inHotbar};
if (ticksSinceLastInventoryMove < Baritone.settings().ticksBetweenInventoryMoves.value) {
logDebug("Inventory move requested but delaying " + ticksSinceLastInventoryMove + " " + Baritone.settings().ticksBetweenInventoryMoves.value);
ticksSinceLastInventoryMove++;
return false;
}
if (Baritone.settings().inventoryMoveOnlyIfStationary.value && !baritone.getInventoryPauserProcess().stationaryForInventoryMove()) {
if (Baritone.settings().stopWhenInventoryOpen.value && !baritone.getInventoryPauserProcess().stationaryForInventoryMove()) {
logDebug("Inventory move requested but delaying until stationary");
return false;
}
ctx.playerController().windowClick(ctx.player().inventoryMenu.containerId, inInventory < 9 ? inInventory + 36 : inInventory, inHotbar, ClickType.SWAP, ctx.player());
ticksSinceLastInventoryMove = 0;
lastTickRequestedMove = null;
if (inventoryOpen) {
ctx.player().connection.send(new ServerboundContainerClosePacket(ctx.player().containerMenu.containerId));
inventoryOpen = false;
}
return true;
}

Expand Down Expand Up @@ -225,8 +236,7 @@ public boolean throwaway(boolean select, Predicate<? super ItemStack> desired, b
for (int i = 9; i < 36; i++) {
if (desired.test(inv.get(i))) {
if (select) {
requestSwapWithHotBar(i, 7);
p.getInventory().selected = 7;
requestSwapWithHotBar(i, p.getInventory().selected);
}
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/baritone/utils/InputOverrideHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import baritone.api.utils.IInputOverrideHandler;
import baritone.api.utils.input.Input;
import baritone.behavior.Behavior;
import net.minecraft.client.gui.screens.inventory.InventoryScreen;
import net.minecraft.client.player.KeyboardInput;

import java.util.HashMap;
Expand Down Expand Up @@ -87,6 +88,10 @@ public final void onTick(TickEvent event) {
if (event.getType() == TickEvent.Type.OUT) {
return;
}
if (Baritone.settings().stopWhenInventoryOpen.value && ctx.minecraft().screen instanceof InventoryScreen) {
ctx.player().input = new KeyboardInput(ctx.minecraft().options);
return;
}
if (isInputForcedDown(Input.CLICK_LEFT)) {
setInputForceState(Input.CLICK_RIGHT, false);
}
Expand Down