diff --git a/src/main/java/voruti/announceplayerserverswitch/AnnouncePlayerServerSwitch.java b/src/main/java/voruti/announceplayerserverswitch/AnnouncePlayerServerSwitch.java index 6329c7b..ec32b2a 100644 --- a/src/main/java/voruti/announceplayerserverswitch/AnnouncePlayerServerSwitch.java +++ b/src/main/java/voruti/announceplayerserverswitch/AnnouncePlayerServerSwitch.java @@ -15,6 +15,7 @@ import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; +import java.util.Objects; import java.util.Optional; @Plugin( @@ -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)); }); } @@ -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);