Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to node-minecraft-protocol 0.16 (major changes including using protodef) #21

Merged
merged 22 commits into from
Jan 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d51effc
Start updating to minecraft-protocol ~0.15.0 (not ^0.16.0; incompatib…
deathcap Jan 2, 2016
a2eb761
Update to minecraft-data ~0.16.3
deathcap Jan 2, 2016
d941812
Use mineflayer exports instead of lib imports, for prismarine-block/b…
deathcap Jan 2, 2016
3e75360
Update minecraft-protocol to ~0.16.6
deathcap Jan 9, 2016
3743900
varint parsing moved to protodef
deathcap Jan 9, 2016
8466191
Packets moved to minecraft-data
deathcap Jan 9, 2016
3cc9696
Factor out MC version
deathcap Jan 9, 2016
0288d2e
Replace beefy with wzrd in demo
deathcap Jan 9, 2016
deb342e
Add global brfs browserify transform to mcwebchat example
deathcap Jan 9, 2016
2a94a9b
Update to mineflayer branch with https://github.com/PrismarineJS/mine…
deathcap Jan 12, 2016
e1832b9
Update minecraft-protocol stream for new required version argument to…
deathcap Jan 12, 2016
4c3965e
Update client event packet listeners for success, keep_alive packet n…
deathcap Jan 12, 2016
41abe97
mineflayer Bot instance variable client is now _client
deathcap Jan 12, 2016
2fbfd59
State move to minecraft-data
deathcap Jan 12, 2016
ed274b0
Use state from minecraft-protocol
deathcap Jan 13, 2016
d7f2305
Fix wsmc protocol state check in raw handler, now an object
deathcap Jan 13, 2016
492567e
Fix wsmc protodef varint types
deathcap Jan 13, 2016
44d55ac
Enable packet debugging
deathcap Jan 13, 2016
ed6fcb9
Catch failure to parse varints instead of throwing exceptions
deathcap Jan 13, 2016
0d4fa27
Crash on packet errors for now
deathcap Jan 17, 2016
cf6e6c9
Add noPacketFramer and noPacketSplitter options to exclude framing/sp…
deathcap Jan 17, 2016
fedd8df
Remove MC packet framing (length varint prepend) from WSMC protocol (…
deathcap Jan 17, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});