added plugin channel suport

This commit is contained in:
plexigras 2016-08-09 21:43:52 +02:00
parent cdaf673a86
commit 8c6d8d592d
2 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,43 @@
var ProtoDef = require('protodef').ProtoDef;
var minecraft = require('../datatypes/minecraft');
module.exports = function(client, options) {
var mcdata = require('minecraft-data')(options.version);
var channels = [];
var proto = new ProtoDef();
proto.addTypes(mcdata.protocol.types);
proto.addTypes(minecraft);
client.registerChannel = registerChannel;
client.unregisterChannel = unregisterChannel;
return client;
function registerChannel(name, parser) {
if (parser) proto.addType(name, parser);
channels.push(name);
if (channels.length === 1)
client.on('custom_payload', onCustomPayload);
}
function unregisterChannel(channel) {
var index = channels.find(function(name) {
return channel === name;
});
if (index) {
proto.types[channel] = undefined;
channels.splice(index, 1);
if (channels.length === 0)
client.removeListener('custom_payload', onCustomPayload);
}
}
function onCustomPayload(packet) {
var channel = channels.find(function(channel) {
return channel === packet.channel;
});
if (channel) {
if (proto.types[channel])
packet.data = proto.parsePacketBuffer(channel, packet.data).data;
client.emit(channel, packet.data);
}
}
};

View File

@ -11,6 +11,7 @@ const setProtocol = require('./client/setProtocol');
const play = require('./client/play');
const tcp_dns = require('./client/tcp_dns');
const autoVersion = require('./client/autoVersion');
const pluginChannels = require('./client/pluginChannels');
module.exports=createClient;
@ -36,6 +37,7 @@ function createClient(options) {
encrypt(client, options);
play(client, options);
compress(client, options);
pluginChannels(client, options);
return client;
}