Skip to content

Commit

Permalink
refactor: refactor ClientStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Dec 15, 2024
1 parent e6ce235 commit 7476828
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,20 @@ default void disconnect() {
*/
ClientStatus getClientStatus();

/**
* Get the last client status of this player.
*
* @return the last client status of this player.
*/
ClientStatus getLastClientStatus();

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

/**
Expand All @@ -119,7 +126,7 @@ default boolean isConnected() {
* @return {@code true} if the player is logged in, {@code false} otherwise.
*/
default boolean isLoggedIn() {
return getClientStatus() == ClientStatus.LOGGED_IN;
return getClientStatus().ordinal() >= ClientStatus.LOGGED_IN.ordinal();
}

/**
Expand All @@ -130,7 +137,7 @@ default boolean isLoggedIn() {
* @return {@code true} if the player has been fully initialized, {@code false} otherwise.
*/
default boolean isInitialized() {
return getClientStatus() == ClientStatus.IN_GAME;
return getClientStatus().ordinal() >= ClientStatus.IN_GAME.ordinal();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions api/src/main/java/org/allaymc/api/network/ClientStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
*/
@AllArgsConstructor
public enum ClientStatus {
NEW(null),
CONNECTED(NEW),
// The previous status of DISCONNECTED can be CONNECTED, LOGGED_IN or IN_GAME
DISCONNECTED(null),
CONNECTED(DISCONNECTED),
LOGGED_IN(CONNECTED),
IN_GAME(LOGGED_IN),
DISCONNECTED(IN_GAME);
IN_GAME(LOGGED_IN);

/**
* The previous status of the client.
Expand All @@ -23,6 +23,6 @@ public enum ClientStatus {
private final ClientStatus previousStatus;

public boolean canHandlePackets() {
return this != NEW && this != DISCONNECTED;
return this != DISCONNECTED;
}
}
6 changes: 5 additions & 1 deletion server/src/main/java/org/allaymc/server/AllayServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.allaymc.api.i18n.TrKeys;
import org.allaymc.api.math.location.Location3f;
import org.allaymc.api.math.location.Location3fc;
import org.allaymc.api.network.ClientStatus;
import org.allaymc.api.network.NetworkInterface;
import org.allaymc.api.permission.DefaultPermissions;
import org.allaymc.api.permission.tree.PermissionTree;
Expand Down Expand Up @@ -290,10 +291,13 @@ public void onLoggedIn(EntityPlayer player) {
public void onDisconnect(EntityPlayer player) {
sendTr(TrKeys.A_NETWORK_CLIENT_DISCONNECTED, player.getClientSession().getSocketAddress().toString());

if (player.isSpawned()) {
// At this time the client have disconnected
if (player.getLastClientStatus().ordinal() >= ClientStatus.LOGGED_IN.ordinal()) {
broadcastTr(TextFormat.YELLOW + "%" + TrKeys.M_MULTIPLAYER_PLAYER_LEFT, player.getOriginName());
players.remove(player.getUUID());
}

if (player.getLastClientStatus().ordinal() >= ClientStatus.IN_GAME.ordinal()) {
player.getDimension().removePlayer(player);
playerStorage.savePlayerData(player);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public void setClientSession(BedrockServerSession session) {
var maxLoginTime = Server.SETTINGS.networkSettings().maxLoginTime();
if (maxLoginTime > 0) {
Server.getInstance().getScheduler().scheduleDelayed(Server.getInstance(), () -> {
if (packetProcessorHolder.getClientStatus().ordinal() < ClientStatus.IN_GAME.ordinal()) {
var status = packetProcessorHolder.getClientStatus();
if (status != ClientStatus.DISCONNECTED && status.ordinal() < ClientStatus.IN_GAME.ordinal()) {
log.warn("Session {} didn't log in within {} seconds, disconnecting...", clientSession.getSocketAddress().toString(), Server.SETTINGS.networkSettings().maxLoginTime() / 20d);
disconnect(TrKeys.M_DISCONNECTIONSCREEN_TIMEOUT);
}
Expand Down Expand Up @@ -259,6 +260,11 @@ public ClientStatus getClientStatus() {
return packetProcessorHolder.getClientStatus();
}

@Override
public ClientStatus getLastClientStatus() {
return packetProcessorHolder.getLastClientStatus();
}

protected void onDisconnect(String disconnectReason) {
thisPlayer.closeAllContainers();
((AllayServer) Server.getInstance()).onDisconnect(thisPlayer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.cloudburstmc.protocol.bedrock.packet.BedrockPacketType;

import java.util.EnumMap;
import java.util.concurrent.atomic.AtomicReference;

/**
* @author Cool_Loong
Expand All @@ -17,14 +16,12 @@
public final class PacketProcessorHolder {
private final EnumMap<ClientStatus, EnumMap<BedrockPacketType, PacketProcessor<BedrockPacket>>> processors;

private final AtomicReference<ClientStatus> clientStatus = new AtomicReference<>(ClientStatus.NEW);
private ClientStatus clientStatus = ClientStatus.DISCONNECTED;
private ClientStatus lastClientStatus = null;

public PacketProcessorHolder() {
this.processors = new EnumMap<>(ClientStatus.class);
for (ClientStatus status : ClientStatus.values()) {
if (status == ClientStatus.NEW) {
continue;
}
processors.put(status, new EnumMap<>(BedrockPacketType.class));
}

Expand All @@ -34,25 +31,34 @@ public PacketProcessorHolder() {
}

public PacketProcessor<BedrockPacket> getProcessor(BedrockPacket packet) {
var map = processors.get(clientStatus.get());
var map = processors.get(clientStatus);
return map != null ? map.get(packet.getPacketType()) : null;
}

public ClientStatus getClientStatus() {
return clientStatus.get();
return clientStatus;
}

public ClientStatus getLastClientStatus() {
return lastClientStatus;
}

public boolean setClientStatus(ClientStatus clientStatus) {
return setClientStatus(clientStatus, true);
}

public boolean setClientStatus(ClientStatus clientStatus, boolean warnIfFailed) {
if (!this.clientStatus.compareAndSet(clientStatus.getPreviousStatus(), clientStatus)) {
// We use lock here because this method won't be called frequently
// Instead, method getClientStatus() will be called frequently
public synchronized boolean setClientStatus(ClientStatus clientStatus, boolean warnIfFailed) {
// PreviousStatus != null means that we should check if the previous status is correct
if (clientStatus.getPreviousStatus() != null && this.clientStatus != clientStatus.getPreviousStatus()) {
if (warnIfFailed) {
log.warn("Failed to set client status to {}. Current is {}", clientStatus, this.clientStatus.get());
log.warn("Failed to set client status to {}. Current is {}", clientStatus, this.clientStatus);
}
return false;
}
this.lastClientStatus = this.clientStatus;
this.clientStatus = clientStatus;
return true;
}

Expand Down

0 comments on commit 7476828

Please sign in to comment.