From 4bf2dd6851692dceb865fd2ec5c81439161ae3d7 Mon Sep 17 00:00:00 2001 From: Brady Date: Sat, 1 Jul 2023 15:16:26 -0500 Subject: [PATCH] Some initial organization --- .../java/baritone/PerformanceCritical.java | 33 +++++++++++++++++++ .../baritone/behavior/InventoryBehavior.java | 17 +++++++--- .../java/baritone/process/FarmProcess.java | 14 ++++---- src/main/java/baritone/utils/ToolSet.java | 3 +- 4 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 src/main/java/baritone/PerformanceCritical.java diff --git a/src/main/java/baritone/PerformanceCritical.java b/src/main/java/baritone/PerformanceCritical.java new file mode 100644 index 000000000..5d5dd9eb4 --- /dev/null +++ b/src/main/java/baritone/PerformanceCritical.java @@ -0,0 +1,33 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation that should be used on methods which are performance critical (i.e. called millions of times per second + * by the pathfinder) and should be modified with care. + * + * @author Brady + */ +@Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) +@Retention(RetentionPolicy.SOURCE) +public @interface PerformanceCritical {} diff --git a/src/main/java/baritone/behavior/InventoryBehavior.java b/src/main/java/baritone/behavior/InventoryBehavior.java index 93dc200cc..9e1b77f0f 100644 --- a/src/main/java/baritone/behavior/InventoryBehavior.java +++ b/src/main/java/baritone/behavior/InventoryBehavior.java @@ -33,6 +33,7 @@ import net.minecraft.util.NonNullList; import java.util.ArrayList; import java.util.OptionalInt; import java.util.Random; +import java.util.function.IntPredicate; import java.util.function.Predicate; public final class InventoryBehavior extends Behavior implements Helper { @@ -70,7 +71,7 @@ public final class InventoryBehavior extends Behavior implements Helper { } } - public boolean attemptToPutOnHotbar(int inMainInvy, Predicate disallowedHotbar) { + public boolean attemptToPutOnHotbar(int inMainInvy, IntPredicate disallowedHotbar) { OptionalInt destination = getTempHotbarSlot(disallowedHotbar); if (destination.isPresent()) { if (!requestSwapWithHotBar(inMainInvy, destination.getAsInt())) { @@ -80,7 +81,7 @@ public final class InventoryBehavior extends Behavior implements Helper { return true; } - public OptionalInt getTempHotbarSlot(Predicate disallowedHotbar) { + public OptionalInt getTempHotbarSlot(IntPredicate disallowedHotbar) { // we're using 0 and 8 for pickaxe and throwaway ArrayList candidates = new ArrayList<>(); for (int i = 1; i < 8; i++) { @@ -152,7 +153,7 @@ public final class InventoryBehavior extends Behavior implements Helper { public boolean hasGenericThrowaway() { for (Item item : Baritone.settings().acceptableThrowawayItems.value) { - if (throwaway(false, stack -> item.equals(stack.getItem()))) { + if (this.canSelectItem(stack -> item.equals(stack.getItem()))) { return true; } } @@ -175,8 +176,16 @@ public final class InventoryBehavior extends Behavior implements Helper { return false; } + public boolean canSelectItem(Predicate desired) { + return this.throwaway(false, desired); + } + + public boolean trySelectItem(Predicate desired) { + return this.throwaway(true, desired); + } + public boolean throwaway(boolean select, Predicate desired) { - return throwaway(select, desired, Baritone.settings().allowInventory.value); + return this.throwaway(select, desired, Baritone.settings().allowInventory.value); } public boolean throwaway(boolean select, Predicate desired, boolean allowInventory) { diff --git a/src/main/java/baritone/process/FarmProcess.java b/src/main/java/baritone/process/FarmProcess.java index 1536bdb61..1213198b9 100644 --- a/src/main/java/baritone/process/FarmProcess.java +++ b/src/main/java/baritone/process/FarmProcess.java @@ -269,7 +269,7 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro for (BlockPos pos : both) { boolean soulsand = openSoulsand.contains(pos); Optional rot = RotationUtils.reachableOffset(ctx, pos, new Vec3d(pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5), ctx.playerController().getBlockReachDistance(), false); - if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().throwaway(true, soulsand ? this::isNetherWart : this::isPlantable)) { + if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().trySelectItem(soulsand ? this::isNetherWart : this::isPlantable)) { RayTraceResult result = RayTraceUtils.rayTraceTowards(ctx.player(), rot.get(), ctx.playerController().getBlockReachDistance()); if (result.typeOfHit == RayTraceResult.Type.BLOCK && result.sideHit == EnumFacing.UP) { baritone.getLookBehavior().updateTarget(rot.get(), true); @@ -287,7 +287,7 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro } Vec3d faceCenter = new Vec3d(pos).add(0.5, 0.5, 0.5).add(new Vec3d(dir.getDirectionVec()).scale(0.5)); Optional rot = RotationUtils.reachableOffset(ctx, pos, faceCenter, ctx.playerController().getBlockReachDistance(), false); - if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().throwaway(true, this::isCocoa)) { + if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().trySelectItem(this::isCocoa)) { RayTraceResult result = RayTraceUtils.rayTraceTowards(ctx.player(), rot.get(), ctx.playerController().getBlockReachDistance()); if (result.typeOfHit == RayTraceResult.Type.BLOCK && result.sideHit == dir) { baritone.getLookBehavior().updateTarget(rot.get(), true); @@ -301,7 +301,7 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro } for (BlockPos pos : bonemealable) { Optional rot = RotationUtils.reachable(ctx, pos); - if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().throwaway(true, this::isBoneMeal)) { + if (rot.isPresent() && isSafeToCancel && baritone.getInventoryBehavior().trySelectItem(this::isBoneMeal)) { baritone.getLookBehavior().updateTarget(rot.get(), true); if (ctx.isLookingAt(pos)) { baritone.getInputOverrideHandler().setInputForceState(Input.CLICK_RIGHT, true); @@ -323,17 +323,17 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro for (BlockPos pos : toBreak) { goalz.add(new BuilderProcess.GoalBreak(pos)); } - if (baritone.getInventoryBehavior().throwaway(false, this::isPlantable)) { + if (baritone.getInventoryBehavior().canSelectItem(this::isPlantable)) { for (BlockPos pos : openFarmland) { goalz.add(new GoalBlock(pos.up())); } } - if (baritone.getInventoryBehavior().throwaway(false, this::isNetherWart)) { + if (baritone.getInventoryBehavior().canSelectItem(this::isNetherWart)) { for (BlockPos pos : openSoulsand) { goalz.add(new GoalBlock(pos.up())); } } - if (baritone.getInventoryBehavior().throwaway(false, this::isCocoa)) { + if (baritone.getInventoryBehavior().canSelectItem(this::isCocoa)) { for (BlockPos pos : openLog) { for (EnumFacing direction : EnumFacing.Plane.HORIZONTAL) { if (ctx.world().getBlockState(pos.offset(direction)).getBlock() instanceof BlockAir) { @@ -342,7 +342,7 @@ public final class FarmProcess extends BaritoneProcessHelper implements IFarmPro } } } - if (baritone.getInventoryBehavior().throwaway(false, this::isBoneMeal)) { + if (baritone.getInventoryBehavior().canSelectItem(this::isBoneMeal)) { for (BlockPos pos : bonemealable) { goalz.add(new GoalBlock(pos)); } diff --git a/src/main/java/baritone/utils/ToolSet.java b/src/main/java/baritone/utils/ToolSet.java index 61f11e563..e9b9043bb 100644 --- a/src/main/java/baritone/utils/ToolSet.java +++ b/src/main/java/baritone/utils/ToolSet.java @@ -18,6 +18,7 @@ package baritone.utils; import baritone.Baritone; +import baritone.PerformanceCritical; import baritone.utils.accessor.IItemTool; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; @@ -72,6 +73,7 @@ public class ToolSet { * @param state the blockstate to be mined * @return the speed of how fast we'll mine it. 1/(time in ticks) */ + @PerformanceCritical public double getStrVsBlock(IBlockState state) { return breakStrengthCache.computeIfAbsent(state.getBlock(), backendCalculation); } @@ -104,7 +106,6 @@ public class ToolSet { * @param b the blockstate to be mined * @return An int containing the index in the tools array that worked best */ - public int getBestSlot(Block b, boolean preferSilkTouch) { return getBestSlot(b, preferSilkTouch, false); }