import { NetMiddleware , NextCaller } from "../middleware" ; import { findOrCreateRemote , IS_CLIENT , IS_RUNNING , NetManagedInstance } from "../internal" ; import MiddlewareEvent , { MiddlewareList } from "./MiddlewareEvent" ; import { MiddlewareOverload } from "../middleware" ; /**
 * Interface for server listening events
 */ export interface ServerListenerEvent < CallArguments extends ReadonlyArray < unknown > > { param callback Connect ( callback ) : RBXScriptConnection ; } /**
 * Interface for server sender events
 */ export interface ServerSenderEvent < CallArguments extends ReadonlyArray < unknown > > { param args SendToAllPlayers ( ... args ) : void ; param blacklist param args SendToAllPlayersExcept ( blacklist , ... args ) : void ; param player param args SendToPlayer ( player , ... args ) : void ; param players param args SendToPlayers ( players , ... args ) : void ; } export default class ServerEvent < ConnectArgs extends ReadonlyArray < unknown > = Array < unknown > , CallArgs extends ReadonlyArray < unknown > = Array < unknown > > extends MiddlewareEvent implements NetManagedInstance , ServerListenerEvent < ConnectArgs > , ServerSenderEvent < CallArgs > { private instance : RemoteEvent ; private defaultHook ? : RBXScriptConnection ; internal private static readonly DefaultEventHook = ( player , ... args ) => { } ; public constructor ( name , middlewares = [ ] ) { super ( middlewares ) ; this . instance = findOrCreateRemote ( "RemoteEvent" , name , ( instance ) => { this . defaultHook = instance . OnServerEvent . Connect ( ServerEvent . DefaultEventHook ) ; } ) ; assert ( ! IS_CLIENT , "Cannot create a NetServerEvent on the client!" ) ; } deprecated public GetInstance ( ) { return this . instance ; } param callback public Connect ( callback ) { this . defaultHook ?. Disconnect ( ) ; const connection = this . instance . OnServerEvent . Connect ( ( player , ... args ) => { this . _processMiddleware ( callback ) ?. ( player , ... ( ( args as unknown ) as ConnectArgs ) ) ; } ) ; return connection ; } param args public SendToAllPlayers ( ... args ) { if ( ! IS_RUNNING ) return ; this . instance . FireAllClients ( ... args ) ; } param blacklist param args public SendToAllPlayersExcept ( blacklist , ... args ) { if ( ! IS_RUNNING ) return ; const Players = game . GetService ( "Players" ) ; if ( typeIs ( blacklist , "Instance" ) ) { const otherPlayers = Players . GetPlayers ( ) . filter ( ( p ) => p !== blacklist ) ; for ( const player of otherPlayers ) { this . instance . FireClient ( player , ... ( args as CallArgs ) ) ; } } else if ( typeIs ( blacklist , "table" ) ) { for ( const player of Players . GetPlayers ( ) ) { if ( blacklist . indexOf ( player ) === - 1 ) { this . instance . FireClient ( player , ... ( args as CallArgs ) ) ; } } } } param player param args public SendToPlayer ( player , ... args ) { if ( ! IS_RUNNING ) return ; this . instance . FireClient ( player , ... ( args as CallArgs ) ) ; } param players param args public SendToPlayers ( players , ... args ) { if ( ! IS_RUNNING ) return ; for ( const player of players ) { this . SendToPlayer ( player , ... args ) ; } } }