-
Notifications
You must be signed in to change notification settings - Fork 2
/
macros.js
140 lines (111 loc) · 3.59 KB
/
macros.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
/// api_version=2
var script = registerScript({
name: "Macros",
version: "2.0.0",
authors: ["Senk Ju"]
});
var File = Java.type("java.io.File");
var FileReader = Java.type("java.io.FileReader");
var BufferedReader = Java.type("java.io.BufferedReader");
var FileWriter = Java.type("java.io.FileWriter");
var BufferedWriter = Java.type("java.io.BufferedWriter");
var Keyboard = Java.type("org.lwjgl.input.Keyboard");
var LiquidBounce = Java.type("net.ccbluex.liquidbounce.LiquidBounce");
var macros = {};
var prefix = LiquidBounce.commandManager.getPrefix();
function chatSyntax(message) {
Chat.print("§8[§9§lMacros§8] §3Syntax: §7" + prefix + message);
}
function chatText(message) {
Chat.print("§8[§9§lMacros§8] §3" + message);
}
function readFile(filePath) {
try {
var file = new File(filePath);
var reader = new BufferedReader(new FileReader(file));
var content = [];
var line;
while ((line = reader.readLine()) !== null) {
content.push(line);
}
return content;
} catch (err) { }
}
function writeFile(path, string) {
try {
writer = new BufferedWriter(new FileWriter(path));
writer.write(string);
writer.close();
} catch (err) { }
}
function saveMacros() {
var macrosJson = JSON.stringify(macros);
writeFile(mc.mcDataDir + "/lb-macros.json", macrosJson);
}
function loadMacros() {
macros = JSON.parse(readFile(mc.mcDataDir + "/lb-macros.json").join(""));
}
function joinArray(array, startIndex) {
var joinedString = "";
for (var i = startIndex; i < array.length; i++) {
joinedString += array[i] + " ";
}
return joinedString.trim();
}
script.registerModule({
name: "Macros",
description: "Allows you to bind modules to keys.",
category: "Misc"
}, function (module) {
module.on("key", function (event) {
var key = event.getKey();
if (macros[key]) {
commandManager.executeCommands(prefix + macros[key]);
}
});
});
script.registerCommand({
name: "macro",
aliases: ["macros"]
}, function (command) {
command.on("execute", function (args) {
if (args.length <= 1) {
chatSyntax("macro <add/remove>");
return;
}
switch (args[1]) {
case "add":
if (args.length <= 3) {
chatSyntax("macro add <key> <command...>");
return;
}
var key = args[2].toUpperCase();
var keyCode = Keyboard.getKeyIndex(key);
var command = joinArray(args, 3);
if (command.startsWith(prefix)) {
command = command.substring(1, command.length);
}
macros[keyCode] = command;
saveMacros();
chatText("Successfully bound command '§7" + command + "§3' to key '§7" + key + "§3'.");
break;
case "remove":
if (args.length <= 2) {
chatSyntax("macro remove <key>");
return;
}
var key = args[2].toUpperCase();
var keyCode = Keyboard.getKeyIndex(key);
if (macros[keyCode]) {
delete macros[keyCode];
chatText("Successfully removed macro.");
saveMacros();
} else {
chatText("Macro does not exist!");
}
break;
default:
chatSyntax("macro <add/remove>");
}
});
});