From 563028a5b3dd5a28faf43eec6dfddd78c4b4bf2f Mon Sep 17 00:00:00 2001 From: Leijurv Date: Tue, 9 Apr 2019 19:30:48 -0700 Subject: [PATCH] fix invalid player move packet, and a bunch of things in world scanner --- src/api/java/baritone/api/utils/Rotation.java | 3 ++ .../baritone/api/utils/RotationUtils.java | 4 ++ src/api/java/baritone/api/utils/VecUtils.java | 7 +++- .../java/baritone/cache/WorldScanner.java | 37 +++++++++++++------ 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/api/java/baritone/api/utils/Rotation.java b/src/api/java/baritone/api/utils/Rotation.java index 54f63ebfa..36beea7bc 100644 --- a/src/api/java/baritone/api/utils/Rotation.java +++ b/src/api/java/baritone/api/utils/Rotation.java @@ -36,6 +36,9 @@ public class Rotation { public Rotation(float yaw, float pitch) { this.yaw = yaw; this.pitch = pitch; + if (Float.isInfinite(yaw) || Float.isNaN(yaw) || Float.isInfinite(pitch) || Float.isNaN(pitch)) { + throw new IllegalStateException(yaw + " " + pitch); + } } /** diff --git a/src/api/java/baritone/api/utils/RotationUtils.java b/src/api/java/baritone/api/utils/RotationUtils.java index c925e44f2..acdc1c21f 100644 --- a/src/api/java/baritone/api/utils/RotationUtils.java +++ b/src/api/java/baritone/api/utils/RotationUtils.java @@ -29,6 +29,7 @@ import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.shapes.VoxelShape; +import net.minecraft.util.math.shapes.VoxelShapes; import java.util.Optional; @@ -180,6 +181,9 @@ public final class RotationUtils { IBlockState state = entity.world.getBlockState(pos); VoxelShape shape = state.getShape(entity.world, pos); + if (shape.isEmpty()) { + shape = VoxelShapes.fullCube(); + } for (Vec3d sideOffset : BLOCK_SIDE_MULTIPLIERS) { double xDiff = shape.getStart(EnumFacing.Axis.X) * sideOffset.x + shape.getEnd(EnumFacing.Axis.X) * (1 - sideOffset.x); double yDiff = shape.getStart(EnumFacing.Axis.Y) * sideOffset.y + shape.getEnd(EnumFacing.Axis.Y) * (1 - sideOffset.y); diff --git a/src/api/java/baritone/api/utils/VecUtils.java b/src/api/java/baritone/api/utils/VecUtils.java index b268c914a..c7a712157 100644 --- a/src/api/java/baritone/api/utils/VecUtils.java +++ b/src/api/java/baritone/api/utils/VecUtils.java @@ -21,7 +21,6 @@ import net.minecraft.block.BlockFire; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.Entity; import net.minecraft.util.EnumFacing; -import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.shapes.VoxelShape; @@ -46,9 +45,15 @@ public final class VecUtils { public static Vec3d calculateBlockCenter(World world, BlockPos pos) { IBlockState b = world.getBlockState(pos); VoxelShape shape = b.getCollisionShape(world, pos); + if (shape.isEmpty()) { + return getBlockPosCenter(pos); + } double xDiff = (shape.getStart(EnumFacing.Axis.X) + shape.getEnd(EnumFacing.Axis.X)) / 2; double yDiff = (shape.getStart(EnumFacing.Axis.Y) + shape.getEnd(EnumFacing.Axis.Y)) / 2; double zDiff = (shape.getStart(EnumFacing.Axis.Z) + shape.getEnd(EnumFacing.Axis.Z)) / 2; + if (Double.isNaN(xDiff) || Double.isNaN(yDiff) || Double.isNaN(zDiff)) { + throw new IllegalStateException(b + " " + pos + " " + shape); + } if (b.getBlock() instanceof BlockFire) {//look at bottom of fire when putting it out yDiff = 0; } diff --git a/src/main/java/baritone/cache/WorldScanner.java b/src/main/java/baritone/cache/WorldScanner.java index 6f81201b1..7cdbe1e81 100644 --- a/src/main/java/baritone/cache/WorldScanner.java +++ b/src/main/java/baritone/cache/WorldScanner.java @@ -28,15 +28,15 @@ import net.minecraft.world.chunk.BlockStateContainer; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.ChunkSection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.*; +import java.util.stream.IntStream; public enum WorldScanner implements IWorldScanner { INSTANCE; + private static final int[] DEFAULT_COORDINATE_ITERATION_ORDER = IntStream.range(0, 16).toArray(); + @Override public List scanChunkRadius(IPlayerContext ctx, List blocks, int max, int yLevelThreshold, int maxSearchRadius) { if (blocks.contains(null)) { @@ -53,6 +53,9 @@ public enum WorldScanner implements IWorldScanner { int playerChunkZ = ctx.playerFeet().getZ() >> 4; int playerY = ctx.playerFeet().getY(); + int playerYBlockStateContainerIndex = playerY >> 4; + int[] coordinateIterationOrder = IntStream.range(0, 16).boxed().sorted(Comparator.comparingInt(y -> Math.abs(y - playerYBlockStateContainerIndex))).mapToInt(x -> x).toArray(); + int searchRadiusSq = 0; boolean foundWithinY = false; while (true) { @@ -72,7 +75,9 @@ public enum WorldScanner implements IWorldScanner { continue; } allUnloaded = false; - scanChunkInto(chunkX << 4, chunkZ << 4, chunk, blocks, res, max, yLevelThreshold, playerY); + if (scanChunkInto(chunkX << 4, chunkZ << 4, chunk, blocks, res, max, yLevelThreshold, playerY, coordinateIterationOrder)) { + foundWithinY = true; + } } } if ((allUnloaded && foundChunks) @@ -100,13 +105,15 @@ public enum WorldScanner implements IWorldScanner { } ArrayList res = new ArrayList<>(); - scanChunkInto(pos.x << 4, pos.z << 4, chunk, blocks, res, max, yLevelThreshold, playerY); + scanChunkInto(pos.x << 4, pos.z << 4, chunk, blocks, res, max, yLevelThreshold, playerY, DEFAULT_COORDINATE_ITERATION_ORDER); return res; } - public void scanChunkInto(int chunkX, int chunkZ, Chunk chunk, List search, Collection result, int max, int yLevelThreshold, int playerY) { + private boolean scanChunkInto(int chunkX, int chunkZ, Chunk chunk, List search, Collection result, int max, int yLevelThreshold, int playerY, int[] coordinateIterationOrder) { ChunkSection[] chunkInternalStorageArray = chunk.getSections(); - for (int y0 = 0; y0 < 16; y0++) { + boolean foundWithinY = false; + for (int yIndex = 0; yIndex < 16; yIndex++) { + int y0 = coordinateIterationOrder[yIndex]; ChunkSection extendedblockstorage = chunkInternalStorageArray[y0]; if (extendedblockstorage == null) { continue; @@ -121,14 +128,22 @@ public enum WorldScanner implements IWorldScanner { IBlockState state = bsc.get(x, y, z); if (search.contains(state.getBlock())) { int yy = yReal | y; - result.add(new BlockPos(chunkX | x, yy, chunkZ | z)); - if (result.size() >= max && Math.abs(yy - playerY) < yLevelThreshold) { - return; + if (result.size() >= max) { + if (Math.abs(yy - playerY) < yLevelThreshold) { + foundWithinY = true; + } else { + if (foundWithinY) { + // have found within Y in this chunk, so don't need to consider outside Y + return true; + } + } } + result.add(new BlockPos(chunkX | x, yy, chunkZ | z)); } } } } } + return foundWithinY; } }