mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-14 19:04:59 -05:00
add logic for custom channel, and corresponding examples
This commit is contained in:
parent
1dcd119d93
commit
a0717a1232
8 changed files with 101 additions and 5 deletions
8
examples/client_channel/package.json
Normal file
8
examples/client_channel/package.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "node-minecraft-protocol-example",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
},
|
||||
"description": "A node-minecraft-protocol example"
|
||||
}
|
25
examples/client_custom_channel/client_custom_channel.js
Normal file
25
examples/client_custom_channel/client_custom_channel.js
Normal 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);
|
||||
}
|
8
examples/client_custom_channel/package.json
Normal file
8
examples/client_custom_channel/package.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "node-minecraft-protocol-example",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
},
|
||||
"description": "A node-minecraft-protocol example"
|
||||
}
|
8
examples/server_channel/package.json
Normal file
8
examples/server_channel/package.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "node-minecraft-protocol-example",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
},
|
||||
"description": "A node-minecraft-protocol example"
|
||||
}
|
|
@ -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
|
||||
|
|
8
examples/server_custom_channel/package.json
Normal file
8
examples/server_custom_channel/package.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "node-minecraft-protocol-example",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
},
|
||||
"description": "A node-minecraft-protocol example"
|
||||
}
|
33
examples/server_custom_channel/server_custom_channel.js
Normal file
33
examples/server_custom_channel/server_custom_channel.js
Normal 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);
|
||||
});
|
|
@ -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;
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue