-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmacros.js
127 lines (110 loc) · 3.06 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
/*
This script allows binding client and server commands as well as chat messages to a key.
Usage:
.macros add <key> <command...>
.macros remove <key>
.macros list
Examples:
.macros add k .help
.macros add o /gamerules doDaylightCycle false
.macros add p Some chat message
*/
const script = registerScript({
name: "Macros",
version: "1.0.0",
authors: ["Senk Ju"]
});
const Files = Java.type("java.nio.file.Files");
const Paths = Java.type("java.nio.file.Paths");
const JString = Java.type("java.lang.String");
function printFormattedText(message) {
Client.displayChatMessage(`§8[§9§lMacros§8] §3 ${message}`);
}
function readMacros() {
try {
const file = new JString(Files.readAllBytes(Paths.get("macros.json")));
return JSON.parse(file);
} catch (e) {
return {};
}
}
function writeMacros() {
Files.write(Paths.get("macros.json"), JSON.stringify(macros).getBytes());
}
const clientCommandPrefix = "."; // TODO: Get from client
const macros = readMacros();
script.registerModule({
name: "Macros",
description: "Allows you to bind commands to keys.",
category: "Client"
}, (module) => {
module.on("key", (event) => {
if (event.getAction() !== 1) {
return;
}
const key = event.getKey().getTranslationKey().split(".").pop();
const command = macros[key];
if (!command) {
return;
}
switch (command[0]) {
case clientCommandPrefix:
Client.commandManager.execute(command.substring(1));
break;
case "/":
NetworkUtil.sendCommand(command.substring(1));
break;
default:
NetworkUtil.sendChatMessage(command);
}
});
});
script.registerCommand({
name: "macroy",
aliases: ["macro"],
hub: true,
subcommands: [
{
name: "add",
parameters: [
{
name: "key",
required: true,
},
{
name: "command",
required: true,
vararg: true
}
],
onExecute(key, commandParts) {
const command = commandParts.join(" ");
macros[key] = command;
writeMacros();
printFormattedText(`Added macro '${command}' to '${key}'.`);
}
},
{
name: "remove",
parameters: [
{
name: "key",
required: true
}
],
onExecute(key) {
delete macros[key];
writeMacros();
printFormattedText(`Removed macro from '${key}'.`);
}
},
{
name: "list",
onExecute() {
for (const key in macros) {
printFormattedText(`${key} -> '${macros[key]}'`);
}
}
}
]
});