PrismarineJS / node-minecraft-protocol

Parse and serialize minecraft packets, plus authentication and encryption.
https://prismarinejs.github.io/node-minecraft-protocol/
BSD 3-Clause "New" or "Revised" License
1.23k stars 239 forks source link

Ladders arent working #1297

Open officialdakari opened 7 months ago

officialdakari commented 7 months ago

[v] The FAQ doesn't contain a resolution to my issue

Versions

Detailed description of a problem

Proxying through node-minecraft-protocol, ladders arent working

Current code

const tunKey = 'no';
const host = '192.168.0.158';
const port = 32323;

const mc = require('minecraft-protocol');
const ws = require('ws');

const sock = new ws.WebSocket('https://minecraft-tunneling.zelix.space/minecraft');

sock.on('open', async () => {
    sock.send(JSON.stringify({
        key: tunKey
    }));
});

function createText(msg, version) {
    if (['1.20.3', '1.20.4'].includes(version)) {
        return {
            type: 'string',
            value: msg
        };
    } else if (['1.19.4', '1.19.3', '1.19.2', '1.19.1'].includes(version)) {
        return JSON.stringify(msg);
    } else if (version == '1.19') {
        return JSON.stringify(msg);
    } else {
        return JSON.stringify(msg);
    }
}

function createChatPacket(msg, version) {
    if (['1.20.3', '1.20.4'].includes(version)) {
        return {
            name: 'system_chat',
            data: {
                content: msg,
                isActionBar: false
            }
        }
    } else if (['1.19.4', '1.19.3', '1.19.2', '1.19.1', '1.20.2', '1.20.1', '1.20'].includes(version)) {
        return {
            name: 'system_chat',
            data: {
                content: JSON.stringify(msg),
                isActionBar: false
            }
        };
    } else if (version == '1.19') {
        return {
            name: 'system_chat',
            data: {
                content: JSON.stringify(msg),
                type: 1
            }
        };
    } else {
        return {
            name: 'chat',
            data: {
                message: JSON.stringify(msg),
                position: 1,
                sender: NIL
            }
        };
    }
}

/**
 * @type {Object.<string, mc.Client>}
 */
const clients = {};
sock.on('message', async (msg) => {
    const j = JSON.parse(msg);
    if (j.ok) {
        console.log(`Tunneling to ${j.address}`);
    }

    if (j.connect) {
        const client = mc.createClient({
            username: j.username,
            version: j.version,
            host,
            port,
            keepAlive: false,
            disableChatSigning: true
        });

        clients[j.username] = client;
        console.log(`${j.username} connects from ${j.remoteAddress} [${j.version}]`);

        client.on('error', async (err) => {
            console.error(err);
        });

        client.on('end', async () => {
            console.log(`${j.username} disconnects`);
            sock.send(JSON.stringify({
                kick: true,
                username: j.username
            }));
            delete clients[j.username];
        });

        client.on('packet', async (data, meta) => {
            if (meta.state == mc.states.PLAY && meta.name != 'custom_payload') {
                if (client.version == '1.20.3' || client.version == '1.20.4') {
                    if (meta.name == 'player_chat') {
                        if (data.unsignedChatContent && data.type == 4) {
                            const packet = createChatPacket(data.unsignedChatContent, client.version);
                            meta.name = packet.name;
                            data = packet.data;
                        }
                    }
                }
                sock.send(JSON.stringify(
                    {
                        packet: meta.name,
                        state: meta.state,
                        data: JSON.stringify(data),
                        username: j.username
                    }
                ));
            }
        });
    }

    if (j.disconnect) {
        const c = clients[j.username];
        console.log(`${j.username} disconnected (request by server)`);
        if (!c) return;
        clients[j.username].end();
        delete clients[j.username];
    }

    if (j.name && j.username) {
        const c = clients[j.username];
        if (!c) {
            // sock.send(JSON.stringify({
            //     kick: true,
            //     username: j.username
            // }));
            return;
        }
        if (c.state == mc.states.PLAY && j.state == 'play') {
            if (j.name == 'custom_payload') return;
            c.write(j.name, JSON.parse(j.data));
        }
    }
});

Expected behavior

Ladders should work

Additional context

video (hosted on matrix)

extremeheat commented 5 months ago

Hi, your video is inaccessible. This doesn't seem to be an issue with nmp, so you would need to do a packet log between a direct vanilla client-vanilla server and against the proxy to see what's going on wrong with your code.

You can try looking at pakkit packet logs which should use nmp as its backend.

officialdakari commented 5 months ago

Ladders aren't working even through Pakkit and proxy example, on 1.20.4

Maybe it's fixed now, but last time I checked it wasn't fixed

вс, 26 мая 2024 г., 22:23 extremeheat @.***>:

Hi, your video is inaccessible. This doesn't seem to be an issue with nmp, so you would need to do a packet log between a direct vanilla client-vanilla server and against the proxy to see what's going on wrong with your code.

You can try looking at pakkit https://github.com/Heath123/pakkit packet logs which should use nmp as its backend.

— Reply to this email directly, view it on GitHub https://github.com/PrismarineJS/node-minecraft-protocol/issues/1297#issuecomment-2132292208, or unsubscribe https://github.com/notifications/unsubscribe-auth/APQEO2K2APCIJO7HSD3I3MLZEILBLAVCNFSM6AAAAABGNDE4PKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMZSGI4TEMRQHA . You are receiving this because you authored the thread.Message ID: @.***>

extremeheat commented 5 months ago

Can you embed a video on github? Dragging the file here should work.