-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.mjs
106 lines (100 loc) · 2.67 KB
/
Helper.mjs
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
import Config from "./Config.mjs";
import * as Baritone from "./Baritone.mjs";
// init helper
const initArray = [];
export function init() {
initArray.forEach((f) => f());
initArray.length = 0;
}
export function addInit(func, first = false) {
if (first) initArray.unshift(func);
else initArray.push(func);
}
// shutdown helper
const stopObject = new Map();
event.stopListener = JavaWrapper.methodToJava(() => {
stopObject.forEach((f) => f());
});
export function addStop(id, func) {
stopObject.set(id, func);
}
export function delStop(id, skipExec = false) {
if (!stopObject.has(id)) return false;
if (!skipExec) stopObject.get(id)();
return stopObject.delete(id);
}
export function restartService() {
JsMacros.runScript(
"js",
`JsMacros.getServiceManager().restartService("${event.serviceName}")`
);
}
// log helper
export function log(text) {
const builder = Chat.createTextBuilder();
builder.append("[");
builder.withColor(0x5);
builder.append(event.serviceName);
builder.withColor(0xd);
builder.append("] ");
builder.withColor(0x5);
builder.append(text);
builder.withColor(0x7);
Chat.log(builder.build());
}
// status helper
const posibleStatus = new Map([
["idle", "Idle"],
["mine", "Mining"],
]);
let currentStatus;
export function isStatus(id) {
return currentStatus === posibleStatus.get(id);
}
export function getStatus(id = undefined) {
if (id !== undefined) return posibleStatus.get(id);
return currentStatus;
}
export function addStatus(id, status) {
posibleStatus.set(id, status);
}
let statusEvent; // BTScreenStatusChange
addInit(() => {
currentStatus = posibleStatus.get(Baritone.isActive() ? "mine" : "idle");
statusEvent = JsMacros.createCustomEvent(Config.eventName); // BTScreenStatusChange
statusEvent.registerEvent();
}, true);
export function setStatus(id) {
statusEvent.putString("oldStatus", currentStatus);
currentStatus = posibleStatus.get(id);
statusEvent.putString("status", currentStatus);
statusEvent.trigger();
}
// number helper
export function toInt(a, round) {
if (round) {
return Math.round(a);
} else {
return Math.floor(a);
}
}
export function random(range) {
return toInt(Math.random() * range, true);
}
// teleport helper
let lastTeleport = "mine";
export function tp(home) {
if (Config.home[lastTeleport] !== Config.home[home]) {
if (lastTeleport === "mine") {
Chat.say(`${Config.home.setcmd} ${Config.home.mine}`, true);
}
Chat.say(tpCommand(home), true);
Time.sleep(Config.sleep.tp);
}
lastTeleport = home;
}
export function tpCommand(home) {
return Config.home[home].startsWith("/")
? Config.home[home]
: `${Config.home.getcmd} ${Config.home[home]}`;
}