Baritone AI pathfinder

Baritone AI pathfinder

72.7k Downloads

Is it possible to simulate a right click using baritone api?

keygun-development opened this issue ยท 2 comments

commented

What do you need help with?

I'm trying to automate a process using baritone and my own mod. It needs to check whether the player has jungle saplings in their inventory.
If that's not the case then baritone should go to a position and simulate a right click.
I'll share some of my code

    @Override
    public void execute(String s, IArgConsumer iArgConsumer) {
        iArgConsumer.requireExactly(3);

        boolean oakMode = iArgConsumer.getAs(Integer.class) == 1;
        boolean jungleMode = iArgConsumer.getAs(Integer.class) == 1;
        boolean autoHarvestLeaves = iArgConsumer.getAs(Integer.class) == 1;

        if (!oakMode && !jungleMode) {
            logDirect("At least one of Oak Mode or Jungle Mode must be enabled.");
            return;
        }

        boolean hasJungleSaplings = baritone.getPlayerContext().player().getInventory().containsAny(Set.of(Items.JUNGLE_SAPLING));

        if (jungleMode && !hasJungleSaplings) {
            baritone.getCustomGoalProcess().setGoalAndPath(new GoalXZ(0, 0));
            // (Right) Click the block 
            baritone.getPlayerContext().playerController().clickBlock(new BlockPos(0, 0, 0), Direction.DOWN);
        }
    }

I'm extending the Command class with my custom command class.

Final checklist

  • I know how to properly use check boxes
  • I have not used any OwO's or UwU's in this issue.
commented

TLDR: it depends.
Baritone has InputOverrideHandler which has some capabilities to "press" WASD, space, shift and mouse buttons, but it doesn't behave exactly like physically pressing keys. Most notably it won't simulate clicks when not looking at a block because it was made for interacting with blocks.
If you want to place a block or interact with a block using InputOverrideHandler is probably the easiest solution (it's on API, right?). There's also some helpers to look at things, but iirc those might be internal.
If you want to eat or otherwise use an item you should probably do the clicking yourself.

You definitely don't want PlayerController.click. That's the function called the first tick you left click a block (i.e. instamine or start breaking). PlayerController.processRightClickBlock/PlayerController.processRightClick might be what you want.

commented

@ZacSharp Right, after trying out a little bit yesterday I indeed find the InputOverrideHandler to be the easiest and probably also the recommended solution.

So now i make the player look at the note block that needs to be right clicked and then call:
baritone.getInputOverrideHandler().setInputForceState(CLICK_RIGHT, true);