fix version in pluginChannels, add writeChannel, add two examples for a vanilla channel (MC|Brand)

This commit is contained in:
Romain Beaumont 2016-08-10 18:54:06 +02:00
parent 947c4c0587
commit 1dcd119d93
No known key found for this signature in database
GPG key ID: DB60E388B3BCF286
3 changed files with 72 additions and 2 deletions
examples
client_channel
server_channel
src/client

View file

@ -0,0 +1,24 @@
var ProtoDef = require('protodef').ProtoDef;
var mc = require('minecraft-protocol');
var proto = new ProtoDef();
if(process.argv.length < 4 || process.argv.length > 6) {
console.log("Usage : node client_channel.js <host> <port> [<name>] [<password>]");
process.exit(1);
}
var client = mc.createClient({version: false,
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[4] ? process.argv[4] : "test",
password: process.argv[5]
});
client.registerChannel('MC|Brand',['string',[]]);
client.on('MC|Brand',console.log);
client.on('login', function() {
client.writeChannel('MC|Brand', "vanilla");
});
client.on('error', console.log);

View file

@ -0,0 +1,37 @@
const ProtoDef = require('protodef').ProtoDef;
var mc = require('minecraft-protocol');
var proto = new ProtoDef();
var server = mc.createServer({
'online-mode': false, // optional
encryption: false, // optional
host: '0.0.0.0', // optional
port: 25565, // optional
version: '1.10'
});
server.on('login', function(client) {
client.registerChannel('MC|Brand',['string',[]]);
client.on('MC|Brand',console.log);
client.write('login', {
entityId: client.id,
levelType: 'default',
gameMode: 0,
dimension: 0,
difficulty: 2,
maxPlayers: server.maxPlayers,
reducedDebugInfo: false
});
client.write('position', {
x: 0,
y: 1.62,
z: 0,
yaw: 0,
pitch: 0,
flags: 0x00
});
client.writeChannel('MC|Brand', "vanilla");
});

View file

@ -2,14 +2,16 @@ var ProtoDef = require('protodef').ProtoDef;
var minecraft = require('../datatypes/minecraft');
module.exports = function(client, options) {
var mcdata = require('minecraft-data')(options.version);
var mcdata = require('minecraft-data')(options.version || require("../version").defaultVersion);
var channels = [];
var proto = new ProtoDef();
proto.addTypes(mcdata.protocol.types);
proto.addTypes(minecraft);
client.registerChannel = registerChannel;
client.unregisterChannel = unregisterChannel;
return client;
client.writeChannel = writeChannel;
function registerChannel(name, parser) {
if (parser) proto.addType(name, parser);
channels.push(name);
@ -39,5 +41,12 @@ module.exports = function(client, options) {
client.emit(channel, packet.data);
}
}
function writeChannel(channel,params) {
client.write("custom_payload",{
channel:channel,
data:proto.createPacketBuffer(channel,params)
});
}
};