-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.js
More file actions
168 lines (139 loc) · 4.6 KB
/
wrapper.js
File metadata and controls
168 lines (139 loc) · 4.6 KB
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
const vec3 = require('vec3')
module.exports = wrapper = (bot, mcData) => {
// Creates a version of the function that returns a promise instead of using a callback
// Useful for async/await syntax
const promisifyMF = (fn)=>(
(...args)=>(
new Promise((res,rej)=>{
fn(...args, (e)=>{
e?rej(e):res()
})
})
)
)
const requestSupplies = ()=>{ bot.chat('resupply') }
const findNearestBlock = (id, random = true) => {
// TODO pass in option for which face is empty. Might be checking for a tree trunk.
// TODO pass in max distance, might need to limit based on performance
// TODO incorporate min pythagorean distance
return findBlock((block) => {
if (block && (block.type == id)) {
const blockAbove = bot.blockAt(block.position.offset(0, 1, 0))
return !blockAbove || (blockAbove.type === 0 && (!random || Math.random()>.5))//empty space above
}
return false
}, {
point: bot.entity.position,
maxDistance: 32
})
}
const findNearestGrass = ()=>(findNearestBlock(mcData.blocksByName.grass.id))
const findNearestSandstone = ()=>(findNearestBlock(mcData.blocksByName.sandstone.id))
const maxPlacementRange = 4
const distance = (v1,v2)=> (Math.sqrt(['x','y','z'].reduce((a,c)=>(a + Math.pow(v1[c] - v2[c], 2)),0)))
const findAllBlocks = (matcher,
{
point=bot.entity.position,
maxDistance=16,
maxHeight=10
}
)=>{
let ret = []
loopBlocks((found)=>{
if (matcher(found)) {
ret.push(found)
}
},point, maxDistance, maxHeight)
return ret
}
const findBlock = (matcher,
{
point=bot.entity.position,
maxDistance=16,
maxHeight=10,
}
)=>{
let nearestBlock = null
let nearestDistance = Number.MAX_VALUE
loopBlocks((found)=>{
if (matcher(found)) {
let blockDistance = distance(point, found.position)
if (blockDistance < nearestDistance) {
nearestBlock = found
nearestDistance = blockDistance
// console.log(`${bot.username}|Found closer block at: ${cursor} [${nearestDistance}]`)
}
}
}, point, maxDistance, maxHeight)
return nearestBlock
}
const loopBlocks = (func, center, distance, height)=>{
for (let x = center.x - distance; x < center.x + distance; x++) {
for (let y = center.y - height; y < center.y + height; y++) {
for (let z = center.z - distance; z < center.z + distance; z++) {
func(bot.blockAt(vec3(x,y,z)))
}
}
}
}
const placeOnTopOfTargetBlock = async (itemName, targetBlock) => {
const itemToPlace = bot.inventory.items().find((item) => (item.name === itemName))
const equipAsync = promisifyMF(bot.equip)
const placeBlockAsync = promisifyMF(bot.placeBlock)
try{
console.error('itemToPlace',itemToPlace)
await equipAsync(itemToPlace, 'hand')
console.error('placing item')
await placeBlockAsync(targetBlock, vec3(0, 1, 0))
console.log(`${bot.username}|placed ${itemToPlace.name} at: ${targetBlock.position}`)
} catch(e) {
console.error('inventory was ',bot.inventory.items())
console.log(e)
throw e
}
return
}
const printInventory= ()=> {
console.log(`${bot.username}| Inventory:`)
Object.keys(bot.inventory.items()).forEach(function (key) {
// Grass Block|grass|2 0: 64
const item = bot.inventory.items()[key];
console.log(`${item.displayName}|${item.name}|${item.type} ${item.metadata}: ${item.count}`)
})
}
const followSpeaker = (chatuser) => {
// TODO: only follow if on the same team, or no team (admins)
const target = bot.players[chatuser].entity
if (target) {
bot.navigate.to(target.position, {tooFarThreshold: 10, timeout: 2000})
}
return target
}
const stop = () => {
bot.navigate.stop();
}
return {
loopBlocks,
findAllBlocks,
findBlock,
findNearestBlock,
findNearestGrass,
findNearestSandstone,
distance,
requestSupplies,
maxPlacementRange,
sendChat: bot.chat,
onPathNotFound: (fn)=>(bot.navigate.on('cannotFind',fn)),
onPathFound: (fn)=>(bot.navigate.on('pathFound',fn)),
onArrived: (fn)=>(bot.navigate.on('arrived',fn)),
onInterrupted: (fn)=>(bot.navigate.on('interrupted',fn)),
receiveChat: (fn)=>( bot.on('chat', (usr, msg)=>{if (!(usr === bot.username)) fn(usr, msg) })),
equip: bot.equip,
walk: bot.navigate.walk,
promisifyMF,
placeOnTopOfTargetBlock,
printInventory,
followSpeaker,
stop
}
}