This commit is contained in:
Oleksandr Honcharov 2024-06-11 03:38:31 +03:00
parent 2113b5ae29
commit 2da34bd382
2 changed files with 32 additions and 12 deletions

View File

@ -9,14 +9,29 @@ import {
import { nativeEvents } from './native.events' import { nativeEvents } from './native.events'
class Server { class Server {
private isNativeEvent(eventName: string): eventName is keyof IServerEvents {
return nativeEvents.includes(eventName)
}
public testRegister(
event: string,
callback: (player: PlayerMp, ...args: unknown[]) => void,
) {
rpc.register(event, async (data, info) => {
callback(info.player as PlayerMp, ...data)
})
}
public register<EventName extends RageFW_ServerEvent>( public register<EventName extends RageFW_ServerEvent>(
eventName: EventName, eventName: EventName,
callback: RageFW_ServerEventCallback<EventName>, callback: RageFW_ServerEventCallback<EventName>,
): void { ): void {
if (nativeEvents.includes(eventName)) { if (this.isNativeEvent(eventName)) {
mp.events.add(eventName, callback) mp.events.add(eventName, callback)
} else { } else {
rpc.register(eventName, callback as rpc.ProcedureListener) rpc.register(eventName, async (data: any[], info) => {
return callback(info.player as PlayerMp, data)
})
} }
} }
@ -54,3 +69,9 @@ export const fw = {
event: new Server(), event: new Server(),
player: new Player(), player: new Player(),
} }
fw.event.register('ggfdgfd', player => {})
fw.event.registerMany({
trailerAttached: player => {},
})

View File

@ -9,16 +9,15 @@ export type RageFW_ServerEvent =
| keyof RageFW_ICustomServerEvent | keyof RageFW_ICustomServerEvent
| keyof IServerEvents | keyof IServerEvents
export type RageFW_ServerEventCallback< export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> =
K extends keyof RageFW_ICustomServerEvent | keyof IServerEvents, K extends keyof RageFW_ICustomServerEvent
> = K extends keyof RageFW_ICustomServerEvent ? (
? ( player: PlayerMp,
player: PlayerMp, ...args: Parameters<RageFW_ICustomServerEvent[K]>
...args: Parameters<RageFW_ICustomServerEvent[K]> ) => ReturnType<RageFW_ICustomServerEvent[K]>
) => ReturnType<RageFW_ICustomServerEvent[K]> : K extends keyof IServerEvents
: K extends keyof IServerEvents ? ThisifyServerEvents[K]
? ThisifyServerEvents[K] : never
: never
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent