Java Code Examples for net.minecraft.entity.player.EntityPlayerMP#getLookVec()

The following examples show how to use net.minecraft.entity.player.EntityPlayerMP#getLookVec() . 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: NearbySmeltCommandsImplementation.java    From malmo with MIT License 4 votes vote down vote up
@Override
public IMessage onMessage(SmeltNearbyMessage message, MessageContext ctx) {
    EntityPlayerMP player = ctx.getServerHandler().playerEntity;
    Vec3d headPos = new Vec3d(player.posX, player.posY + 1.6, player.posZ);

    // Location checking
    boolean closeFurnace = false;
    for (BlockPos furnace : furnaces) {
        Vec3d blockVec = new Vec3d(furnace.getX() + 0.5, furnace.getY() + 0.5, furnace.getZ() + 0.5);

        if (headPos.squareDistanceTo(blockVec) <= 25.0) {
            // Within a reasonable FOV?
            // Lots of trig, let's go
            double fov = Minecraft.getMinecraft().gameSettings.fovSetting;
            double height = Minecraft.getMinecraft().displayHeight;
            double width = Minecraft.getMinecraft().displayWidth;
            Vec3d lookVec = player.getLookVec();
            Vec3d toBlock = blockVec.subtract(headPos);

            // Projection of block onto player look vector - if greater than 0, then in front of us
            double scalarProjection = lookVec.dotProduct(toBlock) / lookVec.lengthVector();
            if (scalarProjection > 0) {
                Vec3d yUnit = new Vec3d(0, 1.0, 0);
                Vec3d lookCross = lookVec.crossProduct(yUnit);
                Vec3d blockProjectedOntoCross = lookCross.scale(lookCross.dotProduct(toBlock) / lookCross.lengthVector());
                Vec3d blockProjectedOntoPlayerPlane = toBlock.subtract(blockProjectedOntoCross);
                double xyDot = lookVec.dotProduct(blockProjectedOntoPlayerPlane);
                double pitchTheta = Math.acos(xyDot / (lookVec.lengthVector() * blockProjectedOntoPlayerPlane.lengthVector()));

                Vec3d playerY = lookCross.crossProduct(lookVec);
                Vec3d blockProjectedOntoPlayerY = playerY.scale(playerY.dotProduct(toBlock) / playerY.lengthVector());
                Vec3d blockProjectedOntoYawPlane = toBlock.subtract(blockProjectedOntoPlayerY);
                double xzDot = lookVec.dotProduct(blockProjectedOntoYawPlane);
                double yawTheta = Math.acos(xzDot / (lookVec.lengthVector() * blockProjectedOntoYawPlane.lengthVector()));

                if (Math.abs(Math.toDegrees(yawTheta)) <= Math.min(1, width / height) * (fov / 2.0) && Math.abs(Math.toDegrees(pitchTheta)) <= Math.min(1, height / width) * (fov / 2.0))
                    closeFurnace = true;
            }
        }
    }

    if (closeFurnace) {
        ItemStack input = CraftingHelper.getSmeltingRecipeForRequestedOutput(message.parameters);
        if (input != null)
            if (CraftingHelper.attemptSmelting(player, input))
                return null;
    }

    return null;
}
 
Example 2
Source File: NearbyCraftCommandsImplementation.java    From malmo with MIT License 4 votes vote down vote up
@Override
public IMessage onMessage(CraftNearbyMessage message, MessageContext ctx) {
    EntityPlayerMP player = ctx.getServerHandler().playerEntity;
    Vec3d headPos = new Vec3d(player.posX, player.posY + 1.6, player.posZ);

    // Location checking
    boolean closeTable = false;
    for (BlockPos furnace : craftingTables) {
        Vec3d blockVec = new Vec3d(furnace.getX() + 0.5, furnace.getY() + 0.5, furnace.getZ() + 0.5);

        if (headPos.squareDistanceTo(blockVec) <= 25.0) {
            // Within a reasonable FOV?
            // Lots of trig, let's go
            double fov = Minecraft.getMinecraft().gameSettings.fovSetting;
            double height = Minecraft.getMinecraft().displayHeight;
            double width = Minecraft.getMinecraft().displayWidth;
            Vec3d lookVec = player.getLookVec();
            Vec3d toBlock = blockVec.subtract(headPos);

            // Projection of block onto player look vector - if greater than 0, then in front of us
            double scalarProjection = lookVec.dotProduct(toBlock) / lookVec.lengthVector();
            if (scalarProjection > 0) {
                Vec3d yUnit = new Vec3d(0, 1.0, 0);
                Vec3d lookCross = lookVec.crossProduct(yUnit);
                Vec3d blockProjectedOntoCross = lookCross.scale(lookCross.dotProduct(toBlock) / lookCross.lengthVector());
                Vec3d blockProjectedOntoPlayerPlane = toBlock.subtract(blockProjectedOntoCross);
                double xyDot = lookVec.dotProduct(blockProjectedOntoPlayerPlane);
                double pitchTheta = Math.acos(xyDot / (lookVec.lengthVector() * blockProjectedOntoPlayerPlane.lengthVector()));

                Vec3d playerY = lookCross.crossProduct(lookVec);
                Vec3d blockProjectedOntoPlayerY = playerY.scale(playerY.dotProduct(toBlock) / playerY.lengthVector());
                Vec3d blockProjectedOntoYawPlane = toBlock.subtract(blockProjectedOntoPlayerY);
                double xzDot = lookVec.dotProduct(blockProjectedOntoYawPlane);
                double yawTheta = Math.acos(xzDot / (lookVec.lengthVector() * blockProjectedOntoYawPlane.lengthVector()));

                if (Math.abs(Math.toDegrees(yawTheta)) <= Math.min(1, width / height) * (fov / 2.0) && Math.abs(Math.toDegrees(pitchTheta)) <= Math.min(1, height / width) * (fov / 2.0))
                    closeTable = true;
            }
        }
    }

    if (closeTable) {
        // We are close enough, try crafting recipes
        List<IRecipe> matching_recipes;
        String[] split = message.parameters.split(" ");
        if (split.length > 1)
            matching_recipes = CraftingHelper.getRecipesForRequestedOutput(message.parameters, true);
        else
            matching_recipes = CraftingHelper.getRecipesForRequestedOutput(message.parameters, false);

        for (IRecipe recipe : matching_recipes)
            if (CraftingHelper.attemptCrafting(player, recipe))
                return null;
    }

    return null;
}