Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using proxy.js if world is already loaded #960

Closed
ililillililil opened this issue Feb 13, 2022 · 4 comments
Closed

Using proxy.js if world is already loaded #960

ililillililil opened this issue Feb 13, 2022 · 4 comments

Comments

@ililillililil
Copy link

Is this possible? If I send login and position packets so client will start rendering world then after trying to connect to target server it's shows me "Loading terrain" infinitely. I tried to send a respawn packet and it works, but then the server thinks I'm flying and I'm stuck in a block.

@rom1504
Copy link
Member

rom1504 commented Feb 13, 2022

Can you start from the beginning? What are you building?

@ililillililil
Copy link
Author

I am trying to connect a player to server after he has sent a message to the chat. If I have already loaded the world (by writing login and position packets), then after trying to connect I get "Loading terrain" infinitely.

@ililillililil
Copy link
Author

ililillililil commented Feb 13, 2022

Code:

index.js:

// index.js
"use strict";

const mc = require("minecraft-protocol");

const connect = require("./connect");

const server = mc.createServer({
  "online-mode": false,
  keepAlive: false,
  port: 25565,
  version: 1.18,
});

server.on("login", (client) => {
  const mcData = require("minecraft-data")(server.version);
  const loginPacket = mcData.loginPacket;
  let isConnected = false;

  client.write("login", {
    entityId: client.id,
    isHardcore: false,
    gameMode: 0,
    previousGameMode: 1,
    worldNames: loginPacket.worldNames,
    dimensionCodec: loginPacket.dimensionCodec,
    dimension: loginPacket.dimension,
    worldName: "minecraft:overworld",
    hashedSeed: [0, 0],
    maxPlayers: server.maxPlayers,
    viewDistance: 10,
    reducedDebugInfo: false,
    enableRespawnScreen: true,
    isDebug: false,
    isFlat: false,
  });

  client.write("position", {
    x: 0,
    y: 1.62,
    z: 0,
    yaw: 0,
    pitch: 0,
    flags: 0x00,
  });

  const keepAliveInterval = setInterval(() => {
    client.write("keep_alive", {
      keepAliveId: Math.floor(Math.random() * 2147483648),
    });
  }, 10000);

  client.on("chat", () => {
    if (!isConnected) {
      isConnected = true;

      clearInterval(keepAliveInterval);
      connect(client, "localhost", 25566);
    }
  });
});

connect.js:

// connect.js
"use strict";

const mc = require("minecraft-protocol");

const connect = (client, host, port) => {
  let endedClient = false;
  let endedTargetClient = false;

  client.on("end", () => {
    endedClient = true;

    if (!endedTargetClient) {
      targetClient.end("End");
    }
  });

  client.on("error", (err) => {
    endedClient = true;

    if (!endedTargetClient) {
      targetClient.end("Error");
    }
  });

  const targetClient = mc.createClient({
    host,
    keepAlive: false,
    port,
    username: client.username,
  });

  const states = mc.states;

  client.on("packet", (data, meta) => {
    if (targetClient.state === states.PLAY && meta.state === states.PLAY) {
      if (!endedTargetClient) {
        targetClient.write(meta.name, data);
      }
    }
  });

  targetClient.on("packet", (data, meta) => {
    if (meta.state === states.PLAY && client.state === states.PLAY) {
      if (!endedClient) {
        client.write(meta.name, data);

        if (meta.name === "set_compression") {
          client.compressionThreshold = data.threshold;
        }
      }
    }
  });

  targetClient.on("raw", (buffer, meta) => {
    if (client.state !== states.PLAY || meta.state !== states.PLAY) {
      return;
    }
  });

  client.on("raw", (buffer, meta) => {
    if (meta.state !== states.PLAY || targetClient.state !== states.PLAY) {
      return;
    }
  });

  targetClient.on("end", () => {
    endedTargetClient = true;

    if (!endedClient) {
      client.end("End");
    }
  });

  targetClient.on("error", (err) => {
    endedTargetClient = true;

    if (!endedClient) {
      client.end("Error");
    }
  });
};

module.exports = connect;

@u9g
Copy link
Member

u9g commented Mar 1, 2022

Yes. This is because you have already been sent the world, so now your client wont receive the world since the server thinks you have the world already.

@rom1504 rom1504 closed this as completed Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants