-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.js
77 lines (62 loc) · 1.55 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const nbt = require("prismarine-nbt");
/**
* @description Automatically equips the best tool for that bot
* @param {import("mineflayer").Bot} bot
* @param {import("prismarine-block").Block} block
*/
async function autoTool(bot, block) {
if (!block) return;
const bestTool = bestHarvestTool(bot, block);
if (!bestTool) return;
const toolInInventory = await getItem(bot, bestTool.name);
if (!toolInInventory) return;
await bot.equip(bestTool);
}
/**
*
* @param {import("mineflayer").Bot} bot
* @param {Bloimport("prismarine-block").Block} block
* @returns
*/
function bestHarvestTool(bot, block) {
const availableTools = bot.inventory.items();
const effects = bot.entity.effects;
let fastest = Number.MAX_VALUE;
let bestTool = null;
for (const tool of availableTools) {
const enchants =
tool && tool.nbt ? nbt.simplify(tool.nbt).Enchantments : [];
const digTime = block.digTime(
tool ? tool.type : null,
false,
false,
false,
enchants,
effects
);
if (digTime < fastest) {
fastest = digTime;
bestTool = tool;
}
}
return bestTool;
}
/**
*
* @param {import("mineflayer").Bot} bot
* @param {string} item
*/
async function getItem(bot, item) {
const itemInRegistry = bot.registry.itemsByName[item];
if (!itemInRegistry) return;
const ItemInInventory = bot.inventory
.items()
.find((iteme) => iteme.name === itemInRegistry.name);
if (!ItemInInventory) return null;
return ItemInInventory;
}
module.exports = {
autoTool,
bestHarvestTool,
getItem,
};