Interface ServerLoginContext
- All Superinterfaces:
ListenerContext
,ServerContext
public interface ServerLoginContext extends ServerContext
ServerNetworking.getLoginReceiver()
, in which a
login query
response packet
is received.
Since the response is always received after the query is sent even when the client
doesn't handle the channel of the query, isUnderstood()
must
be checked when handling in this context.
- See Also:
ServerNetworking.getLoginReceiver()
-
Method Summary
Modifier and Type Method Description net.minecraft.server.network.ServerLoginNetworkHandler
getListener()
Returns the packet listener that received this packet.PacketSender
getPacketSender()
Returns a packet sender that can send additional query request packets.boolean
isUnderstood()
Returns whether the original query request with the same query ID as this response was understood.void
waitFor(Future<?> future)
Allows blocking client log-in until thefuture
isdone
.Methods inherited from interface io.github.fablabsmc.fablabs.api.networking.v1.server.ServerContext
getEngine
-
Method Details
-
getListener
net.minecraft.server.network.ServerLoginNetworkHandler getListener()Returns the packet listener that received this packet.The packet listener offers access to the connection.
In a server login context, the network handler is always a
ServerLoginNetworkHandler
.- Specified by:
getListener
in interfaceListenerContext
- Returns:
- the packet listener
-
getPacketSender
PacketSender getPacketSender()Returns a packet sender that can send additional query request packets.If an upcoming query request packet cannot be immediately sent after receiving this packet, call
waitFor(Future)
, which will make sure the client is not admitted until the future is done.- Returns:
- the packet sender for query requests
-
isUnderstood
boolean isUnderstood()Returns whether the original query request with the same query ID as this response was understood.If the query response is not understood, an
empty packet byte buf
will be passed as thebuf
forChannelHandler.receive(io.github.fablabsmc.fablabs.api.networking.v1.ListenerContext, net.minecraft.network.PacketByteBuf)
.Since it is never guaranteed that a client can always understand query requests, this method should always be checked in packet reception.
- Returns:
- whether the query request was understood
-
waitFor
Allows blocking client log-in until thefuture
isdone
.Since packet reception happens on netty's event loops, this allows handlers to perform logic on the Server Thread, etc. For instance, a handler can prepare an upcoming query request or check necessary login data on the server thread.
Here is an example where the player log-in is blocked so that a credential check and building of a followup query request can be performed properly on the logical server thread before the player successfully logs in:
Usually it is enough to pass the return value forServerNetworking.getLoginReceiver().register(CHECK_CHANNEL, (context, buf) -> { if (!context.isUnderstood()) { handler.disconnect(new LiteralText("Only accept clients that can check!")); return; } String checkMessage = buf.readString(32767); ServerLoginNetworkHandler handler = context.getPacketListener(); PacketSender sender = context.getPacketSender(); MinecraftServer server = context.getEngine(); // Just send the CompletableFuture returned by the server's submit method context.waitFor(server.submit(() -> { LoginInfoChecker checker = LoginInfoChecker.get(server); if (!checker.check(handler.getConnectionInfo(), checkMessage)) { handler.disconnect(new LiteralText("Invalid credentials!")); return; } sender.send(UPCOMING_CHECK, checker.buildSecondQueryPacket(handler, checkMessage)); })); });
ThreadExecutor.submit(Runnable)
forfuture
.- Parameters:
future
- the future that must be done before the player can log in
-