Search in sources :

Example 11 with Vector3d

use of net.minecraft.util.math.vector.Vector3d in project AgriCraft by AgriCraft.

the class MagnifyingGlassViewHandler method inspectionRender.

@SubscribeEvent
@SuppressWarnings("unused")
public void inspectionRender(RenderWorldLastEvent event) {
    // Check if the player is in first person
    if (!Minecraft.getInstance().gameSettings.getPointOfView().func_243192_a()) {
        return;
    }
    // Check if an inspection renderer is present and cache it (another thread might set it to null)
    IMagnifyingGlassInspector inspector = this.inspector;
    if (inspector == null) {
        return;
    }
    // Check if the inspector has a target and cache (another thread might set it to null)
    Target target = this.lastTarget;
    if (target == null) {
        return;
    }
    // Fetch and push matrix to the matrix stack
    MatrixStack transforms = event.getMatrixStack();
    transforms.push();
    // Correct for render view
    Vector3d projectedView = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView();
    transforms.translate(-projectedView.x, -projectedView.y, -projectedView.z);
    // Move to the player's eye position
    Vector3d eyes = this.getPlayer().getEyePosition(event.getPartialTicks());
    transforms.translate(eyes.getX(), eyes.getY(), eyes.getZ());
    // Fetch the player's target
    Vector3d hit = target.getTargetVector(event.getPartialTicks());
    Vector3d view = hit.subtract(eyes).normalize();
    // Translate offset
    transforms.translate(GENOME_OFFSET * view.getX(), GENOME_OFFSET * view.getY(), GENOME_OFFSET * view.getZ());
    // Fetch player look orientation;
    float yaw = (float) (Math.PI * this.getPlayer().getYaw(event.getPartialTicks())) / 180;
    float pitch = (float) (Math.PI * this.getPlayer().getPitch(event.getPartialTicks())) / 180;
    // Rotate for yaw
    transforms.rotate(Vector3f.YP.rotation(-yaw));
    // Render
    transforms.push();
    inspector.doInspectionRender(transforms, event.getPartialTicks(), target.getTargetEntity());
    transforms.pop();
    // Pop last transformation matrix from the stack
    transforms.pop();
}
Also used : Vector3d(net.minecraft.util.math.vector.Vector3d) MatrixStack(com.mojang.blaze3d.matrix.MatrixStack) IMagnifyingGlassInspector(com.infinityraider.agricraft.api.v1.client.IMagnifyingGlassInspector) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 12 with Vector3d

use of net.minecraft.util.math.vector.Vector3d in project BluePower by Qmunity.

the class BPEventHandler method blockHighlightEvent.

@OnlyIn(Dist.CLIENT)
@SubscribeEvent
public void blockHighlightEvent(DrawHighlightEvent event) {
    PlayerEntity player = Minecraft.getInstance().player;
    if (player == null) {
        return;
    }
    World world = player.level;
    if (world == null) {
        return;
    }
    RayTraceResult mop = event.getTarget();
    if (mop instanceof BlockRayTraceResult) {
        BlockPos pos = ((BlockRayTraceResult) mop).getBlockPos();
        BlockState state = world.getBlockState(pos);
        if (state.getBlock() instanceof BlockBPMultipart) {
            BlockState partstate = MultipartUtils.getClosestState(player, pos);
            IVertexBuilder builder = event.getBuffers().getBuffer(RenderType.lines());
            if (partstate != null) {
                VoxelShape shape = partstate.getShape(world, pos, ISelectionContext.of(player));
                Vector3d projectedView = event.getInfo().getPosition();
                double d0 = pos.getX() - projectedView.x();
                double d1 = pos.getY() - projectedView.y();
                double d2 = pos.getZ() - projectedView.z();
                Matrix4f matrix4f = event.getMatrix().last().pose();
                shape.forAllEdges((startX, startY, startZ, endX, endY, endZ) -> {
                    builder.vertex(matrix4f, (float) (startX + d0), (float) (startY + d1), (float) (startZ + d2)).color(0.0F, 0.0F, 0.0F, 0.4F).endVertex();
                    builder.vertex(matrix4f, (float) (endX + d0), (float) (endY + d1), (float) (endZ + d2)).color(0.0F, 0.0F, 0.0F, 0.4F).endVertex();
                });
                event.setCanceled(true);
            }
        }
    }
}
Also used : Matrix4f(net.minecraft.util.math.vector.Matrix4f) BlockState(net.minecraft.block.BlockState) VoxelShape(net.minecraft.util.math.shapes.VoxelShape) Vector3d(net.minecraft.util.math.vector.Vector3d) RayTraceResult(net.minecraft.util.math.RayTraceResult) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) BlockBPMultipart(com.bluepowermod.block.BlockBPMultipart) BlockPos(net.minecraft.util.math.BlockPos) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) World(net.minecraft.world.World) PlayerEntity(net.minecraft.entity.player.PlayerEntity) IVertexBuilder(com.mojang.blaze3d.vertex.IVertexBuilder) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 13 with Vector3d

use of net.minecraft.util.math.vector.Vector3d in project Bookshelf by Darkhax-Minecraft.

the class EntityUtils method pushTowards.

/**
 * Pushes an entity towards another one.
 *
 * @param entityToMove The entity that should be pushed towards the other entity.
 * @param destination The destination entity, that the entity to move should be pushed
 *        towards.
 * @param force The amount of force to push the entityToMove with.
 */
public static void pushTowards(Entity entityToMove, Entity destination, double force) {
    final double distanceX = destination.getX() - entityToMove.getX();
    final double distanceY = destination.getY() - entityToMove.getY();
    final double distanceZ = destination.getZ() - entityToMove.getZ();
    final double distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY + distanceZ * distanceZ);
    if (distance > 0) {
        entityToMove.setDeltaMovement(new Vector3d(distanceX / distance * force, distanceY / distance * force, distanceZ / distance * force));
    }
}
Also used : Vector3d(net.minecraft.util.math.vector.Vector3d)

Example 14 with Vector3d

use of net.minecraft.util.math.vector.Vector3d in project BluePower by Qmunity.

the class BlockBPMicroblock method getStateForPlacement.

@Nullable
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
    PlayerEntity player = context.getPlayer();
    FluidState fluidstate = context.getLevel().getFluidState(context.getClickedPos());
    if (player != null && !player.isCrouching()) {
        return this.defaultBlockState().setValue(FACING, context.getClickedFace()).setValue(WATERLOGGED, fluidstate.getType() == Fluids.WATER);
    }
    Vector3d vec = context.getPlayer().getLookAngle();
    return this.defaultBlockState().setValue(FACING, Direction.getNearest(vec.x, vec.y, vec.z)).setValue(WATERLOGGED, fluidstate.getType() == Fluids.WATER);
}
Also used : Vector3d(net.minecraft.util.math.vector.Vector3d) PlayerEntity(net.minecraft.entity.player.PlayerEntity) FluidState(net.minecraft.fluid.FluidState) Nullable(javax.annotation.Nullable)

Example 15 with Vector3d

use of net.minecraft.util.math.vector.Vector3d in project BluePower by Qmunity.

the class TileDeployer method rightClick.

/**
 * Be sure to set up the fake player's hotbar with the right clicked items. starting with hotbar slot 0.
 * @param player
 * @param useItems this method will set the current selected slot of the fake player to 0, and move on to the next slot useItems - 1 times.
 *          So to use the first slot only, pass 1, to use the full hotbar, 9.
 * @return
 */
