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
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;
client.writePacket(0x03, {
message: packet.message,

View file

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

View file

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

View file

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