diff --git a/examples/echo.js b/examples/echo.js new file mode 100644 index 0000000..1f0ca9a --- /dev/null +++ b/examples/echo.js @@ -0,0 +1,19 @@ +var mc = require('../'); +var client = mc.createClient({ + username: process.env.MC_USERNAME, + email: process.env.MC_EMAIL, + password: process.env.MC_PASSWORD, + verbose: true, +}); +client.on('connect', function() { + console.info("connected"); +}); +client.on('packet', function(packet) { + if (packet.id !== 0x03) return; + var match = packet.message.match(/^<(.+?)> (.*)$/); + if (! match) return; + var username = match[1]; + var msg = match[2]; + if (username === client.username) return; + client.writePacket(0x03, {message: msg}); +}); diff --git a/index.js b/index.js index f2fc06b..2d8828b 100644 --- a/index.js +++ b/index.js @@ -30,6 +30,7 @@ function createClient(options) { }; var client = new Client(); + client.username = options.username; client.on('connect', function() { client.writePacket(0x02, { protocolVersion: packets.meta.protocolVersion, @@ -39,6 +40,7 @@ function createClient(options) { }); }); client.on('packet', function(packet) { + if (options.verbose) console.info("packet", packet); var handler = packetHandlers[packet.id]; if (handler) handler(packet); }); @@ -71,6 +73,7 @@ function createClient(options) { var sharedSecret; if (haveCredentials) { client.session = results[0]; + client.username = client.session.username; client.emit('session'); sharedSecret = results[1]; joinServerRequest(onJoinServerResponse); diff --git a/packets.json b/packets.json index 0478a43..2e3b0fd 100644 --- a/packets.json +++ b/packets.json @@ -744,7 +744,7 @@ }, { "name": "data", - "type": "byteArray" + "type": "bigByteArray" } ], "53": [ diff --git a/test/test.js b/test/test.js index 9e368f1..0f745bc 100644 --- a/test/test.js +++ b/test/test.js @@ -85,10 +85,10 @@ describe("minecraft protocol", function() { } mcServer.on('line', onLine); mcServer.on('line', function(line) { - //process.stderr.write('.'); + process.stderr.write('.'); // uncomment this line when debugging for more insight as to what is // happening on the minecraft server - console.error("[MC]", line); + //console.error("[MC]", line); }); function onLine(line) { if (/\[INFO\] Done/.test(line)) { @@ -198,5 +198,23 @@ describe("minecraft protocol", function() { }); }); }); - it("survives for " + SURVIVE_TIME + "ms"); + it("does not crash for " + SURVIVE_TIME + "ms", function(done) { + startServer({ 'online-mode': 'false' }, function() { + var client = mc.createClient({ + username: process.env.MC_USERNAME, + }); + client.on('packet', function(packet) { + if (packet.id === 0x01) { + client.writePacket(0x03, { + message: "hello everyone; I have logged in." + }); + } else if (packet.id === 0x03) { + assert.strictEqual(packet.message, "<" + process.env.MC_USERNAME + ">" + " hello everyone; I have logged in."); + setTimeout(function() { + done(); + }, SURVIVE_TIME); + } + }); + }); + }); });