/
toolbar.ts
101 lines (83 loc) · 2.48 KB
/
toolbar.ts
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
import * as alt from 'alt-client';
import * as native from 'natives';
import * as AthenaClient from '@AthenaClient/api';
import { SYSTEM_EVENTS } from '@AthenaShared/enums/system';
import { Item } from '@AthenaShared/interfaces/item';
const DELAY_TIME = 1000;
const KeyBinds = {
49: 0,
50: 1,
51: 2,
52: 3,
53: 4,
};
let toolbar: Array<Item> = [];
let debounce = Date.now();
let enabled = true;
let interval: number;
let lastKey: number;
const Internal = {
init() {
if (!enabled) {
return;
}
alt.on('keyup', Internal.handleKeyPress);
},
handleChanges(_inventory: Array<Item>, _toolbar: Array<Item>) {
if (!enabled) {
return;
}
toolbar = _toolbar;
},
drawCooldown() {
if (Date.now() > debounce) {
alt.clearInterval(interval);
interval = undefined;
alt.emitServer(SYSTEM_EVENTS.PLAYER_TOOLBAR_INVOKE, lastKey);
return;
}
if (!enabled) {
return;
}
const timeLeft = ((debounce - Date.now()) / 1000).toFixed(2);
const offsetPos = native.getOffsetFromEntityInWorldCoords(alt.Player.local.scriptID, 0.5, 0, 0.2);
AthenaClient.screen.text.drawText3D(`${timeLeft}s`, offsetPos, 0.5, new alt.RGBA(255, 255, 255, 150));
},
handleKeyPress(key: number) {
if (!enabled) {
return;
}
if (AthenaClient.webview.isAnyMenuOpen()) {
return;
}
if (typeof KeyBinds[key] === 'undefined') {
return;
}
if (Date.now() < debounce) {
if (typeof interval === 'undefined') {
interval = alt.setInterval(Internal.drawCooldown, 0);
}
AthenaClient.systems.sound.play2d('error', 0.2);
return;
}
if (typeof interval !== 'undefined') {
alt.clearInterval(interval);
}
debounce = Date.now() + DELAY_TIME;
const index = toolbar.findIndex((x) => x.slot === KeyBinds[key]);
if (index <= -1) {
return;
}
if (typeof interval === 'undefined') {
interval = alt.setInterval(Internal.drawCooldown, 0);
}
lastKey = KeyBinds[key];
},
};
export const ToolbarSystem = {
disable() {
enabled = false;
},
};
alt.onServer(SYSTEM_EVENTS.PLAYER_TOOLBAR_ENABLE, Internal.init);
alt.onServer(SYSTEM_EVENTS.PLAYER_EMIT_INVENTORY_SYNC, Internal.handleChanges);