From 7e3a2d3c0a676ef955c1b345b1a789d51e8c6834 Mon Sep 17 00:00:00 2001 From: Brady Date: Tue, 5 Nov 2019 18:17:10 -0600 Subject: [PATCH] Create a BlockStateInterface specialized IBlockAccess wrapper --- .../pathing/movement/MovementHelper.java | 8 +- .../movement/movements/MovementParkour.java | 2 +- .../baritone/utils/BlockStateInterface.java | 4 +- .../BlockStateInterfaceAccessWrapper.java | 80 +++++++++++++++++++ 4 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 src/main/java/baritone/utils/BlockStateInterfaceAccessWrapper.java diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index 62a96a18..31a1de7a 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -134,7 +134,7 @@ public interface MovementHelper extends ActionCosts, Helper { return block == Blocks.WATER || block == Blocks.FLOWING_WATER; } - return block.isPassable(bsi.world, bsi.isPassableBlockPos.setPos(x, y, z)); + return block.isPassable(bsi.access, bsi.isPassableBlockPos.setPos(x, y, z)); } /** @@ -149,7 +149,7 @@ public interface MovementHelper extends ActionCosts, Helper { */ static boolean fullyPassable(CalculationContext context, int x, int y, int z) { return fullyPassable( - context.bsi.world, + context.bsi.access, context.bsi.isPassableBlockPos.setPos(x, y, z), context.bsi.get0(x, y, z) ); @@ -159,7 +159,7 @@ public interface MovementHelper extends ActionCosts, Helper { return fullyPassable(ctx.world(), pos, ctx.world().getBlockState(pos)); } - static boolean fullyPassable(IBlockAccess world, BlockPos pos, IBlockState state) { + static boolean fullyPassable(IBlockAccess access, BlockPos pos, IBlockState state) { Block block = state.getBlock(); if (block == Blocks.AIR) { // early return for most common case return true; @@ -181,7 +181,7 @@ public interface MovementHelper extends ActionCosts, Helper { return false; } // door, fence gate, liquid, trapdoor have been accounted for, nothing else uses the world or pos parameters - return block.isPassable(world, pos); + return block.isPassable(access, pos); } static boolean isReplaceable(int x, int y, int z, IBlockState state, BlockStateInterface bsi) { diff --git a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java index 0477c85c..3d3426da 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java @@ -114,7 +114,7 @@ public class MovementParkour extends Movement { return; } IBlockState destInto = context.bsi.get0(destX, y, destZ); - if (!MovementHelper.fullyPassable(context.bsi.world, context.bsi.isPassableBlockPos.setPos(destX, y, destZ), destInto)) { + if (!MovementHelper.fullyPassable(context.bsi.access, context.bsi.isPassableBlockPos.setPos(destX, y, destZ), destInto)) { if (i <= 3 && context.allowParkourAscend && context.canSprint && MovementHelper.canWalkOn(context.bsi, destX, y, destZ, destInto) && checkOvershootSafety(context.bsi, destX + xDiff, y + 1, destZ + zDiff)) { res.x = destX; res.y = y + 1; diff --git a/src/main/java/baritone/utils/BlockStateInterface.java b/src/main/java/baritone/utils/BlockStateInterface.java index bbe5a17e..4f5f2689 100644 --- a/src/main/java/baritone/utils/BlockStateInterface.java +++ b/src/main/java/baritone/utils/BlockStateInterface.java @@ -43,8 +43,9 @@ public class BlockStateInterface { private final Long2ObjectMap loadedChunks; private final WorldData worldData; - public final IBlockAccess world; + protected final IBlockAccess world; public final BlockPos.MutableBlockPos isPassableBlockPos; + public final IBlockAccess access; private Chunk prev = null; private CachedRegion prevCached = null; @@ -75,6 +76,7 @@ public class BlockStateInterface { throw new IllegalStateException(); } this.isPassableBlockPos = new BlockPos.MutableBlockPos(); + this.access = new BlockStateInterfaceAccessWrapper(this); } public boolean worldContainsLoadedChunk(int blockX, int blockZ) { diff --git a/src/main/java/baritone/utils/BlockStateInterfaceAccessWrapper.java b/src/main/java/baritone/utils/BlockStateInterfaceAccessWrapper.java new file mode 100644 index 00000000..6ce70193 --- /dev/null +++ b/src/main/java/baritone/utils/BlockStateInterfaceAccessWrapper.java @@ -0,0 +1,80 @@ +/* + * 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.utils; + +import net.minecraft.block.material.Material; +import net.minecraft.block.state.IBlockState; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.WorldType; +import net.minecraft.world.biome.Biome; + +import javax.annotation.Nullable; + +/** + * @author Brady + * @since 11/5/2019 + */ +@SuppressWarnings("NullableProblems") +public final class BlockStateInterfaceAccessWrapper implements IBlockAccess { + + private final BlockStateInterface bsi; + + BlockStateInterfaceAccessWrapper(BlockStateInterface bsi) { + this.bsi = bsi; + } + + @Nullable + @Override + public TileEntity getTileEntity(BlockPos pos) { + throw new UnsupportedOperationException("getTileEntity not supported by BlockStateInterfaceAccessWrapper"); + } + + @Override + public int getCombinedLight(BlockPos pos, int lightValue) { + throw new UnsupportedOperationException("getCombinedLight not supported by BlockStateInterfaceAccessWrapper"); + } + + @Override + public IBlockState getBlockState(BlockPos pos) { + // BlockStateInterface#get0(BlockPos) btfo! + return this.bsi.get0(pos.getX(), pos.getY(), pos.getZ()); + } + + @Override + public boolean isAirBlock(BlockPos pos) { + return this.bsi.get0(pos.getX(), pos.getY(), pos.getZ()).getMaterial() == Material.AIR; + } + + @Override + public Biome getBiome(BlockPos pos) { + throw new UnsupportedOperationException("getBiome not supported by BlockStateInterfaceAccessWrapper"); + } + + @Override + public int getStrongPower(BlockPos pos, EnumFacing direction) { + throw new UnsupportedOperationException("getStrongPower not supported by BlockStateInterfaceAccessWrapper"); + } + + @Override + public WorldType getWorldType() { + return this.bsi.world.getWorldType(); + } +}