protected boolean rightClick(FakePlayer player, int useItems) {
    if (useItems > 9)
        throw new IllegalArgumentException("Hotbar is 9 items in width! You're trying " + useItems + "!");
    Direction faceDir = getFacingDirection();
    int dx = faceDir.getStepX();
    int dy = faceDir.getStepY();
    int dz = faceDir.getStepZ();
    int x = worldPosition.getX() + dx;
    int y = worldPosition.getY() + dy;
    int z = worldPosition.getZ() + dz;
    player.setPos(x + 0.5, y + 0.5 - player.getEyeHeight(), z + 0.5);
    player.xRot = faceDir.getStepY() * -90;
    switch(faceDir) {
        case NORTH:
            player.yRot = 180;
            break;
        case SOUTH:
            player.yRot = 0;
            break;
        case WEST:
            player.yRot = 90;
            break;
        case EAST:
            player.yRot = -90;
    }
    try {
        PlayerInteractEvent event = new PlayerInteractEvent.RightClickEmpty(player, Hand.MAIN_HAND);
        if (event.isCanceled())
            return false;
        Block block = level.getBlockState(new BlockPos(x, y, z)).getBlock();
        List<LivingEntity> detectedEntities = level.getEntitiesOfClass(LivingEntity.class, new AxisAlignedBB(x, y, z, x + 1, y + 1, z + 1));
        Entity entity = detectedEntities.isEmpty() ? null : detectedEntities.get(level.random.nextInt(detectedEntities.size()));
        if (entity != null) {
            for (int i = 0; i < useItems; i++) {
                player.inventory.selected = i;
                ItemStack stack = player.getMainHandItem();
                if (canDeployItem(stack) && stack.getItem().interactLivingEntity(stack, player, (LivingEntity) entity, Hand.MAIN_HAND).shouldSwing())
                    return true;
                if (entity instanceof AnimalEntity && ((AnimalEntity) entity).mobInteract(player, Hand.MAIN_HAND).shouldSwing())
                    return true;
            }
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            ItemStack stack = player.getMainHandItem();
            if (canDeployItem(stack) && stack.getItem().onItemUseFirst(stack, new ItemUseContext(player, Hand.MAIN_HAND, new BlockRayTraceResult(new Vector3d(dx, dy, dz), faceDir, new BlockPos(x, y, z), false))) == ActionResultType.SUCCESS)
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            if (!level.isEmptyBlock(new BlockPos(x, y, z)) && block.use(level.getBlockState(new BlockPos(x, y, z)), level, new BlockPos(x, y, z), player, Hand.MAIN_HAND, new BlockRayTraceResult(new Vector3d(dx, dy, dz), faceDir, new BlockPos(x, y, z), false)) == ActionResultType.SUCCESS)
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            ItemStack stack = player.getMainHandItem();
            boolean isGoingToShift = false;
            if (!stack.isEmpty()) {
                if (stack.getItem() == Items.SUGAR_CANE || stack.getItem() == Items.REDSTONE) {
                    isGoingToShift = true;
                }
            }
            int useX = isGoingToShift ? worldPosition.getX() : x;
            int useY = isGoingToShift ? worldPosition.getY() : y;
            int useZ = isGoingToShift ? worldPosition.getZ() : z;
            if (canDeployItem(stack) && stack.getItem().useOn(new ItemUseContext(player, Hand.MAIN_HAND, new BlockRayTraceResult(new Vector3d(dx, dy, dz), faceDir, new BlockPos(x, y, z), false))) == ActionResultType.SUCCESS)
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            ItemStack stack = player.getMainHandItem();
            if (canDeployItem(stack)) {
                ItemStack copy = stack.copy();
                // TODO Check this
                player.setItemInHand(Hand.MAIN_HAND, stack.getItem().use(level, player, Hand.MAIN_HAND).getObject());
                if (!copy.sameItem(stack))
                    return true;
            }
        }
        return false;
    } catch (Throwable e) {
        BluePower.log.error("Deployer crashed! Stacktrace: ");
        e.printStackTrace();
        return true;
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) AnimalEntity(net.minecraft.entity.passive.AnimalEntity) Entity(net.minecraft.entity.Entity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) LivingEntity(net.minecraft.entity.LivingEntity) ItemEntity(net.minecraft.entity.item.ItemEntity) ItemUseContext(net.minecraft.item.ItemUseContext) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) AnimalEntity(net.minecraft.entity.passive.AnimalEntity) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) Direction(net.minecraft.util.Direction) LivingEntity(net.minecraft.entity.LivingEntity) Vector3d(net.minecraft.util.math.vector.Vector3d) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Aggregations

Vector3d (net.minecraft.util.math.vector.Vector3d)19 BlockPos (net.minecraft.util.math.BlockPos)7 PlayerEntity (net.minecraft.entity.player.PlayerEntity)4 ItemStack (net.minecraft.item.ItemStack)4 BlockRayTraceResult (net.minecraft.util.math.BlockRayTraceResult)4 Nullable (javax.annotation.Nullable)3 BlockState (net.minecraft.block.BlockState)3 Entity (net.minecraft.entity.Entity)3 IEnergyStorage (net.minecraftforge.energy.IEnergyStorage)3 IVertexBuilder (com.mojang.blaze3d.vertex.IVertexBuilder)2 Nonnull (javax.annotation.Nonnull)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 Direction (net.minecraft.util.Direction)2 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)2 RayTraceContext (net.minecraft.util.math.RayTraceContext)2 Matrix4f (net.minecraft.util.math.vector.Matrix4f)2 StringTextComponent (net.minecraft.util.text.StringTextComponent)2 OnlyIn (net.minecraftforge.api.distmarker.OnlyIn)2 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)2 BlockBPMultipart (com.bluepowermod.block.BlockBPMultipart)1