forked from PrismarineJS/mineflayer-builder
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
234 lines (203 loc) · 7.5 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const { goals, Movements } = require('mineflayer-pathfinder')
const interactable = require('./lib/interactable.json')
function wait (ms) { return new Promise(resolve => setTimeout(resolve, ms)) }
function inject (bot) {
if (!bot.pathfinder) {
throw new Error('pathfinder must be loaded before builder')
}
let interruptBuilding = false
const mcData = require('minecraft-data')(bot.version)
const Item = require('prismarine-item')(bot.version)
const movements = new Movements(bot, mcData)
// movements.canDig = false
movements.digCost = 10
movements.maxDropDown = 3
bot.pathfinder.searchRadius = 10
bot.builder = {}
bot.builder.currentBuild = null
async function equipCreative (id) {
if (bot.inventory.items().length > 30) {
bot.chat('/clear')
await wait(1000)
const slot = bot.inventory.firstEmptyInventorySlot()
await bot.creative.setInventorySlot(slot !== null ? slot : 36, new Item(mcData.itemsByName.dirt.id, 1))
}
if (!bot.inventory.items().find(x => x.type === id)) {
const slot = bot.inventory.firstEmptyInventorySlot()
await bot.creative.setInventorySlot(slot !== null ? slot : 36, new Item(id, 1))
}
const item = bot.inventory.items().find(x => x.type === id)
await bot.equip(item, 'hand')
}
async function equipItem (id) {
if (bot.heldItem?.type === id) return
const item = bot.inventory.findInventoryItem(id, null)
if (!item) {
throw Error('no_blocks')
}
await bot.equip(item.type, 'hand')
}
async function materialCallback (item, noMaterialCallback) {
if (noMaterialCallback && typeof noMaterialCallback === 'function') {
const p = new Promise((resolve, reject) => {
try {
noMaterialCallback(item, (data) => {
resolve(data)
}, (error) => {
reject(error)
})
} catch (e) {
reject(e)
}
})
try {
await p
} catch (e) {
throw new Error(item.name)
}
}
throw new Error(item.name)
}
bot.builder.equipItem = equipItem
bot.builder.stop = function () {
console.log('Stopped building')
interruptBuilding = true
bot.builder.currentBuild = null
bot.pathfinder.setGoal(null)
}
bot.builder.pause = function () {
console.log('Paused building')
interruptBuilding = true
bot.pathfinder.setGoal(null)
}
bot.builder.continue = () => {
if (!bot.builder.currentBuild) return console.log('Nothing to continue building')
bot.builder.build(bot.builder.currentBuild)
}
// /fill ~-20 ~ ~-20 ~20 ~10 ~20 minecraft:air
bot.builder.build = async (build, noMaterialCallback, options = {}) => {
let errorNoBlocks
bot.builder.currentBuild = build
const placementRange = options.range || 3
const placementLOS = 'LOS' in options ? options.LOS : true
const materialMin = options.materialMin || 0
interruptBuilding = false
while (build.actions.length > 0) {
if (interruptBuilding) {
interruptBuilding = false
return
}
const actions = build.getAvailableActions()
console.log(`${actions.length} available actions`)
if (actions.length === 0) {
console.log('No actions to perform')
break
}
actions.sort((a, b) => {
let dA = a.pos.offset(0.5, 0.5, 0.5).distanceSquared(bot.entity.position)
dA += (a.pos.y - bot.entity.position.y) * 100
let dB = b.pos.offset(0.5, 0.5, 0.5).distanceSquared(bot.entity.position)
dB += (b.pos.y - bot.entity.position.y) * 100
return dA - dB
})
const action = actions[0]
console.log('action', action)
try {
if (action.type === 'place') {
const item = build.getItemForState(action.state)
console.log('Selecting ' + item.displayName)
const properties = build.properties[action.state]
const half = properties.half ? properties.half : properties.type
const faces = build.getPossibleDirections(action.state, action.pos)
for (const face of faces) {
const block = bot.blockAt(action.pos.plus(face))
console.log(face, action.pos.plus(face), block.name)
}
const { facing, is3D } = build.getFacing(action.state, properties.facing)
const goal = new goals.GoalPlaceBlock(action.pos, bot.world, {
faces,
facing: facing,
facing3D: is3D,
half,
range: placementRange,
LOS: placementLOS
})
if (!goal.isEnd(bot.entity.position.floored())) {
console.log('pathfinding')
bot.pathfinder.setMovements(movements)
await bot.pathfinder.goto(goal)
console.log('finished pathing')
}
try {
const amount = bot.inventory.count(id)
if (amount <= materialMin) throw Error('no_blocks')
await equipItem(item.id) // equip item after pathfinder
} catch (e) {
if (e.message === 'no_blocks') {
try {
await materialCallback(item, noMaterialCallback)
} catch (e) {
console.info('Throwing error no material')
throw Error('cancel')
}
continue
}
throw e
}
// TODO: const faceAndRef = goal.getFaceAndRef(bot.entity.position.offset(0, 1.6, 0))
const faceAndRef = goal.getFaceAndRef(bot.entity.position.floored().offset(0.5, 1.6, 0.5))
if (!faceAndRef) { throw new Error('no face and ref') }
bot.lookAt(faceAndRef.to, true)
const refBlock = bot.blockAt(faceAndRef.ref)
const sneak = interactable.indexOf(refBlock.name) > 0
const delta = faceAndRef.to.minus(faceAndRef.ref)
if (sneak) bot.setControlState('sneak', true)
await bot._placeBlockWithOptions(refBlock, faceAndRef.face.scaled(-1), { half, delta })
if (sneak) bot.setControlState('sneak', false)
// const block = bot.world.getBlock(action.pos)
const worldState = bot.world.getBlockStateId(action.pos)
// Does not work for 1.12 as blocks dont have the stateId property
if (worldState !== action.state) {
console.log('expected', properties)
console.log('got', worldState)
}
build.removeAction(action)
} else if (action.type === 'dig') {
await bot.pathfinder.goto(new goals.Goal(action.pos.x, action.pos.y, action.pos))
build.removeAction(action)
} else {
build.removeAction(action)
}
} catch (e) {
if (e?.name === 'NoPath') {
console.info('Skipping unreachable action', action)
} else if (e && (e.name === 'cancel' || e.message === 'cancel')) {
console.info('Canceling build no materials')
break
} else if (e?.message.startsWith('No block has been placed')) {
console.info('Block placement failed')
console.error(e)
continue
} else {
console.log(e?.name, e)
}
build.removeAction(action)
}
}
if (errorNoBlocks) {
const message = 'Failed to build no blocks left ' + errorNoBlocks
bot.chat(message)
bot.emit('builder_cancel', message)
} else {
bot.chat('Finished building')
setTimeout(() => {
bot.emit('builder_finished')
}, 0)
}
bot.builder.currentBuild = null
}
}
module.exports = {
Build: require('./lib/Build'),
builder: inject
}