Skip to content

Instantly share code, notes, and snippets.

@jimmyken793
Forked from MrOrz/snow.js
Last active December 31, 2015 20:59
Show Gist options
  • Save jimmyken793/8043450 to your computer and use it in GitHub Desktop.
Save jimmyken793/8043450 to your computer and use it in GitHub Desktop.
var mineflayer = require('mineflayer');
var bot = mineflayer.createBot({
host: "mobile.csie.ntu.edu.tw",
username: "jomican_bot"
});
bot.on('chat', function(username, message) {
var words, name, item, amount, destination;
if (message === 'debug') {
console.log(bot.inventory);
} else if (message === 'list') {
listInventory();
} else if (message === 'look') {
lookAt(username);
} else if (/^toss (\d+) /.test(message)) {
words = message.split(" ");
amount = parseInt(words[1], 10);
name = words[2];
toss(name, amount);
} else if (/^toss /.test(message)) {
words = message.split(" ");
name = words[1];
toss(name);
} else if (/^equip /.test(message)) {
words = message.split(" ");
destination = words[1];
name = words[2];
equip(name, destination);
} else if (/^unequip /.test(message)) {
words = message.split(" ");
destination = words[1];
unequip(destination);
} else if (/^craft (\d+)/.test(message)) {
words = message.split(" ");
amount = parseInt(words[1], 10);
name = words[2];
craft(name, amount);
} else if (message === "makesnow") {
craft("snowBlock", 100);
} else if (message === "use") {
bot.chat("activating");
bot.activateItem();
} else if (message === "stop") {
bot.chat("stopping");
bot.clearControlStates();
bot.deactivateItem();
} else if (message === "dig") {
digSnow();
} else if (message === "food") {
bot.chat("food: " + bot.food);
} else if (message === "jump") {
bot.setControlState('jump', true);
} else if (message === "health") {
bot.chat("health: " + bot.health);
} else if (message === "difficulty") {
bot.chat("difficulty: " + bot.game.difficulty);
}
});
function listInventory() {
bot.chat(bot.inventory.items().map(itemStr).join(", "));
}
function itemStr(item) {
if (item) {
return item.name + " x " + item.count;
} else {
return "(nothing)";
}
}
function itemByName(name) {
return bot.inventory.items().filter(function(item) {
return item.name === name;
})[0];
}
function findItemType(name) {
var id;
for (id in mineflayer.items) {
var item = mineflayer.items[id];
if (item.name === name) return item;
}
for (id in mineflayer.blocks) {
var block = mineflayer.blocks[id];
if (block.name === name) return block;
}
return null;
}
function lookAt(username) {
var entity = bot.players[username].entity;
bot.lookAt(entity.position.offset(0, entity.height, 0));
}
function equip(name, destination) {
item = itemByName(name);
if (item) {
bot.equip(item, destination, function(err) {
if (err) {
bot.chat("unable to equip " + item.name);
console.error(err.stack);
} else {
bot.chat("equipped " + item.name);
}
});
} else {
bot.chat("I have no " + name);
}
}
function unequip(destination) {
bot.unequip(destination, function(err) {
if (err) {
bot.chat("unable to unequip");
console.error(err.stack);
} else {
bot.chat("unequipped");
}
});
}
function toss(name, amount) {
if (amount === undefined) {
item = itemByName(name);
if (item) {
bot.tossStack(item, function(err) {
if (err) {
bot.chat("unable to toss " + item.name);
console.error(err.stack);
} else {
bot.chat("tossed " + item.name);
}
});
} else {
bot.chat("I have no " + name);
}
} else {
item = itemByName(name);
if (item) {
bot.toss(item.type, null, amount, function(err) {
if (err) {
bot.chat("unable to toss " + item.name);
console.error(err.stack);
} else {
bot.chat("tossed " + amount + " " + item.name);
}
});
} else {
bot.chat("I have no " + name);
}
}
}
function craft(name, amount, callback) {
var item = findItemType(name);
var craftingTable = findCraftingTable();
var wbText = craftingTable ? "with a crafting table, " : "without a crafting table, ";
if (item == null) {
console.log(wbText + "unknown item: " + name);
} else {
var recipes = bot.recipesFor(item.id, null, 1, craftingTable);
if (recipes.length) {
console.log(wbText + "I can make " + item.name);
bot.craft(recipes[0], amount, craftingTable, function(err) {
if (err) {
console.log("error making " + item.name);
console.error(err.stack);
} else {
console.log("did the recipe for " + item.name + " " + amount + "times");
}
if (callback !== undefined) {
callback();
}
});
} else {
console.log(wbText + "I can't make " + item.name);
if (callback !== undefined) {
callback();
}
}
}
}
function doDigging() {
// Do digging
//
block = nearestBlock("snow");
if (block !== undefined) {
bot.dig(block, function() {
console.log('dig complete,', countItem('snowball'), "snow balls harvested");
setTimeout(digSnow, 1000);
});
} else {
bot.chat("Snow not found.");
setTimeout(digSnow, 100);
}
};
function nearestBlock(name) {
item = findItemType(name);
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 == item.id) return block;
}
}
}
}
function countItem(name) {
item = findItemType(name);
return bot.inventory.items().filter(function(i) {
return i.type == item.id
}).map(function(i) {
return i.count
}).reduce(function(p, c) {
return p + c
}, 0);
}
function digSnow() {
if (countItem('snowball') >= 16) {
console.log(countItem('snowball'), "snow balls, making block");
setTimeout(function() {
craft("snowBlock", 4, function() {
setTimeout(digSnow, 1000)
});
}, 1000);
} else if (countItem('snowBlock') >= 64) {
setTimeout(function() {
saveToChest("snowBlock", countItem('snowBlock'), digSnow);
}, 100);
} else {
// Check shovel status
//
iron_shovel = findItemType("shovelIron");
// console.log('Tool in hand:', bot.heldItem);
// console.log(iron_shovel.id);
if (!(bot.heldItem && bot.heldItem.type === iron_shovel.id)) {
var shovel = bot.inventory.findInventoryItem(iron_shovel.id);
if (!shovel) {
bot.chat("No shovel left.");
} else {
bot.equip(shovel, 'hand', function(err) {
if (err) throw err;
console.log("Equipped with a new shovel.");
doDigging();
})
}
} else {
doDigging();
}
}
};
bot.on('message', function(message, jsonMsg) {
console.log(message);
if (message.text == "§6若想接受傳送,輸入 §4/tpaccept§6") {
bot.chat("/tpaccept");
}
});
bot.on('spawn', function() {
setTimeout(digSnow, 1500);
});
function findChest() {
return findBlock([54, 130]);
}
function findBlock(listOfIds) {
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 - 2; 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 (listOfIds.indexOf(block.type) >= 0) {
console.log(cursor);
return block;
}
}
}
}
}
function saveToChest(name, amount, callback) {
console.log("saving to chest");
var chestBlock = findChest();
if (!chestBlock) {
bot.chat("no chest found");
callback();
return;
}
var chest = bot.openChest(chestBlock);
chest.on('open', function() {
console.log("chest opened");
setTimeout(function() {
chest.deposit(findItemType(name).id, null, amount, function(err) {
if (err) {
console.log("unable to deposit " + amount + " " + name, err);
} else {
console.log("deposited " + amount + " " + name);
}
chest.close();
});
}, 1000);
});
chest.on('close', function() {
console.log("chest closed");
callback();
});
}
function findCraftingTable() {
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 === 58) return block;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment