From 1dcd119d93adc7239dff4b870de252e906b45dd2 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Wed, 10 Aug 2016 18:54:06 +0200 Subject: [PATCH] fix version in pluginChannels, add writeChannel, add two examples for a vanilla channel (MC|Brand) --- examples/client_channel/client_channel.js | 24 +++++++++++++++ examples/server_channel/server_channel.js | 37 +++++++++++++++++++++++ src/client/pluginChannels.js | 13 ++++++-- 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 examples/client_channel/client_channel.js create mode 100644 examples/server_channel/server_channel.js diff --git a/examples/client_channel/client_channel.js b/examples/client_channel/client_channel.js new file mode 100644 index 0000000..c396da6 --- /dev/null +++ b/examples/client_channel/client_channel.js @@ -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 [] []"); + 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); diff --git a/examples/server_channel/server_channel.js b/examples/server_channel/server_channel.js new file mode 100644 index 0000000..21e6b9a --- /dev/null +++ b/examples/server_channel/server_channel.js @@ -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"); +}); diff --git a/src/client/pluginChannels.js b/src/client/pluginChannels.js index 59ae37a..e6f8262 100644 --- a/src/client/pluginChannels.js +++ b/src/client/pluginChannels.js @@ -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) + }); + } };