-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.ts
66 lines (53 loc) · 1.86 KB
/
manager.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
import type { Packet } from '../../util/packet.js';
import type { AsyncVoid } from '../../util/types.js';
import inventory from '../ddg.inventory/local.js';
import proxy from '../internal.proxy/local.js';
interface UpstreamUseItemPacketData {
hand: number;
sequence: number;
}
interface UpstreamUseEntityPacketData {
target: number;
mouse: number;
x: number | undefined;
y: number | undefined;
z: number | undefined;
hand: number | undefined;
sneaking: boolean;
}
export interface ItemModifier {
onUse?: (packet: Packet<UpstreamUseItemPacketData>) => AsyncVoid;
onUseEntity?: (packet: Packet<UpstreamUseEntityPacketData>) => AsyncVoid;
}
const modifiers: Map<string, ItemModifier> = new Map();
export function registerModifier(id: string, modifier: ItemModifier): void {
modifiers.set(id, modifier);
}
async function broadcast<T extends keyof ItemModifier>(
event: T,
// TODO: Do this differently
// (I tried everything but couldn't, ts is being gay today)
...args: Parameters<Required<ItemModifier>[T]>
): Promise<void> {
const selected = inventory.client.getSelectedSlot();
const nbtData = selected.nbtData;
if (nbtData === undefined) return;
if (nbtData.type !== 'compound') return;
const modifiersData = nbtData.value.modifiers;
if (modifiersData === undefined) return;
if (modifiersData.type !== 'list') return;
for (const id of modifiersData.value.value) {
if (typeof id !== 'string') continue;
const modifier = modifiers.get(id);
if (modifier === undefined) continue;
const f = modifier[event];
if (f === undefined) continue;
await f(...args);
}
}
proxy.upstream.on('use_item', async (packet) => {
await broadcast('onUse', packet as Packet<UpstreamUseItemPacketData>);
});
proxy.upstream.on('use_entity', async (packet) => {
await broadcast('onUseEntity', packet as Packet<UpstreamUseEntityPacketData>);
});