{"size": 1227, "ext": "js", "max_stars_repo_path": "src/createClient.js", "max_stars_repo_name": "mhsjlw/minecraft-classic-protocol", "max_stars_repo_stars_event_min_datetime": "2015-11-21T22:12:01.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-29T10:13:49.000Z", "max_issues_repo_path": "src/createClient.js", "max_issues_repo_name": "codingwatching/minecraft-classic-protocol", "max_issues_repo_issues_event_min_datetime": "2015-11-21T16:22:45.000Z", "max_issues_repo_issues_event_max_datetime": "2016-05-14T22:05:51.000Z", "max_forks_repo_path": "src/createClient.js", "max_forks_repo_name": "codingwatching/minecraft-classic-protocol", "max_forks_repo_forks_event_min_datetime": "2021-06-29T10:13:50.000Z", "max_forks_repo_forks_event_max_datetime": "2021-06-29T10:13:50.000Z"}
{"max_stars_count": 12, "max_issues_count": 5, "max_forks_count": 1, "avg_line_length": 26.6739130435, "max_line_length": 74, "alphanum_fraction": 0.6495517522}
var net = require('net');
var dns = require('dns');
var Client = require('./client');
var assert = require('assert');

module.exports=createClient;

Client.prototype.connect = function(port, host) {
  var self = this;
  if(port == 25565 && net.isIP(host) === 0) {
    dns.resolveSrv("_minecraft._tcp." + host, function(err, addresses) {
      if(addresses && addresses.length > 0) {
        self.setSocket(net.connect(addresses[0].port, addresses[0].name));
      } else {
        self.setSocket(net.connect(port, host));
      }
    });
  } else {
    self.setSocket(net.connect(port, host));
  }
};

function createClient(options) {
  assert.ok(options, "options is required");
  var port = options.port || 25565;
  var host = options.host || 'localhost';
  var customPackets = options.customPackets || {};

  assert.ok(options.username, "username is required");

  var client = new Client(false, customPackets);
  client.on('connect', onConnect);
  client.username = options.username;
  client.connect(port, host);
  function onConnect() {
    client.write('player_identification', {
      protocol_version: 0x07,
      username: client.username,
      verification_key:"",
      unused:0x00
    });
  }

  return client;
}
