-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
316 lines (306 loc) · 10.2 KB
/
main.js
File metadata and controls
316 lines (306 loc) · 10.2 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* ================================================================
main.js
================================================================ */
// escape regex
const escapeRe = /\\(?:u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|u{[0-9a-fA-F]{1,6}}|[0-2][0-7]{0,2}|3[0-7][0-7]?|[4-7][0-7]?|.)/;
/**
* Main plugin instance
*/
class AcodePlugin {
/**
* Init function
*/
async init() {
this.addModes();
}
/**
* Register minecraft files
*/
addModes() {
// register minecraft files
ace.require("ace/ext/modelist").modes.push({
caption: "Minecraft Language",
extRe: /\.lang$/,
extensions: "lang",
mode: "ace/mode/lang",
name: "Lang"
}, {
caption: "Minecraft Function",
extRe: /\.mcfunction$/,
extensions: "mcfunction",
mode: "ace/mode/mcfunction",
name: "Mcfunction"
});
// dot-lang highlighter
define("ace/mode/lang_highlight_rules",[
"require","exports","module",
"ace/lib/oop",
"ace/mode/text_highlight_rules"
],function(require, exports, module){
"use strict";
const oop = require("../lib/oop");
const { TextHighlightRules } = require("./text_highlight_rules");
function LangHighlightRules() {
// add highlighting rules
this.$rules = {
start: [
{
// a comment
token: "comment.mclang",
regex: /(^\s*##.*|\s+##.*)$/
},
{
// "=" character
token: "keyword.mclang",
regex: /=/
},
{
// translation value
token: "string.mclang",
regex: /(?<==)/,
push: [
{
// parameters
token: "identifier.mclang",
regex: /%(%|[a-z]|[1-9]\d*(?:\$[a-z])?|\.\d+[df\$])/
},
{
// icons
token: "storage.mclang",
regex: /:[_a-zA-Z0-9.]+:/
},
{
defaultToken: "string.mclang"
},
{
// end
token: "string.mclang",
regex: /((?=\t+##)|$)/,
next: "pop"
}
]
},
{
// translation key
token: "variable.mclang",
regex: /^[^=]+/
}
]
};
this.normalizeRules();
}
LangHighlightRules.metaData = {
fileTypes: ["lang"],
name: "Lang"
};
oop.inherits(LangHighlightRules, TextHighlightRules);
exports.LangHighlightRules = LangHighlightRules;
});
// dot-lang mode
define("ace/mode/lang",[
"require","exports","module",
"ace/lib/oop",
"ace/mode/text",
"ace/mode/lang_highlight_rules"
],function(require,exports,module){
"use strict";
const oop = require("../lib/oop");
const { Mode: TextMode } = require("./text");
const { LangHighlightRules } = require("./lang_highlight_rules");
function LangMode() {
this.HighlightRules = LangHighlightRules;
this.$behaviour = this.$defaultBehaviour;
};
oop.inherits(LangMode, TextMode);
LangMode.prototype.$id = "ace/mode/lang";
exports.Mode = LangMode;
});
// mcfunction highlighter
define("ace/mode/mcfunction_highlight_rules",[
"require","exports","module",
"ace/lib/oop",
"ace/mode/text_highlight_rules"
],function(require, exports, module){
"use strict";
const oop = require("../lib/oop");
const { TextHighlightRules } = require("./text_highlight_rules");
// commands and sub-commands, as of 1.20.12
// commands
const cmds = [
"\?", "ability", "alwaysday", "camerashake", "clear", "clearspawnpoint",
"clone", "connect", "damage", "daylock", "deop", "dialogue", "difficulty",
"effect", "enchant", "event", "execute", "fill", "fog", "function",
"gamemode", "gamerule", "gametest", "give", "help", "immutableworld",
"inputpermission", "kick", "kill", "list", "locate", "loot", "me",
"mobevent", "msg", "music", "op", "particle", "playanimation", "playsound",
"reload", "replaceitem", "ride", "say", "schedule", "scoreboard",
"script", "scriptevent", "setblock", "setmaxplayers", "setworldspawn",
"spawnpoint", "spreadplayers", "stopsound", "structure", "summon",
"tag", "teleport", "tell", "tellraw", "testfor", "testforblock",
"testforblocks", "tickingarea", "time", "title", "titleraw",
"tp", "w", "xp", "event", "inputpermission", "camera", "camerashake",
"particle", "msg", "damage"
];
// sub-commands
const subCmds = [
"actionbar", "add", "align", "all", "anchored", "as", "ascending", "at",
"belowname", "biome", "block", "circle", "structure", "block", "blocks",
"change", "clear", "clearall", "close", "connect", "create", "debugger",
"delete", "descending", "destroy", "dummy", "entity", "evict_riders",
"exportstats", "eyes", "facing", "feet", "fill", "filtered", "fog", "give",
"if", "in", "insert", "keep", "kill", "list", "listen", "load", "loot",
"mainhand", "masked", "matches", "objectives", "offhand", "on_area_loaded",
"open", "operation", "play", "players", "pos", "positioned", "preload",
"profiler", "push", "queue", "query", "rain", "random", "remove", "remove_all",
"replace", "reset", "rotated", "run", "runset", "runsetuntilfail", "runthese",
"runthis", "save", "score", "set", "setdisplay", "sidebar", "spawn", "start",
"start_riding", "stop", "stop_riding", "stopall", "subtitle", "summon_ride",
"summon_rider", "test", "thunder", "tickingarea", "times", "title", "unless",
"volume", "watchdog", "@p", "@a", "@e", "@s", "@r", "@v"
];
// dimensions, used in some commands
const dims = [
"overworld", "nether", "the_end"
];
function McfunctionHighlightRules() {
// add keywords
this.createKeywordMapper({
keyword: [...cmds, ...subCmds, ...dims].join("|")
});
// add highlighting rules
this.$rules = {
start: [
{
// a comment
token: "comment.mcfunction",
regex: /\s*#.*/
},
{
// a string (text within double quotes)
token: "string.mcfunction",
regex: /"/,
push: [
{
// escape characters
token: "constant.language.escape.mcfunction",
regex: escapeRe
},
{
// back to parent rules
token: "string.mcfunction",
regex: /"/,
next: "pop"
},
{
// part of the string
defaultToken: "string.mcfunction"
}
]
},
{
// numbers
token: "constant.numeric.mcfunction",
regex: /[-+]?(0|[1-9]\d*)(\.\d+)?/
},
{
// boolean
token: "constant.language.boolean.mcfunction",
regex: /\b(true|false)\b/
},
{
// punctuation operators
token: "punctuation.operator.mcfunction",
regex: /[!,.:]/
},
{
// keyword operators
token: "keyword.operator.mcfunction",
regex: /[-+*/=^~]/
},
{
// selector
token: "storage.selector.mcfunction",
regex: /@[aerpsv]/
},
{
// variable (like in shell script), for target selectors
token: "variable.mcfunction",
regex: /[a-zA-Z0-9_]+(?==)/
},
{
// bracket
token: "paren.mcfunction",
regex: /[\[\]\{\}]/
},
{
// minecraft namespace
token: "identifier.class.mcfunction",
regex: /\bminecraft\b/
},
{
// commands
token: "keyword.mcfunction",
regex: "\\b(" + cmds.join("|") + ")\\b"
},
{
// sub-commands
token: "identifier.mcfunction",
regex: "\\b(" + subCmds.join("|") + ")\\b"
},
{
// dimensions
token: "constant.language.mcfunction",
regex: "\\b(" + dims.join("|") + ")\\b"
}
]
};
this.normalizeRules();
}
McfunctionHighlightRules.metaData = {
fileTypes: ["mcfunction"],
name: "Mcfunction"
};
oop.inherits(McfunctionHighlightRules, TextHighlightRules);
exports.McfunctionHighlightRules = McfunctionHighlightRules;
});
// mcfunction mode
define("ace/mode/mcfunction",[
"require","exports","module",
"ace/lib/oop",
"ace/mode/text",
"ace/mode/mcfunction_highlight_rules"
],function(require,exports,module){
"use strict";
const oop = require("../lib/oop");
const { Mode: TextMode } = require("./text");
const { McfunctionHighlightRules } = require("./mcfunction_highlight_rules");
function McfunctionMode() {
this.HighlightRules = McfunctionHighlightRules;
this.$behaviour = this.$defaultBehaviour;
};
oop.inherits(McfunctionMode, TextMode);
McfunctionMode.prototype.$id = "ace/mode/mcfunction";
exports.Mode = McfunctionMode;
});
}
/**
* Unmount function
*/
async destroy() {
// no-op
}
}
if (window.acode) {
const acodePlugin = new AcodePlugin();
const pluginId = "vytdev.minecraft.util";
acode.setPluginInit(pluginId, async (baseUrl, $page, { cacheFileUrl, cacheFile }) => {
if (!baseUrl.endsWith('/')) {
baseUrl += '/';
}
acodePlugin.baseUrl = baseUrl;
await acodePlugin.init($page, cacheFile, cacheFileUrl);
});
acode.setPluginUnmount(pluginId, () => {
acodePlugin.destroy();
});
}