mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2025-05-23 03:17:21 -04:00
fix version in pluginChannels, add writeChannel, add two examples for a vanilla channel (MC|Brand)
This commit is contained in:
parent
947c4c0587
commit
1dcd119d93
3 changed files with 72 additions and 2 deletions
24
examples/client_channel/client_channel.js
Normal file
24
examples/client_channel/client_channel.js
Normal 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);
|
37
examples/server_channel/server_channel.js
Normal file
37
examples/server_channel/server_channel.js
Normal 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");
|
||||||
|
});
|
|
@ -2,14 +2,16 @@ var ProtoDef = require('protodef').ProtoDef;
|
||||||
var minecraft = require('../datatypes/minecraft');
|
var minecraft = require('../datatypes/minecraft');
|
||||||
|
|
||||||
module.exports = function(client, options) {
|
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 channels = [];
|
||||||
var proto = new ProtoDef();
|
var proto = new ProtoDef();
|
||||||
proto.addTypes(mcdata.protocol.types);
|
proto.addTypes(mcdata.protocol.types);
|
||||||
proto.addTypes(minecraft);
|
proto.addTypes(minecraft);
|
||||||
client.registerChannel = registerChannel;
|
client.registerChannel = registerChannel;
|
||||||
client.unregisterChannel = unregisterChannel;
|
client.unregisterChannel = unregisterChannel;
|
||||||
return client;
|
client.writeChannel = writeChannel;
|
||||||
|
|
||||||
|
|
||||||
function registerChannel(name, parser) {
|
function registerChannel(name, parser) {
|
||||||
if (parser) proto.addType(name, parser);
|
if (parser) proto.addType(name, parser);
|
||||||
channels.push(name);
|
channels.push(name);
|
||||||
|
@ -39,5 +41,12 @@ module.exports = function(client, options) {
|
||||||
client.emit(channel, packet.data);
|
client.emit(channel, packet.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function writeChannel(channel,params) {
|
||||||
|
client.write("custom_payload",{
|
||||||
|
channel:channel,
|
||||||
|
data:proto.createPacketBuffer(channel,params)
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue