From 10eed8e8cd69a961db6f971c9c74dbdb7e35a82a Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Tue, 29 Jun 2021 22:08:00 +0200 Subject: [PATCH] Improved chat organization and color codes for team and private chat. --- .../andrewlalis/aos_client/ChatManager.java | 15 ++++++++++++-- .../aos_client/view/GamePanel.java | 20 +++++++++---------- .../aos_core/net/chat/ChatMessage.java | 8 +++++++- .../aos_core/net/chat/ChatType.java | 8 ++++++++ .../aos_core/net/chat/CommandMessage.java | 7 ------- .../aos_core/net/chat/PlayerChatMessage.java | 4 ++-- .../aos_core/net/chat/SystemChatMessage.java | 2 +- .../andrewlalis/aos_server/ChatManager.java | 3 ++- .../command/chat/TeamChatCommand.java | 4 +++- 9 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 core/src/main/java/nl/andrewlalis/aos_core/net/chat/ChatType.java delete mode 100644 core/src/main/java/nl/andrewlalis/aos_core/net/chat/CommandMessage.java diff --git a/client/src/main/java/nl/andrewlalis/aos_client/ChatManager.java b/client/src/main/java/nl/andrewlalis/aos_client/ChatManager.java index 94f4d99..031da39 100644 --- a/client/src/main/java/nl/andrewlalis/aos_client/ChatManager.java +++ b/client/src/main/java/nl/andrewlalis/aos_client/ChatManager.java @@ -1,6 +1,7 @@ package nl.andrewlalis.aos_client; import nl.andrewlalis.aos_core.net.chat.ChatMessage; +import nl.andrewlalis.aos_core.net.chat.ChatType; import nl.andrewlalis.aos_core.net.chat.PlayerChatMessage; import nl.andrewlalis.aos_core.net.data.Sound; import nl.andrewlalis.aos_core.net.data.SoundType; @@ -8,6 +9,15 @@ import nl.andrewlalis.aos_core.net.data.SoundType; import java.util.LinkedList; import java.util.List; +/** + * This chat manager is responsible for storing the list of recent messages that + * the client has received, so that they can be displayed in the user interface. + *

+ * It is also responsible for managing the client's "chatting" state (if the + * client is chatting or not), and uses a provided {@link MessageTransceiver} + * to send chat messages to the server. + *

+ */ public class ChatManager { public static final int MAX_CHAT_MESSAGES = 10; @@ -34,7 +44,7 @@ public class ChatManager { public synchronized void addChatMessage(ChatMessage message) { this.chatMessages.add(message); - if (message.getClass() == PlayerChatMessage.class) { + if (message instanceof PlayerChatMessage) { this.soundManager.play(new Sound(null, 1.0f, SoundType.CHAT)); } while (this.chatMessages.size() > MAX_CHAT_MESSAGES) { @@ -43,6 +53,7 @@ public class ChatManager { } public ChatMessage[] getLatestChatMessages() { + if (this.chatMessages.isEmpty()) return new ChatMessage[0]; return this.chatMessages.toArray(new ChatMessage[0]); } @@ -70,7 +81,7 @@ public class ChatManager { public void sendChat() { String message = this.chatBuffer.toString().trim(); if (!message.isBlank() && !message.equals("/") && this.messageTransceiver != null) { - this.messageTransceiver.send(new ChatMessage(message)); + this.messageTransceiver.send(new ChatMessage(message, ChatType.PUBLIC_PLAYER_CHAT)); } this.setChatting(false); } diff --git a/client/src/main/java/nl/andrewlalis/aos_client/view/GamePanel.java b/client/src/main/java/nl/andrewlalis/aos_client/view/GamePanel.java index 7d193a7..c50b1cb 100644 --- a/client/src/main/java/nl/andrewlalis/aos_client/view/GamePanel.java +++ b/client/src/main/java/nl/andrewlalis/aos_client/view/GamePanel.java @@ -1,10 +1,11 @@ package nl.andrewlalis.aos_client.view; +import nl.andrewlalis.aos_client.ChatManager; import nl.andrewlalis.aos_client.Client; import nl.andrewlalis.aos_core.model.*; import nl.andrewlalis.aos_core.model.tools.Gun; import nl.andrewlalis.aos_core.net.chat.ChatMessage; -import nl.andrewlalis.aos_core.net.chat.PlayerChatMessage; +import nl.andrewlalis.aos_core.net.chat.ChatType; import nl.andrewlalis.aos_core.net.chat.SystemChatMessage; import javax.swing.*; @@ -51,7 +52,7 @@ public class GamePanel extends JPanel { drawWorld(g2, world); drawStatus(g2, world); } - drawChat(g2, world); + drawChat(g2); } private void drawWorld(Graphics2D g2, World world) { @@ -186,7 +187,7 @@ public class GamePanel extends JPanel { } } - private void drawChat(Graphics2D g2, World world) { + private void drawChat(Graphics2D g2) { int height = g2.getFontMetrics().getHeight(); int y = height; var cm = this.client.getChatManager(); @@ -201,13 +202,12 @@ public class GamePanel extends JPanel { } else if (sysMsg.getLevel() == SystemChatMessage.Level.SEVERE) { color = Color.RED; } - } else if (message instanceof PlayerChatMessage pcm) { - String author = Integer.toString(pcm.getPlayerId()); - if (world != null) { - Player p = world.getPlayers().get(pcm.getPlayerId()); - if (p != null) author = p.getName(); + } else { + if (message.getChatType() == ChatType.TEAM_PLAYER_CHAT) { + color = Color.GREEN; + } else if (message.getChatType() == ChatType.PRIVATE_PLAYER_CHAT) { + color = Color.CYAN; } - text = author + ": " + text; } g2.setColor(color); g2.drawString(text, 5, y); @@ -216,7 +216,7 @@ public class GamePanel extends JPanel { if (cm.isChatting()) { g2.setColor(Color.WHITE); - g2.drawString("> " + cm.getCurrentChatBuffer(), 5, height * 11); + g2.drawString("> " + cm.getCurrentChatBuffer(), 5, height * (ChatManager.MAX_CHAT_MESSAGES + 1)); } } diff --git a/core/src/main/java/nl/andrewlalis/aos_core/net/chat/ChatMessage.java b/core/src/main/java/nl/andrewlalis/aos_core/net/chat/ChatMessage.java index 8fc740b..fb9ac07 100644 --- a/core/src/main/java/nl/andrewlalis/aos_core/net/chat/ChatMessage.java +++ b/core/src/main/java/nl/andrewlalis/aos_core/net/chat/ChatMessage.java @@ -5,13 +5,19 @@ import nl.andrewlalis.aos_core.net.Type; public class ChatMessage extends Message { private final String text; + private final ChatType chatType; - public ChatMessage(String text) { + public ChatMessage(String text, ChatType chatType) { super(Type.CHAT); this.text = text; + this.chatType = chatType; } public String getText() { return text; } + + public ChatType getChatType() { + return chatType; + } } diff --git a/core/src/main/java/nl/andrewlalis/aos_core/net/chat/ChatType.java b/core/src/main/java/nl/andrewlalis/aos_core/net/chat/ChatType.java new file mode 100644 index 0000000..a466250 --- /dev/null +++ b/core/src/main/java/nl/andrewlalis/aos_core/net/chat/ChatType.java @@ -0,0 +1,8 @@ +package nl.andrewlalis.aos_core.net.chat; + +public enum ChatType { + PUBLIC_PLAYER_CHAT, + TEAM_PLAYER_CHAT, + PRIVATE_PLAYER_CHAT, + SYSTEM_MESSAGE +} diff --git a/core/src/main/java/nl/andrewlalis/aos_core/net/chat/CommandMessage.java b/core/src/main/java/nl/andrewlalis/aos_core/net/chat/CommandMessage.java deleted file mode 100644 index 7a4758a..0000000 --- a/core/src/main/java/nl/andrewlalis/aos_core/net/chat/CommandMessage.java +++ /dev/null @@ -1,7 +0,0 @@ -package nl.andrewlalis.aos_core.net.chat; - -public class CommandMessage extends PlayerChatMessage { - public CommandMessage(int id, String text) { - super(id, text); - } -} diff --git a/core/src/main/java/nl/andrewlalis/aos_core/net/chat/PlayerChatMessage.java b/core/src/main/java/nl/andrewlalis/aos_core/net/chat/PlayerChatMessage.java index 67acafe..c328549 100644 --- a/core/src/main/java/nl/andrewlalis/aos_core/net/chat/PlayerChatMessage.java +++ b/core/src/main/java/nl/andrewlalis/aos_core/net/chat/PlayerChatMessage.java @@ -3,8 +3,8 @@ package nl.andrewlalis.aos_core.net.chat; public class PlayerChatMessage extends ChatMessage { private final int playerId; - public PlayerChatMessage(int id, String text) { - super(text); + public PlayerChatMessage(int id, String text, ChatType chatType) { + super(text, chatType); this.playerId = id; } diff --git a/core/src/main/java/nl/andrewlalis/aos_core/net/chat/SystemChatMessage.java b/core/src/main/java/nl/andrewlalis/aos_core/net/chat/SystemChatMessage.java index 4e4ece8..18101a9 100644 --- a/core/src/main/java/nl/andrewlalis/aos_core/net/chat/SystemChatMessage.java +++ b/core/src/main/java/nl/andrewlalis/aos_core/net/chat/SystemChatMessage.java @@ -6,7 +6,7 @@ public class SystemChatMessage extends ChatMessage { private final Level level; public SystemChatMessage(Level level, String text) { - super(text); + super(text, ChatType.SYSTEM_MESSAGE); this.level = level; } diff --git a/server/src/main/java/nl/andrewlalis/aos_server/ChatManager.java b/server/src/main/java/nl/andrewlalis/aos_server/ChatManager.java index b6ec4ab..15a424c 100644 --- a/server/src/main/java/nl/andrewlalis/aos_server/ChatManager.java +++ b/server/src/main/java/nl/andrewlalis/aos_server/ChatManager.java @@ -2,6 +2,7 @@ package nl.andrewlalis.aos_server; import nl.andrewlalis.aos_core.model.Player; import nl.andrewlalis.aos_core.net.chat.ChatMessage; +import nl.andrewlalis.aos_core.net.chat.ChatType; import nl.andrewlalis.aos_core.net.chat.PlayerChatMessage; import nl.andrewlalis.aos_server.command.GunsCommand; import nl.andrewlalis.aos_server.command.ResetCommand; @@ -44,7 +45,7 @@ public class ChatManager { cmd.execute(handler, player, Arrays.copyOfRange(words, 1, words.length)); } } else { - this.server.broadcastMessage(new PlayerChatMessage(player.getId(), msg.getText())); + this.server.broadcastMessage(new PlayerChatMessage(player.getId(), player.getName() + ": "+ msg.getText(), ChatType.PUBLIC_PLAYER_CHAT)); } } } diff --git a/server/src/main/java/nl/andrewlalis/aos_server/command/chat/TeamChatCommand.java b/server/src/main/java/nl/andrewlalis/aos_server/command/chat/TeamChatCommand.java index 998991c..c19e40d 100644 --- a/server/src/main/java/nl/andrewlalis/aos_server/command/chat/TeamChatCommand.java +++ b/server/src/main/java/nl/andrewlalis/aos_server/command/chat/TeamChatCommand.java @@ -1,6 +1,7 @@ package nl.andrewlalis.aos_server.command.chat; import nl.andrewlalis.aos_core.model.Player; +import nl.andrewlalis.aos_core.net.chat.ChatType; import nl.andrewlalis.aos_core.net.chat.PlayerChatMessage; import nl.andrewlalis.aos_core.net.chat.SystemChatMessage; import nl.andrewlalis.aos_server.ClientHandler; @@ -15,6 +16,7 @@ public class TeamChatCommand implements ChatCommand { handler.send(new SystemChatMessage(SystemChatMessage.Level.WARNING, "You're not in a team, so you can't send team chat messages.")); return; } - handler.getServer().sendTeamMessage(player.getTeam(), new PlayerChatMessage(player.getId(), String.join(" ", args))); + var msg = new PlayerChatMessage(player.getId(), player.getName() + ": " + String.join(" ", args), ChatType.TEAM_PLAYER_CHAT); + handler.getServer().sendTeamMessage(player.getTeam(), msg); } }