Skip to content

Instantly share code, notes, and snippets.

@MrOrz
Created December 19, 2013 16:20
Show Gist options
  • Save MrOrz/8041939 to your computer and use it in GitHub Desktop.
Save MrOrz/8041939 to your computer and use it in GitHub Desktop.
The script drive the bot to dig snow on start, and starts crafting snow bricks when the iron shovel is ran out, or when there is going to be full in the bot's inventory.
var mineflayer = require('mineflayer'),
repl = require("repl");
// [EDIT ME] Snow block coordinate
var SNOW_X = -516, SNOW_Y = 71, SNOW_Z = -245;
var SNOW_BRICK = 80, SNOW_BLOCK_COUNT = 4,
STONE_SHOVEL = 273, WOOD_SHOVEL = 269, IRON_SHOVEL = 256,
SNOW_POINT, SNOW_BLOCK; // Set after bot user spawns.
// [EDIT ME]
var bot = mineflayer.createBot({
host: "<server IP>",
username: "<bot name>"
});
var exitExecution = function(){
console.log( "Execution ended. Entering REPL." );
var terminal = repl.start({useColors: true, useGlobal: true});
terminal.context.bot = bot;
}
var craftErrorCount = 0;
var craftSnowBrick = function(){
var recipes = bot.recipesFor(SNOW_BRICK, null, SNOW_BLOCK_COUNT);
console.log( 'recipies found:', recipes );
if(recipes.length){
bot.craft(recipes[0], SNOW_BLOCK_COUNT, null, function(err){
if(err){
console.error(craftErrorCount, "error crafting.");
craftErrorCount += 1;
// if(craftErrorCount >= 3){
// console.error(err.stack);
// return exitExecution();
// }
}else{
craftErrorCount = 0; // reset count
console.log(SNOW_BLOCK_COUNT + "Brick crafted.");
}
setTimeout(craftSnowBrick,100); // Tossing bug
});
}else{
console.log("No more crafting recipies.");
exitExecution();
}
};
var doDigging = function(){
// Do digging
//
bot.dig(SNOW_BLOCK, function(){
console.log( 'dig complete' );
if(bot.inventory.emptySlotCount() > 2){
// console.log('Should get another dig');
digSnow();
}else{
console.log("Empty slot is low. Start crafting.");
// exitExecution();
craftSnowBrick();
}
});
};
var digSnow = function(){
// Check shovel status
//
console.log('Tool in hand:', bot.heldItem);
if(!( bot.heldItem && bot.heldItem.type === IRON_SHOVEL)){
var shovel = bot.inventory.findInventoryItem(IRON_SHOVEL);
if(!shovel){
console.log("No shovel left.");
return exitExecution();
}else{
bot.equip(shovel, 'hand', function(err){
if(err) throw err;
console.log("Equipped with a new shovel.");
doDigging();
})
}
}else{
doDigging();
}
};
bot.on('spawn', function(){
// Set the constants.
//
SNOW_POINT = mineflayer.vec3([SNOW_X, SNOW_Y, SNOW_Z]);
SNOW_BLOCK = bot.blockAt(SNOW_POINT);
console.log(SNOW_BLOCK);
// Dig the snow.
//
digSnow();
craftSnowBrick();
// bot.lookAt(SNOW_POINT);
// var recipe = bot.recipesFor(SNOW_BRICK);
// bot.craft(recipe[0], null, null);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment