-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmeinbot.js
235 lines (200 loc) · 6.53 KB
/
meinbot.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
235
#!/usr/bin/env node
//Copyright 2022 MeindoMC
//This file is licensed under the MIT license
//Version 1.4
const mf = require('mineflayer')
const pvp = require('mineflayer-pvp').plugin
const armorManager = require('mineflayer-armor-manager')
//WEBSERVERS. Will automatically disable if the [disableWeb] argument is set to true
if(process.argv[4] === "true"){
var inventoryViewer = require('mineflayer-web-inventory');
var { mineflayer: mineflayerViewer } = require('prismarine-viewer');
}
const autoeat = require("mineflayer-auto-eat")
const chalk = require("chalk")
const navigatePlugin = require('mineflayer-navigate')(mf);
const scaffoldPlugin = require('mineflayer-scaffold')(mf);
const { pathfinder, Movements, goals} = require('mineflayer-pathfinder')
const usrNameOfOwnerDC = process.argv[7] || "Steve";
//start boilerplate
const options = {
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[6] ? process.argv[7] : 'MeinBot',
password: process.argv[8],
version: process.argv[5]
}
const invOptions = {
port: 6001
}
const bot = mf.createBot(options)
if(process.argv[4] === "true"){
inventoryViewer(bot, invOptions)
bot.once('spawn', () =>{
mineflayerViewer(bot, { port: 3007, firstPerson: true })
})
}
bot.loadPlugin(pathfinder)
bot.loadPlugin(armorManager)
bot.loadPlugin(pvp)
bot.loadPlugin(autoeat)
bot
navigatePlugin(bot)
scaffoldPlugin(bot)
bot.on('spawn', () =>{
setTimeout(() =>{
bot.autoEat.options.priority = "saturation"
bot.autoEat.options.bannedFood = []
bot.autoEat.options.eatingTimeout = 2
}, 500)
})
if (process.argv.length < 4 || process.argv.length > 8) {
console.log('Usage : node meinbot.js <host> <port> [<disableWeb>] [<version>] [<name>] [<name of the owner>] [<password>]')
process.exit(1)
}
//end boilerplate
bot.on('playerCollect', (collector, itemDrop) =>{
if(collector !== bot.entity) return
setTimeout(() => {
const sword = bot.inventory.items().find(item => item.name.includes('sword'))
if(sword) bot.equip(sword, 'hand')
}, 150)
setTimeout(() =>{
const shield = bot.inventory.items().find(item => item.name.includes('shield'))
if (shield) bot.equip(shield, 'off-hand')
}, 250)
})
bot.once('resourcePack', () => {
bot.acceptResourcePack()
})
bot.on('kicked', (reason, loggedIn) => {
console.log(reason, loggedIn)
});
//Message and command handling
bot.on('chat', (username, message) => {
if(username === 'MeinBot') return
const player = bot.players[username]
switch(message){
case "meinbot":
setTimeout( () => bot.chat('MeinBot v1.4'), 100)
setTimeout( () => bot.chat('CMDS:'), 150)
setTimeout( () => bot.chat('fight'), 200)
setTimeout( () => bot.chat('guard'), 250)
setTimeout( () => bot.chat('stop'), 300)
setTimeout( () => bot.chat('dc'), 350)
setTimeout( () => bot.chat('come'), 400)
setTimeout( () => bot.chat('eat'), 450)
break;
case "meinbot come":
var target = bot.players[username].entity;
bot.chat("coming")
bot.scaffold.to(target.position, function(err) {
if (err) {
bot.chat("didn't make it: " + err.code);
}
});
break;
case "meinbot fight":
if(!player){
bot.chat("I cant see you.")
return
}
bot.chat('Okay, prepare to fight ' + player.displayName.toString())
bot.pvp.attack(player.entity)
break;
case "meinbot guard":
if(!player){
bot.chat("I cant see you.")
return
}
bot.chat("Okay, i will guard that position")
guardArea(player.entity.position)
break;
case "meinbot eat":
const prevItem = bot.heldItem
const gapple = bot.inventory.items().find(item => item.name.includes('apple'))
if(gapple){
bot.equip(gapple)
bot.autoEat.eat(function (err) {
if(err){
return
} else{
console.log(chalk.yellow('I ate an apple!'))
}
bot.equip(prevItem)
})
} else{
console.log(chalk.red("Could not find apple in inventory of player"))
}
break;
case "meinbot stop":
bot.chat("Okay, stopped")
stopGuarding()
break;
case "meinbot dc":
if(player.displayName.toString() !== usrNameOfOwnerDC) return
bot.chat("Okay, bye!")
process.exit(1)
break;
}
})
let guardPos = null
function guardArea(pos){
guardPos = pos.clone()
if(!bot.pvp.target){
moveToGuardPos()
}
}
function stopGuarding(){
guardPos = null
bot.pvp.stop()
bot.pathfinder.setGoal(null)
}
function moveToGuardPos(){
const mcData = require('minecraft-data')(bot.version)
bot.pathfinder.setMovements(new Movements(bot, mcData))
bot.pathfinder.setGoal(new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z))
}
bot.on('physicTick', () =>{
if(!guardPos) return
const filter = e => e.type === 'mob' && e.position.distanceTo(bot.entity.position) < 16 && e.mobType !== 'Armor Stand'
const entity = bot.nearestEntity(filter)
if(entity){
bot.pvp.attack(entity)
}
})
bot.on('stoppedAttacking', () => {
if(guardPos){
moveToGuardPos()
}
})
bot.on('physicTick', () => {
if(bot.pvp.target) return
if(bot.pathfinder.isMoving()) return
const entity = bot.nearestEntity()
if(entity) bot.lookAt(entity.position.offset(0, entity.height, 0))
})
bot.on('physicTick', () => {
if(bot.health < 10){
const prevItem = bot.heldItem
const gapple = bot.inventory.items().find(item => item.name.includes('apple'))
const pot = bot.inventory.items().find(item => item.name.includes('potion'))
if(gapple){
bot.equip(gapple)
bot.autoEat.eat(function (err) {
if(err){
return
} else {
console.log(chalk.yellow('I ate an apple!'))
}
bot.equip(prevItem)
})
} else if(pot){
bot.equip(pot)
bot.autoEat.eat()
bot.equip(prevItem)
} else{
console.log(chalk.red("Could not find apple or potion in inventory of player"))
}
}
})