Skip to content

Commit

Permalink
Merge pull request #21 from deathcap/nmp0.16
Browse files Browse the repository at this point in the history
Update to node-minecraft-protocol 0.16
  • Loading branch information
deathcap committed Jan 17, 2016
2 parents 57ceb6a + fedd8df commit c44de84
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 57 deletions.
6 changes: 4 additions & 2 deletions examples/mcwebchat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
"tellraw2dom": "^0.0.4"
},
"devDependencies": {
"beefy": "^2.0.2"
"brfs": "^1.4.2",
"browserify": "^13.0.0",
"wzrd": "^1.3.1"
},
"scripts": {
"start": "beefy mcwebchat.js:bundle.js"
"start": "wzrd mcwebchat.js:bundle.js"
},
"license": "MIT"
}
1 change: 1 addition & 0 deletions mcversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = '1.8';
21 changes: 17 additions & 4 deletions minecraft-protocol-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

//process.env.NODE_DEBUG = 'mc-proto'; // for node-minecraft-protocol console packet debugging TODO: envify

var EmptyTransformStream = require('through')();
var Client = require('minecraft-protocol').Client;
var protocol = require('minecraft-protocol').protocol;
var protocol = require('minecraft-protocol');
var assert = require('assert');
var states = protocol.states;

Expand All @@ -21,13 +22,25 @@ function createClient(options) {
assert.ok(options.username, "username is required");
var keepAlive = options.keepAlive == null ? true : options.keepAlive;

var optVersion = options.version || require('./mcversion.js');
var mcData = require('minecraft-data')(optVersion);
var version = mcData.version;

var client = new Client(false, version.majorVersion);

// Options to opt-out of MC protocol packet framing (useful since WS is alreay framed)
if (options.noPacketFramer) {
client.framer = EmptyTransformStream;
}
if (options.noPacketSplitter) {
client.splitter = EmptyTransformStream;
}

var client = new Client(false);
client.on('connect', onConnect);
client.once([states.LOGIN, 0x02], onLogin);
client.once('success', onLogin);
client.once('compress', onCompressionRequest);
client.once('set_compression', onCompressionRequest);
if (keepAlive) client.on([states.PLAY, 0x00], onKeepAlive);
if (keepAlive) client.on('keep_alive', onKeepAlive);

client.username = options.username;
client.setSocket(stream);
Expand Down
42 changes: 21 additions & 21 deletions mineflayer-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ if (PACKET_DEBUG) global.hex = hex;
module.exports = {
//vec3: require('vec3'), // not really needed
createBot: createBot,
Block: require('mineflayer/lib/block'),
Location: require('mineflayer/lib/location'),
Biome: require('mineflayer/lib/biome'),
Entity: require('mineflayer/lib/entity'),
Painting: require('mineflayer/lib/painting'),
Item: require('mineflayer/lib/item'),
Recipe: require('mineflayer/lib/recipe'),
windows: require('mineflayer/lib/windows'),
Chest: require('mineflayer/lib/chest'),
Furnace: require('mineflayer/lib/furnace'),
Dispenser: require('mineflayer/lib/dispenser'),
EnchantmentTable: require('mineflayer/lib/enchantment_table'),
Block: require('mineflayer').Block,
Location: require('mineflayer').Location,
Biome: require('mineflayer').Biome,
Entity: require('mineflayer').Entity,
Painting: require('mineflayer').Painting,
Item: require('mineflayer').Item,
Recipe: require('mineflayer').Recipe,
windows: require('mineflayer').windows,
Chest: require('mineflayer').Chest,
Furnace: require('mineflayer').Furnace,
Dispenser: require('mineflayer').Dispenser,
EnchantmentTable: require('mineflayer').EnchantmentTable,
blocks: mcData.blocks,
biomes: mcData.biomes,
items: mcData.items,
Expand All @@ -69,30 +69,30 @@ function createBot(options) {

function Bot() {
EventEmitter.call(this);
this.client = null;
this._client = null;
}
util.inherits(Bot, EventEmitter);

Bot.prototype.connect = function(options) {
var self = this;
self.client = mc.createClient(options);
self.username = self.client.username;
self.client.on('raw', function(raw) {
self._client = mc.createClient(options);
self.username = self._client.username;
self._client.on('raw', function(raw) {
if (PACKET_DEBUG) {
console.log('received ',raw.length+' raw bytes');
console.log(hex(raw));
}
});
self.client.on('session', function() {
self.username = self.client.username;
self._client.on('session', function() {
self.username = self._client.username;
});
self.client.on('connect', function() {
self._client.on('connect', function() {
self.emit('connect');
});
self.client.on('error', function(err) {
self._client.on('error', function(err) {
self.emit('error', err);
});
self.client.on('end', function() {
self._client.on('end', function() {
self.emit('end');
});
for (var pluginName in plugins) {
Expand Down
2 changes: 2 additions & 0 deletions mineflayer-ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ function createBot(options) {
var path = options.path || 'server';
var url = options.url || (protocol + '://' + host + ':' + port + '/' + path);

options.noPacketFramer = true;
//options.noPacketSplitter = true;
options.stream = websocket_stream(url);

return mineflayer.createBot(options);
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
"dependencies": {
"browser-hex": "^1.0.0",
"hex": "^0.1.0",
"minecraft-data": "0.0.1",
"minecraft-protocol": "git://github.com/prismarinejs/node-minecraft-protocol.git#master",
"mineflayer": "git://github.com/andrewrk/mineflayer.git#master",
"minecraft-data": "~0.16.3",
"minecraft-protocol": "~0.16.6",
"mineflayer": "deathcap/mineflayer#update-browser",
"optimist": "^0.6.1",
"protodef": "^0.2.5",
"through": "^2.3.8",
"websocket-stream": "^1.4.0",
"ws": "^0.4.31"
},
Expand Down
54 changes: 27 additions & 27 deletions wsmc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';

var mcversion = require('./mcversion.js');
var minecraft_protocol = require('minecraft-protocol');
var readVarInt = minecraft_protocol.protocol.types.varint[0];
var writeVarInt = minecraft_protocol.protocol.types.varint[1];
var sizeOfVarInt = minecraft_protocol.protocol.types.varint[2];
var minecraft_data = require('minecraft-data')(mcversion);
var protodef = require('protodef');
var readVarInt = protodef.types.varint[0];
var writeVarInt = protodef.types.varint[1];
var sizeOfVarInt = protodef.types.varint[2];
var hex = require('hex');
var WebSocketServer = (require('ws')).Server;
var websocket_stream = require('websocket-stream');
Expand All @@ -15,13 +18,12 @@ var argv = (require('optimist'))
.default('prefix', 'webuser-')
.argv;

var PACKET_DEBUG = false;
var PACKET_DEBUG = true;

console.log('WS('+argv.wshost+':'+argv.wsport+') <--> MC('+argv.mchost+':'+argv.mcport+')');

var states = minecraft_protocol.protocol.states;
var ids = minecraft_protocol.protocol.packetIds.play.toClient;
var sids = minecraft_protocol.protocol.packetIds.play.toServer;
var ids = minecraft_data.protocol.states.play.toClient;
var sids = minecraft_data.protocol.states.play.toServer;


var userIndex = 1;
Expand Down Expand Up @@ -68,14 +70,16 @@ wss.on('connection', function(new_websocket_connection) {
});


mc.on('raw', function(buffer, state) {
mc.on('raw', function(buffer, packet_state) {
var state = packet_state.state;
if (PACKET_DEBUG) {
console.log('mc received '+buffer.length+' bytes');
hex(buffer);
}

if (state !== 'play' && state !== 'login') {
console.log('Skipping state '+state+' packet: ',buffer);
console.log(state);
return;
}

Expand Down Expand Up @@ -142,29 +146,25 @@ wss.on('connection', function(new_websocket_connection) {

//console.log "websocket received '+raw.length+' bytes: '+JSON.stringify(raw));

try {
// strip length prefix then writeRaw(), let it add length, compression, etc.
// TODO: remove vestigal MC length from WS protocol
var lengthFieldSize = readVarInt(raw, 0).size;

var uncompressedLengthFieldSize;
if (compressionThreshold > -2) {
uncompressedLengthFieldSize = readVarInt(raw, lengthFieldSize).size; // the compressed packet format uncompressed data length
} else {
uncompressedLengthFieldSize = 0;
var uncompressedLengthFieldSize;
if (compressionThreshold > -2) {
var uncompressedLengthField = readVarInt(raw, 0);
if (!uncompressedLengthField) {
throw new Error('Failed to parse varint uncompressed length in raw buffer from ws:', raw);
}

var rawWithoutLength = raw.slice(lengthFieldSize + uncompressedLengthFieldSize);
uncompressedLengthFieldSize = uncompressedLengthField.size; // the compressed packet format uncompressed data length
} else {
uncompressedLengthFieldSize = 0;
}

var rawWithoutLength = raw.slice(uncompressedLengthFieldSize);

if (PACKET_DEBUG) {
console.log('forwarding ws -> mc: '+rawWithoutLength.length+' bytes');
hex(rawWithoutLength);
}
mc.writeRaw(rawWithoutLength);
} catch (e) {
console.log('error in mc.writeRaw:',e);
mc.socket.end();
if (PACKET_DEBUG) {
console.log('forwarding ws -> mc: '+rawWithoutLength.length+' bytes');
hex(rawWithoutLength);
}
mc.writeRaw(rawWithoutLength);
});
});

0 comments on commit c44de84

Please sign in to comment.