From 299540725e70f5b06a1ceb77537c89a7e7c2c239 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Thu, 7 Feb 2019 13:59:37 -0800 Subject: [PATCH] a start on inventory movement --- .../baritone/behavior/InventoryBehavior.java | 31 ++++++++++ .../java/baritone/process/BuilderProcess.java | 60 +++++++++++++------ .../utils/ExampleBaritoneControl.java | 2 +- 3 files changed, 74 insertions(+), 19 deletions(-) diff --git a/src/main/java/baritone/behavior/InventoryBehavior.java b/src/main/java/baritone/behavior/InventoryBehavior.java index 908e304b..f3ce4a93 100644 --- a/src/main/java/baritone/behavior/InventoryBehavior.java +++ b/src/main/java/baritone/behavior/InventoryBehavior.java @@ -28,6 +28,9 @@ import net.minecraft.inventory.ClickType; import net.minecraft.item.*; import net.minecraft.util.NonNullList; +import java.util.ArrayList; +import java.util.OptionalInt; +import java.util.Random; import java.util.function.Predicate; public class InventoryBehavior extends Behavior { @@ -56,6 +59,34 @@ public class InventoryBehavior extends Behavior { } } + public void attemptToPutOnHotbar(int inMainInvy, Predicate disallowedHotbar) { + OptionalInt destination = getTempHotbarSlot(disallowedHotbar); + if (destination.isPresent()) { + swapWithHotBar(inMainInvy, destination.getAsInt()); + } + } + + public OptionalInt getTempHotbarSlot(Predicate disallowedHotbar) { + // we're using 0 and 8 for pickaxe and throwaway + ArrayList candidates = new ArrayList<>(); + for (int i = 1; i < 8; i++) { + if (ctx.player().inventory.mainInventory.get(i).isEmpty() && !disallowedHotbar.test(i)) { + candidates.add(i); + } + } + if (candidates.isEmpty()) { + for (int i = 1; i < 8; i++) { + if (!disallowedHotbar.test(i)) { + candidates.add(i); + } + } + } + if (candidates.isEmpty()) { + return OptionalInt.empty(); + } + return OptionalInt.of(candidates.get(new Random().nextInt(candidates.size()))); + } + private void swapWithHotBar(int inInventory, int inHotbar) { ctx.playerController().windowClick(ctx.player().inventoryContainer.windowId, inInventory < 9 ? inInventory + 36 : inInventory, inHotbar, ClickType.SWAP, ctx.player()); } diff --git a/src/main/java/baritone/process/BuilderProcess.java b/src/main/java/baritone/process/BuilderProcess.java index bfc8c3b9..351f0253 100644 --- a/src/main/java/baritone/process/BuilderProcess.java +++ b/src/main/java/baritone/process/BuilderProcess.java @@ -158,7 +158,7 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro } } - public Optional searchForPlacables(BuilderCalculationContext bcc) { + public Optional searchForPlacables(BuilderCalculationContext bcc, List desirableOnHotbar) { BetterBlockPos center = ctx.playerFeet(); for (int dx = -5; dx <= 5; dx++) { for (int dy = -5; dy <= 1; dy++) { @@ -175,6 +175,7 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro if (dy == 1 && bcc.bsi.get0(x, y + 1, z).getBlock() == Blocks.AIR) { continue; } + desirableOnHotbar.add(desired); Optional opt = possibleToPlace(desired, x, y, z, bcc.bsi); if (opt.isPresent()) { return opt; @@ -298,7 +299,8 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro } return new PathingCommand(null, PathingCommandType.REQUEST_PAUSE); } - Optional toPlace = searchForPlacables(bcc); + List desirableOnHotbar = new ArrayList<>(); + Optional toPlace = searchForPlacables(bcc, desirableOnHotbar); if (toPlace.isPresent() && isSafeToCancel && ctx.player().onGround && ticks <= 0) { Rotation rot = toPlace.get().rot; baritone.getLookBehavior().updateTarget(rot, true); @@ -310,11 +312,41 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro return new PathingCommand(null, PathingCommandType.REQUEST_PAUSE); } - Goal goal = assemble(bcc); + List approxPlacable = placable(36); + if (Baritone.settings().allowInventory.get()) { + ArrayList usefulSlots = new ArrayList<>(); + List noValidHotbarOption = new ArrayList<>(); + outer: + for (IBlockState desired : desirableOnHotbar) { + for (int i = 0; i < 9; i++) { + if (valid(approxPlacable.get(i), desired)) { + usefulSlots.add(i); + continue outer; + } + } + noValidHotbarOption.add(desired); + } + + outer: + for (int i = 9; i < 36; i++) { + for (IBlockState desired : noValidHotbarOption) { + if (valid(approxPlacable.get(i), desired)) { + baritone.getInventoryBehavior().attemptToPutOnHotbar(i, usefulSlots::contains); + break outer; + } + } + } + } + + + Goal goal = assemble(bcc, approxPlacable.subList(0, 9)); if (goal == null) { - logDirect("Unable to do it =("); - onLostControl(); - return null; + goal = assemble(bcc, approxPlacable); // we're far away, so assume that we have our whole inventory to recalculate placable properly + if (goal == null) { + logDirect("Unable to do it =("); + onLostControl(); + return null; + } } return new PathingCommandContext(goal, PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH, bcc); } @@ -371,8 +403,7 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro } } - private Goal assemble(BuilderCalculationContext bcc) { - List approxPlacable = placable(); + private Goal assemble(BuilderCalculationContext bcc, List approxPlacable) { List placable = incorrectPositions.stream().filter(pos -> bcc.bsi.get0(pos).getBlock() == Blocks.AIR && approxPlacable.contains(bcc.getSchematic(pos.x, pos.y, pos.z))).collect(Collectors.toList()); Goal[] toBreak = incorrectPositions.stream().filter(pos -> bcc.bsi.get0(pos).getBlock() != Blocks.AIR).map(GoalBreak::new).toArray(Goal[]::new); Goal[] toPlace = placable.stream().filter(pos -> !placable.contains(pos.down()) && !placable.contains(pos.down(2))).map(pos -> placementgoal(pos, bcc)).toArray(Goal[]::new); @@ -484,16 +515,9 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro return "Building " + name; } - /** - * Hotbar contents, if they were placed - *

- * Always length nine, empty slots become Blocks.AIR.getDefaultState() - * - * @return - */ - public List placable() { + public List placable(int size) { List result = new ArrayList<>(); - for (int i = 0; i < 9; i++) { + for (int i = 0; i < size; i++) { ItemStack stack = ctx.player().inventory.mainInventory.get(i); if (stack.isEmpty() || !(stack.getItem() instanceof ItemBlock)) { result.add(Blocks.AIR.getDefaultState()); @@ -520,7 +544,7 @@ public class BuilderProcess extends BaritoneProcessHelper implements IBuilderPro public BuilderCalculationContext() { super(BuilderProcess.this.baritone, true); // wew lad - this.placable = placable(); + this.placable = placable(9); this.schematic = BuilderProcess.this.schematic; this.originX = origin.getX(); this.originY = origin.getY(); diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index aeca0a0c..68c9eae1 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -258,7 +258,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { String file; BlockPos origin; try { - String[] coords = msg.substring("build".length()).split(" "); + String[] coords = msg.substring("build".length()).trim().split(" "); file = coords[0] + ".schematic"; origin = new BlockPos(Integer.parseInt(coords[1]), Integer.parseInt(coords[2]), Integer.parseInt(coords[3])); } catch (Exception ex) {