Repository navigation
Expand file tree
/
Copy pathvmenu.js
More file actions
2299 lines (2278 loc) · 79.9 KB
/
Copy pathvmenu.js
File metadata and controls
2299 lines (2278 loc) · 79.9 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// vMenu Enhanced plugin API for JavaScript, built for vMenu Enhanced v1.0.22.
// Copy this file into your resource and load it before your own scripts, see https://docs.vespura.com/vmenu/enhanced/plugins/javascript/
"use strict";
var vMenu = (() => {
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target2, all) => {
for (var name in all)
__defProp(target2, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/iife.ts
var iife_exports = {};
__export(iife_exports, {
Gate: () => Gate,
MenuPriority: () => MenuPriority,
NotifyStyle: () => NotifyStyle,
OpenMenuResult: () => OpenMenuResult,
PluginBoolSetting: () => PluginBoolSetting,
PluginButton: () => PluginButton,
PluginCheckbox: () => PluginCheckbox,
PluginConfirmButton: () => PluginConfirmButton,
PluginConfirmList: () => PluginConfirmList,
PluginDynamicList: () => PluginDynamicList,
PluginFloatSetting: () => PluginFloatSetting,
PluginIntSetting: () => PluginIntSetting,
PluginItem: () => PluginItem,
PluginKey: () => PluginKey,
PluginList: () => PluginList,
PluginMenu: () => PluginMenu,
PluginPlayerActions: () => PluginPlayerActions,
PluginPlayerButton: () => PluginPlayerButton,
PluginPlayerConfirmButton: () => PluginPlayerConfirmButton,
PluginPlayerList: () => PluginPlayerList,
PluginSeparator: () => PluginSeparator,
PluginSetting: () => PluginSetting,
PluginSettings: () => PluginSettings,
PluginSlider: () => PluginSlider,
PluginStringSetting: () => PluginStringSetting,
PluginSubmenu: () => PluginSubmenu,
PluginThemes: () => PluginThemes,
PluginTranslations: () => PluginTranslations,
ServerPluginDeclaration: () => ServerPluginDeclaration,
Text: () => Text,
VMenuPlugin: () => VMenuPlugin,
VMenuServer: () => VMenuServer,
version: () => version
});
// src/version.ts
var version = true ? "1.0.22" : "0.0.0-local";
// src/text.ts
var Text = class _Text {
constructor(value, isKey, args) {
this.value = value;
this.isKey = isKey;
this.args = args;
}
/** Text shown exactly as written, never translated. */
static literal(text) {
return new _Text(text, false);
}
/**
* A translation key, optionally with named placeholder values. In the translated string,
* `{name}` is replaced with the matching value, which can itself be a literal or another key.
*/
static key(key, args) {
return new _Text(key, true, args);
}
/** @internal */
static toRef(text) {
if (text === null || text === void 0) {
return void 0;
}
if (!(text instanceof _Text)) {
return { text: String(text) };
}
if (!text.isKey) {
return { text: text.value };
}
const reference = { key: text.value };
if (text.args) {
const args = {};
let any = false;
for (const [name, value] of Object.entries(text.args)) {
const argument = _Text.toRef(value);
if (argument) {
args[name] = argument;
any = true;
}
}
if (any) {
reference.args = args;
}
}
return reference;
}
/** @internal */
static toRefs(options) {
const refs = [];
for (const option of options) {
const reference = _Text.toRef(option);
if (reference) {
refs.push(reference);
}
}
return refs;
}
};
// src/client/gate.ts
var Gate = class _Gate {
constructor(node) {
this.node = node;
}
/** One of the permissions your server side declared, by its short name. */
static permission(shortName) {
return new _Gate({ permission: shortName });
}
/** One of your bool settings: the item is available while the convar reads true. */
static setting(setting) {
return new _Gate({ setting: typeof setting === "string" ? setting : setting.name });
}
/** Passes when every gate passes. */
static all(...gates) {
return new _Gate({ all: gates.map(_Gate.toNode) });
}
/** Passes when at least one gate passes. */
static any(...gates) {
return new _Gate({ any: gates.map(_Gate.toNode) });
}
/** Passes when this gate and the other one both pass. */
and(other) {
return _Gate.all(this, other);
}
/** Passes when this gate or the other one passes. */
or(other) {
return _Gate.any(this, other);
}
/** @internal */
static toNode(gate) {
return typeof gate === "string" ? { permission: gate } : gate.node;
}
};
// src/fivem.ts
var natives = globalThis;
var currentResource = () => natives.GetCurrentResourceName();
var emitLocal = (eventName, ...args) => natives.emit(eventName, ...args);
var onLocal = (eventName, handler) => natives.on(eventName, handler);
var getConvar = (name, fallback) => natives.GetConvar(name, fallback) ?? fallback;
var getKvp = (key) => natives.GetResourceKvpString(key) ?? void 0;
var setKvp = (key, value) => natives.SetResourceKvp(key, value);
var isAceAllowed = (playerSource, object) => Boolean(natives.IsPlayerAceAllowed(playerSource, object));
var log = {
info: (resource, message) => console.log(`[${resource}] ${message}`),
warn: (resource, message) => console.warn(`[${resource}] ${message}`),
error: (resource, message) => console.error(`[${resource}] ${message}`)
};
function parseJson(json) {
if (typeof json !== "string") {
return void 0;
}
try {
return JSON.parse(json);
} catch {
return void 0;
}
}
function sanitizeId(resource) {
return resource.replace(/[^A-Za-z0-9]/g, "_");
}
// src/format.ts
function formatFloat(value) {
const text = value.toFixed(4).replace(/0+$/, "");
return text.endsWith(".") ? `${text}0` : text;
}
function int(value) {
const number = Math.trunc(Number(value));
return Number.isFinite(number) ? number : 0;
}
// src/protocol.ts
var PROTOCOL_VERSION = 3;
var BASE_PROTOCOL_VERSION = 2;
var MAX_STANDALONE_MENUS = 50;
var SERVER_PROTOCOL_VERSION = 1;
var VMENU_RESOURCE = "vMenu.Enhanced";
var PluginEvents = {
Probe: "vMenu.Enhanced:Plugins:Probe",
Register: "vMenu.Enhanced:Plugins:Register",
Unregister: "vMenu.Enhanced:Plugins:Unregister",
Update: "vMenu.Enhanced:Plugins:Update",
Notify: "vMenu.Enhanced:Plugins:Notify",
Prompt: "vMenu.Enhanced:Plugins:Prompt",
SetTheme: "vMenu.Enhanced:Plugins:SetTheme",
RegisterThemes: "vMenu.Enhanced:Plugins:RegisterThemes",
Ready: "vMenu.Enhanced:Plugins:Ready",
ServerProbe: "vMenu.Enhanced:Plugins:Server:Probe",
ServerRegister: "vMenu.Enhanced:Plugins:Server:Register",
ServerDenied: "vMenu.Enhanced:Plugins:Server:Denied",
ServerReady: "vMenu.Enhanced:Plugins:Server:Ready",
readyFor: (resource) => `vMenu.Enhanced:Plugins:${resource}:Ready`,
registerResultFor: (resource) => `vMenu.Enhanced:Plugins:${resource}:RegisterResult`,
eventFor: (resource) => `vMenu.Enhanced:Plugins:${resource}:Event`,
promptResultFor: (resource) => `vMenu.Enhanced:Plugins:${resource}:PromptResult`,
openResultFor: (resource) => `vMenu.Enhanced:Plugins:${resource}:OpenResult`,
themesFor: (resource) => `vMenu.Enhanced:Plugins:${resource}:Themes`,
themesRegisteredFor: (resource) => `vMenu.Enhanced:Plugins:${resource}:ThemesRegistered`,
serverReadyFor: (resource) => `vMenu.Enhanced:Plugins:${resource}:Server:Ready`,
serverRegisterResultFor: (resource) => `vMenu.Enhanced:Plugins:${resource}:Server:RegisterResult`
};
var RefreshPermissionsEvent = "vMenu.Enhanced:Permissions:Refresh";
var EntryTypes = {
Button: "button",
Checkbox: "checkbox",
List: "list",
Slider: "slider",
DynamicList: "dynamicList",
Submenu: "submenu",
Separator: "separator",
ConfirmButton: "confirmButton",
ConfirmList: "confirmList"
};
var NodeEvents = {
Opened: "opened",
Closed: "closed",
IndexChanged: "indexChanged",
Replaced: "replaced",
ForceClosed: "forceClosed",
Highlighted: "highlighted"
};
var SettingTypes = {
Bool: "bool",
Int: "int",
Float: "float",
String: "string"
};
var CallbackTypes = {
ItemSelected: "itemSelected",
CheckboxChanged: "checkboxChanged",
ListIndexChanged: "listIndexChanged",
ListSelected: "listSelected",
SliderMoved: "sliderMoved",
SliderSelected: "sliderSelected",
DynamicSelected: "dynamicSelected",
DynamicChanging: "dynamicChanging",
Confirmed: "confirmed",
ItemHighlighted: "itemHighlighted",
MenuOpened: "menuOpened",
MenuClosed: "menuClosed",
MenuIndexChanged: "menuIndexChanged",
MenuReplaced: "menuReplaced",
MenuForceClosed: "menuForceClosed",
PlayerActionSelected: "playerActionSelected",
PlayerActionConfirmed: "playerActionConfirmed",
PlayerActionListSelected: "playerActionListSelected",
KeyPressed: "keyPressed"
};
var UpdateOps = {
SetText: "setText",
SetDescription: "setDescription",
SetLabel: "setLabel",
SetLockedDescription: "setLockedDescription",
SetConfirmationDescription: "setConfirmationDescription",
SetIcons: "setIcons",
SetChecked: "setChecked",
SetOptions: "setOptions",
SetSelectedIndex: "setSelectedIndex",
SetSliderPosition: "setSliderPosition",
SetValue: "setValue",
SetVisible: "setVisible",
SetEnabled: "setEnabled",
SetGate: "setGate",
SetLog: "setLog",
SetBehaviour: "setBehaviour",
SetItemEvents: "setItemEvents",
AddItems: "addItems",
RemoveItems: "removeItems",
ClearMenu: "clearMenu",
MoveItem: "moveItem",
AddPlayerActions: "addPlayerActions",
SetMenuTitle: "setMenuTitle",
SetMenuSubtitle: "setMenuSubtitle",
OpenMenu: "openMenu",
CloseMenu: "closeMenu",
SelectItem: "selectItem",
SetMenuEvents: "setMenuEvents",
SetFilter: "setFilter",
ClearFilter: "clearFilter",
AddStandaloneMenu: "addStandaloneMenu",
AddKeys: "addKeys",
SetKeyText: "setKeyText",
SetKeyEnabled: "setKeyEnabled",
SetKeyGate: "setKeyGate",
MergeTranslations: "mergeTranslations"
};
function normalizeResult(raw) {
if (!raw || typeof raw !== "object") {
return void 0;
}
return {
accepted: raw.accepted === true,
protocolVersion: typeof raw.protocolVersion === "number" ? raw.protocolVersion : 0,
errors: Array.isArray(raw.errors) ? raw.errors : [],
warnings: Array.isArray(raw.warnings) ? raw.warnings : []
};
}
// src/client/settings.ts
var PluginSetting = class {
/** @internal */
constructor(name, fullName, defaultValue) {
this.name = name;
this.fullName = fullName;
this.defaultValue = defaultValue;
}
raw(fallback) {
return getConvar(this.fullName, fallback);
}
};
var PluginBoolSetting = class extends PluginSetting {
get value() {
return this.raw(this.defaultValue ? "true" : "false").toLowerCase() === "true";
}
};
var PluginIntSetting = class extends PluginSetting {
get value() {
const raw = this.raw("").trim();
return /^[+-]?\d+$/.test(raw) ? Number.parseInt(raw, 10) : this.defaultValue;
}
};
var PluginFloatSetting = class extends PluginSetting {
get value() {
const raw = this.raw("").trim();
const parsed = Number(raw);
return raw.length > 0 && Number.isFinite(parsed) ? parsed : this.defaultValue;
}
};
var PluginStringSetting = class extends PluginSetting {
get value() {
const raw = this.raw(this.defaultValue);
return raw.length === 0 ? this.defaultValue : raw;
}
};
var PluginSettings = class {
/** @internal */
constructor(pluginId2) {
/** @internal */
this.nodes = [];
this.prefix = `vMenu.Enhanced.Plugins.${pluginId2}.`;
}
/** Declares an on or off setting. */
bool(name, defaultValue, description) {
this.declare(name, SettingTypes.Bool, defaultValue ? "true" : "false", description);
return new PluginBoolSetting(name, this.prefix + name, defaultValue);
}
/** Declares a whole number setting. */
int(name, defaultValue, description) {
const value = Math.trunc(defaultValue);
this.declare(name, SettingTypes.Int, String(value), description);
return new PluginIntSetting(name, this.prefix + name, value);
}
/** Declares a decimal number setting. */
float(name, defaultValue, description) {
this.declare(name, SettingTypes.Float, formatFloat(defaultValue), description);
return new PluginFloatSetting(name, this.prefix + name, defaultValue);
}
/** Declares a text setting. */
string(name, defaultValue, description) {
this.declare(name, SettingTypes.String, defaultValue, description);
return new PluginStringSetting(name, this.prefix + name, defaultValue);
}
declare(name, type, defaultText, description) {
this.nodes.push({ name, type, default: defaultText, description });
}
};
// src/signal.ts
var Signal = class {
constructor(resource, name) {
this.resource = resource;
this.name = name;
this.handlers = [];
}
get hasHandlers() {
return this.handlers.length > 0;
}
add(handler) {
this.handlers.push(handler);
return () => {
this.handlers = this.handlers.filter((existing) => existing !== handler);
};
}
fire(...args) {
for (const handler of [...this.handlers]) {
try {
handler(...args);
} catch (error) {
log.error(this.resource(), `A ${this.name} handler threw: ${describe(error)}`);
}
}
}
};
function describe(error) {
return error instanceof Error ? error.stack ?? error.message : String(error);
}
// src/client/diff.ts
function sameText(left, right) {
if (left === right) {
return true;
}
if (!left || !right) {
return false;
}
return left.text === right.text && left.key === right.key && sameArgs(left.args, right.args);
}
function sameTexts(left, right) {
if (left === right) {
return true;
}
if (!left || !right || left.length !== right.length) {
return false;
}
return left.every((value, index) => sameText(value, right[index]));
}
function sameGate(left, right) {
if (left === right) {
return true;
}
if (!left || !right) {
return false;
}
return left.permission === right.permission && left.setting === right.setting && sameGates(left.all, right.all) && sameGates(left.any, right.any);
}
function sameGates(left, right) {
if (left === right) {
return true;
}
if (!left || !right || left.length !== right.length) {
return false;
}
return left.every((value, index) => sameGate(value, right[index]));
}
function sameArgs(left, right) {
if (left === right) {
return true;
}
if (!left || !right) {
return false;
}
const keys = Object.keys(left);
if (keys.length !== Object.keys(right).length) {
return false;
}
return keys.every((key) => key in right && sameText(left[key], right[key]));
}
// src/client/item.ts
var PluginItem = class {
/** @internal */
constructor(node, text) {
this.node = node;
this.textValue = text;
this.highlighted = this.signal("highlighted");
}
/** The row's id, how vMenu refers to it. */
get id() {
return this.node.id;
}
/** The row's text. */
get text() {
return this.textValue;
}
set text(value) {
this.textValue = value;
this.setTextField("text", value, UpdateOps.SetText);
}
/** The line shown under the menu while the row is highlighted. */
get description() {
return this.descriptionValue;
}
set description(value) {
this.descriptionValue = value;
this.setTextField("description", value, UpdateOps.SetDescription);
}
/** Right aligned text. Ignored by rows whose label the menu draws itself. */
get label() {
return this.labelValue;
}
set label(value) {
this.labelValue = value;
this.setTextField("label", value, UpdateOps.SetLabel);
}
/** What the row says while its gate locks it. Empty uses vMenu's own wording. */
get lockedDescription() {
return this.lockedDescriptionValue;
}
set lockedDescription(value) {
this.lockedDescriptionValue = value;
this.setTextField("lockedDescription", value, UpdateOps.SetLockedDescription);
}
/** Decides whether the row is available, evaluated live by vMenu. */
get gate() {
return this.gateValue;
}
set gate(value) {
this.gateValue = value;
const gate = value === void 0 ? void 0 : Gate.toNode(value);
if (sameGate(this.node.gate, gate)) {
return;
}
this.node.gate = gate;
this.emit({ op: UpdateOps.SetGate, itemId: this.id, gate });
}
/** What a failing gate does to the row: greyed out with a lock, or gone entirely. */
get hideWhenLocked() {
return this.node.behaviour?.toLowerCase() === "hide";
}
set hideWhenLocked(value) {
const behaviour = value ? "hide" : "lock";
if (this.node.behaviour === behaviour) {
return;
}
this.node.behaviour = behaviour;
this.emit({ op: UpdateOps.SetBehaviour, itemId: this.id, value: behaviour });
}
/** Whether the row is shown at all. */
get visible() {
return this.node.visible !== false;
}
set visible(value) {
if (this.visible === value) {
return;
}
this.node.visible = value;
this.emit({ op: UpdateOps.SetVisible, itemId: this.id, flag: value });
}
/** A disabled row is greyed out but still visible. Independent of the gate. */
get enabled() {
return this.node.enabled !== false;
}
set enabled(value) {
if (this.enabled === value) {
return;
}
this.node.enabled = value;
this.emit({ op: UpdateOps.SetEnabled, itemId: this.id, flag: value });
}
/**
* Ask vMenu to log use of this row to the server owner's webhook. Does nothing on its own: the
* plugin's server half has to declare the same id with `addLoggedItem`.
*/
get log() {
return this.node.log === true;
}
set log(value) {
if (this.log === value) {
return;
}
this.node.log = value;
this.emit({ op: UpdateOps.SetLog, itemId: this.id, flag: value });
}
/** Icon names from the vMenu icon set, for example "LOCK" or "STAR". */
setIcons(leftIcon, rightIcon) {
if (this.node.leftIcon === leftIcon && this.node.rightIcon === rightIcon) {
return;
}
this.node.leftIcon = leftIcon;
this.node.rightIcon = rightIcon;
this.emit({ op: UpdateOps.SetIcons, itemId: this.id, leftIcon, rightIcon });
}
/** Called while the player's cursor sits on this row. Chatty, subscribe deliberately. */
onHighlighted(handler) {
const unsubscribe = this.highlighted.add(handler);
this.subscribeNodeEvent(NodeEvents.Highlighted);
return unsubscribe;
}
/** @internal */
applyOptions(options) {
if (!options) {
return;
}
if (options.description !== void 0) this.description = options.description;
if (options.label !== void 0) this.label = options.label;
if (options.lockedDescription !== void 0) this.lockedDescription = options.lockedDescription;
if (options.gate !== void 0) this.gate = options.gate;
if (options.hideWhenLocked !== void 0) this.hideWhenLocked = options.hideWhenLocked;
if (options.visible !== void 0) this.visible = options.visible;
if (options.enabled !== void 0) this.enabled = options.enabled;
if (options.log !== void 0) this.log = options.log;
if (options.leftIcon !== void 0 || options.rightIcon !== void 0) this.setIcons(options.leftIcon, options.rightIcon);
}
/** @internal */
handle(callback) {
if (callback.type === CallbackTypes.ItemHighlighted) {
this.highlighted.fire();
}
}
signal(name) {
return new Signal(() => this.plugin?.resource ?? "", name);
}
subscribeNodeEvent(name) {
var _a;
(_a = this.node).events ?? (_a.events = []);
if (this.node.events.includes(name)) {
return;
}
this.node.events.push(name);
this.emit({ op: UpdateOps.SetItemEvents, itemId: this.id, events: [...this.node.events] });
}
emit(op) {
this.plugin?.emitOp(op);
}
setTextField(field, value, opName) {
const next = Text.toRef(value);
if (sameText(this.node[field], next)) {
return;
}
this.node[field] = next;
this.emit({ op: opName, itemId: this.id, textValue: next });
}
};
// src/client/preferences.ts
var KeyPrefix = "vmenu_plugin_pref_";
function readBool(itemId) {
const raw = getKvp(KeyPrefix + itemId);
if (raw === "true") {
return true;
}
if (raw === "false") {
return false;
}
return void 0;
}
function writeBool(itemId, value) {
setKvp(KeyPrefix + itemId, value ? "true" : "false");
}
// src/client/items.ts
var PluginButton = class extends PluginItem {
constructor() {
super(...arguments);
this.selected = this.signal("selected");
}
/** Called when the player presses the row. */
onSelected(handler) {
return this.selected.add(handler);
}
/** @internal */
handle(callback) {
super.handle(callback);
if (callback.type === CallbackTypes.ItemSelected) {
this.selected.fire();
}
}
};
var PluginConfirmButton = class extends PluginItem {
constructor() {
super(...arguments);
this.confirmed = this.signal("confirmed");
}
/** What the row asks before its second press. Empty uses vMenu's own wording. */
get confirmationDescription() {
return this.confirmationDescriptionValue;
}
set confirmationDescription(value) {
this.confirmationDescriptionValue = value;
this.setTextField("confirmationDescription", value, UpdateOps.SetConfirmationDescription);
}
/** Called on the confirming second press, never on the first. */
onConfirmed(handler) {
return this.confirmed.add(handler);
}
/** @internal */
handle(callback) {
super.handle(callback);
if (callback.type === CallbackTypes.Confirmed) {
this.confirmed.fire();
}
}
};
var PluginCheckbox = class extends PluginItem {
constructor() {
super(...arguments);
this.changed = this.signal("changed");
/** @internal */
this.persistedValue = false;
}
/** Whether the state is saved in this resource's key value store and restored on start. */
get persisted() {
return this.persistedValue;
}
/** Whether the box is ticked. */
get checked() {
return this.node.checked === true;
}
set checked(value) {
if (this.checked === value) {
return;
}
this.node.checked = value;
this.remember(value);
this.emit({ op: UpdateOps.SetChecked, itemId: this.id, flag: value });
}
/** Called when the player toggles the box. The new state is already in `checked`. */
onChanged(handler) {
return this.changed.add(handler);
}
/** @internal */
handle(callback) {
super.handle(callback);
if (callback.type === CallbackTypes.CheckboxChanged && typeof callback.checked === "boolean") {
this.node.checked = callback.checked;
this.remember(callback.checked);
this.changed.fire(callback.checked);
}
}
remember(state) {
if (this.persistedValue) {
writeBool(this.id, state);
}
}
};
var PluginList = class extends PluginItem {
constructor() {
super(...arguments);
this.indexChanged = this.signal("indexChanged");
this.selected = this.signal("selected");
}
/** The selected option, counted from 0. */
get selectedIndex() {
return this.node.selectedIndex ?? 0;
}
set selectedIndex(value) {
value = int(value);
if (this.selectedIndex === value) {
return;
}
this.node.selectedIndex = value;
this.emit({ op: UpdateOps.SetSelectedIndex, itemId: this.id, index: value });
}
/** Replaces the options, optionally moving the selection at the same time. */
setOptions(options, selectedIndex) {
const refs = Text.toRefs(options);
selectedIndex = selectedIndex === void 0 ? void 0 : int(selectedIndex);
if (sameTexts(this.node.options, refs) && (selectedIndex === void 0 || selectedIndex === this.selectedIndex)) {
return;
}
this.node.options = refs;
if (selectedIndex !== void 0) {
this.node.selectedIndex = selectedIndex;
}
this.emit({ op: UpdateOps.SetOptions, itemId: this.id, options: refs, index: selectedIndex });
}
/** Called when the player scrolls the value. The new index is already in `selectedIndex`. */
onIndexChanged(handler) {
return this.indexChanged.add(handler);
}
/** Called when the player presses the row, with the index they had selected. */
onSelected(handler) {
return this.selected.add(handler);
}
/** @internal */
handle(callback) {
super.handle(callback);
switch (callback.type) {
case CallbackTypes.ListIndexChanged:
if (typeof callback.newIndex === "number") {
this.node.selectedIndex = callback.newIndex;
this.indexChanged.fire(callback.oldIndex ?? 0, callback.newIndex);
}
break;
case CallbackTypes.ListSelected:
if (typeof callback.selectedIndex === "number") {
this.selected.fire(callback.selectedIndex);
}
break;
case CallbackTypes.Confirmed:
if (typeof callback.selectedIndex === "number") {
this.raiseConfirmed(callback.selectedIndex);
}
break;
}
}
raiseConfirmed(_index) {
}
};
var PluginConfirmList = class extends PluginList {
constructor() {
super(...arguments);
this.confirmed = this.signal("confirmed");
}
/** What the row asks before its second press. Empty uses vMenu's own wording. */
get confirmationDescription() {
return this.confirmationDescriptionValue;
}
set confirmationDescription(value) {
this.confirmationDescriptionValue = value;
this.setTextField("confirmationDescription", value, UpdateOps.SetConfirmationDescription);
}
/** Called on the confirming second press, with the index that was confirmed. */
onConfirmed(handler) {
return this.confirmed.add(handler);
}
raiseConfirmed(index) {
this.confirmed.fire(index);
}
};
var PluginSlider = class extends PluginItem {
constructor() {
super(...arguments);
this.moved = this.signal("moved");
this.selected = this.signal("selected");
}
/** The lowest position. */
get min() {
return this.node.min ?? 0;
}
/** The highest position. */
get max() {
return this.node.max ?? 0;
}
/** Where the bar sits. */
get position() {
return this.node.position ?? this.min;
}
set position(value) {
value = int(value);
if (this.position === value) {
return;
}
this.node.position = value;
this.emit({ op: UpdateOps.SetSliderPosition, itemId: this.id, index: value });
}
/** Called while the player drags the bar. The new position is already in `position`. */
onMoved(handler) {
return this.moved.add(handler);
}
/** Called when the player presses the row, with the position it sat at. */
onSelected(handler) {
return this.selected.add(handler);
}
/** @internal */
handle(callback) {
super.handle(callback);
switch (callback.type) {
case CallbackTypes.SliderMoved:
if (typeof callback.newPosition === "number") {
this.node.position = callback.newPosition;
this.moved.fire(callback.oldPosition ?? 0, callback.newPosition);
}
break;
case CallbackTypes.SliderSelected:
if (typeof callback.position === "number") {
this.selected.fire(callback.position);
}
break;
}
}
};
var PluginDynamicList = class extends PluginItem {
constructor() {
super(...arguments);
this.selected = this.signal("selected");
}
/** The value the row shows. */
get value() {
return this.node.value ?? "";
}
set value(value) {
value = String(value);
if (this.value === value) {
return;
}
this.node.value = value;
this.emit({ op: UpdateOps.SetValue, itemId: this.id, value });
}
/** Called when the player presses the row, with the value it showed. */
onSelected(handler) {
return this.selected.add(handler);
}
/** @internal */
handle(callback) {
super.handle(callback);
switch (callback.type) {
case CallbackTypes.DynamicChanging:
if (typeof callback.left === "boolean") {
const next = this.changeRequested?.(callback.currentValue ?? "", callback.left);
if (next !== null && next !== void 0) {
this.value = next;
}
}
break;
case CallbackTypes.DynamicSelected:
this.selected.fire(callback.value ?? "");
break;
}
}
};
var PluginSeparator = class extends PluginItem {
};
var PluginSubmenu = class extends PluginItem {
/** @internal */
constructor(node, text, menu) {
super(node, text);
this.menu = menu;
}
};
// src/client/key.ts
var PluginKey = class {
/** @internal */
constructor(plugin, node, text) {
this.plugin = plugin;
this.node = node;
this.textValue = text;
this.pressed = new Signal(() => plugin.resource, "pressed");
}
/** The key's id, which also names the binding in the player's key settings. */
get id() {
return this.node.id;
}
/** The instructional button's label. Empty hides the button, the key still works. */
get text() {
return this.textValue;
}
set text(value) {
this.textValue = value;
const text = Text.toRef(value);
if (sameText(this.node.text, text)) {
return;
}
this.node.text = text;
this.plugin.emitOp({ op: UpdateOps.SetKeyText, keyId: this.id, textValue: text });
}
/** A disabled key does nothing and shows no button. */
get enabled() {
return this.node.enabled !== false;
}
set enabled(value) {
if (this.enabled === value) {
return;
}
this.node.enabled = value;
this.plugin.emitOp({ op: UpdateOps.SetKeyEnabled, keyId: this.id, flag: value });
}
/** While the gate fails the key does nothing and shows no button. */
get gate() {
return this.gateValue;
}
set gate(value) {
this.gateValue = value;
const gate = value === void 0 ? void 0 : Gate.toNode(value);
if (sameGate(this.node.gate, gate)) {
return;
}
this.node.gate = gate;
this.plugin.emitOp({ op: UpdateOps.SetKeyGate, keyId: this.id, gate });
}
/** Called when the player presses the key, with the row the cursor was on. */
onPressed(handler) {
return this.pressed.add(handler);
}
/** @internal */
handle(press) {
this.pressed.fire(press);
}
};
// src/client/menu.ts
var MenuPriority = {
/** Only opens when no vMenu or plugin menu is open. */
None: "none",
/** Replaces vMenu's menus and other menus at low priority. */
Low: "low",
/** Replaces anything at low or medium priority. */
Medium: "medium",
/**
* Replaces anything that is not locked. Only use this in extreme situations, prevents vMenu or other plugins with a
* lower priority from opening any menus while yours is open. Use responsibly!
*/
High: "high"
};
var OpenMenuResult = {