From a55d2bc42cb04fd69b4cf6b0856e15b0d55d90cc Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sun, 12 Mar 2017 12:23:13 +0100 Subject: [PATCH] produce a decent error when connecting with the wrong version --- src/client/versionChecking.js | 11 +++++++++++ src/createClient.js | 2 ++ test/clientTest.js | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 src/client/versionChecking.js diff --git a/src/client/versionChecking.js b/src/client/versionChecking.js new file mode 100644 index 0000000..090ae00 --- /dev/null +++ b/src/client/versionChecking.js @@ -0,0 +1,11 @@ +module.exports = function(client,options) { + client.on("disconnect",message => { + if(!message.reason) + return; + const versionRequired=/Outdated client! Please use (.+)/.exec(JSON.parse(message.reason).text); + if(!versionRequired) + return; + client.emit("error",new Error("This server is version "+versionRequired[1]+ + ", you are using version "+client.version+", please specify the correct version in the options.")) + }) +}; diff --git a/src/createClient.js b/src/createClient.js index f16f8d8..faaf635 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -12,6 +12,7 @@ const play = require('./client/play'); const tcp_dns = require('./client/tcp_dns'); const autoVersion = require('./client/autoVersion'); const pluginChannels = require('./client/pluginChannels'); +const versionChecking = require('./client/versionChecking'); module.exports=createClient; @@ -38,6 +39,7 @@ function createClient(options) { play(client, options); compress(client, options); pluginChannels(client, options); + versionChecking(client,options); return client; } diff --git a/test/clientTest.js b/test/clientTest.js index 4f5fe60..575705b 100644 --- a/test/clientTest.js +++ b/test/clientTest.js @@ -221,5 +221,29 @@ mc.supportedVersions.forEach(function(supportedVersion) { }); }); }); + + it("produce a decent error when connecting with the wrong version",function(done) { + wrap.startServer({ + 'online-mode': 'false', + 'server-port':PORT + }, function(err) { + if(err) + return done(err); + var client = mc.createClient({ + username: 'Player', + version: version.minecraftVersion=="1.8.8" ? "1.11.2" : "1.8.8", + port:PORT + }); + client.once("error",function(err) { + if(err.message.startsWith("This server is version")) { + console.log("Correctly got an error for wrong version : "+err.message); + done(); + } + else { + done(err); + } + }); + }) + }) }); });