Skip to content

Commit

Permalink
feat: introduce client status for packet handling
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Dec 5, 2024
1 parent fd61b84 commit 8c55f3d
Show file tree
Hide file tree
Showing 42 changed files with 339 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.allaymc.api.i18n.LangCode;
import org.allaymc.api.i18n.MayContainTrKey;
import org.allaymc.api.i18n.TrKeys;
import org.allaymc.api.network.ClientStatus;
import org.allaymc.api.network.PacketReceiver;
import org.cloudburstmc.protocol.bedrock.BedrockServerSession;

Expand Down Expand Up @@ -97,25 +98,29 @@ default void disconnect() {
void disconnect(@MayContainTrKey String reason);

/**
* Check if the player is disconnected.
* Get the client status of this player.
*
* @return {@code true} if the player is disconnected, {@code false} otherwise.
* @return the client status of this player.
*/
boolean isDisconnected();
ClientStatus getClientStatus();

/**
* Check if network encryption is enabled for the player.
* Check if the player is connected.
*
* @return {@code true} if network encryption is enabled, {@code false} otherwise.
* @return {@code true} if the player is connected, {@code false} otherwise.
*/
boolean isNetworkEncryptionEnabled();
default boolean isConnected() {
return getClientStatus() == ClientStatus.CONNECTED;
}

/**
* Get the encryption secret key for the player.
* Check if the player is logged in.
*
* @return the encryption secret key for the player.
* @return {@code true} if the player is logged in, {@code false} otherwise.
*/
SecretKey getEncryptionSecretKey();
default boolean isLoggedIn() {
return getClientStatus() == ClientStatus.LOGGED_IN;
}

/**
* Check if the player has been fully initialized.
Expand All @@ -124,14 +129,32 @@ default void disconnect() {
*
* @return {@code true} if the player has been fully initialized, {@code false} otherwise.
*/
boolean isInitialized();
default boolean isInitialized() {
return getClientStatus() == ClientStatus.IN_GAME;
}

/**
* Check if the player is logged in.
* Check if the player is disconnected.
*
* @return {@code true} if the player is logged in, {@code false} otherwise.
* @return {@code true} if the player is disconnected, {@code false} otherwise.
*/
default boolean isDisconnected() {
return getClientStatus() == ClientStatus.DISCONNECTED;
}

/**
* Check if network encryption is enabled for the player.
*
* @return {@code true} if network encryption is enabled, {@code false} otherwise.
*/
boolean isLoggedIn();
boolean isNetworkEncryptionEnabled();

/**
* Get the encryption secret key for the player.
*
* @return the encryption secret key for the player.
*/
SecretKey getEncryptionSecretKey();

/**
* Get the client session for the player.
Expand Down
24 changes: 24 additions & 0 deletions api/src/main/java/org/allaymc/api/network/ClientStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.allaymc.api.network;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* Represents the status of a client.
*
* @author daoge_cmd
*/
@AllArgsConstructor
public enum ClientStatus {
NEW(null),
CONNECTED(NEW),
LOGGED_IN(CONNECTED),
IN_GAME(LOGGED_IN),
DISCONNECTED(IN_GAME);

/**
* The previous status of the client.
*/
@Getter
private final ClientStatus previousStatus;
}
Loading

0 comments on commit 8c55f3d

Please sign in to comment.