| 1 | package net.minecraft.world.item.context;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.world.InteractionHand;
|
| 6 | import net.minecraft.world.entity.player.Player;
|
| 7 | import net.minecraft.world.item.ItemStack;
|
| 8 | import net.minecraft.world.level.Level;
|
| 9 | import net.minecraft.world.phys.BlockHitResult;
|
| 10 | import net.minecraft.world.phys.Vec3;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | /**
|
| 14 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 15 | */
|
| 16 | public class BlockPlaceContext extends UseOnContext {
|
| 17 | private final BlockPos relativePos;
|
| 18 | protected boolean replaceClicked = true;
|
| 19 |
|
| 20 | public BlockPlaceContext(final Player player, final InteractionHand hand, final ItemStack itemInHand, final BlockHitResult hitResult) {
|
| 21 | this(player.level(), player, hand, itemInHand, hitResult);
|
| 22 | }
|
| 23 |
|
| 24 | public BlockPlaceContext(final UseOnContext context) {
|
| 25 | this(context.getLevel(), context.getPlayer(), context.getHand(), context.getItemInHand(), context.getHitResult());
|
| 26 | }
|
| 27 |
|
| 28 | /**
|
| 29 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 30 | */
|
| 31 | public BlockPlaceContext(
|
| 32 | final Level level, final @Nullable Player player, final InteractionHand hand, final ItemStack itemStackInHand, final BlockHitResult hitResult
|
| 33 | ) {
|
| 34 | super(level, player, hand, itemStackInHand, hitResult);
|
| 35 | this.relativePos = hitResult.getBlockPos().relative(hitResult.getDirection());
|
| 36 | this.replaceClicked = level.getBlockState(hitResult.getBlockPos()).canBeReplaced(this);
|
| 37 | }
|
| 38 |
|
| 39 | public static BlockPlaceContext at(final BlockPlaceContext context, final BlockPos pos, final Direction direction) {
|
| 40 | return new BlockPlaceContext(
|
| 41 | context.getLevel(),
|
| 42 | context.getPlayer(),
|
| 43 | context.getHand(),
|
| 44 | context.getItemInHand(),
|
| 45 | new BlockHitResult(
|
| 46 | new Vec3(pos.getX() + 0.5 + direction.getStepX() * 0.5, pos.getY() + 0.5 + direction.getStepY() * 0.5, pos.getZ() + 0.5 + direction.getStepZ() * 0.5),
|
| 47 | direction,
|
| 48 | pos,
|
| 49 | false
|
| 50 | )
|
| 51 | );
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public BlockPos getClickedPos() {
|
| 56 | return this.replaceClicked ? super.getClickedPos() : this.relativePos;
|
| 57 | }
|
| 58 |
|
| 59 | public boolean canPlace() {
|
| 60 | return this.replaceClicked || this.getLevel().getBlockState(this.getClickedPos()).canBeReplaced(this);
|
| 61 | }
|
| 62 |
|
| 63 | public boolean replacingClickedOnBlock() {
|
| 64 | return this.replaceClicked;
|
| 65 | }
|
| 66 |
|
| 67 | public Direction getNearestLookingDirection() {
|
| 68 | return Direction.orderedByNearest(this.getPlayer())[0];
|
| 69 | }
|
| 70 |
|
| 71 | public Direction getNearestLookingVerticalDirection() {
|
| 72 | return Direction.getFacingAxis(this.getPlayer(), Direction.Axis.Y);
|
| 73 | }
|
| 74 |
|
| 75 | public Direction[] getNearestLookingDirections() {
|
| 76 | Direction[] directions = Direction.orderedByNearest(this.getPlayer());
|
| 77 | if (this.replaceClicked) {
|
| 78 | return directions;
|
| 79 | }
|
| 80 |
|
| 81 | Direction clickedFace = this.getClickedFace();
|
| 82 | int index = 0;
|
| 83 |
|
| 84 | while (index < directions.length && directions[index] != clickedFace.getOpposite()) {
|
| 85 | index++;
|
| 86 | }
|
| 87 |
|
| 88 | if (index > 0) {
|
| 89 | System.arraycopy(directions, 0, directions, 1, index);
|
| 90 | directions[0] = clickedFace.getOpposite();
|
| 91 | }
|
| 92 |
|
| 93 | return directions;
|
| 94 | }
|
| 95 | }
|
| 96 |
|