add logic for custom channel, and corresponding examples

This commit is contained in:
Romain Beaumont 2016-08-10 19:27:50 +02:00
parent 1dcd119d93
commit a0717a1232
No known key found for this signature in database
GPG key ID: DB60E388B3BCF286
8 changed files with 101 additions and 5 deletions

View file

@ -0,0 +1,8 @@
{
"name": "node-minecraft-protocol-example",
"version": "0.0.0",
"private": true,
"dependencies": {
},
"description": "A node-minecraft-protocol example"
}

View file

@ -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 <host> <port> [<name>] [<password>]");
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);
}

View file

@ -0,0 +1,8 @@
{
"name": "node-minecraft-protocol-example",
"version": "0.0.0",
"private": true,
"dependencies": {
},
"description": "A node-minecraft-protocol example"
}

View file

@ -0,0 +1,8 @@
{
"name": "node-minecraft-protocol-example",
"version": "0.0.0",
"private": true,
"dependencies": {
},
"description": "A node-minecraft-protocol example"
}

View file

@ -1,8 +1,5 @@
const ProtoDef = require('protodef').ProtoDef;
var mc = require('minecraft-protocol'); var mc = require('minecraft-protocol');
var proto = new ProtoDef();
var server = mc.createServer({ var server = mc.createServer({
'online-mode': false, // optional 'online-mode': false, // optional
encryption: false, // optional encryption: false, // optional

View file

@ -0,0 +1,8 @@
{
"name": "node-minecraft-protocol-example",
"version": "0.0.0",
"private": true,
"dependencies": {
},
"description": "A node-minecraft-protocol example"
}

View file

@ -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);
});

View file

@ -11,15 +11,24 @@ module.exports = function(client, options) {
client.unregisterChannel = unregisterChannel; client.unregisterChannel = unregisterChannel;
client.writeChannel = writeChannel; 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); if (parser) proto.addType(name, parser);
channels.push(name); channels.push(name);
if (channels.length === 1) if (channels.length === 1)
client.on('custom_payload', onCustomPayload); client.on('custom_payload', onCustomPayload);
} }
function unregisterChannel(channel) { function unregisterChannel(channel, custom) {
if(custom) {
client.writeChannel("UNREGISTER",channel);
}
var index = channels.find(function(name) { var index = channels.find(function(name) {
return channel === name; return channel === name;
}); });