How to use minecraft-protocol - 10 common examples

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 / server / server.js View on Github external
const mc = require('minecraft-protocol')

const options = {
  motd: 'Vox Industries',
  'max-players': 127,
  port: 25565,
  'online-mode': false
}

const server = mc.createServer(options)

server.on('login', function (client) {
  broadcast(client.username + ' joined the game.')
  const addr = client.socket.remoteAddress + ':' + client.socket.remotePort
  console.log(client.username + ' connected', '(' + addr + ')')

  client.on('end', function () {
    broadcast(client.username + ' left the game.', client)
    console.log(client.username + ' disconnected', '(' + addr + ')')
  })

  // send init data so client will start rendering world
  client.write('login', {
    entityId: client.id,
    levelType: 'default',
    gameMode: 1,
github DragonetMC / SelectorServer / server.js View on Github external
// item settings
const item_server = 3;
const item_category = 4;
const item_functional = 2;
// messages
const message_loading = '\u00a7aLoading... ';
const menu_title = "\u00a70Choose a server";
const message_error_exit = "You didn't select a server to join! ";
const message_bye = "Bye! ";

/* == END OF SETTINGS AREA == */

const type = require("type-detect");

const mc = require('minecraft-protocol');
const server = mc.createServer({
  'online-mode': false,   // optional
  encryption: true,      // optional
  host: listen_host,       // optional
  port: listen_port,           // optional
});

console.log("Server is now started! ");

server.on('login', function(client) {
  modifyClient(client);
  client.write('login', {
    entityId: 0,
    levelType: 'default',
    gameMode: 3,
    dimension: 0,
    difficulty: 2,
github themoonisacheese / 2bored2wait / main.js View on Github external
}
		stop();
		// setTimeout(startQueuing, 100); // reconnect after 100 ms
	});

	client.on('error', (err) => {
		if (proxyClient) {
            proxyClient.end(`Connection error by 2b2t server.\n Error message: ${err}\nReconnecting...`);
            proxyClient = null
		}
		console.log('err', err);
		stop();
		// setTimeout(startQueuing, 100); // reconnect after 100 ms
	});

	server = mc.createServer({ // create a server for us to connect to
		'online-mode': false,
		encryption: true,
		host: '0.0.0.0',
		port: config.ports.minecraft,
		version: config.MCversion,
		'max-players': maxPlayers = 1
	});

	server.on('login', (newProxyClient) => { // handle login
		newProxyClient.write('login', {
			entityId: newProxyClient.id,
			levelType: 'default',
			gameMode: 0,
			dimension: 0,
			difficulty: 2,
			maxPlayers: server.maxPlayers,
github PrismarineJS / node-minecraft-protocol / examples / server_channel / server_channel.js View on Github external
const mc = require('minecraft-protocol')

const server = mc.createServer({
  'online-mode': false, // optional
  encryption: false, // optional
  host: '0.0.0.0', // optional
  port: 25565, // optional
  version: '1.10'
})

server.on('login', function (client) {
  client.registerChannel('MC|Brand', ['string', []])
  client.on('MC|Brand', console.log)
  client.write('login', {
    entityId: client.id,
    levelType: 'default',
    gameMode: 0,
    dimension: 0,
    difficulty: 2,
github PrismarineJS / prismarine-server / src / app.js View on Github external
var mc = require('minecraft-protocol');
var states = mc.protocol.states;

var options = {
    motd: 'Bushido Studios',
    'max-players': 127,
    port: 25565,
    'online-mode': true,
};

var server = mc.createServer(options);

server.on('login', function(client) {
    broadcast(client.username+' joined the game.');
    var addr = client.socket.remoteAddress + ':' + client.socket.remotePort;
    console.log(client.username+' connected', '('+addr+')');

    client.on('end', function() {
        broadcast(client.username+' left the game.', client);
        console.log(client.username+' disconnected', '('+addr+')');
    });

    // send init data so client will start rendering world
    client.write('login', {
        entityId: client.id,
        levelType: 'default',
        gameMode: 1,
github PrismarineJS / node-minecraft-protocol / examples / server_helloworld / server_helloworld.js View on Github external
const mc = require('minecraft-protocol')

const options = {
  'online-mode': true
}

const server = mc.createServer(options)

server.on('login', function (client) {
  const addr = client.socket.remoteAddress
  console.log('Incoming connection', '(' + addr + ')')

  client.on('end', function () {
    console.log('Connection closed', '(' + addr + ')')
  })

  client.on('error', function (error) {
    console.log('Error:', error)
  })

  // send init data so client will start rendering world
  client.write('login', {
    entityId: client.id,
github PrismarineJS / node-minecraft-protocol / examples / server_custom_channel / server_custom_channel.js View on Github external
const mc = require('minecraft-protocol')

const server = mc.createServer({
  'online-mode': false, // optional
  encryption: false, // optional
  host: '0.0.0.0', // optional
  port: 25565, // optional
  version: '1.10'
})

server.on('login', function (client) {
  client.write('login', {
    entityId: client.id,
    levelType: 'default',
    gameMode: 0,
    dimension: 0,
    difficulty: 2,
    maxPlayers: server.maxPlayers,
    reducedDebugInfo: false
github deathcap / wsmc / minecraft-protocol-stream.js View on Github external
function createClient(options) {
  assert.ok(options, "options is required");
  var stream = options.stream;
  assert.ok(stream, "stream is required");

  assert.ok(options.username, "username is required");
  var keepAlive = options.keepAlive == null ? true : options.keepAlive;

  var optVersion = options.version || require('./mcversion.js');
  var mcData = require('minecraft-data')(optVersion);
  var version = mcData.version;

  var client = new Client(false, version.majorVersion);

  // Options to opt-out of MC protocol packet framing (useful since WS is alreay framed)
  if (options.noPacketFramer) {
    client.framer = EmptyTransformStream;
  }
  if (options.noPacketSplitter) {
    client.splitter = EmptyTransformStream;
  }

  client.on('connect', onConnect);
  client.once('success', onLogin);
  client.once('compress', onCompressionRequest);
  client.once('set_compression', onCompressionRequest);
  if (keepAlive) client.on('keep_alive', onKeepAlive);

  client.username = options.username;