var mineflayer = require('../');

if(process.argv.length < 4 || process.argv.length > 6) {
  console.log("Usage : node sleeper.js <host> <port> [<name>] [<password>]");
  process.exit(1);
}

var bot = mineflayer.createBot({
  host: process.argv[2],
  port: parseInt(process.argv[3]),
  username: process.argv[4] ? process.argv[4] : "sleeper",
  password: process.argv[5],
  verbose: true,
});

bot.on('chat', function(username, message) {
  if(message === 'sleep') {
    var bedBlock = findBed();
    if(bedBlock) {
      bot.sleep(bedBlock, function(err) {
        if(err) {
          console.log(err);
          bot.chat("I can't sleep");
        }
        else
          bot.chat("counting sheep");
      });
    } else {
      bot.chat("no nearby bed");
    }
  } else if(message === 'wake') {
    bot.wake(function(err) {
      if(err) {
        console.log(err);
        bot.chat("I can't wake up");
      }
    });
  }
});

bot.on('sleep', function() {
  bot.chat("zzzz");
});

bot.on('wake', function() {
  bot.chat("I woke up");
});

function findBed() {
  var cursor = mineflayer.vec3();
  for(cursor.x = bot.entity.position.x - 4; cursor.x < bot.entity.position.x + 4; cursor.x++) {
    for(cursor.y = bot.entity.position.y - 4; cursor.y < bot.entity.position.y + 4; cursor.y++) {
      for(cursor.z = bot.entity.position.z - 4; cursor.z < bot.entity.position.z + 4; cursor.z++) {
        var block = bot.blockAt(cursor);
        if(block.type === 26) return block;
      }
    }
  }
}

@@ -1,12 +1,236 @@
+/*%0A * Even bots need to rest sometimes.%0A *%0A * That's why we created an example that demonstrates how easy it is%0A * to find and use a bed properly.%0A *%0A * You can ask the bot to sleep or wake up by sending a chat message.%0A */%0A
 var mineflay
@@ -797,36 +797,8 @@
 ) %7B%0A
-          console.log(err);%0A
     
@@ -826,17 +826,33 @@
 't sleep
-%22
+: %22 + err.message
 );%0A     
@@ -859,21 +859,15 @@
    %7D
-%0A       
  else
+ %7B
 %0A   
@@ -887,25 +887,32 @@
 at(%22
-counting sheep%22);
+Good night!%22);%0A        %7D
 %0A   
@@ -947,17 +947,17 @@
 t.chat(%22
-n
+N
 o nearby
@@ -1061,59 +1061,99 @@
     
-console.log(err);%0A        bot.chat(%22I can't wake up
+bot.chat(%22I can't wake up: %22 + err.message);%0A      %7D else %7B%0A        bot.chat(%22Good morning!
 %22);%0A
@@ -1310,16 +1310,54 @@
 Bed() %7B%0A
+  var position = bot.entity.position;%0A
   var cu
@@ -1382,16 +1382,29 @@
 vec3();%0A
+  var block;%0A
   for(cu
@@ -1412,27 +1412,16 @@
 sor.x = 
-bot.entity.
 position
@@ -1439,27 +1439,16 @@
 sor.x %3C 
-bot.entity.
 position
@@ -1488,27 +1488,16 @@
 sor.y = 
-bot.entity.
 position
@@ -1515,27 +1515,16 @@
 sor.y %3C 
-bot.entity.
 position
@@ -1566,27 +1566,16 @@
 sor.z = 
-bot.entity.
 position
@@ -1593,27 +1593,16 @@
 sor.z %3C 
-bot.entity.
 position
@@ -1631,20 +1631,16 @@
         
-var 
 block = 

Improve sleeper.js