Search in sources :

Example 26 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Electrodynamics by aurilisdev.

the class MathUtils method getRaytracedBlock.

public static Location getRaytracedBlock(Level world, Vec3 direction, Vec3 from, double rayLength) {
    // Just normalize for safety. Allows the direction
    // vector to be from some block to another with no
    // consideration for more math.
    Vec3 rayPath = direction.normalize().scale(rayLength);
    Vec3 to = from.add(rayPath);
    ClipContext rayContext = new ClipContext(from, to, ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, null);
    BlockHitResult rayHit = world.clip(rayContext);
    return rayHit.getType() != Type.BLOCK ? null : new Location(rayHit.getBlockPos());
}
Also used : ClipContext(net.minecraft.world.level.ClipContext) Vec3(net.minecraft.world.phys.Vec3) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Location(electrodynamics.prefab.utilities.object.Location)

Example 27 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project JustEnoughResources by way2muchnoise.

the class ChunkProfiler method profileChunk.

private void profileChunk(ChunkAccess chunk) {
    final ResourceKey<Level> worldRegistryKey = level.dimension();
    this.timer.startChunk(worldRegistryKey);
    Map<String, Integer[]> temp = new HashMap<>();
    BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
    HitResult rayTraceResult = new BlockHitResult(new Vec3(0, 0, 0), Direction.DOWN, blockPos, true);
    Player player = Minecraft.getInstance().player;
    final int maxY = chunk.getHighestSectionPosition();
    for (int y = 0; y < maxY; y++) for (int x = 0; x < CHUNK_SIZE; x++) for (int z = 0; z < CHUNK_SIZE; z++) {
        blockPos.set(x + chunk.getPos().x * CHUNK_SIZE, y, z + chunk.getPos().z * CHUNK_SIZE);
        BlockState blockState = chunk.getBlockState(new BlockPos(x, y, z));
        if (blacklist.contains(blockState))
            continue;
        final String key = MapKeys.getKey(blockState, rayTraceResult, level, blockPos, player);
        if (!dimensionData.dropsMap.containsKey(key)) {
            dimensionData.dropsMap.put(key, getDrops(level, blockPos, blockState));
        }
        if (!dimensionData.silkTouchMap.containsKey(key)) {
            Block block = blockState.getBlock();
            boolean canSilkTouch = block.canHarvestBlock(blockState, level, blockPos, player);
            dimensionData.silkTouchMap.put(key, canSilkTouch);
        }
        Integer[] array = temp.get(key);
        if (array == null) {
            array = new Integer[CHUNK_HEIGHT];
            Arrays.fill(array, 0);
        }
        array[y]++;
        temp.put(key, array);
    }
    for (Map.Entry<String, Integer[]> entry : temp.entrySet()) {
        Integer[] array = dimensionData.distributionMap.get(entry.getKey());
        if (array == null) {
            array = new Integer[CHUNK_HEIGHT];
            Arrays.fill(array, 0);
        }
        for (int i = 0; i < CHUNK_HEIGHT; i++) array[i] += entry.getValue()[i];
        dimensionData.distributionMap.put(entry.getKey(), array);
    }
    this.timer.endChunk(dimensionKey);
}
Also used : Player(net.minecraft.world.entity.player.Player) HashMap(java.util.HashMap) BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) BlockState(net.minecraft.world.level.block.state.BlockState) Vec3(net.minecraft.world.phys.Vec3) Block(net.minecraft.world.level.block.Block) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project AlexsMobs by Alex-the-666.

the class EntityDropBear method hasLineOfSightBlock.

private boolean hasLineOfSightBlock(BlockPos destinationBlock) {
    Vec3 Vector3d = new Vec3(this.getX(), this.getEyeY(), this.getZ());
    Vec3 blockVec = net.minecraft.world.phys.Vec3.atCenterOf(destinationBlock);
    BlockHitResult result = this.level.clip(new ClipContext(Vector3d, blockVec, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this));
    return result.getBlockPos().equals(destinationBlock);
}
Also used : ClipContext(net.minecraft.world.level.ClipContext) Vec3(net.minecraft.world.phys.Vec3) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 29 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project pollen by MoonflowerTeam.

the class BucketItemBase method use.

