исправь ошибки сделай новые механики  вот код <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Minecraft 3D</title>
    <style>
        body { margin: 0; overflow: hidden; }
        canvas { display: block; }
    </style>
</head>
<body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/PointerLockControls.js"></script>

    <script>
        const scene = new THREE.Scene();
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

        const cubeSize = 5;
        const chunkSize = 10;
        const worldDepth = 5;
        const gravity = -0.05;
        const moveSpeed = 0.2;
        const jumpPower = 0.5;

        const materials = {
            stone: new THREE.MeshBasicMaterial({ color: 0x808080 }),
            dirt: new THREE.MeshBasicMaterial({ color: 0x8B4513 }),
            water: new THREE.MeshBasicMaterial({ color: 0x1E90FF, transparent: true, opacity: 0.6 })
        };

        const blocks = [];
        const raycaster = new THREE.Raycaster();
        let velocityY = 0;
        let onGround = false;
        let generatedChunks = new Set();

        function createBlock(type, x, y, z) {
            const geometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);

            const edges = new THREE.EdgesGeometry(geometry);
            const line = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({ color: 0x000000 }));

            const block = new THREE.Mesh(geometry, materials[type]);
            block.position.set(x, y, z);
            block.add(line);
            block.userData.type = type;
            scene.add(block);
            blocks.push(block);
        }

        function generateChunk(position) {
            const chunkKey = `${position.x}_${position.z}`;
            if (generatedChunks.has(chunkKey)) return; // Проверка на повторную генерацию

            generatedChunks.add(chunkKey);
            for (let x = 0; x < chunkSize; x++) {
                for (let z = 0; z < chunkSize; z++) {
                    for (let y = 0; y < worldDepth; y++) {
                        const type = Math.random() > 0.7 ? 'water' : (Math.random() > 0.5 ? 'stone' : 'dirt');
                        createBlock(type, position.x + x * cubeSize, y * cubeSize, position.z + z * cubeSize);
                    }
                }
            }
        }

        function checkForNewChunk() {
            const currentChunkX = Math.floor(player.position.x / (cubeSize * chunkSize));
            const currentChunkZ = Math.floor(player.position.z / (cubeSize * chunkSize));

            const directions = [
                { x: 1, z: 0 },
                { x: -1, z: 0 },
                { x: 0, z: 1 },
                { x: 0, z: -1 }
            ];

            directions.forEach(direction => {
                const nextChunkX = currentChunkX + direction.x;
                const nextChunkZ = currentChunkZ + direction.z;
                const nextChunkPosition = { x: nextChunkX * cubeSize * chunkSize, z: nextChunkZ * cubeSize * chunkSize };
                
                const distanceToEdgeX = Math.abs(player.position.x - nextChunkPosition.x);
                const distanceToEdgeZ = Math.abs(player.position.z - nextChunkPosition.z);

                if (distanceToEdgeX < cubeSize * 2 || distanceToEdgeZ < cubeSize * 2) {
                    generateChunk(nextChunkPosition);
                }
            });
        }

        const playerGeometry = new THREE.BoxGeometry(1, 2, 1);
        const playerMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
        const player = new THREE.Mesh(playerGeometry, playerMaterial);
        player.position.set(0, worldDepth * cubeSize + 2, 0);
        scene.add(player);

        camera.position.y = player.position.y + 2;
        camera.position.z = player.position.z + 5;

        const controls = new THREE.PointerLockControls(camera, document.body);
        document.body.addEventListener('click', () => controls.lock());

        const keys = {};
        document.addEventListener('keydown', (event) => keys[event.key.toLowerCase()] = true);
        document.addEventListener('keyup', (event) => keys[event.key.toLowerCase()] = false);

        function handleMovement() {
            const moveDirection = new THREE.Vector3();
            if (keys['w']) moveDirection.z -= moveSpeed;
            if (keys['s']) moveDirection.z += moveSpeed;
            if (keys['a']) moveDirection.x -= moveSpeed;
            if (keys['d']) moveDirection.x += moveSpeed;

            moveDirection.applyQuaternion(camera.quaternion);

            const nextPosition = player.position.clone().add(moveDirection);
            if (!checkCollision(nextPosition, 'horizontal')) {
                player.position.add(moveDirection);
            }

            if (onGround && keys[' ']) {
                velocityY += jumpPower;
                onGround = false;
            }

            velocityY += gravity;
            const nextVerticalPosition = player.position.clone();
            nextVerticalPosition.y += velocityY;

            if (!checkCollision(nextVerticalPosition, 'vertical')) {
                player.position.y += velocityY;
            }

            checkGround();
            checkForNewChunk();
        }

        function checkGround() {
            raycaster.set(player.position, new THREE.Vector3(0, -1, 0));
            const intersects = raycaster.intersectObjects(blocks);

            if (intersects.length > 0 && intersects[0].distance <= 1) {
                const blockUnderPlayer = intersects[0].object.userData.type;

                if (blockUnderPlayer === 'water') {
                    onGround = false;
                } else {
                    onGround = true;
                    velocityY = 0;
                    player.position.y = intersects[0].point.y + 1;
                }
            } else {
                onGround = false;
            }
        }

        function checkCollision(nextPosition, direction) {
            const directionVector = direction === 'horizontal' ? new THREE.Vector3(1, 0, 1) : new THREE.Vector3(0, 1, 0);
            raycaster.set(nextPosition, directionVector);
            const intersects = raycaster.intersectObjects(blocks);

            for (let i = 0; i < intersects.length; i++) {
                const blockType = intersects[i].object.userData.type;
                if (blockType === 'stone' || blockType === 'dirt') {
                    return true;
                }
            }
            return false;
        }

        function animate() {
            requestAnimationFrame(animate);
            handleMovement();
            camera.position.set(player.position.x, player.position.y + 2, player.position.z);
            renderer.render(scene, camera);
        }

        generateChunk({ x: 0, z: 0 });
        animate();
    </script>
</body>
</html>