diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index 43ace5e31..145a129fb 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -98,8 +98,15 @@ public final class Settings { public final Setting 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 stopWhenInventoryOpen = new Setting<>(false); + + /** + * Just here so mods that use the API don't break. Does nothing. + */ + @Deprecated + @JavaOnly public final Setting inventoryMoveOnlyIfStationary = new Setting<>(false); /** diff --git a/src/api/java/baritone/api/utils/SettingsUtil.java b/src/api/java/baritone/api/utils/SettingsUtil.java index 57ba8af90..b147d39b2 100644 --- a/src/api/java/baritone/api/utils/SettingsUtil.java +++ b/src/api/java/baritone/api/utils/SettingsUtil.java @@ -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) { diff --git a/src/main/java/baritone/behavior/InventoryBehavior.java b/src/main/java/baritone/behavior/InventoryBehavior.java index 43a553d4d..9934f07a9 100644 --- a/src/main/java/baritone/behavior/InventoryBehavior.java +++ b/src/main/java/baritone/behavior/InventoryBehavior.java @@ -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; @@ -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); @@ -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 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 disallowedHotbar) { @@ -110,18 +108,31 @@ public OptionalInt getTempHotbarSlot(Predicate 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; } @@ -225,8 +236,7 @@ public boolean throwaway(boolean select, Predicate 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; } diff --git a/src/main/java/baritone/utils/InputOverrideHandler.java b/src/main/java/baritone/utils/InputOverrideHandler.java index 38a32f515..05f8ae323 100755 --- a/src/main/java/baritone/utils/InputOverrideHandler.java +++ b/src/main/java/baritone/utils/InputOverrideHandler.java @@ -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; @@ -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); }