From 8c6d8d592db30ea60030806b01b24120903eac14 Mon Sep 17 00:00:00 2001 From: plexigras Date: Tue, 9 Aug 2016 21:43:52 +0200 Subject: [PATCH] added plugin channel suport --- src/client/pluginChannels.js | 43 ++++++++++++++++++++++++++++++++++++ src/createClient.js | 2 ++ 2 files changed, 45 insertions(+) create mode 100644 src/client/pluginChannels.js diff --git a/src/client/pluginChannels.js b/src/client/pluginChannels.js new file mode 100644 index 0000000..59ae37a --- /dev/null +++ b/src/client/pluginChannels.js @@ -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); + } + } +}; + diff --git a/src/createClient.js b/src/createClient.js index 6601d24..f16f8d8 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -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; }