-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal.ts
94 lines (76 loc) · 2.52 KB
/
local.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
import { PublicEventHandler } from '../../util/events.js';
import type { Packet } from '../../util/packet.js';
import type { AsyncVoid } from '../../util/types.js';
import proxy from '../internal.proxy/local.js';
export interface PartialPlayer {
uuid: string;
player?: {
name: string;
properties: { key: string; value: string; signature: string }[];
};
// TODO: chatSession
chatSession?: undefined;
gamemode?: number;
listed?: boolean;
latency?: number;
displayName?: string;
}
interface DownstreamPlayerInfoPacketData {
action: number;
data: PartialPlayer[];
}
interface DownstreamPlayerRemovePacketData {
players: string[];
}
export interface EventMap {
/**
* @param delta reference to the packet's delta, changes will be reflected on client
*/
'player.update': (player: PartialPlayer, delta: PartialPlayer) => AsyncVoid;
'player.join': (player: PartialPlayer) => AsyncVoid;
'player.leave': (uuid: string) => AsyncVoid;
}
class PDTPlugin extends PublicEventHandler<EventMap> {
public getPlayerByUUID(uuid: string): PartialPlayer | undefined {
return players.get(uuid);
}
public getPlayers(): IterableIterator<PartialPlayer> {
return players.values();
}
public getPlayerArray(): PartialPlayer[] {
return Array.from(this.getPlayers());
}
}
export const pdt = new PDTPlugin();
export default pdt;
const players = new Map<string, PartialPlayer>();
proxy.downstream.on(
'player_info',
async (packet: Packet<DownstreamPlayerInfoPacketData>) => {
for (const delta of packet.data.data) {
const hadPlayer = players.has(delta.uuid);
const player = players.get(delta.uuid) ?? ({} as PartialPlayer);
players.set(delta.uuid, player);
for (const [key, value] of Object.entries(delta)) {
if (value === undefined) continue;
// why can't ts be just a little bit smarter
// (I even tried making Object.entries return [keyof PartialPlayer, PartialPlayer[keyof PartialPlayer]] and it didn't work)
(player as any)[key] = value;
}
await pdt.emit('player.update', player, delta);
// the bitflag for having player is called add_player
// but, when checking for it, it would also trigger
// on latency updates
if (!hadPlayer) await pdt.emit('player.join', player);
}
},
);
proxy.downstream.on(
'player_remove',
async (packet: Packet<DownstreamPlayerRemovePacketData>) => {
for (const uuid of packet.data.players) {
await pdt.emit('player.leave', uuid);
players.delete(uuid);
}
},
);