| 1 | package net.minecraft.world.phys;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.network.codec.StreamCodec;
|
| 7 |
|
| 8 | public class BlockHitResult extends HitResult {
|
| 9 | public static final StreamCodec<ByteBuf, BlockHitResult> STREAM_CODEC = new StreamCodec<ByteBuf, BlockHitResult>() {
|
| 10 | public BlockHitResult decode(final ByteBuf input) {
|
| 11 | BlockPos pos = BlockPos.STREAM_CODEC.decode(input);
|
| 12 | Direction face = Direction.STREAM_CODEC.decode(input);
|
| 13 | float clickX = input.readFloat();
|
| 14 | float clickY = input.readFloat();
|
| 15 | float clickZ = input.readFloat();
|
| 16 | boolean inside = input.readBoolean();
|
| 17 | boolean worldBorder = input.readBoolean();
|
| 18 | return new BlockHitResult(new Vec3((double)pos.getX() + clickX, (double)pos.getY() + clickY, (double)pos.getZ() + clickZ), face, pos, inside, worldBorder);
|
| 19 | }
|
| 20 |
|
| 21 | public void encode(final ByteBuf output, final BlockHitResult blockHit) {
|
| 22 | BlockPos blockPos = blockHit.getBlockPos();
|
| 23 | BlockPos.STREAM_CODEC.encode(output, blockPos);
|
| 24 | Direction.STREAM_CODEC.encode(output, blockHit.getDirection());
|
| 25 | Vec3 location = blockHit.getLocation();
|
| 26 | output.writeFloat((float)(location.x - blockPos.getX()));
|
| 27 | output.writeFloat((float)(location.y - blockPos.getY()));
|
| 28 | output.writeFloat((float)(location.z - blockPos.getZ()));
|
| 29 | output.writeBoolean(blockHit.isInside());
|
| 30 | output.writeBoolean(blockHit.isWorldBorderHit());
|
| 31 | }
|
| 32 | };
|
| 33 | private final Direction direction;
|
| 34 | private final BlockPos blockPos;
|
| 35 | private final boolean miss;
|
| 36 | private final boolean inside;
|
| 37 | private final boolean worldBorderHit;
|
| 38 |
|
| 39 | public static BlockHitResult miss(final Vec3 location, final Direction direction, final BlockPos pos) {
|
| 40 | return new BlockHitResult(true, location, direction, pos, false, false);
|
| 41 | }
|
| 42 |
|
| 43 | public BlockHitResult(final Vec3 location, final Direction direction, final BlockPos pos, final boolean inside) {
|
| 44 | this(false, location, direction, pos, inside, false);
|
| 45 | }
|
| 46 |
|
| 47 | public BlockHitResult(final Vec3 location, final Direction direction, final BlockPos pos, final boolean inside, final boolean worldBorderHit) {
|
| 48 | this(false, location, direction, pos, inside, worldBorderHit);
|
| 49 | }
|
| 50 |
|
| 51 | private BlockHitResult(
|
| 52 | final boolean miss, final Vec3 location, final Direction direction, final BlockPos blockPos, final boolean inside, final boolean worldBorderHit
|
| 53 | ) {
|
| 54 | super(location);
|
| 55 | this.miss = miss;
|
| 56 | this.direction = direction;
|
| 57 | this.blockPos = blockPos;
|
| 58 | this.inside = inside;
|
| 59 | this.worldBorderHit = worldBorderHit;
|
| 60 | }
|
| 61 |
|
| 62 | public BlockHitResult withDirection(final Direction direction) {
|
| 63 | return new BlockHitResult(this.miss, this.location, direction, this.blockPos, this.inside, this.worldBorderHit);
|
| 64 | }
|
| 65 |
|
| 66 | public BlockHitResult withPosition(final BlockPos blockPos) {
|
| 67 | return new BlockHitResult(this.miss, this.location, this.direction, blockPos, this.inside, this.worldBorderHit);
|
| 68 | }
|
| 69 |
|
| 70 | public BlockHitResult hitBorder() {
|
| 71 | return new BlockHitResult(this.miss, this.location, this.direction, this.blockPos, this.inside, true);
|
| 72 | }
|
| 73 |
|
| 74 | public BlockPos getBlockPos() {
|
| 75 | return this.blockPos;
|
| 76 | }
|
| 77 |
|
| 78 | public Direction getDirection() {
|
| 79 | return this.direction;
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public HitResult.Type getType() {
|
| 84 | return this.miss ? HitResult.Type.MISS : HitResult.Type.BLOCK;
|
| 85 | }
|
| 86 |
|
| 87 | public boolean isInside() {
|
| 88 | return this.inside;
|
| 89 | }
|
| 90 |
|
| 91 | public boolean isWorldBorderHit() {
|
| 92 | return this.worldBorderHit;
|
| 93 | }
|
| 94 | }
|
| 95 |
|