net.minecraft.util.math.BlockRayTraceResult Java Examples

The following examples show how to use net.minecraft.util.math.BlockRayTraceResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Scaffold.java    From bleachhack-1.14 with GNU General Public License v3.0 6 votes vote down vote up
public void placeBlockAuto(BlockPos block) {
	if (lastPlaced.containsKey(block)) return;
	for (Direction d: Direction.values()) {
		if (!WorldUtils.NONSOLID_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
			if (WorldUtils.RIGHTCLICKABLE_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
				mc.player.connection.sendPacket(new CEntityActionPacket(mc.player, Action.START_SNEAKING));}
			mc.player.connection.sendPacket(new CPlayerTryUseItemOnBlockPacket(Hand.MAIN_HAND,
					new BlockRayTraceResult(new Vec3d(block), d.getOpposite(), block.offset(d), false)));
			mc.player.swingArm(Hand.MAIN_HAND);
			mc.world.playSound(block, SoundEvents.BLOCK_NOTE_BLOCK_HAT, SoundCategory.BLOCKS, 1f, 1f, false);
			if (WorldUtils.RIGHTCLICKABLE_BLOCKS.contains(mc.world.getBlockState(block.offset(d)).getBlock())) {
				mc.player.connection.sendPacket(new CEntityActionPacket(mc.player, Action.STOP_SNEAKING));}
			lastPlaced.put(block, 5);
			return;
		}
	}
}
 
Example #2
Source File: CustomParticleHandler.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
@OnlyIn (Dist.CLIENT)
public static boolean handleRunningEffects(World world, BlockPos pos, BlockState state, Entity entity) {
    //Spoof a raytrace from the feet.
    BlockRayTraceResult traceResult = new BlockRayTraceResult(entity.getPositionVec().add(0, 1, 0), Direction.UP, pos, false);
    BlockModelShapes modelShapes = Minecraft.getInstance().getBlockRendererDispatcher().getBlockModelShapes();
    IBakedModel model = modelShapes.getModel(state);
    if (model instanceof IModelParticleProvider) {
        IModelData modelData = ModelDataManager.getModelData(world, pos);
        ParticleManager particleManager = Minecraft.getInstance().particles;
        List<TextureAtlasSprite> sprites = new ArrayList<>(((IModelParticleProvider) model).getHitEffects(traceResult, state, world, pos, modelData));
        TextureAtlasSprite rolledSprite = sprites.get(world.rand.nextInt(sprites.size()));
        double x = entity.getPosX() + (world.rand.nextFloat() - 0.5D) * entity.getWidth();
        double y = entity.getBoundingBox().minY + 0.1D;
        double z = entity.getPosZ() + (world.rand.nextFloat() - 0.5D) * entity.getWidth();
        particleManager.addEffect(new CustomBreakingParticle(world, x, y, z, -entity.getMotion().x * 4.0D, 1.5D, -entity.getMotion().z * 4.0D, rolledSprite));
        return true;
    }

    return false;
}
 
Example #3
Source File: CustomParticleHandler.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
@OnlyIn (Dist.CLIENT)
public static void addLandingEffects(World world, BlockPos pos, BlockState state, Vector3 entityPos, int numParticles) {
    //Spoof a raytrace from the feet.
    BlockRayTraceResult traceResult = new BlockRayTraceResult(new Vec3d(entityPos.x, pos.getY() + 1, entityPos.z), Direction.UP, pos, false);
    ParticleManager manager = Minecraft.getInstance().particles;
    Random randy = new Random();
    BlockModelShapes modelShapes = Minecraft.getInstance().getBlockRendererDispatcher().getBlockModelShapes();
    IBakedModel model = modelShapes.getModel(state);
    if (model instanceof IModelParticleProvider) {
        IModelData modelData = ModelDataManager.getModelData(world, pos);
        List<TextureAtlasSprite> sprites = new ArrayList<>(((IModelParticleProvider) model).getHitEffects(traceResult, state, world, pos, modelData));

        double speed = 0.15000000596046448D;
        if (numParticles != 0) {
            for (int i = 0; i < numParticles; i++) {
                double mX = randy.nextGaussian() * speed;
                double mY = randy.nextGaussian() * speed;
                double mZ = randy.nextGaussian() * speed;
                manager.addEffect(CustomBreakingParticle.newLandingParticle(world, entityPos.x, entityPos.y, entityPos.z, mX, mY, mZ, sprites.get(randy.nextInt(sprites.size()))));
            }
        }
    }
}
 
