un-namespace packet events. remove generic packet event.

This commit is contained in:
Andrew Kelley 2013-01-03 20:59:58 -05:00
parent 0247fc9cb8
commit fadb9e0c0a
4 changed files with 46 additions and 54 deletions

View file

@ -38,7 +38,7 @@ var client = mc.createClient({
email: "email@example.com", // email and password are required only for email: "email@example.com", // email and password are required only for
password: "12345678", // online-mode=true servers password: "12345678", // online-mode=true servers
}); });
client.on('packet-3', function(packet) { client.on(0x03, function(packet) {
if (packet.message.indexOf(client.session.username) !== -1) return; if (packet.message.indexOf(client.session.username) !== -1) return;
client.writePacket(0x03, { client.writePacket(0x03, {
message: packet.message, message: packet.message,

View file

@ -8,7 +8,7 @@ var client = mc.createClient({
client.on('connect', function() { client.on('connect', function() {
console.info("connected"); console.info("connected");
}); });
client.on('packet-3', function(packet) { client.on(0x03, function(packet) {
var match = packet.message.match(/^<(.+?)> (.*)$/); var match = packet.message.match(/^<(.+?)> (.*)$/);
if (! match) return; if (! match) return;
var username = match[1]; var username = match[1];

View file

@ -39,11 +39,9 @@ function createClient(options) {
serverPort: port, serverPort: port,
}); });
}); });
client.on('packet', function(packet) { client.on(0x00, onKeepAlive);
if (options.verbose) console.info("packet", packet); client.on(0xFC, onEncryptionKeyResponse);
var handler = packetHandlers[packet.id]; client.on(0xFD, onEncryptionKeyRequest);
if (handler) handler(packet);
});
client.connect(port, host); client.connect(port, host);
return client; return client;
@ -170,8 +168,7 @@ Client.prototype.connect = function(port, host) {
packet = parsed.results; packet = parsed.results;
hax(packet); // fuck you, notch hax(packet); // fuck you, notch
incomingBuffer = incomingBuffer.slice(parsed.size); incomingBuffer = incomingBuffer.slice(parsed.size);
self.emit('packet', packet); self.emit(packet.id, packet);
self.emit('packet-' + packet.id, packet);
} }
}); });

View file

@ -123,24 +123,23 @@ describe("minecraft protocol", function() {
mcServer.stdin.write("say hello\n"); mcServer.stdin.write("say hello\n");
}); });
var chatCount = 0; var chatCount = 0;
client.on('packet', function(packet) { client.on(0x01, function(packet) {
if (packet.id === 0x01) { assert.strictEqual(packet.levelType, 'default');
assert.strictEqual(packet.levelType, 'default'); assert.strictEqual(packet.difficulty, 1);
assert.strictEqual(packet.difficulty, 1); assert.strictEqual(packet.dimension, 0);
assert.strictEqual(packet.dimension, 0); assert.strictEqual(packet.gameMode, 0);
assert.strictEqual(packet.gameMode, 0); client.writePacket(0x03, {
client.writePacket(0x03, { message: "hello everyone; I have logged in."
message: "hello everyone; I have logged in." });
}); });
} else if (packet.id === 0x03) { client.on(0x03, function(packet) {
chatCount += 1; chatCount += 1;
assert.ok(chatCount <= 2); assert.ok(chatCount <= 2);
if (chatCount === 1) { if (chatCount === 1) {
assert.strictEqual(packet.message, "<" + client.session.username + ">" + " hello everyone; I have logged in."); assert.strictEqual(packet.message, "<" + client.session.username + ">" + " hello everyone; I have logged in.");
} else if (chatCount === 2) { } else if (chatCount === 2) {
assert.strictEqual(packet.message, "[Server] hello"); assert.strictEqual(packet.message, "[Server] hello");
done(); done();
}
} }
}); });
}); });
@ -158,8 +157,7 @@ describe("minecraft protocol", function() {
mcServer.stdin.write("say hello\n"); mcServer.stdin.write("say hello\n");
}); });
var chatCount = 0; var chatCount = 0;
client.on('packet', function(packet) { client.on(0x01, function(packet) {
if (packet.id === 0x01) {
assert.strictEqual(packet.levelType, 'default'); assert.strictEqual(packet.levelType, 'default');
assert.strictEqual(packet.difficulty, 1); assert.strictEqual(packet.difficulty, 1);
assert.strictEqual(packet.dimension, 0); assert.strictEqual(packet.dimension, 0);
@ -167,15 +165,15 @@ describe("minecraft protocol", function() {
client.writePacket(0x03, { client.writePacket(0x03, {
message: "hello everyone; I have logged in." message: "hello everyone; I have logged in."
}); });
} else if (packet.id === 0x03) { });
chatCount += 1; client.on(0x03, function(packet) {
assert.ok(chatCount <= 2); chatCount += 1;
if (chatCount === 1) { assert.ok(chatCount <= 2);
assert.strictEqual(packet.message, "<" + process.env.MC_USERNAME + ">" + " hello everyone; I have logged in."); if (chatCount === 1) {
} else if (chatCount === 2) { assert.strictEqual(packet.message, "<" + process.env.MC_USERNAME + ">" + " hello everyone; I have logged in.");
assert.strictEqual(packet.message, "[Server] hello"); } else if (chatCount === 2) {
done(); assert.strictEqual(packet.message, "[Server] hello");
} done();
} }
}); });
}); });
@ -186,11 +184,9 @@ describe("minecraft protocol", function() {
username: process.env.MC_USERNAME, username: process.env.MC_USERNAME,
}); });
var gotKicked = false; var gotKicked = false;
client.on('packet', function(packet) { client.on(0xff, function(packet) {
if (packet.id === 0xff) { assert.strictEqual(packet.reason, "Failed to verify username!");
assert.strictEqual(packet.reason, "Failed to verify username!"); gotKicked = true;
gotKicked = true;
}
}); });
client.on('end', function() { client.on('end', function() {
assert.ok(gotKicked); assert.ok(gotKicked);
@ -203,17 +199,16 @@ describe("minecraft protocol", function() {
var client = mc.createClient({ var client = mc.createClient({
username: process.env.MC_USERNAME, username: process.env.MC_USERNAME,
}); });
client.on('packet', function(packet) { client.on(0x01, function(packet) {
if (packet.id === 0x01) { client.writePacket(0x03, {
client.writePacket(0x03, { message: "hello everyone; I have logged in."
message: "hello everyone; I have logged in." });
}); });
} else if (packet.id === 0x03) { client.on(0x03, function(packet) {
assert.strictEqual(packet.message, "<" + process.env.MC_USERNAME + ">" + " hello everyone; I have logged in."); assert.strictEqual(packet.message, "<" + process.env.MC_USERNAME + ">" + " hello everyone; I have logged in.");
setTimeout(function() { setTimeout(function() {
done(); done();
}, SURVIVE_TIME); }, SURVIVE_TIME);
}
}); });
}); });
}); });