-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchestInteraction.js
119 lines (108 loc) · 3.32 KB
/
chestInteraction.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* 箱子交互模块
*
* 该模块提供了与箱子进行交互的功能。
* 主要功能包括:
* - 打开附近的箱子
* - 将物品存入箱子
* - 从箱子中取出物品
*/
const mineflayer = require('mineflayer');
/**
* 与箱子交互
* @param {Bot} bot - Mineflayer机器人实例
* @param {string} action - 交互操作:'deposit' 存入, 'withdraw' 取出, 'depositAll' 全部存入, 'withdrawAll' 全部取出
* @param {string} [itemName] - 物品名称
* @param {number} [itemCount] - 物品数量
*/
async function interactWithChest(bot, action, itemName, itemCount) {
const chestBlock = bot.findBlock({
matching: block => block.name.includes('chest'),
maxDistance: 5,
});
if (!chestBlock) {
bot.chat('附近没有箱子');
return;
}
const chest = await bot.openChest(chestBlock);
console.log(`已打开箱子: ${chestBlock.name}`);
switch (action) {
case 'depositAll':
await depositAllItems(chest);
break;
case 'withdrawAll':
await withdrawAllItems(chest);
break;
case 'deposit':
await depositItems(chest, itemName, itemCount);
break;
case 'withdraw':
await withdrawItems(chest, itemName, itemCount);
break;
default:
bot.chat(`未知操作: ${action}`);
}
chest.close();
console.log(`已关闭箱子: ${chestBlock.name}`);
}
/**
* 将所有物品存入箱子
* @param {Chest} chest - 箱子实例
*/
async function depositAllItems(chest) {
const inventory = chest.window.slots;
for (const item of inventory) {
if (item) {
await chest.deposit(item.type, null, item.count);
}
}
console.log('已将所有物品存入箱子');
}
/**
* 从箱子中取出所有物品
* @param {Chest} chest - 箱子实例
*/
async function withdrawAllItems(chest) {
const chestItems = chest.items();
for (const item of chestItems) {
await chest.withdraw(item.type, null, item.count);
}
console.log('已从箱子中取出所有物品');
}
/**
* 将指定物品存入箱子
* @param {Chest} chest - 箱子实例
* @param {string} itemName - 物品名称
* @param {number} itemCount - 物品数量
*/
async function depositItems(chest, itemName, itemCount) {
const inventory = chest.window.slots;
for (const item of inventory) {
if (item && item.name === itemName && itemCount > 0) {
const count = Math.min(item.count, itemCount);
await chest.deposit(item.type, null, count);
itemCount -= count;
}
}
console.log(`已将 ${itemName} x ${itemCount} 存入箱子`);
}
/**
* 从箱子中取出指定物品
* @param {Chest} chest - 箱子实例
* @param {string} itemName - 物品名称
* @param {number} itemCount - 物品数量
*/
async function withdrawItems(chest, itemName, itemCount) {
const chestItems = chest.items();
for (const item of chestItems) {
if (item.name === itemName && itemCount > 0) {
const count = Math.min(item.count, itemCount);
await chest.withdraw(item.type, null, count);
itemCount -= count;
}
}
console.log(`已从箱子中取出 ${itemName} x ${itemCount}`);
}
module.exports = {
interactWithChest,
};