Example #4
Source File: DryingRackBlock.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Deprecated
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult blockRayTraceResult)
{
    if (worldIn.isRemote)
        return ActionResultType.SUCCESS;

    TileEntity tileEntity = worldIn.getTileEntity(pos);
    if (!(tileEntity instanceof INamedContainerProvider))
        return ActionResultType.FAIL;

    NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tileEntity);

    return ActionResultType.SUCCESS;
}
 
Example #5
Source File: SawmillBlock.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult blockRayTraceResult)
{
    if (worldIn.isRemote)
        return ActionResultType.SUCCESS;

    TileEntity tileEntity = worldIn.getTileEntity(pos);
    if (!(tileEntity instanceof INamedContainerProvider))
        return ActionResultType.FAIL;

    NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tileEntity);

    return ActionResultType.SUCCESS;
}
 
Example #6
Source File: SubHitVoxelShape.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
private CuboidRayTraceResult rayTrace(Vec3d start, Vec3d end, BlockPos pos, IndexedCuboid6 cuboid, VoxelShape shape) {
    BlockRayTraceResult hit = shape.rayTrace(start, end, pos);
    if (hit != null) {
        Vector3 hitVec = new Vector3(hit.getHitVec());
        return new CuboidRayTraceResult(hitVec, hit.getFace(), pos, hit.isInside(), cuboid, hitVec.copy().subtract(start).magSquared());
    }
    return null;
}
 
Example #7
Source File: SubHitVoxelShape.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Nullable
@Override
public BlockRayTraceResult rayTrace(Vec3d start, Vec3d end, BlockPos pos) {
    CuboidRayTraceResult closest = null;
    double dist = Double.MAX_VALUE;
    for (Pair<IndexedCuboid6, VoxelShape> cuboidShape : cuboidShapes) {
        CuboidRayTraceResult hit = rayTrace(start, end, pos, cuboidShape.getLeft(), cuboidShape.getRight());
        if (hit != null && dist > hit.dist) {
            closest = hit;
            dist = hit.dist;
        }
    }
    return closest;
}
 
Example #8
Source File: AbstractBakedPropertiesModel.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public Set<TextureAtlasSprite> getHitEffects(@Nonnull BlockRayTraceResult traceResult, BlockState state, IBlockReader world, BlockPos pos, IModelData modelData) {
    Vector3 vec = new Vector3(traceResult.getHitVec()).subtract(traceResult.getPos());
    return getAllQuads(state, modelData).stream()//
            .filter(quad -> quad.getFace() == traceResult.getFace())//
            .filter(quad -> checkDepth(quad, vec, traceResult.getFace()))//
            .map(BakedQuad::func_187508_a)//
            .collect(Collectors.toSet());//
}
 
Example #9
Source File: CCBakeryModel.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public Set<TextureAtlasSprite> getHitEffects(BlockRayTraceResult traceResult, BlockState state, IBlockReader world, BlockPos pos, IModelData data) {
    IBakedModel model = ModelBakery.getCachedModel(state, data);
    if (model instanceof IModelParticleProvider) {
        return ((IModelParticleProvider) model).getHitEffects(traceResult, state, world, pos, data);
    }
    return Collections.emptySet();
}
 
