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'
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>(
eventName: EventName,
callback: RageFW_ServerEventCallback<EventName>,
): void {
if (nativeEvents.includes(eventName)) {
if (this.isNativeEvent(eventName)) {
mp.events.add(eventName, callback)
} 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(),
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 IServerEvents
export type RageFW_ServerEventCallback<
K extends keyof RageFW_ICustomServerEvent | keyof IServerEvents,
> = K extends keyof RageFW_ICustomServerEvent
? (
player: PlayerMp,
...args: Parameters<RageFW_ICustomServerEvent[K]>
) => ReturnType<RageFW_ICustomServerEvent[K]>
: K extends keyof IServerEvents
? ThisifyServerEvents[K]
: never
export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> =
K extends keyof RageFW_ICustomServerEvent
? (
player: PlayerMp,
...args: Parameters<RageFW_ICustomServerEvent[K]>
) => ReturnType<RageFW_ICustomServerEvent[K]>
: K extends keyof IServerEvents
? ThisifyServerEvents[K]
: never
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent