-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
122 lines (106 loc) · 3.02 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
const mineflayer = require('mineflayer')
const antiAfk = require('./lib/antiafk');
const cmd = require('mineflayer-cmd').plugin
const fs = require('fs');
const server = require('server');
const { get, post } = server.router;
let rawdata = fs.readFileSync('config.json');
let data = JSON.parse(rawdata);
var lasttime = -1;
var moving = 0;
var first = false;
var connected = 0;
var actions = ['forward', 'back', 'left', 'right']
var lastaction;
var pi = 3.14159;
var moveinterval = 2; // 2 second movement interval
var maxrandom = 5; // 0-5 seconds added to movement interval (randomly)
var host = data["ip"];
var username = data["name"]
var nightskip = data["auto-night-skip"]
var options = {
host: host,
port: data["port"],
username: username
};
var bot = mineflayer.createBot(options);
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
server([
get('/', ctx => 'Hello world!')
]);
bindEvents(bot)
function bindEvents(bot) {
bot.loadPlugin(cmd)
bot.once('login', () => {
console.log("Trying to login")
bot.chat('/login adns_050721')
console.log("Logged In")
});
bot.on('time', function(time) {
if (nightskip == "true") {
if (bot.time.timeOfDay >= 13000) {
bot.chat('/time set day')
}
}
if (connected < 1) {
return;
}
if (lasttime < 0) {
lasttime = bot.time.age;
} else {
var randomadd = Math.random() * maxrandom * 20;
var interval = moveinterval * 20 + randomadd;
if (bot.time.age - lasttime > interval) {
if (moving == 1) {
bot.setControlState(lastaction, false);
moving = 0;
lasttime = bot.time.age;
} else {
var yaw = Math.random() * pi - (0.5 * pi);
var pitch = Math.random() * pi - (0.5 * pi);
bot.look(yaw, pitch, false);
lastaction = actions[Math.floor(Math.random() * actions.length)];
bot.setControlState(lastaction, true);
moving = 1;
lasttime = bot.time.age;
bot.activateItem();
}
}
}
});
bot.on('spawn', function() {
connected = 1;
bot.loadPlugin(antiAfk);
bot.afk.start();
});
bot.on('death', function() {
bot.emit("respawn")
setTimeout(backondeath, 1200);
});
bot.on('kicked', function(reason) {
console.log("Kicked for ", reason);
});
bot.on('error', function(err) {
console.log('Error attempting to reconnect: ' + err.errno + '.');
if (err.code == undefined) {
console.log('Invalid credentials OR bot needs to wait because it relogged too quickly.');
console.log('Will retry to connect in 30 seconds. ');
setTimeout(relog, 30000);
}
});
bot.on('end', function() {
console.log("Bot has ended");
// If set less than 30s you will get an invalid credentials error, which we handle above.
setTimeout(relog, 30000);
});
}
function backondeath() {
bot.chat('/back')
}
function relog() {
console.log("Attempting to reconnect...");
bot = mineflayer.createBot(options);
bindEvents(bot);
}