Skip to content

Commit

Permalink
Add disconnect message
Browse files Browse the repository at this point in the history
  • Loading branch information
voruti committed Jan 27, 2023
1 parent b8ae575 commit c0f1ff4
Showing 1 changed file with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

import java.util.Objects;
import java.util.Optional;

@Plugin(
Expand Down Expand Up @@ -52,7 +53,7 @@ public EventTask onServerPostConnect(ServerPostConnectEvent event) {
.map(registeredServer -> registeredServer.getServerInfo().getName())
.orElse(null);

this.sendMessage(this.generateMessage(playerName, serverName, previousServerName));
this.sendMessage(this.generateInwardsMessage(playerName, serverName, previousServerName));
});
}

Expand All @@ -64,27 +65,61 @@ public EventTask onDisconnect(DisconnectEvent event) {
String playerName = event.getPlayer().getGameProfile().getName();
String previousServerName = event.getPlayer().getCurrentServer()
.map(serverConnection -> serverConnection.getServerInfo().getName())
.orElse("unknown");
.orElse(null);

this.sendMessage(this.generateMessage(playerName, null, previousServerName));
this.sendMessage(this.generateOutwardsMessage(playerName, previousServerName));
});
}


private String generateMessage(@NotNull String playerName, @Nullable String serverName,
@Nullable String previousServerName) {
private String generateOutwardsMessage(@NotNull String playerName, @Nullable String previousServerName) {
logger.trace("generateMessage playerName: {}, previousServerName: {}", playerName, previousServerName);

if (Objects.isNull(previousServerName) || previousServerName.isBlank()) {
return this.generateDisconnectMessage(playerName);
} else {
return this.generateLeaveMessage(playerName, previousServerName);
}
}

private String generateInwardsMessage(@NotNull String playerName, @NotNull String serverName,
@Nullable String previousServerName) {
logger.trace("generateMessage playerName: {}, serverName: {}, previousServerName: {}",
playerName, serverName, previousServerName);

if (previousServerName == null || previousServerName.isEmpty()) {
return String.format("%s joined %s", playerName, serverName);
} else if (serverName == null || serverName.isEmpty()) {
return String.format("%s left %s", playerName, previousServerName);
if (Objects.isNull(previousServerName) || previousServerName.isBlank()) {
return this.generateJoinMessage(playerName, serverName);
} else {
return String.format("%s moved %s -> %s", playerName, previousServerName, serverName);
return this.generateMoveMessage(playerName, serverName, previousServerName);
}
}

private String generateMoveMessage(@NotNull String playerName, @NotNull String serverName,
@NotNull String previousServerName) {
logger.trace("generateMessage playerName: {}, serverName: {}, previousServerName: {}",
playerName, serverName, previousServerName);

return String.format("%s moved %s -> %s", playerName, previousServerName, serverName);
}

private String generateDisconnectMessage(@NotNull String playerName) {
logger.trace("generateJoinMessage playerName: {}", playerName);

return String.format("%s disconnected", playerName);
}

private String generateLeaveMessage(@NotNull String playerName, @NotNull String previousServerName) {
logger.trace("generateJoinMessage playerName: {}, previousServerName: {}", playerName, previousServerName);

return String.format("%s left %s", playerName, previousServerName);
}

private String generateJoinMessage(@NotNull String playerName, @NotNull String serverName) {
logger.trace("generateJoinMessage playerName: {}, serverName: {}", playerName, serverName);

return String.format("%s joined %s", playerName, serverName);
}

private void sendMessage(String message) {
logger.trace("sendMessage \"{}\"", message);

Expand Down

0 comments on commit c0f1ff4

Please sign in to comment.