From 2d1b81dc20c360db96888eb9c1c1b496dbb3dab1 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Thu, 14 Jul 2022 22:00:18 -0700 Subject: [PATCH] vastly increase cuteness by removing the optional boolean --- .../pathing/movement/MovementHelper.java | 34 ++++++++++++------- .../pathing/precompute/PrecomputedData.java | 25 +++++++------- .../baritone/pathing/precompute/Ternary.java | 22 ++++++++++++ 3 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 src/main/java/baritone/pathing/precompute/Ternary.java diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index f915dd78..3d115d1d 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -25,6 +25,7 @@ import baritone.api.pathing.movement.MovementStatus; import baritone.api.utils.*; import baritone.api.utils.input.Input; import baritone.pathing.movement.MovementState.MovementTarget; +import baritone.pathing.precompute.Ternary; import baritone.utils.BlockStateInterface; import baritone.utils.ToolSet; import net.minecraft.block.*; @@ -40,6 +41,7 @@ import net.minecraft.world.IBlockAccess; import java.util.Optional; import static baritone.pathing.movement.Movement.HORIZONTALS_BUT_ALSO_DOWN_____SO_EVERY_DIRECTION_EXCEPT_UP; +import static baritone.pathing.precompute.Ternary.*; /** * Static helpers for cost calculation @@ -48,10 +50,6 @@ import static baritone.pathing.movement.Movement.HORIZONTALS_BUT_ALSO_DOWN_____S */ public interface MovementHelper extends ActionCosts, Helper { - Optional YES = Optional.of(true); - Optional NO = Optional.of(false); - Optional MAYBE = Optional.empty(); - static boolean avoidBreaking(BlockStateInterface bsi, int x, int y, int z, IBlockState state) { if (!bsi.worldBorder.canPlaceAt(x, y)) { return true; @@ -101,14 +99,17 @@ public interface MovementHelper extends ActionCosts, Helper { } static boolean canWalkThrough(BlockStateInterface bsi, int x, int y, int z, IBlockState state) { - Optional canWalkThrough = canWalkThroughBlockState(state); - if (canWalkThrough.isPresent()) { // note: don't replace this with the functional style, because the lambda is impure (it captures local variables as context), meaning it allocates - return canWalkThrough.get(); + Ternary canWalkThrough = canWalkThroughBlockState(state); + if (canWalkThrough == YES) { + return true; + } + if (canWalkThrough == NO) { + return false; } return canWalkThroughPosition(bsi, x, y, z, state); } - static Optional canWalkThroughBlockState(IBlockState state) { + static Ternary canWalkThroughBlockState(IBlockState state) { Block block = state.getBlock(); if (block == Blocks.AIR) { @@ -156,7 +157,11 @@ public interface MovementHelper extends ActionCosts, Helper { } try { // A dodgy catch-all at the end, for most blocks with default behaviour this will work, however where blocks are special this will error out, and we can handle it when we have this information - return Optional.of(block.isPassable(null, null)); + if (block.isPassable(null, null)) { + return YES; + } else { + return NO; + } } catch (Throwable exception) { System.out.println("The block " + state.getBlock().getLocalizedName() + " requires a special case due to the exception " + exception.getMessage()); return MAYBE; @@ -346,14 +351,17 @@ public interface MovementHelper extends ActionCosts, Helper { * @return Whether or not the specified block can be walked on */ static boolean canWalkOn(BlockStateInterface bsi, int x, int y, int z, IBlockState state) { - Optional canWalkOn = canWalkOnBlockState(state); - if (canWalkOn.isPresent()) { - return canWalkOn.get(); + Ternary canWalkOn = canWalkOnBlockState(state); + if (canWalkOn == YES) { + return true; + } + if (canWalkOn == NO) { + return false; } return canWalkOnPosition(bsi, x, y, z, state); } - static Optional canWalkOnBlockState(IBlockState state) { + static Ternary canWalkOnBlockState(IBlockState state) { Block block = state.getBlock(); if (block == Blocks.AIR || block == Blocks.MAGMA) { return NO; diff --git a/src/main/java/baritone/pathing/precompute/PrecomputedData.java b/src/main/java/baritone/pathing/precompute/PrecomputedData.java index 3ca2c466..5b5bbc52 100644 --- a/src/main/java/baritone/pathing/precompute/PrecomputedData.java +++ b/src/main/java/baritone/pathing/precompute/PrecomputedData.java @@ -22,7 +22,8 @@ import baritone.utils.BlockStateInterface; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; -import java.util.Optional; +import static baritone.pathing.precompute.Ternary.MAYBE; +import static baritone.pathing.precompute.Ternary.YES; public class PrecomputedData { // TODO add isFullyPassable @@ -37,21 +38,19 @@ public class PrecomputedData { // TODO add isFullyPassable private int fillData(int id, IBlockState state) { int blockData = 0; - Optional canWalkOnState = MovementHelper.canWalkOnBlockState(state); - if (canWalkOnState.isPresent()) { - if (canWalkOnState.get()) { - blockData |= canWalkOnMask; - } - } else { + Ternary canWalkOnState = MovementHelper.canWalkOnBlockState(state); + if (canWalkOnState == YES) { + blockData |= canWalkOnMask; + } + if (canWalkOnState == MAYBE) { blockData |= canWalkOnSpecialMask; } - Optional canWalkThroughState = MovementHelper.canWalkThroughBlockState(state); - if (canWalkThroughState.isPresent()) { - if (canWalkThroughState.get()) { - blockData |= canWalkThroughMask; - } - } else { + Ternary canWalkThroughState = MovementHelper.canWalkThroughBlockState(state); + if (canWalkThroughState == YES) { + blockData |= canWalkThroughMask; + } + if (canWalkOnState == MAYBE) { blockData |= canWalkThroughSpecialMask; } diff --git a/src/main/java/baritone/pathing/precompute/Ternary.java b/src/main/java/baritone/pathing/precompute/Ternary.java new file mode 100644 index 00000000..d4d8424e --- /dev/null +++ b/src/main/java/baritone/pathing/precompute/Ternary.java @@ -0,0 +1,22 @@ +/* + * 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.pathing.precompute; + +public enum Ternary { + YES, MAYBE, NO +}