-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal.ts
159 lines (119 loc) · 3.38 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
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
import { Vec3 } from 'vec3';
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';
// #region Types
// #region Base
interface RawVec3 {
x: number;
y: number;
z: number;
}
interface Orientation {
pitch: number;
yaw: number;
}
// #endregion
// #region Packet Data
interface UpstreamPositionPacketData extends RawVec3 {
onGround: boolean;
}
interface UpstreamLookPacketData extends Orientation {
onGround: boolean;
}
interface DownstreamPositionPacketData extends RawVec3, Orientation {
flags: number;
teleportId: number;
}
// #endregion
// #region Event Map
interface UpstreamEventMap {
position: (packet: Packet<UpstreamPositionPacketData>) => AsyncVoid;
// minecraft calls this "look", however, I prefer "orientation"
orientation: (packet: Packet<UpstreamLookPacketData>) => AsyncVoid;
}
interface DownstreamEventMap {
// minecraft calls this "position", however, it also includes
// orientation data, so I decided to call it "pose"
pose: (packet: Packet<DownstreamPositionPacketData>) => AsyncVoid;
}
interface BiEventMap {
position: (
packet:
| Packet<UpstreamPositionPacketData>
| Packet<DownstreamPositionPacketData>,
) => AsyncVoid;
orientation: (
packet:
| Packet<UpstreamLookPacketData>
| Packet<DownstreamPositionPacketData>,
) => AsyncVoid;
}
// #endregion
// #endregion
export const pose = {
upstream: new PublicEventHandler<UpstreamEventMap>(),
downstream: new PublicEventHandler<DownstreamEventMap>(),
bi: new PublicEventHandler<BiEventMap>(),
position: new Vec3(NaN, NaN, NaN),
orientation: { pitch: NaN, yaw: NaN } as Orientation,
onGround: false,
};
// #region Events
// #region bi events -> pose data
pose.bi.on('position', async (packet) => {
const data = packet.data;
pose.position.set(data.x, data.y, data.z);
if ('onGround' in data) pose.onGround = data.onGround;
});
pose.bi.on('orientation', async (packet) => {
const data = packet.data;
pose.orientation.yaw = data.yaw;
pose.orientation.pitch = data.pitch;
if ('onGround' in data) pose.onGround = data.onGround;
});
// #endregion
// #region pose events -> bi events
pose.upstream.on('position', async (packet) => {
await pose.bi.emit('position', packet);
});
pose.upstream.on('orientation', async (packet) => {
await pose.bi.emit('orientation', packet);
});
pose.downstream.on('pose', async (packet) => {
await pose.bi.emit('position', packet);
await pose.bi.emit('orientation', packet);
});
// #endregion
// #region proxy events -> pose events
proxy.upstream.on('position', async (packet) => {
await pose.upstream.emit(
'position',
packet as Packet<UpstreamPositionPacketData>,
);
});
proxy.upstream.on('look', async (packet) => {
await pose.upstream.emit(
'orientation',
packet as Packet<UpstreamLookPacketData>,
);
});
proxy.upstream.on('position_look', async (packet) => {
await pose.upstream.emit(
'position',
packet as Packet<UpstreamPositionPacketData>,
);
await pose.upstream.emit(
'orientation',
packet as Packet<UpstreamLookPacketData>,
);
});
proxy.downstream.on('position', async (packet) => {
await pose.downstream.emit(
'pose',
packet as Packet<DownstreamPositionPacketData>,
);
});
// #endregion
// #endregion