-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvention.js
More file actions
211 lines (176 loc) · 4.88 KB
/
convention.js
File metadata and controls
211 lines (176 loc) · 4.88 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
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
lodash = require('lodash')
let bot
function load(botclass) {
bot = botclass
bot.on('physicTick', miningTick)
bot.hasFood = hasFood
bot.eat = eat
bot.getPlayer = getPlayer
bot.mountNearest = mountNearest
bot.locateBlock = locateBlock
bot.mineBlock = mineBlock
bot.mineBlockAt = mineBlockAt
bot.stopMining = stopMining
}
// Eating
function hasFood() {
/*
Returns either there is or not
available food in the inventory
*/
const mcData = require('minecraft-data')(bot.version)
var data = mcData.foodsArray
var names = data.map((item) => item.name)
var found_food = bot.inventory
.items()
.filter((item) => names.includes(item.name))
if (found_food.length === 0 || !found_food) {
return false
}
return true
}
function eat() {
/*
From github.com/LINKdiscordd/mineflayer-auto-eat
MIT License
Copyright (c) 2020 Link
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
Returns either it found food in the inventory
or not (bool)
*/
const mcData = require('minecraft-data')(bot.version)
var data = mcData.foodsArray
var names = data.map((item) => item.name)
var foodps = data.map((item) => item.foodPoints)
var found_food = bot.inventory
.items()
.filter((item) => names.includes(item.name))
if (found_food.length === 0 || !found_food) {
return false
}
var available_food = []
bot.inventory.items().forEach((element) => {
if (names.includes(element.name)) {
element.foodPoints = foodps[names.indexOf(element.name)] // item does not contain foodPoints by default
available_food.push(element)
}
})
var best_food
best_food = lodash.maxBy(available_food, 'foodPoints')
if (best_food) {
/*
Passing food item to main hand
and activating the item from
main hand (which is food)
*/
bot.substate = 'eating'
//console.log(best_food)
//console.log(available_food)
bot.equip(best_food, 'hand', function () {
bot.consume(function () {
bot.substate = 'none'
})
})
return true
}
return false
}
function getPlayer(username) {
/*
Returns the player object for the
given username, or null if it
can't find the player
*/
player = bot.players[username]
if (player) return player
return null
}
function locateBlock(name) {
/*
Returns position for the nearest
block of that kind
*/
blocks = bot.findBlockSync({
point: bot.entity.position,
matching: name,
maxDistance: 128,
count: 1
})
if (blocks.length > 0) {
return blocks[0].position
}
return null
}
var mining = false
var miningCount = 0
var miningName = null
function miningTick() {
if (mining) bot.lookAt(mining)
if (mining && !bot.targetDigBlock && bot.canDigBlock(bot.blockAt(mining))) {
function finishedMining(err) {
bot.stopMoving()
mining = false
miningCount -= 1
}
target = bot.blockAt(mining)
bot.tool.equipForBlock(target, {}, () => {
bot.substate = 'digging'
bot.dig(target, finishedMining)
})
}
else if (!mining && miningName && miningCount > 0) {
position = bot.locateBlock(miningName)
if (position) {
bot.mineBlockAt(position)
}
else {
miningName = null
}
}
}
function mineBlockAt(position) {
mining = position
bot.moveTo(position)
bot.substate = 'moving'
}
function mineBlock(name, count = 1) {
bot.state = 'mining'
miningCount = count
miningName = name
}
function stopMining() {
mining = false
miningCount = 0
miningName = null
bot.stopMoving()
bot.stopDigging()
if (bot.state === 'mining') {
bot.state = 'idle'
bot.substate = 'none'
}
}
// Vehicle
// TODO: Vehicle movement
function mountNearest() {
/*
Mounts the nearest vehicle
*/
mountable = ['boat', 'minecart', 'horse', 'donkey']
const filter = e => e.name && mountable.includes(e.name)
const entity = bot.nearestEntity(filter)
if (entity) {
bot.mount(entity)
return true
}
return false
}
module.exports = {
load: load,
}