Example #10
Source File: CCRenderEventHandler.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
@OnlyIn (Dist.CLIENT)
@SubscribeEvent (priority = EventPriority.LOW)
public void onBlockHighlight(DrawHighlightEvent.HighlightBlock event) {
    //We have found a CuboidRayTraceResult, Lets render it properly..
    BlockRayTraceResult hit = event.getTarget();
    if (hit instanceof CuboidRayTraceResult) {
        CuboidRayTraceResult cuboidHit = (CuboidRayTraceResult) hit;
        event.setCanceled(true);
        Matrix4 mat = new Matrix4(event.getMatrix());
        mat.translate(cuboidHit.getPos());
        RenderUtils.bufferHitbox(mat, event.getBuffers(), event.getInfo(), cuboidHit.cuboid6);
    }
}
 
Example #11
Source File: MiningCollect.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static List<BlockPos> collect(PlayerEntity player, BlockRayTraceResult startBlock, World world, int range) {
    List<BlockPos> coordinates = new ArrayList<>();
    BlockPos startPos = startBlock.getPos();

    if (range == 1) {
        if( !isValid(player, startBlock.getPos(), world) )
            return coordinates;

        coordinates.add(startBlock.getPos());
        return coordinates;
    }

    Direction side = startBlock.getFace();
    boolean vertical = side.getAxis().isVertical();
    Direction up = vertical ? player.getHorizontalFacing() : Direction.UP;
    Direction down = up.getOpposite();
    Direction right = vertical ? up.rotateY() : side.rotateYCCW();
    Direction left = right.getOpposite();

    coordinates.add(startPos.offset(up).offset(left));
    coordinates.add(startPos.offset(up));
    coordinates.add(startPos.offset(up).offset(right));
    coordinates.add(startPos.offset(left));
    coordinates.add(startPos);
    coordinates.add(startPos.offset(right));
    coordinates.add(startPos.offset(down).offset(left));
    coordinates.add(startPos.offset(down));
    coordinates.add(startPos.offset(down).offset(right));

    return coordinates.stream().filter(e -> isValid(player, e, world)).collect(Collectors.toList());
}
 
Example #12
Source File: VectorHelper.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static BlockRayTraceResult getLookingAt(PlayerEntity player, RayTraceContext.FluidMode rayTraceFluid, int range) {
    World world = player.world;

    Vec3d look = player.getLookVec();
    Vec3d start = new Vec3d(player.getPosX(), player.getPosY() + player.getEyeHeight(), player.getPosZ());

    Vec3d end = new Vec3d(player.getPosX() + look.x * (double) range, player.getPosY() + player.getEyeHeight() + look.y * (double) range, player.getPosZ() + look.z * (double) range);
    RayTraceContext context = new RayTraceContext(start, end, RayTraceContext.BlockMode.COLLIDER, rayTraceFluid, player);
    return world.rayTraceBlocks(context);
}
 
Example #13
Source File: ModificationTable.java    From MiningGadgets with MIT License 5 votes vote down vote up
@Override
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult result) {
    if (!world.isRemote) {
        TileEntity tileEntity = world.getTileEntity(pos);
        if (tileEntity instanceof INamedContainerProvider) {
            NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tileEntity, tileEntity.getPos());
        } else {
            throw new IllegalStateException("Our named container provider is missing!");
        }
        return ActionResultType.SUCCESS;
    }
    return ActionResultType.SUCCESS;
}
 
Example #14
Source File: BlockOverlayRender.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void render(RenderWorldLastEvent event, ItemStack item) {
    final Minecraft mc = Minecraft.getInstance();

    IRenderTypeBuffer.Impl buffer = Minecraft.getInstance().getRenderTypeBuffers().getBufferSource();

    int range = MiningProperties.getBeamRange(item);
    BlockRayTraceResult lookingAt = VectorHelper.getLookingAt(mc.player, RayTraceContext.FluidMode.NONE, range);
    if (mc.world.getBlockState(VectorHelper.getLookingAt(mc.player, item, range).getPos()) == Blocks.AIR.getDefaultState()) {
        return;
    }

    List<BlockPos> coords = MiningCollect.collect(mc.player, lookingAt, mc.world, MiningProperties.getRange(item));
    Vec3d view = mc.gameRenderer.getActiveRenderInfo().getProjectedView();

    MatrixStack matrix = event.getMatrixStack();
    matrix.push();
    matrix.translate(-view.getX(), -view.getY(), -view.getZ());

    IVertexBuilder builder;
    builder = buffer.getBuffer(MyRenderType.BlockOverlay);
    coords.forEach(e -> {
        if (mc.world.getBlockState(e).getBlock() != ModBlocks.RENDER_BLOCK.get()) {

            matrix.push();
            matrix.translate(e.getX(), e.getY(), e.getZ());
            matrix.translate(-0.005f, -0.005f, -0.005f);
            matrix.scale(1.01f, 1.01f, 1.01f);
            matrix.rotate(Vector3f.YP.rotationDegrees(-90.0F));

            Matrix4f positionMatrix = matrix.getLast().getMatrix();
            BlockOverlayRender.render(positionMatrix, builder, e, Color.GREEN);
            matrix.pop();
        }
    });
    matrix.pop();
    RenderSystem.disableDepthTest();
    buffer.finish(MyRenderType.BlockOverlay);
}
 
Example #15
Source File: VectorHelper.java    From MiningGadgets with MIT License 4 votes vote down vote up
public static BlockRayTraceResult getLookingAt(PlayerEntity player, ItemStack tool, int range) {
    return getLookingAt(player, RayTraceContext.FluidMode.NONE, range);
}
 
Example #16
Source File: ChoppingBlock.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Deprecated
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult blockRayTraceResult)
{
    ItemStack heldItem = player.getHeldItem(hand);

    if (worldIn.isRemote)
    {
        return (heldItem.getCount() <= 0) || ChoppingRecipe.getRecipe(worldIn, heldItem).isPresent() ?
                ActionResultType.SUCCESS : ActionResultType.PASS;
    }

    TileEntity tileEntity = worldIn.getTileEntity(pos);

    if (!(tileEntity instanceof ChoppingBlockTileEntity) || player.isSneaking())
        return ActionResultType.PASS;

    ChoppingBlockTileEntity chopper = (ChoppingBlockTileEntity) tileEntity;

    if (heldItem.getCount() <= 0)
    {
        ItemStack extracted = chopper.getSlotInventory().extractItem(0, 1, false);
        if (extracted.getCount() > 0)
        {
            ItemHandlerHelper.giveItemToPlayer(player, extracted);
            return ActionResultType.SUCCESS;
        }

        return ActionResultType.PASS;
    }

    if (ChoppingRecipe.getRecipe(worldIn, heldItem)
            .isPresent())
    {
        ItemStack remaining = chopper.getSlotInventory().insertItem(0, heldItem, false);
        if (!player.isCreative())
        {
            if (remaining.getCount() > 0)
            {
                player.setHeldItem(hand, remaining);
            }
            else
            {
                player.setHeldItem(hand, ItemStack.EMPTY);
            }
        }
        return remaining.getCount() < heldItem.getCount() ?
                ActionResultType.SUCCESS : ActionResultType.PASS;
    }

    return ActionResultType.PASS;
}
 
Example #17
Source File: IModelParticleProvider.java    From CodeChickenLib with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Used to retrieve the particles to randomly choose from for hit particles.
 *
 * @param traceResult The trace result.
 * @param state       The state, getActualState and getExtendedState has been called.
 * @param world       The world.
 * @param pos         The pos.
 * @return A Set of Textures to use.
 */
Set<TextureAtlasSprite> getHitEffects(@Nonnull BlockRayTraceResult traceResult, BlockState state, IBlockReader world, BlockPos pos, IModelData modelData);