From a0717a1232f83799bd87fb7e918392cb175183d4 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Wed, 10 Aug 2016 19:27:50 +0200 Subject: [PATCH] add logic for custom channel, and corresponding examples --- examples/client_channel/package.json | 8 +++++ .../client_custom_channel.js | 25 ++++++++++++++ examples/client_custom_channel/package.json | 8 +++++ examples/server_channel/package.json | 8 +++++ examples/server_channel/server_channel.js | 3 -- examples/server_custom_channel/package.json | 8 +++++ .../server_custom_channel.js | 33 +++++++++++++++++++ src/client/pluginChannels.js | 13 ++++++-- 8 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 examples/client_channel/package.json create mode 100644 examples/client_custom_channel/client_custom_channel.js create mode 100644 examples/client_custom_channel/package.json create mode 100644 examples/server_channel/package.json create mode 100644 examples/server_custom_channel/package.json create mode 100644 examples/server_custom_channel/server_custom_channel.js diff --git a/examples/client_channel/package.json b/examples/client_channel/package.json new file mode 100644 index 0000000..56fcdf2 --- /dev/null +++ b/examples/client_channel/package.json @@ -0,0 +1,8 @@ +{ + "name": "node-minecraft-protocol-example", + "version": "0.0.0", + "private": true, + "dependencies": { + }, + "description": "A node-minecraft-protocol example" +} diff --git a/examples/client_custom_channel/client_custom_channel.js b/examples/client_custom_channel/client_custom_channel.js new file mode 100644 index 0000000..d8a08b5 --- /dev/null +++ b/examples/client_custom_channel/client_custom_channel.js @@ -0,0 +1,25 @@ +var mc = require('minecraft-protocol'); + +if(process.argv.length < 4 || process.argv.length > 6) { + console.log("Usage : node client_channel.js [] []"); + process.exit(1); +} + +var client = mc.createClient({ + host: process.argv[2], + port: parseInt(process.argv[3]), + username: process.argv[4] ? process.argv[4] : "test", + password: process.argv[5], + version: '1.10' +}); + + +client.on('login', onlogin); +client.on('error', console.log); + +function onlogin() { + client.registerChannel('CUSTOM|ChannelOne',['i32',[]],true); + client.registerChannel('CUSTOM|ChannelTwo',['i32',[]],true); + client.writeChannel('CUSTOM|ChannelOne', 4); + client.on('CUSTOM|ChannelTwo',console.log); +} diff --git a/examples/client_custom_channel/package.json b/examples/client_custom_channel/package.json new file mode 100644 index 0000000..56fcdf2 --- /dev/null +++ b/examples/client_custom_channel/package.json @@ -0,0 +1,8 @@ +{ + "name": "node-minecraft-protocol-example", + "version": "0.0.0", + "private": true, + "dependencies": { + }, + "description": "A node-minecraft-protocol example" +} diff --git a/examples/server_channel/package.json b/examples/server_channel/package.json new file mode 100644 index 0000000..56fcdf2 --- /dev/null +++ b/examples/server_channel/package.json @@ -0,0 +1,8 @@ +{ + "name": "node-minecraft-protocol-example", + "version": "0.0.0", + "private": true, + "dependencies": { + }, + "description": "A node-minecraft-protocol example" +} diff --git a/examples/server_channel/server_channel.js b/examples/server_channel/server_channel.js index 21e6b9a..9b7fc60 100644 --- a/examples/server_channel/server_channel.js +++ b/examples/server_channel/server_channel.js @@ -1,8 +1,5 @@ -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 diff --git a/examples/server_custom_channel/package.json b/examples/server_custom_channel/package.json new file mode 100644 index 0000000..56fcdf2 --- /dev/null +++ b/examples/server_custom_channel/package.json @@ -0,0 +1,8 @@ +{ + "name": "node-minecraft-protocol-example", + "version": "0.0.0", + "private": true, + "dependencies": { + }, + "description": "A node-minecraft-protocol example" +} diff --git a/examples/server_custom_channel/server_custom_channel.js b/examples/server_custom_channel/server_custom_channel.js new file mode 100644 index 0000000..acb4ee2 --- /dev/null +++ b/examples/server_custom_channel/server_custom_channel.js @@ -0,0 +1,33 @@ +var mc = require('minecraft-protocol'); + +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.write('login', { + entityId: client.id, + levelType: 'default', + gameMode: 0, + dimension: 0, + difficulty: 2, + maxPlayers: server.maxPlayers, + reducedDebugInfo: false + }); + client.registerChannel('CUSTOM|ChannelOne',['i32',[]],true); + client.registerChannel('CUSTOM|ChannelTwo',['i32',[]],true); + client.write('position', { + x: 0, + y: 1.62, + z: 0, + yaw: 0, + pitch: 0, + flags: 0x00 + }); + client.writeChannel('CUSTOM|ChannelTwo', 10); + client.on('CUSTOM|ChannelOne',console.log); +}); diff --git a/src/client/pluginChannels.js b/src/client/pluginChannels.js index e6f8262..d9c18f9 100644 --- a/src/client/pluginChannels.js +++ b/src/client/pluginChannels.js @@ -11,15 +11,24 @@ module.exports = function(client, options) { client.unregisterChannel = unregisterChannel; client.writeChannel = writeChannel; + client.registerChannel("REGISTER",["string",[]]); + client.registerChannel("UNREGISTER",["string",[]]); - function registerChannel(name, parser) { + + function registerChannel(name, parser, custom) { + if(custom) { + client.writeChannel("REGISTER",name); + } if (parser) proto.addType(name, parser); channels.push(name); if (channels.length === 1) client.on('custom_payload', onCustomPayload); } - function unregisterChannel(channel) { + function unregisterChannel(channel, custom) { + if(custom) { + client.writeChannel("UNREGISTER",channel); + } var index = channels.find(function(name) { return channel === name; });