@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
    Fluid content = this.getFluid();
    ItemStack itemStack = player.getItemInHand(hand);
    BlockHitResult hitResult = getPlayerPOVHitResult(level, player, content == Fluids.EMPTY ? ClipContext.Fluid.SOURCE_ONLY : ClipContext.Fluid.NONE);
    if (hitResult.getType() == HitResult.Type.MISS)
        return InteractionResultHolder.pass(itemStack);
    if (hitResult.getType() != HitResult.Type.BLOCK)
        return InteractionResultHolder.pass(itemStack);
    BlockPos pos = hitResult.getBlockPos();
    Direction direction = hitResult.getDirection();
    BlockPos offsetPos = pos.relative(direction);
    if (level.mayInteract(player, pos) && player.mayUseItemAt(offsetPos, direction, itemStack)) {
        BlockState blockState = level.getBlockState(pos);
        if (content == Fluids.EMPTY) {
            if (blockState.getBlock() instanceof BucketPickup) {
                Fluid fluid = ((BucketPickup) blockState.getBlock()).takeLiquid(level, pos, blockState);
                if (fluid != Fluids.EMPTY) {
                    player.awardStat(Stats.ITEM_USED.get(this));
                    this.playFillSound(player, level, pos);
                    ItemStack itemStack2 = ItemUtils.createFilledResult(itemStack, player, new ItemStack(fluid.getBucket()));
                    if (player instanceof ServerPlayer)
                        CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayer) player, new ItemStack(fluid.getBucket()));
                    return InteractionResultHolder.sidedSuccess(itemStack2, level.isClientSide());
                }
            }
        } else {
            BlockPos emptyPos = blockState.getBlock() instanceof LiquidBlockContainer && content == Fluids.WATER ? pos : offsetPos;
            if (this.emptyBucket(player, level, emptyPos, hitResult)) {
                this.checkExtraContent(level, itemStack, emptyPos);
                if (player instanceof ServerPlayer)
                    CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer) player, emptyPos, itemStack);
                player.awardStat(Stats.ITEM_USED.get(this));
                return InteractionResultHolder.sidedSuccess(this.getEmptySuccessItem(itemStack, player), level.isClientSide());
            }
        }
    }
    return InteractionResultHolder.fail(itemStack);
}
Also used : BucketPickup(net.minecraft.world.level.block.BucketPickup) LiquidBlockContainer(net.minecraft.world.level.block.LiquidBlockContainer) BlockState(net.minecraft.world.level.block.state.BlockState) Fluid(net.minecraft.world.level.material.Fluid) PollinatedFluid(gg.moonflower.pollen.api.fluid.PollinatedFluid) FlowingFluid(net.minecraft.world.level.material.FlowingFluid) ServerPlayer(net.minecraft.server.level.ServerPlayer) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Direction(net.minecraft.core.Direction)

Example 30 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project TerraFirmaCraft by TerraFirmaCraft.

the class WoodenBucketItem method use.

@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
    final ItemStack stack = player.getItemInHand(hand);
    final IFluidHandler handler = stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY).resolve().orElse(null);
    if (handler == null) {
        return InteractionResultHolder.pass(stack);
    }
    if (handler.getFluidInTank(0).isEmpty()) {
        final BlockHitResult hit = Helpers.rayTracePlayer(level, player, ClipContext.Fluid.SOURCE_ONLY);
        if (hit.getType() == HitResult.Type.BLOCK) {
            final BlockPos pos = hit.getBlockPos();
            final BlockState state = level.getBlockState(pos);
            if (FluidHelpers.pickupFluidInto(level, pos, state, player, handler)) {
                return InteractionResultHolder.success(stack);
            }
            final IFluidHandler blockHandler = FluidHelpers.getBlockEntityFluidHandler(level, pos, state);
            if (blockHandler != null && FluidHelpers.transferExact(blockHandler, handler, getCapacity())) {
                return InteractionResultHolder.success(stack);
            }
        }
        return afterFillFailed(handler, level, player, stack, hand);
    } else {
        final BlockHitResult hit = Helpers.rayTracePlayer(level, player, ClipContext.Fluid.SOURCE_ONLY);
        if (hit.getType() == HitResult.Type.BLOCK) {
            final BlockPos pos = hit.getBlockPos();
            final BlockState state = level.getBlockState(pos);
            final IFluidHandler blockHandler = FluidHelpers.getBlockEntityFluidHandler(level, pos, state);
            if (blockHandler != null && FluidHelpers.transferExact(handler, blockHandler, getCapacity())) {
                return InteractionResultHolder.success(stack);
            }
            if (emptyContents(handler, level, pos, state, hit)) {
                return InteractionResultHolder.sidedSuccess(player.isCreative() ? stack : new ItemStack(this), level.isClientSide);
            }
        }
        return afterEmptyFailed(handler, level, player, stack, hand);
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Aggregations

BlockHitResult (net.minecraft.world.phys.BlockHitResult)180 BlockPos (net.minecraft.core.BlockPos)117 Vec3 (net.minecraft.world.phys.Vec3)78 BlockState (net.minecraft.world.level.block.state.BlockState)66 ItemStack (net.minecraft.world.item.ItemStack)62 HitResult (net.minecraft.world.phys.HitResult)51 Direction (net.minecraft.core.Direction)46 Level (net.minecraft.world.level.Level)41 Player (net.minecraft.world.entity.player.Player)32 ClipContext (net.minecraft.world.level.ClipContext)32 ServerLevel (net.minecraft.server.level.ServerLevel)23 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)20 Entity (net.minecraft.world.entity.Entity)19 InteractionHand (net.minecraft.world.InteractionHand)18 BlockPlaceContext (net.minecraft.world.item.context.BlockPlaceContext)18 ServerPlayer (net.minecraft.server.level.ServerPlayer)17 LivingEntity (net.minecraft.world.entity.LivingEntity)17 AABB (net.minecraft.world.phys.AABB)17 EntityHitResult (net.minecraft.world.phys.EntityHitResult)17 Minecraft (net.minecraft.client.Minecraft)16