| 1 | package thunder.hack.utility.player;
|
| 2 |
|
| 3 | import net.minecraft.client.multiplayer.prediction.BlockStatePredictionHandler;
|
| 4 | import net.minecraft.client.multiplayer.prediction.PredictiveAction;
|
| 5 | import net.minecraft.core.*;
|
| 6 | import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
|
| 7 | import net.minecraft.network.protocol.game.ServerboundPlayerCommandPacket;
|
| 8 | import net.minecraft.network.protocol.game.ServerboundSwingPacket;
|
| 9 | import net.minecraft.network.protocol.game.ServerboundUseItemOnPacket;
|
| 10 | import net.minecraft.util.Mth;
|
| 11 | import net.minecraft.world.InteractionHand;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import net.minecraft.world.entity.ExperienceOrb;
|
| 14 | import net.minecraft.world.entity.item.ItemEntity;
|
| 15 | import net.minecraft.world.level.ClipContext;
|
| 16 | import net.minecraft.world.level.block.Block;
|
| 17 | import net.minecraft.world.level.block.Blocks;
|
| 18 | import net.minecraft.world.phys.*;
|
| 19 | import org.jetbrains.annotations.NotNull;
|
| 20 | import org.jetbrains.annotations.Nullable;
|
| 21 | import thunder.hack.injection.accesors.IClientWorldMixin;
|
| 22 | import thunder.hack.utility.world.ExplosionUtility;
|
| 23 |
|
| 24 | import java.util.*;
|
| 25 |
|
| 26 | import static thunder.hack.features.modules.Module.mc;
|
| 27 |
|
| 28 | public final class InteractionUtility {
|
| 29 | private static final List<Block> SHIFT_BLOCKS = new ArrayList<>();
|
| 30 | static {
|
| 31 | SHIFT_BLOCKS.addAll(Arrays.asList(
|
| 32 | Blocks.ENDER_CHEST, Blocks.CHEST, Blocks.TRAPPED_CHEST, Blocks.CRAFTING_TABLE,
|
| 33 | Blocks.BIRCH_TRAPDOOR, Blocks.BAMBOO_TRAPDOOR, Blocks.DARK_OAK_TRAPDOOR, Blocks.CHERRY_TRAPDOOR,
|
| 34 | Blocks.ANVIL, Blocks.BREWING_STAND, Blocks.HOPPER, Blocks.DROPPER, Blocks.DISPENSER,
|
| 35 | Blocks.ACACIA_TRAPDOOR, Blocks.ENCHANTING_TABLE, Blocks.SHULKER_BOX));
|
| 36 | SHIFT_BLOCKS.addAll(Blocks.DYED_SHULKER_BOX.asList());
|
| 37 | }
|
| 38 |
|
| 39 | public static Map<BlockPos, Long> awaiting = new HashMap<>();
|
| 40 |
|
| 41 | public static boolean canSee(Vec3 vec) {
|
| 42 | return canSee(vec, vec);
|
| 43 | }
|
| 44 |
|
| 45 | public static boolean canSee(Entity entity) {
|
| 46 | Vec3 entityEyes = getEyesPos(entity);
|
| 47 | Vec3 entityPos = entity.position();
|
| 48 | return canSee(entityEyes, entityPos);
|
| 49 | }
|
| 50 |
|
| 51 | public static boolean canSee(Vec3 entityEyes, Vec3 entityPos) {
|
| 52 | if (mc.player == null || mc.level == null) return false;
|
| 53 |
|
| 54 | Vec3 playerEyes = getEyesPos(mc.player);
|
| 55 | if (ExplosionUtility.raycast(playerEyes, entityEyes, false) == HitResult.Type.MISS)
|
| 56 | return true;
|
| 57 |
|
| 58 | if (playerEyes.y() > entityPos.y())
|
| 59 | return ExplosionUtility.raycast(playerEyes, entityEyes, false) == HitResult.Type.MISS;
|
| 60 | return false;
|
| 61 | }
|
| 62 |
|
| 63 | public static Vec3 getEyesPos(@NotNull Entity entity) {
|
| 64 | return entity.position().add(0, entity.getEyeHeight(entity.getPose()), 0);
|
| 65 | }
|
| 66 |
|
| 67 | public static float @NotNull [] calculateAngle(Vec3 to) {
|
| 68 | return calculateAngle(getEyesPos(mc.player), to);
|
| 69 | }
|
| 70 |
|
| 71 | public static float @NotNull [] calculateAngle(@NotNull Vec3 from, @NotNull Vec3 to) {
|
| 72 | double difX = to.x - from.x;
|
| 73 | double difY = (to.y - from.y) * -1.0;
|
| 74 | double difZ = to.z - from.z;
|
| 75 | double dist = Mth.sqrt((float) (difX * difX + difZ * difZ));
|
| 76 |
|
| 77 | float yD = (float) Mth.wrapDegrees(Math.toDegrees(Math.atan2(difZ, difX)) - 90.0);
|
| 78 | float pD = (float) Mth.clamp(Mth.wrapDegrees(Math.toDegrees(Math.atan2(difY, dist))), -90f, 90f);
|
| 79 |
|
| 80 | return new float[]{yD, pD};
|
| 81 | }
|
| 82 |
|
| 83 | public static boolean placeBlock(BlockPos bp, Rotate rotate, Interact interact, PlaceMode mode, int slot, boolean returnSlot, boolean ignoreEntities) {
|
| 84 | int prevItem = mc.player.getInventory().getSelectedSlot();
|
| 85 | if (slot != -1) InventoryUtility.switchTo(slot);
|
| 86 | else return false;
|
| 87 |
|
| 88 | boolean result = placeBlock(bp, rotate, interact, mode, ignoreEntities);
|
| 89 |
|
| 90 | if (returnSlot) InventoryUtility.switchTo(prevItem);
|
| 91 | return result;
|
| 92 | }
|
| 93 |
|
| 94 | public static boolean placeBlock(BlockPos bp, Rotate rotate, Interact interact, PlaceMode mode, @NotNull SearchInvResult invResult, boolean returnSlot, boolean ignoreEntities) {
|
| 95 | int prevItem = mc.player.getInventory().getSelectedSlot();
|
| 96 | invResult.switchTo();
|
| 97 | boolean result = placeBlock(bp, rotate, interact, mode, ignoreEntities);
|
| 98 | if (returnSlot) InventoryUtility.switchTo(prevItem);
|
| 99 |
|
| 100 | return result;
|
| 101 | }
|
| 102 |
|
| 103 | public static boolean placeBlock(BlockPos bp, Rotate rotate, Interact interact, PlaceMode mode, boolean ignoreEntities) {
|
| 104 | BlockHitResult result = getPlaceResult(bp, interact, ignoreEntities);
|
| 105 | if (result == null || mc.level == null || mc.gameMode == null || mc.player == null) return false;
|
| 106 |
|
| 107 | boolean sprint = mc.player.isSprinting();
|
| 108 | boolean sneak = needSneak(mc.level.getBlockState(result.getBlockPos()).getBlock()) && !mc.player.isShiftKeyDown();
|
| 109 |
|
| 110 | if (sprint)
|
| 111 | mc.player.connection.send(new ServerboundPlayerCommandPacket(mc.player, ServerboundPlayerCommandPacket.Action.STOP_SPRINTING));
|
| 112 | if (sneak)
|
| 113 | mc.player.connection.send(new net.minecraft.network.protocol.game.ServerboundPlayerInputPacket(new net.minecraft.world.entity.player.Input(false, false, false, false, false, true, false)));
|
| 114 |
|
| 115 | float[] angle = calculateAngle(result.getLocation());
|
| 116 |
|
| 117 | switch (rotate) {
|
| 118 | case None -> {
|
| 119 |
|
| 120 | }
|
| 121 | case Default -> mc.player.connection.send(new ServerboundMovePlayerPacket.Rot(angle[0], angle[1], mc.player.onGround(), mc.player.horizontalCollision));
|
| 122 | case Grim -> mc.player.connection.send(new ServerboundMovePlayerPacket.PosRot(mc.player.getX(), mc.player.getY(), mc.player.getZ(), angle[0], angle[1], mc.player.onGround(), mc.player.horizontalCollision));
|
| 123 | }
|
| 124 |
|
| 125 | if (mode == PlaceMode.Normal)
|
| 126 | mc.gameMode.useItemOn(mc.player, InteractionHand.MAIN_HAND, result);
|
| 127 |
|
| 128 | if (mode == PlaceMode.Packet)
|
| 129 | sendSequencedPacket(id -> new ServerboundUseItemOnPacket(InteractionHand.MAIN_HAND, result, id));
|
| 130 |
|
| 131 | awaiting.put(bp, System.currentTimeMillis());
|
| 132 |
|
| 133 | if (rotate == Rotate.Grim)
|
| 134 | mc.player.connection.send(new ServerboundMovePlayerPacket.PosRot(mc.player.getX(), mc.player.getY(), mc.player.getZ(), mc.player.getYRot(), mc.player.getXRot(), mc.player.onGround(), mc.player.horizontalCollision));
|
| 135 |
|
| 136 | if (sneak)
|
| 137 | mc.player.connection.send(new net.minecraft.network.protocol.game.ServerboundPlayerInputPacket(new net.minecraft.world.entity.player.Input(false, false, false, false, false, false, false)));
|
| 138 |
|
| 139 | if (sprint)
|
| 140 | mc.player.connection.send(new ServerboundPlayerCommandPacket(mc.player, ServerboundPlayerCommandPacket.Action.START_SPRINTING));
|
| 141 |
|
| 142 | mc.player.connection.send(new ServerboundSwingPacket(InteractionHand.MAIN_HAND));
|
| 143 | return true;
|
| 144 | }
|
| 145 |
|
| 146 | public static boolean canPlaceBlock(@NotNull BlockPos bp, Interact interact, boolean ignoreEntities) {
|
| 147 | if (awaiting.containsKey(bp)) return false;
|
| 148 | return getPlaceResult(bp, interact, ignoreEntities) != null;
|
| 149 | }
|
| 150 |
|
| 151 | public static float @Nullable [] getPlaceAngle(@NotNull BlockPos bp, Interact interact, boolean ignoreEntities) {
|
| 152 | BlockHitResult result = getPlaceResult(bp, interact, ignoreEntities);
|
| 153 | if (result != null) return calculateAngle(result.getLocation());
|
| 154 | return null;
|
| 155 | }
|
| 156 |
|
| 157 | public static void sendSequencedPacket(PredictiveAction packetCreator) {
|
| 158 | if (mc.getConnection() == null || mc.level == null) return;
|
| 159 | try (BlockStatePredictionHandler pendingUpdateManager = ((IClientWorldMixin) mc.level).getPendingUpdateManager().startPredicting();) {
|
| 160 | int i = pendingUpdateManager.currentSequence();
|
| 161 | mc.getConnection().send(packetCreator.predict(i));
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | @Nullable
|
| 166 | public static BlockHitResult getPlaceResult(@NotNull BlockPos bp, Interact interact, boolean ignoreEntities) {
|
| 167 | if (!ignoreEntities)
|
| 168 | for (Entity entity : new ArrayList<>(mc.level.getEntitiesOfClass(Entity.class, new AABB(bp))))
|
| 169 | if (!(entity instanceof ItemEntity) && !(entity instanceof ExperienceOrb))
|
| 170 | return null;
|
| 171 |
|
| 172 | if (!mc.level.getBlockState(bp).canBeReplaced())
|
| 173 | return null;
|
| 174 |
|
| 175 | if (interact == Interact.AirPlace)
|
| 176 | return ExplosionUtility.rayCastBlock(new ClipContext(InteractionUtility.getEyesPos(mc.player), net.minecraft.world.phys.Vec3.atBottomCenterOf(bp), ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, mc.player), bp);
|
| 177 |
|
| 178 | ArrayList<BlockPosWithFacing> supports = getSupportBlocks(bp);
|
| 179 | for (BlockPosWithFacing support : supports) {
|
| 180 | if (interact != Interact.Vanilla) {
|
| 181 | @NotNull List<Direction> dirs = getStrictDirections(bp);
|
| 182 | if (dirs.isEmpty())
|
| 183 | return null;
|
| 184 |
|
| 185 | if (!dirs.contains(support.facing))
|
| 186 | continue;
|
| 187 | }
|
| 188 | BlockHitResult result = null;
|
| 189 | if (interact == Interact.Legit) {
|
| 190 | Vec3 p = getVisibleDirectionPoint(support.facing, support.position, 0, 6); //TODO Implement Range
|
| 191 | if (p != null)
|
| 192 | return new BlockHitResult(p, support.facing, support.position, false);
|
| 193 | } else {
|
| 194 | Vec3 directionVec = new Vec3(support.position.getX() + 0.5 + support.facing.getUnitVec3i().getX() * 0.5, support.position.getY() + 0.5 + support.facing.getUnitVec3i().getY() * 0.5, support.position.getZ() + 0.5 + support.facing.getUnitVec3i().getZ() * 0.5);
|
| 195 | result = new BlockHitResult(directionVec, support.facing, support.position, false);
|
| 196 | }
|
| 197 | return result;
|
| 198 | }
|
| 199 | return null;
|
| 200 | }
|
| 201 |
|
| 202 |
|
| 203 | public static @NotNull ArrayList<BlockPosWithFacing> getSupportBlocks(@NotNull BlockPos bp) {
|
| 204 | ArrayList<BlockPosWithFacing> list = new ArrayList<>();
|
| 205 |
|
| 206 | if (mc.level.getBlockState(bp.offset(0, -1, 0)).isRedstoneConductor(mc.level, bp.offset(0, -1, 0)) || awaiting.containsKey(bp.offset(0, -1, 0)))
|
| 207 | list.add(new BlockPosWithFacing(bp.offset(0, -1, 0), Direction.UP));
|
| 208 |
|
| 209 | if (mc.level.getBlockState(bp.offset(0, 1, 0)).isRedstoneConductor(mc.level, bp.offset(0, 1, 0)) || awaiting.containsKey(bp.offset(0, 1, 0)))
|
| 210 | list.add(new BlockPosWithFacing(bp.offset(0, 1, 0), Direction.DOWN));
|
| 211 |
|
| 212 | if (mc.level.getBlockState(bp.offset(-1, 0, 0)).isRedstoneConductor(mc.level, bp.offset(-1, 0, 0)) || awaiting.containsKey(bp.offset(-1, 0, 0)))
|
| 213 | list.add(new BlockPosWithFacing(bp.offset(-1, 0, 0), Direction.EAST));
|
| 214 |
|
| 215 | if (mc.level.getBlockState(bp.offset(1, 0, 0)).isRedstoneConductor(mc.level, bp.offset(1, 0, 0)) || awaiting.containsKey(bp.offset(1, 0, 0)))
|
| 216 | list.add(new BlockPosWithFacing(bp.offset(1, 0, 0), Direction.WEST));
|
| 217 |
|
| 218 | if (mc.level.getBlockState(bp.offset(0, 0, 1)).isRedstoneConductor(mc.level, bp.offset(0, 0, 1)) || awaiting.containsKey(bp.offset(0, 0, 1)))
|
| 219 | list.add(new BlockPosWithFacing(bp.offset(0, 0, 1), Direction.NORTH));
|
| 220 |
|
| 221 | if (mc.level.getBlockState(bp.offset(0, 0, -1)).isRedstoneConductor(mc.level, bp.offset(0, 0, -1)) || awaiting.containsKey(bp.offset(0, 0, -1)))
|
| 222 | list.add(new BlockPosWithFacing(bp.offset(0, 0, -1), Direction.SOUTH));
|
| 223 |
|
| 224 | return list;
|
| 225 | }
|
| 226 |
|
| 227 | public static @Nullable BlockPosWithFacing checkNearBlocks(@NotNull BlockPos blockPos) {
|
| 228 | if (mc.level.getBlockState(blockPos.offset(0, -1, 0)).isRedstoneConductor(mc.level, blockPos.offset(0, -1, 0)))
|
| 229 | return new BlockPosWithFacing(blockPos.offset(0, -1, 0), Direction.UP);
|
| 230 |
|
| 231 | else if (mc.level.getBlockState(blockPos.offset(-1, 0, 0)).isRedstoneConductor(mc.level, blockPos.offset(-1, 0, 0)))
|
| 232 | return new BlockPosWithFacing(blockPos.offset(-1, 0, 0), Direction.EAST);
|
| 233 |
|
| 234 | else if (mc.level.getBlockState(blockPos.offset(1, 0, 0)).isRedstoneConductor(mc.level, blockPos.offset(1, 0, 0)))
|
| 235 | return new BlockPosWithFacing(blockPos.offset(1, 0, 0), Direction.WEST);
|
| 236 |
|
| 237 | else if (mc.level.getBlockState(blockPos.offset(0, 0, 1)).isRedstoneConductor(mc.level, blockPos.offset(0, 0, 1)))
|
| 238 | return new BlockPosWithFacing(blockPos.offset(0, 0, 1), Direction.NORTH);
|
| 239 |
|
| 240 | else if (mc.level.getBlockState(blockPos.offset(0, 0, -1)).isRedstoneConductor(mc.level, blockPos.offset(0, 0, -1)))
|
| 241 | return new BlockPosWithFacing(blockPos.offset(0, 0, -1), Direction.SOUTH);
|
| 242 | return null;
|
| 243 | }
|
| 244 |
|
| 245 | public static float squaredDistanceFromEyes(@NotNull Vec3 vec) {
|
| 246 | double d0 = vec.x - mc.player.getX();
|
| 247 | double d1 = vec.z - mc.player.getZ();
|
| 248 | double d2 = vec.y - (mc.player.getY() + mc.player.getEyeHeight(mc.player.getPose()));
|
| 249 | return (float) (d0 * d0 + d1 * d1 + d2 * d2);
|
| 250 | }
|
| 251 |
|
| 252 | public static float squaredDistanceFromEyes2d(@NotNull Vec3 vec) {
|
| 253 | double d0 = vec.x - mc.player.getX();
|
| 254 | double d1 = vec.z - mc.player.getZ();
|
| 255 | return (float) (d0 * d0 + d1 * d1);
|
| 256 | }
|
| 257 |
|
| 258 | public static @NotNull List<Direction> getStrictDirections(@NotNull BlockPos bp) {
|
| 259 | List<Direction> visibleSides = new ArrayList<>();
|
| 260 | Vec3 positionVector = net.minecraft.world.phys.Vec3.atBottomCenterOf(bp);
|
| 261 |
|
| 262 | double westDelta = getEyesPos(mc.player).x - (positionVector.add(0.5, 0, 0).x);
|
| 263 | double eastDelta = getEyesPos(mc.player).x - (positionVector.add(-0.5, 0, 0).x);
|
| 264 | double northDelta = getEyesPos(mc.player).z - (positionVector.add(0, 0, 0.5).z);
|
| 265 | double southDelta = getEyesPos(mc.player).z - (positionVector.add(0, 0, -0.5).z);
|
| 266 | double upDelta = getEyesPos(mc.player).y - (positionVector.add(0, 0.5, 0).y);
|
| 267 | double downDelta = getEyesPos(mc.player).y - (positionVector.add(0, -0.5, 0).y);
|
| 268 |
|
| 269 | if (westDelta > 0 && isSolid(bp.west()))
|
| 270 | visibleSides.add(Direction.EAST);
|
| 271 | if (westDelta < 0 && isSolid(bp.east()))
|
| 272 | visibleSides.add(Direction.WEST);
|
| 273 | if (eastDelta < 0 && isSolid(bp.east()))
|
| 274 | visibleSides.add(Direction.WEST);
|
| 275 | if (eastDelta > 0 && isSolid(bp.west()))
|
| 276 | visibleSides.add(Direction.EAST);
|
| 277 |
|
| 278 | if (northDelta > 0 && isSolid(bp.north()))
|
| 279 | visibleSides.add(Direction.SOUTH);
|
| 280 | if (northDelta < 0 && isSolid(bp.south()))
|
| 281 | visibleSides.add(Direction.NORTH);
|
| 282 | if (southDelta < 0 && isSolid(bp.south()))
|
| 283 | visibleSides.add(Direction.NORTH);
|
| 284 | if (southDelta > 0 && isSolid(bp.north()))
|
| 285 | visibleSides.add(Direction.SOUTH);
|
| 286 |
|
| 287 | if (upDelta > 0 && isSolid(bp.below()))
|
| 288 | visibleSides.add(Direction.UP);
|
| 289 | if (upDelta < 0 && isSolid(bp.above()))
|
| 290 | visibleSides.add(Direction.DOWN);
|
| 291 | if (downDelta < 0 && isSolid(bp.above()))
|
| 292 | visibleSides.add(Direction.DOWN);
|
| 293 | if (downDelta > 0 && isSolid(bp.below()))
|
| 294 | visibleSides.add(Direction.UP);
|
| 295 |
|
| 296 | return visibleSides;
|
| 297 | }
|
| 298 |
|
| 299 | public static boolean isSolid(BlockPos bp) {
|
| 300 | return mc.level.getBlockState(bp).isRedstoneConductor(mc.level, bp) || awaiting.containsKey(bp);
|
| 301 | }
|
| 302 |
|
| 303 | public static @NotNull List<Direction> getStrictBlockDirections(@NotNull BlockPos bp) {
|
| 304 | List<Direction> visibleSides = new ArrayList<>();
|
| 305 | Vec3 pV = net.minecraft.world.phys.Vec3.atBottomCenterOf(bp);
|
| 306 |
|
| 307 | double westDelta = getEyesPos(mc.player).x - (pV.add(0.5, 0, 0).x);
|
| 308 | double eastDelta = getEyesPos(mc.player).x - (pV.add(-0.5, 0, 0).x);
|
| 309 | double northDelta = getEyesPos(mc.player).z - (pV.add(0, 0, 0.5).z);
|
| 310 | double southDelta = getEyesPos(mc.player).z - (pV.add(0, 0, -0.5).z);
|
| 311 | double upDelta = getEyesPos(mc.player).y - (pV.add(0, 0.5, 0).y);
|
| 312 | double downDelta = getEyesPos(mc.player).y - (pV.add(0, -0.5, 0).y);
|
| 313 |
|
| 314 | if (westDelta > 0 && mc.level.getBlockState(bp.east()).canBeReplaced())
|
| 315 | visibleSides.add(Direction.EAST);
|
| 316 |
|
| 317 | if (eastDelta < 0 && mc.level.getBlockState(bp.west()).canBeReplaced())
|
| 318 | visibleSides.add(Direction.WEST);
|
| 319 |
|
| 320 | if (northDelta > 0 && mc.level.getBlockState(bp.south()).canBeReplaced())
|
| 321 | visibleSides.add(Direction.SOUTH);
|
| 322 |
|
| 323 | if (southDelta < 0 && mc.level.getBlockState(bp.north()).canBeReplaced())
|
| 324 | visibleSides.add(Direction.NORTH);
|
| 325 |
|
| 326 | if (upDelta > 0 && mc.level.getBlockState(bp.above()).canBeReplaced())
|
| 327 | visibleSides.add(Direction.UP);
|
| 328 |
|
| 329 | if (downDelta < 0 && mc.level.getBlockState(bp.below()).canBeReplaced())
|
| 330 | visibleSides.add(Direction.DOWN);
|
| 331 |
|
| 332 | return visibleSides;
|
| 333 | }
|
| 334 |
|
| 335 | public static @Nullable BreakData getBreakData(BlockPos bp, Interact interact) {
|
| 336 | if (interact == Interact.Vanilla) return new BreakData(Direction.UP, net.minecraft.world.phys.Vec3.atBottomCenterOf(bp).add(0, 0.5, 0));
|
| 337 | if (interact == Interact.Strict) {
|
| 338 | float bestDistance = 999f;
|
| 339 | Direction bestDirection = Direction.UP;
|
| 340 | Vec3 bestVector = null;
|
| 341 |
|
| 342 | for (Direction dir : Direction.values()) {
|
| 343 | Vec3 directionVec = new Vec3(bp.getX() + 0.5 + dir.getUnitVec3i().getX() * 0.5, bp.getY() + 0.5 + dir.getUnitVec3i().getY() * 0.5, bp.getZ() + 0.5 + dir.getUnitVec3i().getZ() * 0.5);
|
| 344 | float distance = squaredDistanceFromEyes(directionVec);
|
| 345 | if (bestDistance > distance) {
|
| 346 | bestDirection = dir;
|
| 347 | bestVector = directionVec;
|
| 348 | bestDistance = distance;
|
| 349 | }
|
| 350 | }
|
| 351 |
|
| 352 | if (bestVector == null) return null;
|
| 353 | return new BreakData(bestDirection, bestVector);
|
| 354 | }
|
| 355 |
|
| 356 | if (interact == Interact.Legit) {
|
| 357 | float bestDistance = 999f;
|
| 358 | BreakData bestData = null;
|
| 359 | for (float x = 0f; x <= 1f; x += 0.2f) {
|
| 360 | for (float y = 0f; y <= 1; y += 0.2f) {
|
| 361 | for (float z = 0f; z <= 1; z += 0.2f) {
|
| 362 | Vec3 point = new Vec3(bp.getX() + x, bp.getY() + y, bp.getZ() + z);
|
| 363 | BlockHitResult wallCheck = mc.level.clip(new ClipContext(InteractionUtility.getEyesPos(mc.player), point, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, mc.player));
|
| 364 | if (wallCheck != null && wallCheck.getType() == HitResult.Type.BLOCK && !wallCheck.getBlockPos().equals(bp))
|
| 365 | continue;
|
| 366 | BlockHitResult result = ExplosionUtility.rayCastBlock(new ClipContext(InteractionUtility.getEyesPos(mc.player), point, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, mc.player), bp);
|
| 367 |
|
| 368 | if (squaredDistanceFromEyes(point) < bestDistance)
|
| 369 | if (result != null && result.getType() == HitResult.Type.BLOCK)
|
| 370 | bestData = new BreakData(result.getDirection(), result.getLocation());
|
| 371 | }
|
| 372 | }
|
| 373 | }
|
| 374 | if (bestData == null) return null;
|
| 375 | if (bestData.vector == null || bestData.dir == null) return null;
|
| 376 | return bestData;
|
| 377 | }
|
| 378 | return null;
|
| 379 | }
|
| 380 |
|
| 381 | public static @Nullable Vec3 getVisibleDirectionPoint(@NotNull Direction dir, @NotNull BlockPos bp, float wallRange, float range) {
|
| 382 | AABB brutBox = getDirectionBox(dir);
|
| 383 |
|
| 384 | // EAST, WEST
|
| 385 | if (brutBox.maxX - brutBox.minX == 0)
|
| 386 | for (double y = brutBox.minY; y < brutBox.maxY; y += 0.1f)
|
| 387 | for (double z = brutBox.minZ; z < brutBox.maxZ; z += 0.1f) {
|
| 388 | Vec3 point = new Vec3(bp.getX() + brutBox.minX, bp.getY() + y, bp.getZ() + z);
|
| 389 |
|
| 390 | if (shouldSkipPoint(point, bp, dir, wallRange, range))
|
| 391 | continue;
|
| 392 |
|
| 393 | return point;
|
| 394 | }
|
| 395 |
|
| 396 |
|
| 397 | // DOWN, UP
|
| 398 | if (brutBox.maxY - brutBox.minY == 0)
|
| 399 | for (double x = brutBox.minX; x < brutBox.maxX; x += 0.1f)
|
| 400 | for (double z = brutBox.minZ; z < brutBox.maxZ; z += 0.1f) {
|
| 401 | Vec3 point = new Vec3(bp.getX() + x, bp.getY() + brutBox.minY, bp.getZ() + z);
|
| 402 |
|
| 403 | if (shouldSkipPoint(point, bp, dir, wallRange, range))
|
| 404 | continue;
|
| 405 |
|
| 406 | return point;
|
| 407 | }
|
| 408 |
|
| 409 |
|
| 410 | // NORTH, SOUTH
|
| 411 | if (brutBox.maxZ - brutBox.minZ == 0)
|
| 412 | for (double x = brutBox.minX; x < brutBox.maxX; x += 0.1f)
|
| 413 | for (double y = brutBox.minY; y < brutBox.maxY; y += 0.1f) {
|
| 414 | Vec3 point = new Vec3(bp.getX() + x, bp.getY() + y, bp.getZ() + brutBox.minZ);
|
| 415 |
|
| 416 | if (shouldSkipPoint(point, bp, dir, wallRange, range))
|
| 417 | continue;
|
| 418 |
|
| 419 | return point;
|
| 420 | }
|
| 421 |
|
| 422 |
|
| 423 | return null;
|
| 424 | }
|
| 425 |
|
| 426 | private static @NotNull AABB getDirectionBox(Direction dir) {
|
| 427 | return switch (dir) {
|
| 428 | case UP -> new AABB(.15f, 1f, .15f, .85f, 1f, .85f);
|
| 429 | case DOWN -> new AABB(.15f, 0f, .15f, .85f, 0f, .85f);
|
| 430 |
|
| 431 | case EAST -> new AABB(1f, .15f, .15f, 1f, .85f, .85f);
|
| 432 | case WEST -> new AABB(0f, .15f, .15f, 0f, .85f, .85f);
|
| 433 |
|
| 434 | case NORTH -> new AABB(.15f, .15f, 0f, .85f, .85f, 0f);
|
| 435 | case SOUTH -> new AABB(.15f, .15f, 1f, .85f, .85f, 1f);
|
| 436 | };
|
| 437 | }
|
| 438 |
|
| 439 | private static boolean shouldSkipPoint(Vec3 point, BlockPos bp, Direction dir, float wallRange, float range) {
|
| 440 | ClipContext context = new ClipContext(InteractionUtility.getEyesPos(mc.player), point, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, mc.player);
|
| 441 | BlockHitResult result = mc.level.clip(context);
|
| 442 |
|
| 443 | float dst = InteractionUtility.squaredDistanceFromEyes(point);
|
| 444 |
|
| 445 | if (result != null
|
| 446 | && result.getType() == HitResult.Type.BLOCK
|
| 447 | && !result.getBlockPos().equals(bp)
|
| 448 | && dst > wallRange * wallRange)
|
| 449 | return true;
|
| 450 |
|
| 451 | return dst > range * range;
|
| 452 | }
|
| 453 |
|
| 454 | public static boolean needSneak(Block in) {
|
| 455 | return SHIFT_BLOCKS.contains(in);
|
| 456 | }
|
| 457 |
|
| 458 | public static void lookAt(BlockPos bp) {
|
| 459 | if (bp != null) {
|
| 460 | float[] angle = calculateAngle(net.minecraft.world.phys.Vec3.atBottomCenterOf(bp));
|
| 461 | mc.player.setYRot(angle[0]);
|
| 462 | mc.player.setXRot(angle[1]);
|
| 463 | }
|
| 464 | }
|
| 465 |
|
| 466 | public static boolean isVecInFOV(Vec3 pos, Integer fov) {
|
| 467 | double deltaX = pos.x() - mc.player.getX();
|
| 468 | double deltaZ = pos.z() - mc.player.getZ();
|
| 469 | float yawDelta = Mth.wrapDegrees((float) Mth.wrapDegrees(Math.toDegrees(Math.atan2(deltaZ, deltaX)) - 90.0) - Mth.wrapDegrees(mc.player.getYRot()));
|
| 470 | return Math.abs(yawDelta) <= fov;
|
| 471 | }
|
| 472 |
|
| 473 | public record BlockPosWithFacing(BlockPos position, Direction facing) {
|
| 474 | }
|
| 475 |
|
| 476 | public record BreakData(Direction dir, Vec3 vector) {
|
| 477 | }
|
| 478 |
|
| 479 | public enum PlaceMode {
|
| 480 | Packet,
|
| 481 | Normal
|
| 482 | }
|
| 483 |
|
| 484 | public enum Rotate {
|
| 485 | None,
|
| 486 | Default,
|
| 487 | Grim
|
| 488 | }
|
| 489 |
|
| 490 | public enum Interact {
|
| 491 | Vanilla,
|
| 492 | Strict,
|
| 493 | Legit,
|
| 494 | AirPlace
|
| 495 | }
|
| 496 | }
|
| 497 |
|