Skip to content

Instantly share code, notes, and snippets.

@AStevensTaylor
Created July 24, 2012 14:45
Show Gist options
  • Save AStevensTaylor/3170373 to your computer and use it in GitHub Desktop.
Save AStevensTaylor/3170373 to your computer and use it in GitHub Desktop.
Node.js Minecraft Server interface.
/*
===NOTE===
Not working on any of the snapshots that include encryption (as of 12w18a). Simply put, I am hoping to chat to @dinnerbone about how this encryption works, but it won't be ready in time for 1.3.
*/
//Setup variables
var sPort = 25565; //The port you wish to broadcast on (Default: 25565)
var sName = 'MC-Server'; //Also known as the MOTD
var sKickMessage = 'This Server is currently down for maintenance'; //The message sent when they get kicked
var sMaxSlots = '100'; //The max slots shown after the ping (A value below 1 will be shown as '???' in client)
var sUsedSlots = '0'; //The current used slots shown after the ping
/*
==============
===LISCENCE===
==============
This software ('MC.js', v1.00) is written by Ahren Stevens-Taylor (a.stevenstaylor@me.com).
This software uses the protocol for Minecraft (of MojangAB) version 1.2.5 as published on 'http://mc.kev009.com/Protocol'
I am not responsible or liable for this wiki page, nor any of the damages caused by this software. The resposiblity of this
software lies with those who use it to cause damage.
I am releasing this software for educational use, so others can learn about the protocols behind Minecraft servers.
===USAGE===
Allowed usage is as follows, without further consent from the author (further consent to be granted via email).
- Dissasembly for other educational scripts
- Live usage on servers where the official minecraft server is not operational.
- STRICTLY NO WEAPONISATION!!
*/
//Setup dependencies
var net = require('net');
var readline = require('readline');
//Readline
var rl = readline.createInterface({input: process.stdin, output: process.stdout});
rl.on('line', function(cmd){
if(cmd.search('exit') != -1){
process.exit()
}
})
//Setup Socket server
var server = net.createServer(connectionListener);
console.log('Server Started');
server.listen(sPort);
console.log('Server Listening on port: '.concat(sPort));
function connectionListener(con){
con.on('data', function(data){onData(data, con);})
console.log(String('Connected to: ').concat(con.remoteAddress));
}
function onData(data, socket){
switch (data.toString('hex').substr(0,2)){
case 'fe':
socket.write(bufferTrim(new Buffer(String.fromCharCode(0xFF).concat(String.fromCharCode(sName.length + sMaxSlots.length + sUsedSlots.length + 2)).concat(sName).concat(String.fromCharCode(0xA7)).concat(sUsedSlots).concat(String.fromCharCode(0xA7)).concat(sMaxSlots), 'ucs2'), 1));
break;
case '01':
socket.write(bufferTrim(new Buffer(String.fromCharCode(0xFF).concat(String.fromCharCode(sKickMessage.length)).concat(sKickMessage), 'ucs2'), 1));
break;
case '02':
socket.write(bufferTrim(new Buffer(String.fromCharCode(0x02).concat(String.fromCharCode(1)).concat('-'), 'ucs2'), 1));
break;
}
}
function bufferTrim(buf, trm){
var len = buf.length;
return buf.slice(0, len-trm);
}
@haykam821
Copy link

@NihatJS I assume it's not working because it was made 6 years prior to your comment, and 8 major versions of Minecraft ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment