Class CrowdControl
- java.lang.Object
-
- dev.qixils.crowdcontrol.CrowdControl
-
- All Implemented Interfaces:
SocketManager
public final class CrowdControl extends Object implements SocketManager
API for interacting with Crowd Control via theSimpleTCPConnector
orSimpleTCPClientConnector
.Creating an instance
To create an instance of this class, use either the
client()
orserver()
methods.-
client()
creates a client instance that can be used to connect to a central Crowd Control server. It corresponds with theSimpleTCPConnector
in your project's .cs file. In this mode, your project will only be able to connect to one streamer, although multiple instances of your project will all be able to connect to this streamer at the same time. This mode is ideal for singleplayer games. -
server()
creates a server instance that can be used to connect to multiple Crowd Control clients. It corresponds with theSimpleTCPClientConnector
. This allows you to receive requests from multiple streamers, making this ideal for multiplayer games.
You should only ever create one instance of this class. If you must recreate this instance, remember to call
shutdown(String)
first.Registering effect handlers
Effect handlers process incoming effect
Request
s, apply them to players or environments as appropriate, and then return aResponse
that informs the client of whether the effect was successfully applied.To start handling incoming effects, you much register handlers for each effect you want to handle using one of
registerHandler(String, Consumer)
,registerHandler(String, Function)
, orregisterHandlers(Object)
.Registering checks
Checks allow you to block an effect from being processed if certain conditions are not met. This is typically used to ensure effects are not run before the game has loaded into a world. You can register checks using
registerCheck(Supplier)
orregisterCheck(Function)
.
-
-
Constructor Summary
Constructors Constructor Description CrowdControl(@Nullable String IP, int port, @Nullable String password, @NotNull Function<@NotNull CrowdControl,@NotNull SocketManager> socketManagerCreator)
Creates a new Crowd Control client or server instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static CrowdControlClientBuilder
client()
Returns a builder for a newCrowdControl
instance operating in client mode.@Nullable String
getIP()
Returns the IP that theSocketManager
will listen on.@Nullable String
getPassword()
Returns the password required for clients to connect to this server as a SHA-512 encrypted, Base64-encoded string.int
getPort()
Returns the port that theSocketManager
will listen on.void
handle(@NotNull Request request)
Handles an incomingRequest
by executing the relevant handler.static boolean
isCause(@NotNull Class<? extends Throwable> potentialCause, @Nullable Throwable exception)
Helper method for determining if a provided exception class is part of an exception's stacktrace.void
registerCheck(@NotNull Function<Request,CheckResult> check)
Registers a check which will be called for every incomingRequest
.void
registerCheck(@NotNull Supplier<CheckResult> check)
Registers a check which will be called for every incomingRequest
.void
registerHandler(@NotNull String effect, @NotNull Consumer<Request> handler)
Registers an effect handler which does not immediately return aResponse
.void
registerHandler(@NotNull String effect, @NotNull Function<Request,Response> handler)
Registers a function to handle an effect.void
registerHandlers(@NotNull Object object)
Registers method handlers within a class.static CrowdControlServerBuilder
server()
Returns a builder for a newCrowdControl
instance operating in server mode.void
shutdown()
Deprecated.providing error messages viashutdown(String)
is recommendedvoid
shutdown(@Nullable Request cause, @Nullable String reason)
Shuts down the internal connection to the Crowd Control server and sends a corresponding error message to the streamer(s).void
shutdown(@Nullable String reason)
Shuts down the internal connection to the Crowd Control server and sends a corresponding error message to the streamer(s).
-
-
-
Constructor Detail
-
CrowdControl
public CrowdControl(@Nullable @Nullable String IP, int port, @Nullable @Nullable String password, @NotNull @NotNull Function<@NotNull CrowdControl,@NotNull SocketManager> socketManagerCreator)
Creates a new Crowd Control client or server instance.You should generally be using
server()
orclient()
instead. This constructor's parameters are prone to changes.- Parameters:
IP
- IP address to connect to (if applicable)port
- port to listen on or connect topassword
- password required to connect (if applicable)socketManagerCreator
- creator of a newSocketManager
-
-
Method Detail
-
isCause
public static boolean isCause(@NotNull @NotNull Class<? extends Throwable> potentialCause, @Nullable @Nullable Throwable exception)
Helper method for determining if a provided exception class is part of an exception's stacktrace.- Parameters:
potentialCause
- class to search for in stacktraceexception
- exception to be searched- Returns:
- true if the exception class is found
-
client
public static CrowdControlClientBuilder client()
Returns a builder for a newCrowdControl
instance operating in client mode. It will connect to a singular Crowd Control server instance.- Returns:
- a new client builder
-
server
public static CrowdControlServerBuilder server()
Returns a builder for a newCrowdControl
instance operating in server mode. It will allow numerous Crowd Control clients to connect.- Returns:
- a new server builder
-
getIP
@Nullable @CheckReturnValue public @Nullable String getIP()
Returns the IP that theSocketManager
will listen on. If running in server mode, this will be null.- Returns:
- IP if available
-
getPort
@CheckReturnValue public int getPort()
Returns the port that theSocketManager
will listen on.- Returns:
- IP port
-
getPassword
@Nullable @CheckReturnValue public @Nullable String getPassword()
Returns the password required for clients to connect to this server as a SHA-512 encrypted, Base64-encoded string. If running in client mode, this will be null.- Returns:
- password required to connect
-
registerHandlers
public void registerHandlers(@NotNull @NotNull Object object)
Registers method handlers within a class. These methods must:- be public
- have the
Subscribe
annotation with a non-null effect name - only one parameter, which has the type
Request
- a return type of one of the following:
Response
Response.Builder
- Void (assumes you will call
Response.send()
yourself)
- Parameters:
object
- class instance to register
-
registerHandler
public void registerHandler(@NotNull @NotNull String effect, @NotNull @NotNull Function<Request,Response> handler)
Registers a function to handle an effect.- Parameters:
effect
- name of the effect to handlehandler
- function to handle the effect- See Also:
registerHandler(String, Consumer)
-
registerHandler
public void registerHandler(@NotNull @NotNull String effect, @NotNull @NotNull Consumer<Request> handler)
Registers an effect handler which does not immediately return aResponse
. It is expected to callResponse.send()
on its own.- Parameters:
effect
- name of the effect to handlehandler
- function to handle the effect- See Also:
registerHandler(String, Function)
-
registerCheck
public void registerCheck(@NotNull @NotNull Function<Request,CheckResult> check)
Registers a check which will be called for every incomingRequest
. A resulting value ofCheckResult.DISALLOW
will result in anFAILURE
response packet.This is used for validating that your service is accepting requests, and should return
CheckResult.DISALLOW
if, for example, the game has not fully initialized or no players are connected.- Parameters:
check
- global check to register
-
registerCheck
public void registerCheck(@NotNull @NotNull Supplier<CheckResult> check)
Registers a check which will be called for every incomingRequest
. A resulting value ofCheckResult.DISALLOW
will result in anFAILURE
response packet.This is used for validating that your service is accepting requests, and should return
CheckResult.DISALLOW
if, for example, the game has not fully initialized or no players are connected.- Parameters:
check
- global check to register
-
handle
public void handle(@NotNull @NotNull Request request)
Handles an incomingRequest
by executing the relevant handler.- Parameters:
request
- an incoming request
-
shutdown
@Deprecated public void shutdown()
Deprecated.providing error messages viashutdown(String)
is recommendedShuts down the internal connection to the Crowd Control server.- Specified by:
shutdown
in interfaceSocketManager
- See Also:
shutdown(String)
,shutdown(Request, String)
-
shutdown
public void shutdown(@Nullable @Nullable String reason)
Shuts down the internal connection to the Crowd Control server and sends a corresponding error message to the streamer(s).- Specified by:
shutdown
in interfaceSocketManager
- Parameters:
reason
- the reason for shutting down
-
shutdown
public void shutdown(@Nullable @Nullable Request cause, @Nullable @Nullable String reason)
Shuts down the internal connection to the Crowd Control server and sends a corresponding error message to the streamer(s).- Specified by:
shutdown
in interfaceSocketManager
- Parameters:
cause
- cause for shutting downreason
- the reason for shutting down
-
-