| 1 | package com.jellypudding.offlineclient.util;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.OfflineClient;
|
| 4 | import net.minecraft.client.Minecraft;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
|
| 8 | import net.minecraft.resources.Identifier;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.world.InteractionHand;
|
| 11 | import net.minecraft.world.InteractionResult;
|
| 12 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 13 | import net.minecraft.world.item.BlockItem;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 17 | import net.minecraft.world.effect.MobEffects;
|
| 18 | import net.minecraft.world.effect.MobEffectUtil;
|
| 19 | import net.minecraft.world.effect.MobEffectInstance;
|
| 20 | import net.minecraft.tags.FluidTags;
|
| 21 | import net.minecraft.world.level.ClipContext;
|
| 22 | import net.minecraft.world.level.EmptyBlockGetter;
|
| 23 | import net.minecraft.world.level.block.AnvilBlock;
|
| 24 | import net.minecraft.world.level.block.BaseEntityBlock;
|
| 25 | import net.minecraft.world.level.block.BasePressurePlateBlock;
|
| 26 | import net.minecraft.world.level.block.BedBlock;
|
| 27 | import net.minecraft.world.level.block.Block;
|
| 28 | import net.minecraft.world.level.block.Blocks;
|
| 29 | import net.minecraft.world.level.block.ButtonBlock;
|
| 30 | import net.minecraft.world.level.block.CakeBlock;
|
| 31 | import net.minecraft.world.level.block.CartographyTableBlock;
|
| 32 | import net.minecraft.world.level.block.ComparatorBlock;
|
| 33 | import net.minecraft.world.level.block.CraftingTableBlock;
|
| 34 | import net.minecraft.world.level.block.DoorBlock;
|
| 35 | import net.minecraft.world.level.block.EnchantingTableBlock;
|
| 36 | import net.minecraft.world.level.block.FallingBlock;
|
| 37 | import net.minecraft.world.level.block.FenceGateBlock;
|
| 38 | import net.minecraft.world.level.block.FlowerPotBlock;
|
| 39 | import net.minecraft.world.level.block.GrindstoneBlock;
|
| 40 | import net.minecraft.world.level.block.LeverBlock;
|
| 41 | import net.minecraft.world.level.block.LoomBlock;
|
| 42 | import net.minecraft.world.level.block.NoteBlock;
|
| 43 | import net.minecraft.world.level.block.RepeaterBlock;
|
| 44 | import net.minecraft.world.level.block.RespawnAnchorBlock;
|
| 45 | import net.minecraft.world.level.block.StonecutterBlock;
|
| 46 | import net.minecraft.world.level.block.TrapDoorBlock;
|
| 47 | import net.minecraft.world.level.block.state.BlockState;
|
| 48 | import net.minecraft.world.phys.AABB;
|
| 49 | import net.minecraft.world.phys.BlockHitResult;
|
| 50 | import net.minecraft.world.phys.HitResult;
|
| 51 | import net.minecraft.world.phys.Vec3;
|
| 52 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 53 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 54 |
|
| 55 | import java.util.ArrayList;
|
| 56 | import java.util.Collection;
|
| 57 | import java.util.Comparator;
|
| 58 | import java.util.List;
|
| 59 | import java.util.function.Consumer;
|
| 60 | import java.util.function.Predicate;
|
| 61 |
|
| 62 | public final class BlockUtil {
|
| 63 |
|
| 64 | private static final Minecraft MC = OfflineClient.MC;
|
| 65 |
|
| 66 | // Blast resistance a block needs to survive a crystal. Obsidian and up.
|
| 67 | public static final float BLAST_PROOF = 600;
|
| 68 |
|
| 69 | // The widest cube scan that still fits in one tick.
|
| 70 | private static final double MAX_SCAN_RANGE = 16;
|
| 71 |
|
| 72 | private static final AABB FULL_CUBE = new AABB(0, 0, 0, 1, 1, 1);
|
| 73 |
|
| 74 | // The vanilla break time formula. Off the ground mines five times slower and
|
| 75 | // the wrong tool takes over three times as long.
|
| 76 | private static final float AIR_PENALTY = 5;
|
| 77 | private static final int RIGHT_TOOL_DIVISOR = 30;
|
| 78 | private static final int WRONG_TOOL_DIVISOR = 100;
|
| 79 |
|
| 80 | // Top covers the head. Full seals the sides at head height as well.
|
| 81 | public enum TrapMode { TOP, FULL }
|
| 82 |
|
| 83 | private BlockUtil() {
|
| 84 | }
|
| 85 |
|
| 86 | public static BlockState state(BlockPos pos) {
|
| 87 | return MC.level.getBlockState(pos);
|
| 88 | }
|
| 89 |
|
| 90 | // Outside the world the game hands back void air which reads as replaceable.
|
| 91 | // The server refuses every placement there.
|
| 92 | public static boolean isReplaceable(BlockPos pos) {
|
| 93 | return MC.level.isInWorldBounds(pos) && state(pos).canBeReplaced();
|
| 94 | }
|
| 95 |
|
| 96 | public static boolean isSolid(BlockPos pos) {
|
| 97 | BlockState state = state(pos);
|
| 98 | return !state.isAir() && blocksMotion(state);
|
| 99 | }
|
| 100 |
|
| 101 | // Deprecated in vanilla with no replacement. Still the only check that treats
|
| 102 | // cobwebs and bamboo saplings as passable.
|
| 103 | @SuppressWarnings("deprecation")
|
| 104 | public static boolean blocksMotion(BlockState state) {
|
| 105 | return state.blocksMotion();
|
| 106 | }
|
| 107 |
|
| 108 | public static boolean isBreakable(BlockPos pos) {
|
| 109 | BlockState state = state(pos);
|
| 110 | return !state.isAir() && state.getDestroySpeed(MC.level, pos) >= 0;
|
| 111 | }
|
| 112 |
|
| 113 | // The three coordinates with spaces between.
|
| 114 | public static String text(BlockPos pos) {
|
| 115 | return pos.getX() + " " + pos.getY() + " " + pos.getZ();
|
| 116 | }
|
| 117 |
|
| 118 | public static double distanceTo(BlockPos pos) {
|
| 119 | return MC.player.getEyePosition().distanceTo(Vec3.atCenterOf(pos));
|
| 120 | }
|
| 121 |
|
| 122 | private record Scored(BlockPos pos, double distanceSq) {
|
| 123 | }
|
| 124 |
|
| 125 | // Nearest first.
|
| 126 | public static List<BlockPos> positionsWithin(double range) {
|
| 127 | range = Math.min(range, MAX_SCAN_RANGE);
|
| 128 | Vec3 eye = MC.player.getEyePosition();
|
| 129 | double limitSq = range * range;
|
| 130 | int r = (int) Math.ceil(range);
|
| 131 | BlockPos center = BlockPos.containing(eye);
|
| 132 | // Sorting on a fresh Vec3 per comparison allocates millions at this radius.
|
| 133 | List<Scored> scored = new ArrayList<>();
|
| 134 | for (int dx = -r; dx <= r; dx++) {
|
| 135 | for (int dy = -r; dy <= r; dy++) {
|
| 136 | for (int dz = -r; dz <= r; dz++) {
|
| 137 | BlockPos pos = center.offset(dx, dy, dz);
|
| 138 | double ox = pos.getX() + 0.5 - eye.x;
|
| 139 | double oy = pos.getY() + 0.5 - eye.y;
|
| 140 | double oz = pos.getZ() + 0.5 - eye.z;
|
| 141 | double distanceSq = ox * ox + oy * oy + oz * oz;
|
| 142 | if (distanceSq <= limitSq) {
|
| 143 | scored.add(new Scored(pos, distanceSq));
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 | }
|
| 148 | scored.sort(Comparator.comparingDouble(Scored::distanceSq));
|
| 149 | List<BlockPos> result = new ArrayList<>(scored.size());
|
| 150 | for (Scored entry : scored) {
|
| 151 | result.add(entry.pos());
|
| 152 | }
|
| 153 | return result;
|
| 154 | }
|
| 155 |
|
| 156 | // Every position in range in no particular order. The same mutable
|
| 157 | // position is handed over each time and is only valid inside the call.
|
| 158 | public static void forEachWithin(double range, Consumer<BlockPos> action) {
|
| 159 | range = Math.min(range, MAX_SCAN_RANGE);
|
| 160 | Vec3 eye = MC.player.getEyePosition();
|
| 161 | BlockPos centre = BlockPos.containing(eye);
|
| 162 | int r = (int) Math.ceil(range);
|
| 163 | double limitSq = range * range;
|
| 164 | BlockPos.MutableBlockPos cursor = new BlockPos.MutableBlockPos();
|
| 165 | for (int dx = -r; dx <= r; dx++) {
|
| 166 | for (int dy = -r; dy <= r; dy++) {
|
| 167 | for (int dz = -r; dz <= r; dz++) {
|
| 168 | double ox = centre.getX() + dx + 0.5 - eye.x;
|
| 169 | double oy = centre.getY() + dy + 0.5 - eye.y;
|
| 170 | double oz = centre.getZ() + dz + 0.5 - eye.z;
|
| 171 | if (ox * ox + oy * oy + oz * oz <= limitSq) {
|
| 172 | action.accept(cursor.set(centre.getX() + dx, centre.getY() + dy, centre.getZ() + dz));
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | public static Iterable<BlockPos> positionsAround(BlockPos center, int radius) {
|
| 180 | return BlockPos.betweenClosed(
|
| 181 | center.offset(-radius, -radius, -radius), center.offset(radius, radius, radius));
|
| 182 | }
|
| 183 |
|
| 184 | public static boolean diggable(BlockPos pos) {
|
| 185 | BlockState state = state(pos);
|
| 186 | if (state.isAir() || !isBreakable(pos)) {
|
| 187 | return false;
|
| 188 | }
|
| 189 | return !state.getShape(MC.level, pos).isEmpty();
|
| 190 | }
|
| 191 |
|
| 192 | public static boolean intersectsPlayer(BlockPos pos) {
|
| 193 | AABB block = new AABB(pos);
|
| 194 | return MC.player.getBoundingBox().intersects(block);
|
| 195 | }
|
| 196 |
|
| 197 | // Places against the neighbour in the support direction. True when the click was taken.
|
| 198 | public static boolean place(BlockPos target, Direction support, boolean rotate, boolean swing) {
|
| 199 | BlockPos against = target.relative(support);
|
| 200 | Direction face = support.getOpposite();
|
| 201 | Vec3 hit = Vec3.atCenterOf(against).add(
|
| 202 | face.getStepX() * 0.5, face.getStepY() * 0.5, face.getStepZ() * 0.5);
|
| 203 |
|
| 204 | if (rotate) {
|
| 205 | faceVector(hit);
|
| 206 | }
|
| 207 |
|
| 208 | return click(new BlockHitResult(hit, face, against, false), swing);
|
| 209 | }
|
| 210 |
|
| 211 | // Right clicks a face. True when the game accepted the click.
|
| 212 | private static boolean click(BlockHitResult hit, boolean swing) {
|
| 213 | InteractionResult outcome = MC.gameMode.useItemOn(MC.player, InteractionHand.MAIN_HAND, hit);
|
| 214 | if (!outcome.consumesAction()) {
|
| 215 | return false;
|
| 216 | }
|
| 217 | if (swing) {
|
| 218 | MC.player.swing(InteractionHand.MAIN_HAND);
|
| 219 | }
|
| 220 | return true;
|
| 221 | }
|
| 222 |
|
| 223 | // Places against a neighbour when there is one and clicks the spot itself otherwise.
|
| 224 | public static boolean placeAny(BlockPos target, boolean rotate, boolean swing) {
|
| 225 | Direction support = findPlaceSupport(target);
|
| 226 | return support != null ? place(target, support, rotate, swing) : placeDirect(target, rotate, swing);
|
| 227 | }
|
| 228 |
|
| 229 | // The server accepts a click on a replaceable block as a placement there.
|
| 230 | public static boolean placeDirect(BlockPos target, boolean rotate, boolean swing) {
|
| 231 | return placeDirect(target, Vec3.atCenterOf(target), rotate, swing);
|
| 232 | }
|
| 233 |
|
| 234 | // The hit point decides which half a slab or a stair lands in.
|
| 235 | public static boolean placeDirect(BlockPos target, Vec3 hit, boolean rotate, boolean swing) {
|
| 236 | if (!isReplaceable(target)) {
|
| 237 | return false;
|
| 238 | }
|
| 239 | if (rotate) {
|
| 240 | faceVector(hit);
|
| 241 | }
|
| 242 | return click(new BlockHitResult(hit, facingSide(target), target, false), swing);
|
| 243 | }
|
| 244 |
|
| 245 | // Looks at a point without moving the view.
|
| 246 | public static void faceVector(Vec3 point) {
|
| 247 | faceVector(point, RotationPriority.PLACE);
|
| 248 | }
|
| 249 |
|
| 250 | public static void faceVector(Vec3 point, RotationPriority priority) {
|
| 251 | RotationManager.request(RotationManager.yawTo(point), RotationManager.pitchTo(point), priority);
|
| 252 | }
|
| 253 |
|
| 254 | public static void centerPlayer() {
|
| 255 | double x = Mth.floor(MC.player.getX()) + 0.5;
|
| 256 | double z = Mth.floor(MC.player.getZ()) + 0.5;
|
| 257 | if (Math.abs(MC.player.getX() - x) < 0.01 && Math.abs(MC.player.getZ() - z) < 0.01) {
|
| 258 | return;
|
| 259 | }
|
| 260 | MC.player.setPos(x, MC.player.getY(), z);
|
| 261 | MC.player.connection.send(new ServerboundMovePlayerPacket.Pos(
|
| 262 | x, MC.player.getY(), z, MC.player.onGround(), MC.player.horizontalCollision));
|
| 263 | }
|
| 264 |
|
| 265 | // Lower wins. Minus one means the block is not on the list.
|
| 266 | private static int rankOf(Block block, Collection<Identifier> preferred) {
|
| 267 | Identifier id = BuiltInRegistries.BLOCK.getKey(block);
|
| 268 | int rank = 0;
|
| 269 | for (Identifier chosen : preferred) {
|
| 270 | if (chosen.equals(id)) {
|
| 271 | return rank;
|
| 272 | }
|
| 273 | rank++;
|
| 274 | }
|
| 275 | return -1;
|
| 276 | }
|
| 277 |
|
| 278 | // Hotbar slot holding a block item or minus one. Prefers the selected slot.
|
| 279 | public static int findBlockSlot() {
|
| 280 | return InventoryUtil.hotbarSlot(stack -> stack.getItem() instanceof BlockItem);
|
| 281 | }
|
| 282 |
|
| 283 | // The closest position within the range that passes the test.
|
| 284 | public static BlockPos nearestWithin(double range, Predicate<BlockPos> test) {
|
| 285 | range = Math.min(range, MAX_SCAN_RANGE);
|
| 286 | Vec3 eye = MC.player.getEyePosition();
|
| 287 | BlockPos centre = BlockPos.containing(eye);
|
| 288 | int r = (int) Math.ceil(range);
|
| 289 | double bestDistanceSq = range * range;
|
| 290 | BlockPos best = null;
|
| 291 | for (int dx = -r; dx <= r; dx++) {
|
| 292 | for (int dy = -r; dy <= r; dy++) {
|
| 293 | for (int dz = -r; dz <= r; dz++) {
|
| 294 | double ox = centre.getX() + dx + 0.5 - eye.x;
|
| 295 | double oy = centre.getY() + dy + 0.5 - eye.y;
|
| 296 | double oz = centre.getZ() + dz + 0.5 - eye.z;
|
| 297 | double distanceSq = ox * ox + oy * oy + oz * oz;
|
| 298 | // Anything further out than the best hit cannot win.
|
| 299 | if (distanceSq > bestDistanceSq) {
|
| 300 | continue;
|
| 301 | }
|
| 302 | BlockPos pos = centre.offset(dx, dy, dz);
|
| 303 | if (test.test(pos)) {
|
| 304 | bestDistanceSq = distanceSq;
|
| 305 | best = pos;
|
| 306 | }
|
| 307 | }
|
| 308 | }
|
| 309 | }
|
| 310 | return best;
|
| 311 | }
|
| 312 |
|
| 313 | private static boolean isBlastProof(BlockPos pos) {
|
| 314 | return state(pos).getBlock().getExplosionResistance() >= BLAST_PROOF;
|
| 315 | }
|
| 316 |
|
| 317 | // True whilst the player stands in a hole walled with blast proof blocks.
|
| 318 | // A two block hole counts when its second block is walled on its own.
|
| 319 | public static boolean playerInHole() {
|
| 320 | BlockPos feet = MC.player.blockPosition();
|
| 321 | if (!isBlastProof(feet.below())) {
|
| 322 | return false;
|
| 323 | }
|
| 324 | int open = 0;
|
| 325 | for (Direction side : Direction.Plane.HORIZONTAL) {
|
| 326 | BlockPos next = feet.relative(side);
|
| 327 | if (isBlastProof(next)) {
|
| 328 | continue;
|
| 329 | }
|
| 330 | open++;
|
| 331 | if (!isBlastProof(next.below())) {
|
| 332 | return false;
|
| 333 | }
|
| 334 | for (Direction other : Direction.Plane.HORIZONTAL) {
|
| 335 | if (other != side.getOpposite() && !isBlastProof(next.relative(other))) {
|
| 336 | return false;
|
| 337 | }
|
| 338 | }
|
| 339 | }
|
| 340 | return open < 2;
|
| 341 | }
|
| 342 |
|
| 343 | // A hotbar slot holding something that survives a crystal and can be broken again.
|
| 344 | // Bedrock and other unbreakable blocks are no use.
|
| 345 | public static int findBlastProofSlot() {
|
| 346 | return findBlockSlot(block ->
|
| 347 | block.getExplosionResistance() >= BLAST_PROOF && block.defaultDestroyTime() >= 0);
|
| 348 | }
|
| 349 |
|
| 350 | public static int findBlockSlot(Predicate<Block> filter) {
|
| 351 | return InventoryUtil.hotbarSlot(stack ->
|
| 352 | stack.getItem() instanceof BlockItem item && filter.test(item.getBlock()));
|
| 353 | }
|
| 354 |
|
| 355 | // The hotbar slot holding whichever allowed block sits highest up the list.
|
| 356 | public static int findRankedBlockSlot(Collection<Identifier> preferred, Predicate<Block> allowed) {
|
| 357 | int best = -1;
|
| 358 | int bestRank = Integer.MAX_VALUE;
|
| 359 | for (int i = 0; i < InventoryUtil.HOTBAR_SIZE; i++) {
|
| 360 | ItemStack stack = MC.player.getInventory().getItem(i);
|
| 361 | if (!(stack.getItem() instanceof BlockItem item) || !allowed.test(item.getBlock())) {
|
| 362 | continue;
|
| 363 | }
|
| 364 | int rank = rankOf(item.getBlock(), preferred);
|
| 365 | if (rank != -1 && rank < bestRank) {
|
| 366 | bestRank = rank;
|
| 367 | best = i;
|
| 368 | }
|
| 369 | }
|
| 370 | return best;
|
| 371 | }
|
| 372 |
|
| 373 | // The open spots above the head and at head height around it.
|
| 374 | public static List<BlockPos> trapSpots(BlockPos feet, boolean sealSides) {
|
| 375 | return trapSpots(feet, true, sealSides, false);
|
| 376 | }
|
| 377 |
|
| 378 | // Any mix of the block over the head and the four at head height and the one underfoot.
|
| 379 | public static List<BlockPos> trapSpots(BlockPos feet, boolean top, boolean sides, boolean bottom) {
|
| 380 | List<BlockPos> spots = new ArrayList<>();
|
| 381 | if (top) {
|
| 382 | addOpen(spots, feet.above(2));
|
| 383 | }
|
| 384 | if (sides) {
|
| 385 | for (Direction side : Direction.Plane.HORIZONTAL) {
|
| 386 | addOpen(spots, feet.above().relative(side));
|
| 387 | }
|
| 388 | }
|
| 389 | if (bottom) {
|
| 390 | addOpen(spots, feet.below());
|
| 391 | }
|
| 392 | return spots;
|
| 393 | }
|
| 394 |
|
| 395 | // A spot only counts when a full block would actually fit in it.
|
| 396 | private static void addOpen(List<BlockPos> spots, BlockPos pos) {
|
| 397 | if (isReplaceable(pos) && MC.level.isUnobstructed(
|
| 398 | Blocks.OBSIDIAN.defaultBlockState(), pos, CollisionContext.empty())) {
|
| 399 | spots.add(pos);
|
| 400 | }
|
| 401 | }
|
| 402 |
|
| 403 | // Clicks a block face. Vanilla skips a block interaction whilst the player sneaks.
|
| 404 | // The sneak is dropped for the click and put back straight after.
|
| 405 | public static boolean interact(BlockPos pos, Direction side) {
|
| 406 | boolean sneaking = MC.player.isShiftKeyDown();
|
| 407 | if (sneaking) {
|
| 408 | MC.player.setShiftKeyDown(false);
|
| 409 | }
|
| 410 | BlockHitResult result = new BlockHitResult(hitPoint(pos, side), side, pos, false);
|
| 411 | boolean used = MC.gameMode
|
| 412 | .useItemOn(MC.player, InteractionHand.MAIN_HAND, result).consumesAction();
|
| 413 | if (sneaking) {
|
| 414 | MC.player.setShiftKeyDown(true);
|
| 415 | }
|
| 416 | if (used) {
|
| 417 | MC.player.swing(InteractionHand.MAIN_HAND);
|
| 418 | }
|
| 419 | return used;
|
| 420 | }
|
| 421 |
|
| 422 | // True for a plain full standable cube.
|
| 423 | // Sand and gravel need something under the target to hold them up.
|
| 424 | public static boolean isBuildingBlock(Block block, BlockPos target) {
|
| 425 | BlockState state = block.defaultBlockState();
|
| 426 | if (!state.isCollisionShapeFullBlock(EmptyBlockGetter.INSTANCE, BlockPos.ZERO)) {
|
| 427 | return false;
|
| 428 | }
|
| 429 | if (block instanceof FallingBlock && FallingBlock.isFree(state(target.below()))) {
|
| 430 | return false;
|
| 431 | }
|
| 432 | return !opensOnClick(state);
|
| 433 | }
|
| 434 |
|
| 435 | // Placing against one of these opens it instead.
|
| 436 | public static boolean opensOnClick(BlockState state) {
|
| 437 | Block block = state.getBlock();
|
| 438 | return block instanceof BaseEntityBlock
|
| 439 | || block instanceof CraftingTableBlock
|
| 440 | || block instanceof AnvilBlock
|
| 441 | || block instanceof LoomBlock
|
| 442 | || block instanceof CartographyTableBlock
|
| 443 | || block instanceof GrindstoneBlock
|
| 444 | || block instanceof StonecutterBlock
|
| 445 | || block instanceof EnchantingTableBlock
|
| 446 | || block instanceof ButtonBlock
|
| 447 | || block instanceof LeverBlock
|
| 448 | || block instanceof BasePressurePlateBlock
|
| 449 | || block instanceof BedBlock
|
| 450 | || block instanceof FenceGateBlock
|
| 451 | || block instanceof DoorBlock
|
| 452 | || block instanceof TrapDoorBlock
|
| 453 | || block instanceof NoteBlock
|
| 454 | || block instanceof RepeaterBlock
|
| 455 | || block instanceof ComparatorBlock
|
| 456 | || block instanceof CakeBlock
|
| 457 | || block instanceof FlowerPotBlock
|
| 458 | || block instanceof RespawnAnchorBlock;
|
| 459 | }
|
| 460 |
|
| 461 | // A solid neighbour to place against. Blocks that open on click are skipped.
|
| 462 | // The face closest to the eyes wins.
|
| 463 | public static Direction findPlaceSupport(BlockPos target) {
|
| 464 | Vec3 eye = MC.player.getEyePosition();
|
| 465 | Direction best = null;
|
| 466 | double bestDistance = Double.MAX_VALUE;
|
| 467 | for (Direction side : Direction.values()) {
|
| 468 | BlockPos neighbor = target.relative(side);
|
| 469 | if (!isSolid(neighbor) || opensOnClick(state(neighbor))) {
|
| 470 | continue;
|
| 471 | }
|
| 472 | double distance = eye.distanceToSqr(hitPoint(neighbor, side.getOpposite()));
|
| 473 | if (distance < bestDistance) {
|
| 474 | bestDistance = distance;
|
| 475 | best = side;
|
| 476 | }
|
| 477 | }
|
| 478 | return best;
|
| 479 | }
|
| 480 |
|
| 481 | // Taken from the real shape and not from a full cube.
|
| 482 | public static Vec3 hitPoint(BlockPos pos, Direction side) {
|
| 483 | VoxelShape shape = state(pos).getShape(MC.level, pos);
|
| 484 | AABB box = shape.isEmpty() ? FULL_CUBE : shape.bounds();
|
| 485 | Vec3 center = Vec3.atLowerCornerOf(pos).add(box.getCenter());
|
| 486 | double halfX = (box.maxX - box.minX) * 0.5;
|
| 487 | double halfY = (box.maxY - box.minY) * 0.5;
|
| 488 | double halfZ = (box.maxZ - box.minZ) * 0.5;
|
| 489 | return center.add(side.getStepX() * halfX, side.getStepY() * halfY, side.getStepZ() * halfZ);
|
| 490 | }
|
| 491 |
|
| 492 | public static Direction facingSide(BlockPos pos) {
|
| 493 | Vec3 eye = MC.player.getEyePosition();
|
| 494 | Direction best = Direction.UP;
|
| 495 | double bestDistance = Double.MAX_VALUE;
|
| 496 | for (Direction side : Direction.values()) {
|
| 497 | double distance = eye.distanceToSqr(hitPoint(pos, side));
|
| 498 | if (distance < bestDistance) {
|
| 499 | bestDistance = distance;
|
| 500 | best = side;
|
| 501 | }
|
| 502 | }
|
| 503 | return best;
|
| 504 | }
|
| 505 |
|
| 506 | // True when nothing solid sits between the eyes and the point.
|
| 507 | public static boolean canSee(Vec3 point) {
|
| 508 | BlockHitResult hit = MC.level.clip(new ClipContext(MC.player.getEyePosition(), point,
|
| 509 | ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, MC.player));
|
| 510 | return hit.getType() == HitResult.Type.MISS;
|
| 511 | }
|
| 512 |
|
| 513 | public static boolean canSee(BlockPos pos) {
|
| 514 | Vec3 eye = MC.player.getEyePosition();
|
| 515 | Vec3 point = hitPoint(pos, facingSide(pos));
|
| 516 | BlockHitResult hit = MC.level.clip(new ClipContext(eye, point,
|
| 517 | ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, MC.player));
|
| 518 | // A target with no collider lets the ray run all the way to the end point.
|
| 519 | return hit.getType() == HitResult.Type.MISS || hit.getBlockPos().equals(pos);
|
| 520 | }
|
| 521 |
|
| 522 | // A plain right click on the nearest face. Nothing is placed by the call itself.
|
| 523 | public static boolean useOn(BlockPos pos, boolean rotate, boolean swing) {
|
| 524 | Direction side = facingSide(pos);
|
| 525 | Vec3 hit = hitPoint(pos, side);
|
| 526 | if (rotate) {
|
| 527 | faceVector(hit);
|
| 528 | }
|
| 529 | return click(new BlockHitResult(hit, side, pos, false), swing);
|
| 530 | }
|
| 531 |
|
| 532 | public static boolean isStandingOn(BlockPos pos) {
|
| 533 | VoxelShape shape = state(pos).getCollisionShape(MC.level, pos);
|
| 534 | if (shape.isEmpty()) {
|
| 535 | return false;
|
| 536 | }
|
| 537 | AABB feet = MC.player.getBoundingBox();
|
| 538 | AABB below = new AABB(feet.minX, feet.minY - 0.1, feet.minZ, feet.maxX, feet.minY + 0.05, feet.maxZ);
|
| 539 | return shape.bounds().move(pos).intersects(below);
|
| 540 | }
|
| 541 |
|
| 542 | public static boolean canInstantBreak(BlockPos pos) {
|
| 543 | if (MC.player.getAbilities().instabuild) {
|
| 544 | return true;
|
| 545 | }
|
| 546 | return state(pos).getDestroyProgress(MC.player, MC.level, pos) >= 1;
|
| 547 | }
|
| 548 |
|
| 549 | // Progress one tick of mining with this tool would make. The tool need not be held.
|
| 550 | // Follows the vanilla dig speed maths. A packet miner can time a tool it swapped away.
|
| 551 | public static float breakDelta(ItemStack tool, BlockPos pos) {
|
| 552 | BlockState state = state(pos);
|
| 553 | float hardness = state.getDestroySpeed(MC.level, pos);
|
| 554 | if (hardness < 0) {
|
| 555 | return 0;
|
| 556 | }
|
| 557 | if (hardness == 0) {
|
| 558 | return 1;
|
| 559 | }
|
| 560 | Player player = MC.player;
|
| 561 | float speed = ItemUtil.miningSpeed(tool, state);
|
| 562 | if (MobEffectUtil.hasDigSpeed(player)) {
|
| 563 | speed *= 1 + (MobEffectUtil.getDigSpeedAmplification(player) + 1) * 0.2f;
|
| 564 | }
|
| 565 | MobEffectInstance fatigue = player.getEffect(MobEffects.MINING_FATIGUE);
|
| 566 | if (fatigue != null) {
|
| 567 | speed *= switch (fatigue.getAmplifier()) {
|
| 568 | case 0 -> 0.3f;
|
| 569 | case 1 -> 0.09f;
|
| 570 | case 2 -> 0.0027f;
|
| 571 | default -> 8.1E-4f;
|
| 572 | };
|
| 573 | }
|
| 574 | speed *= (float) player.getAttributeValue(Attributes.BLOCK_BREAK_SPEED);
|
| 575 | if (player.isEyeInFluid(FluidTags.WATER)) {
|
| 576 | speed *= (float) player.getAttributeValue(Attributes.SUBMERGED_MINING_SPEED);
|
| 577 | }
|
| 578 | if (!player.onGround()) {
|
| 579 | speed /= AIR_PENALTY;
|
| 580 | }
|
| 581 | boolean rightTool = !state.requiresCorrectToolForDrops() || tool.isCorrectToolForDrops(state);
|
| 582 | return speed / hardness / (rightTool ? RIGHT_TOOL_DIVISOR : WRONG_TOOL_DIVISOR);
|
| 583 | }
|
| 584 |
|
| 585 | public static int breakTicks(BlockPos pos) {
|
| 586 | float perTick = state(pos).getDestroyProgress(MC.player, MC.level, pos);
|
| 587 | if (perTick <= 0) {
|
| 588 | return Integer.MAX_VALUE;
|
| 589 | }
|
| 590 | return (int) Math.ceil(1 / perTick);
|
| 591 | }
|
| 592 |
|
| 593 | // Ancient debris and gilded blackstone count too.
|
| 594 | public static boolean isOre(BlockState state) {
|
| 595 | Block block = state.getBlock();
|
| 596 | if (block == Blocks.ANCIENT_DEBRIS || block == Blocks.GILDED_BLACKSTONE) {
|
| 597 | return true;
|
| 598 | }
|
| 599 | return BuiltInRegistries.BLOCK.getKey(block).getPath().endsWith("_ore");
|
| 600 | }
|
| 601 |
|
| 602 | // Iron ore and deepslate iron ore share a family.
|
| 603 | public static String family(Block block) {
|
| 604 | String path = BuiltInRegistries.BLOCK.getKey(block).getPath();
|
| 605 | return path.startsWith("deepslate_") ? path.substring("deepslate_".length()) : path;
|
| 606 | }
|
| 607 |
|
| 608 | public static String blockName(Block block) {
|
| 609 | return block.getName().getString();
|
| 610 | }
|
| 611 | }
|
| 612 |
|