How to use the minecraft-protocol.createClient function in minecraft-protocol

To help you get started, we’ve selected a few minecraft-protocol examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github omegablitz / chunky / chunky_proxy / src / proxy / Proxy.js View on Github external
let remoteClient = this.clients[remoteClientId];
    if (!remoteClient) {
      console.log("INVALID RemoteClientId, DOING NOTHING");
      return "failed";
    }

    let oldServerName = remoteClient.currentServer;
    let newServer = this.serverList[newServerName];

    if (remoteClient.currentServer === newServerName) return;

    this.emit('playerMoving', remoteClientId, oldServerName, newServerName);

    this.clients[remoteClientId].currentServer = newServerName;

    let newLocalClient = mc.createClient({
      host: newServer.host,
      port: newServer.port,
      username: remoteClient.username,
      keepAlive: false, // keep alive is set to false because the remote server will send the packets and the remote client will respond
      version: remoteClient.version
    });

    newLocalClient.on('error', (err) => {
      this.emit('playerMoveFailed', err, remoteClientId, oldServerName, newServerName)
      this.emit('error', err)
      console.error("Move Error:", err);
      // this.setRemoteServer(remoteClientId, newServerName);
      // this.fallback(remoteClientId)
    });

    if (!remoteClient.isFirstConnection) {
github PrismarineJS / node-minecraft-protocol / examples / client_forge / client_forge.js View on Github external
mc.ping({host, port}, function(err, response) {
  if (err) throw err;
  console.log('ping response',response);
  if (!response.modinfo || response.modinfo.type !== 'FML') {
    throw new Error('not an FML server, aborting connection');
    // TODO: gracefully connect non-FML
    // TODO: could also use ping pre-connect to save description, type, negotiate protocol etc.
    //  ^ see https://github.com/PrismarineJS/node-minecraft-protocol/issues/327 
  }
  // Use the list of Forge mods from the server ping, so client will match server
  var forgeMods = response.modinfo.modList;
  console.log('Using forgeMods:',forgeMods);

  var client = mc.createClient({
    forgeMods: forgeMods,
    host: host,
    port: port,
    username: username,
    password: password
  });

  client.on('connect', function() {
    console.info('connected');
  });
  client.on('disconnect', function(packet) {
    console.log('disconnected: '+ packet.reason);
  });
  client.on('end', function(err) {
    console.log('Connection lost');
  });
github PrismarineJS / node-minecraft-protocol / examples / client_auto / client_auto.js View on Github external
'use strict'

const mc = require('minecraft-protocol')

if (process.argv.length < 4 || process.argv.length > 6) {
  console.log('Usage : node echo.js   [] []')
  process.exit(1)
}

const client = mc.createClient({
  version: false,
  host: process.argv[2],
  port: parseInt(process.argv[3]),
  username: process.argv[4] ? process.argv[4] : 'echo',
  password: process.argv[5]
})

client.on('connect', function () {
  console.info('connected')
})
client.on('disconnect', function (packet) {
  console.log('disconnected: ' + packet.reason)
})
client.on('chat', function (packet) {
  const jsonMsg = JSON.parse(packet.message)
  if (jsonMsg.translate === 'chat.type.announcement' || jsonMsg.translate === 'chat.type.text') {
github PrismarineJS / node-minecraft-protocol / examples / client_channel / client_channel.js View on Github external
const mc = require('minecraft-protocol')

if (process.argv.length < 4 || process.argv.length > 6) {
  console.log('Usage : node client_channel.js   [] []')
  process.exit(1)
}

const client = mc.createClient({
  version: false,
  host: process.argv[2],
  port: parseInt(process.argv[3]),
  username: process.argv[4] ? process.argv[4] : 'test',
  password: process.argv[5]
})

client.registerChannel('MC|Brand', ['string', []])
client.on('MC|Brand', console.log)

client.on('login', function () {
  client.writeChannel('MC|Brand', 'vanilla')
})
client.on('error', console.log)
github PrismarineJS / node-minecraft-protocol / examples / client_chat / client_chat.js View on Github external
})

let host = process.argv[2]
let port = parseInt(process.argv[3])
const user = process.argv[4]
const passwd = process.argv[5]

if (host.indexOf(':') !== -1) {
  port = host.substring(host.indexOf(':') + 1)
  host = host.substring(0, host.indexOf(':'))
}

console.log('connecting to ' + host + ':' + port)
console.log('user: ' + user)

const client = mc.createClient({
  host: host,
  port: port,
  username: user,
  password: passwd
})

client.on('kick_disconnect', function (packet) {
  console.info(color('Kicked for ' + packet.reason, 'blink+red'))
  process.exit(1)
})

const chats = []

client.on('connect', function () {
  console.info(color('Successfully connected to ' + host + ':' + port, 'blink+green'))
})
github PrismarineJS / node-minecraft-protocol / examples / client_echo / client_echo.js View on Github external
var mc = require('minecraft-protocol');

if(process.argv.length < 4 || process.argv.length > 6) {
  console.log("Usage : node echo.js   [] []");
  process.exit(1);
}

var client = mc.createClient({
  host: process.argv[2],
  port: parseInt(process.argv[3]),
  username: process.argv[4] ? process.argv[4] : "echo",
  password: process.argv[5]
});

client.on('connect', function() {
  console.info('connected');
});
client.on('disconnect', function(packet) {
  console.log('disconnected: '+ packet.reason);
});
client.on('chat', function(packet) {
  var jsonMsg = JSON.parse(packet.message);
  if(jsonMsg.translate == 'chat.type.announcement' || jsonMsg.translate == 'chat.type.text') {
    var username = jsonMsg.with[0].text;
github PrismarineJS / node-minecraft-protocol / examples / client_http_proxy / client_http_proxy.js View on Github external
const mc = require('minecraft-protocol')
const Http = require('http')

if (process.argv.length < 6 || process.argv.length > 8) {
  console.log('Usage : node client_http_proxy.js     [] []')
  process.exit(1)
}

const proxyHost = process.argv[4]
const proxyPort = process.argv[5]

const client = mc.createClient({
  connect: (client) => {
    const req = Http.request({
      host: proxyHost,
      port: proxyPort,
      method: 'CONNECT',
      path: process.argv[2] + ':' + parseInt(process.argv[3])
    })
    req.end()

    req.on('connect', function (res, stream) {
      client.setSocket(stream)
      client.emit('connect')
    })
  },
  username: process.argv[6] ? process.argv[6] : 'echo',
  password: process.argv[7]
github PrismarineJS / node-minecraft-protocol-forge / examples / client_forge / client_forge.js View on Github external
var mc = require('minecraft-protocol');
var forgeHandshake = require('../../src/client/forgeHandshake');
var autoVersionForge = require('../../src/client/autoVersionForge');

if(process.argv.length < 4 || process.argv.length > 6) {
  console.log("Usage : node echo.js   [] []");
  process.exit(1);
}

var host = process.argv[2];
var port = parseInt(process.argv[3]);
var username =  process.argv[4] ? process.argv[4] : "echo";
var password = process.argv[5];

  var client = mc.createClient({
    version: false,
    host: host,
    port: port,
    username: username,
    password: password
  });
  autoVersionForge(client);
  //forgeHandshake(client, {forgeMods});
  //forgeHandshake(client, {});

  client.on('connect', function() {
    console.info('connected');
  });
  client.on('disconnect', function(packet) {
    console.log('disconnected: '+ packet.reason);
  });
github PrismarineJS / node-minecraft-protocol / examples / proxy / proxy.js View on Github external
const addr = client.socket.remoteAddress
  console.log('Incoming connection', '(' + addr + ')')
  let endedClient = false
  let endedTargetClient = false
  client.on('end', function () {
    endedClient = true
    console.log('Connection closed by client', '(' + addr + ')')
    if (!endedTargetClient) { targetClient.end('End') }
  })
  client.on('error', function (err) {
    endedClient = true
    console.log('Connection error by client', '(' + addr + ')')
    console.log(err.stack)
    if (!endedTargetClient) { targetClient.end('Error') }
  })
  const targetClient = mc.createClient({
    host: host,
    port: port,
    username: client.username,
    keepAlive: false,
    version: version
  })
  client.on('packet', function (data, meta) {
    if (targetClient.state === states.PLAY && meta.state === states.PLAY) {
      if (shouldDump(meta.name, 'o')) {
        console.log('client->server:',
          client.state + ' ' + meta.name + ' :',
          JSON.stringify(data))
      }
      if (!endedTargetClient) { targetClient.write(meta.name, data) }
    }
  })
github PrismarineJS / node-minecraft-protocol / examples / client_custom_packets / client_custom_packets.js View on Github external
compareTo: 'name',
                    fields: {
                      custom_name: 'packet_custom_name'
                    }
                  }
                ]
              }
            ]
          ]
        }
      }
    }
  }
}

const client = mc.createClient({
  host: process.argv[2],
  port: parseInt(process.argv[3]),
  username: process.argv[4] ? process.argv[4] : 'echo',
  password: process.argv[5],
  customPackets: customPackets
})

client.on('connect', function () {
  console.info('connected')
})
client.on('disconnect', function (packet) {
  console.log('disconnected: ' + packet.reason)
})
client.on('end', function () {
  console.log('Connection lost')
})