mirror of
https://github.com/unmojang/node-minecraft-protocol.git
synced 2025-09-29 06:03:33 -04:00
added plugin channel suport
This commit is contained in:
parent
cdaf673a86
commit
8c6d8d592d
43
src/client/pluginChannels.js
Normal file
43
src/client/pluginChannels.js
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -11,6 +11,7 @@ const setProtocol = require('./client/setProtocol');
|
|||||||
const play = require('./client/play');
|
const play = require('./client/play');
|
||||||
const tcp_dns = require('./client/tcp_dns');
|
const tcp_dns = require('./client/tcp_dns');
|
||||||
const autoVersion = require('./client/autoVersion');
|
const autoVersion = require('./client/autoVersion');
|
||||||
|
const pluginChannels = require('./client/pluginChannels');
|
||||||
|
|
||||||
module.exports=createClient;
|
module.exports=createClient;
|
||||||
|
|
||||||
@ -36,6 +37,7 @@ function createClient(options) {
|
|||||||
encrypt(client, options);
|
encrypt(client, options);
|
||||||
play(client, options);
|
play(client, options);
|
||||||
compress(client, options);
|
compress(client, options);
|
||||||
|
pluginChannels(client, options);
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user