From a6ce4694fcb2020529a339c452a27cdc37d88464 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sat, 12 Jan 2019 19:51:21 -0800 Subject: [PATCH] hotbar selection uwu --- src/main/java/baritone/Baritone.java | 7 ++- .../baritone/behavior/InventoryBehavior.java | 60 +++++++++++++++++++ .../pathing/movement/CalculationContext.java | 2 +- .../pathing/movement/MovementHelper.java | 45 ++------------ .../movement/movements/MovementAscend.java | 2 +- .../movement/movements/MovementParkour.java | 2 +- .../movement/movements/MovementPillar.java | 3 +- .../movement/movements/MovementTraverse.java | 2 +- .../java/baritone/process/BuilderProcess.java | 28 +++++++-- 9 files changed, 98 insertions(+), 53 deletions(-) diff --git a/src/main/java/baritone/Baritone.java b/src/main/java/baritone/Baritone.java index 358a4f2bf..8a3bf8c3a 100755 --- a/src/main/java/baritone/Baritone.java +++ b/src/main/java/baritone/Baritone.java @@ -74,6 +74,7 @@ public class Baritone implements IBaritone { private PathingBehavior pathingBehavior; private LookBehavior lookBehavior; private MemoryBehavior memoryBehavior; + private InventoryBehavior inventoryBehavior; private InputOverrideHandler inputOverrideHandler; private FollowProcess followProcess; @@ -105,7 +106,7 @@ public class Baritone implements IBaritone { pathingBehavior = new PathingBehavior(this); lookBehavior = new LookBehavior(this); memoryBehavior = new MemoryBehavior(this); - new InventoryBehavior(this); + inventoryBehavior = new InventoryBehavior(this); inputOverrideHandler = new InputOverrideHandler(this); new ExampleBaritoneControl(this); } @@ -174,6 +175,10 @@ public class Baritone implements IBaritone { return this.builderProcess; } + public InventoryBehavior getInventoryBehavior() { + return this.inventoryBehavior; + } + @Override public LookBehavior getLookBehavior() { return this.lookBehavior; diff --git a/src/main/java/baritone/behavior/InventoryBehavior.java b/src/main/java/baritone/behavior/InventoryBehavior.java index a49cbb7de..29e45a962 100644 --- a/src/main/java/baritone/behavior/InventoryBehavior.java +++ b/src/main/java/baritone/behavior/InventoryBehavior.java @@ -21,8 +21,10 @@ import baritone.Baritone; import baritone.api.event.events.TickEvent; import baritone.utils.ToolSet; import net.minecraft.block.Block; +import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.init.Blocks; import net.minecraft.inventory.ClickType; +import net.minecraft.item.Item; import net.minecraft.item.ItemPickaxe; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemTool; @@ -87,4 +89,62 @@ public class InventoryBehavior extends Behavior { } return bestInd; } + + public boolean hasGenericThrowaway() { + for (Item item : Baritone.settings().acceptableThrowawayItems.get()) { + if (throwaway(false, item)) { + return true; + } + } + return false; + } + + public boolean selectThrowawayForLocation(int x, int y, int z) { + Item maybe = baritone.getBuilderProcess().placeAt(x, y, z); + if (maybe != null && throwaway(true, maybe)) { + return true; // gotem + } + for (Item item : Baritone.settings().acceptableThrowawayItems.get()) { + if (throwaway(true, item)) { + return true; + } + } + return false; + } + + private boolean throwaway(boolean select, Item desired) { + EntityPlayerSP p = ctx.player(); + NonNullList inv = p.inventory.mainInventory; + for (byte i = 0; i < 9; i++) { + ItemStack item = inv.get(i); + // this usage of settings() is okay because it's only called once during pathing + // (while creating the CalculationContext at the very beginning) + // and then it's called during execution + // since this function is never called during cost calculation, we don't need to migrate + // acceptableThrowawayItems to the CalculationContext + if (desired.equals(item.getItem())) { + if (select) { + p.inventory.currentItem = i; + } + return true; + } + } + if (desired.equals(p.inventory.offHandInventory.get(0).getItem())) { + // main hand takes precedence over off hand + // that means that if we have block A selected in main hand and block B in off hand, right clicking places block B + // we've already checked above ^ and the main hand can't possible have an acceptablethrowawayitem + // so we need to select in the main hand something that doesn't right click + // so not a shovel, not a hoe, not a block, etc + for (byte i = 0; i < 9; i++) { + ItemStack item = inv.get(i); + if (item.isEmpty() || item.getItem() instanceof ItemPickaxe) { + if (select) { + p.inventory.currentItem = i; + } + return true; + } + } + } + return false; + } } diff --git a/src/main/java/baritone/pathing/movement/CalculationContext.java b/src/main/java/baritone/pathing/movement/CalculationContext.java index 6af1dae95..ebca5f194 100644 --- a/src/main/java/baritone/pathing/movement/CalculationContext.java +++ b/src/main/java/baritone/pathing/movement/CalculationContext.java @@ -81,7 +81,7 @@ public class CalculationContext { this.worldData = (WorldData) baritone.getWorldProvider().getCurrentWorld(); this.bsi = new BlockStateInterface(world, worldData, forUseOnAnotherThread); this.toolSet = new ToolSet(player); - this.hasThrowaway = Baritone.settings().allowPlace.get() && MovementHelper.throwaway(baritone.getPlayerContext(), false); + this.hasThrowaway = Baritone.settings().allowPlace.get() && ((Baritone) baritone).getInventoryBehavior().hasGenericThrowaway(); this.hasWaterBucket = Baritone.settings().allowWaterBucketFall.get() && InventoryPlayer.isHotbar(player.inventory.getSlotFor(STACK_BUCKET_WATER)) && !world.provider.isNether(); this.canSprint = Baritone.settings().allowSprint.get() && player.getFoodStats().getFoodLevel() > 6; this.placeBlockCost = Baritone.settings().blockPlacementPenalty.get(); diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index 790144f9d..9bfebcc2a 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -18,6 +18,7 @@ package baritone.pathing.movement; import baritone.Baritone; +import baritone.api.IBaritone; import baritone.api.pathing.movement.ActionCosts; import baritone.api.pathing.movement.MovementStatus; import baritone.api.utils.*; @@ -31,10 +32,7 @@ import net.minecraft.block.properties.PropertyBool; import net.minecraft.block.state.IBlockState; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.init.Blocks; -import net.minecraft.item.ItemPickaxe; -import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; -import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; @@ -403,42 +401,6 @@ public interface MovementHelper extends ActionCosts, Helper { ctx.player().inventory.currentItem = ts.getBestSlot(b.getBlock()); } - static boolean throwaway(IPlayerContext ctx, boolean select) { - EntityPlayerSP p = ctx.player(); - NonNullList inv = p.inventory.mainInventory; - for (byte i = 0; i < 9; i++) { - ItemStack item = inv.get(i); - // this usage of settings() is okay because it's only called once during pathing - // (while creating the CalculationContext at the very beginning) - // and then it's called during execution - // since this function is never called during cost calculation, we don't need to migrate - // acceptableThrowawayItems to the CalculationContext - if (Baritone.settings().acceptableThrowawayItems.get().contains(item.getItem())) { - if (select) { - p.inventory.currentItem = i; - } - return true; - } - } - if (Baritone.settings().acceptableThrowawayItems.get().contains(p.inventory.offHandInventory.get(0).getItem())) { - // main hand takes precedence over off hand - // that means that if we have block A selected in main hand and block B in off hand, right clicking places block B - // we've already checked above ^ and the main hand can't possible have an acceptablethrowawayitem - // so we need to select in the main hand something that doesn't right click - // so not a shovel, not a hoe, not a block, etc - for (byte i = 0; i < 9; i++) { - ItemStack item = inv.get(i); - if (item.isEmpty() || item.getItem() instanceof ItemPickaxe) { - if (select) { - p.inventory.currentItem = i; - } - return true; - } - } - } - return false; - } - static void moveTowards(IPlayerContext ctx, MovementState state, BlockPos pos) { EntityPlayerSP player = ctx.player(); state.setTarget(new MovementTarget( @@ -493,12 +455,13 @@ public interface MovementHelper extends ActionCosts, Helper { && state.getValue(BlockLiquid.LEVEL) != 0; } - static PlaceResult attemptToPlaceABlock(MovementState state, IPlayerContext ctx, BlockPos placeAt, boolean preferDown) { + static PlaceResult attemptToPlaceABlock(MovementState state, IBaritone baritone, BlockPos placeAt, boolean preferDown) { + IPlayerContext ctx = baritone.getPlayerContext(); boolean found = false; for (int i = 0; i < 5; i++) { BlockPos against1 = placeAt.offset(HORIZONTALS_BUT_ALSO_DOWN____SO_EVERY_DIRECTION_EXCEPT_UP[i]); if (MovementHelper.canPlaceAgainst(ctx, against1)) { - if (!MovementHelper.throwaway(ctx, true)) { // get ready to place a throwaway block + if (!((Baritone) baritone).getInventoryBehavior().selectThrowawayForLocation(placeAt.getX(), placeAt.getY(), placeAt.getZ())) { // get ready to place a throwaway block Helper.HELPER.logDebug("bb pls get me some blocks. dirt or cobble"); state.setStatus(MovementStatus.UNREACHABLE); return PlaceResult.NO_OPTION; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java index 8508e776d..d937cc8f7 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java @@ -157,7 +157,7 @@ public class MovementAscend extends Movement { IBlockState jumpingOnto = BlockStateInterface.get(ctx, positionToPlace); if (!MovementHelper.canWalkOn(ctx, positionToPlace, jumpingOnto)) { ticksWithoutPlacement++; - if (MovementHelper.attemptToPlaceABlock(state, ctx, dest.down(), false) == PlaceResult.READY_TO_PLACE) { + if (MovementHelper.attemptToPlaceABlock(state, baritone, dest.down(), false) == PlaceResult.READY_TO_PLACE) { state.setInput(Input.SNEAK, true); if (ctx.player().isSneaking()) { state.setInput(Input.CLICK_RIGHT, true); diff --git a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java index 2d668bfb3..98898690b 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java @@ -205,7 +205,7 @@ public class MovementParkour extends Movement { } } else if (!ctx.playerFeet().equals(src)) { if (ctx.playerFeet().equals(src.offset(direction)) || ctx.player().posY - ctx.playerFeet().getY() > 0.0001) { - if (!MovementHelper.canWalkOn(ctx, dest.down()) && !ctx.player().onGround && MovementHelper.attemptToPlaceABlock(state, ctx, dest.down(), true) == PlaceResult.READY_TO_PLACE) { + if (!MovementHelper.canWalkOn(ctx, dest.down()) && !ctx.player().onGround && MovementHelper.attemptToPlaceABlock(state, baritone, dest.down(), true) == PlaceResult.READY_TO_PLACE) { // go in the opposite order to check DOWN before all horizontals -- down is preferable because you don't have to look to the side while in midair, which could mess up the trajectory state.setInput(Input.CLICK_RIGHT, true); } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java index 92ac6cbee..a9d1e20e7 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java @@ -17,6 +17,7 @@ package baritone.pathing.movement.movements; +import baritone.Baritone; import baritone.api.IBaritone; import baritone.api.pathing.movement.MovementStatus; import baritone.api.utils.BetterBlockPos; @@ -202,7 +203,7 @@ public class MovementPillar extends Movement { return state; } else { // Get ready to place a throwaway block - if (!MovementHelper.throwaway(ctx, true)) { + if (!((Baritone) baritone).getInventoryBehavior().selectThrowawayForLocation(src.x, src.y, src.z)) { return state.setStatus(MovementStatus.UNREACHABLE); } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java index fa8922918..40597e97b 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java @@ -257,7 +257,7 @@ public class MovementTraverse extends Movement { } } double dist1 = Math.max(Math.abs(ctx.player().posX - (dest.getX() + 0.5D)), Math.abs(ctx.player().posZ - (dest.getZ() + 0.5D))); - PlaceResult p = MovementHelper.attemptToPlaceABlock(state, ctx, dest.down(), false); + PlaceResult p = MovementHelper.attemptToPlaceABlock(state, baritone, dest.down(), false); if ((p == PlaceResult.READY_TO_PLACE || dist1 < 0.6) && !Baritone.settings().assumeSafeWalk.get()) { state.setInput(Input.SNEAK, true); } diff --git a/src/main/java/baritone/process/BuilderProcess.java b/src/main/java/baritone/process/BuilderProcess.java index a679c0287..875fa81bc 100644 --- a/src/main/java/baritone/process/BuilderProcess.java +++ b/src/main/java/baritone/process/BuilderProcess.java @@ -37,6 +37,7 @@ import baritone.utils.Schematic; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.init.Blocks; +import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompressedStreamTools; @@ -93,6 +94,21 @@ public class BuilderProcess extends BaritoneProcessHelper { return schematic != null; } + public Item placeAt(int x, int y, int z) { + if (!isActive()) { + return null; + } + if (!schematic.inSchematic(x - origin.getX(), y - origin.getY(), z - origin.getZ())) { + return null; + } + IBlockState state = schematic.desiredState(x - origin.getX(), y - origin.getY(), z - origin.getZ()); + if (state.getBlock() == Blocks.AIR) { + return null; + } + return new ItemBlock(state.getBlock()); + } + + public Optional> toBreakNearPlayer(BuilderCalculationContext bcc) { BetterBlockPos center = ctx.playerFeet(); for (int dx = -5; dx <= 5; dx++) { @@ -129,7 +145,7 @@ public class BuilderProcess extends BaritoneProcessHelper { // need to iterate over incorrectPositions and see which ones we can "correct" from our current standing position - BuilderCalculationContext bcc = new BuilderCalculationContext(schematic, origin); + BuilderCalculationContext bcc = new BuilderCalculationContext(); if (!recalc(bcc)) { logDirect("Done building"); onLostControl(); @@ -321,13 +337,13 @@ public class BuilderProcess extends BaritoneProcessHelper { private final int originY; private final int originZ; - public BuilderCalculationContext(ISchematic schematic, Vec3i schematicOrigin) { + public BuilderCalculationContext() { super(BuilderProcess.this.baritone, true); // wew lad this.placable = placable(); - this.schematic = schematic; - this.originX = schematicOrigin.getX(); - this.originY = schematicOrigin.getY(); - this.originZ = schematicOrigin.getZ(); + this.schematic = BuilderProcess.this.schematic; + this.originX = origin.getX(); + this.originY = origin.getY(); + this.originZ = origin.getZ(); this.jumpPenalty += 10; this.backtrackCostFavoringCoefficient = 1;