From fab7e4a4618ba5c47401d8bb6b27301c1c1888f9 Mon Sep 17 00:00:00 2001 From: AnimeGitB Date: Wed, 17 Aug 2022 00:17:41 +0930 Subject: [PATCH] ChatSystem nitpick --- .../emu/grasscutter/game/chat/ChatSystem.java | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/main/java/emu/grasscutter/game/chat/ChatSystem.java b/src/main/java/emu/grasscutter/game/chat/ChatSystem.java index 7f039d96f..b7e26b9ff 100644 --- a/src/main/java/emu/grasscutter/game/chat/ChatSystem.java +++ b/src/main/java/emu/grasscutter/game/chat/ChatSystem.java @@ -51,34 +51,24 @@ public class ChatSystem implements ChatSystemHandler { * Chat history handling ********************/ private void putInHistory(int uid, int partnerId, ChatInfo info) { - if (!this.history.containsKey(uid)) { - this.history.put(uid, new HashMap<>()); - } - if (!this.history.get(uid).containsKey(partnerId)) { - this.history.get(uid).put(partnerId, new ArrayList<>()); - } - - this.history.get(uid).get(partnerId).add(info); + this.history.computeIfAbsent(uid, HashMap::new) + .computeIfAbsent(partnerId, ArrayList::new) + .add(info); } public void clearHistoryOnLogout(Player player) { - if (this.history.containsKey(player.getUid())) { - this.history.remove(player.getUid()); - } + this.history.remove(player.getUid()); } public void handlePullPrivateChatReq(Player player, int partnerId) { - if (this.history.getOrDefault(player.getUid(), Map.of()).containsKey(partnerId)) { - player.sendPacket(new PacketPullPrivateChatRsp(this.history.get(player.getUid()).get(partnerId))); - } - else { - player.sendPacket(new PacketPullPrivateChatRsp(List.of())); - } + var chatHistory = this.history.computeIfAbsent(player.getUid(), HashMap::new) + .computeIfAbsent(partnerId, ArrayList::new); + player.sendPacket(new PacketPullPrivateChatRsp(chatHistory)); } public void handlePullRecentChatReq(Player player) { // If this user has no chat history yet, create it by sending the server welcome messages. - if (!this.history.getOrDefault(player.getUid(), Map.of()).containsKey(GameConstants.SERVER_CONSOLE_UID)) { + if (!this.history.computeIfAbsent(player.getUid(), HashMap::new).containsKey(GameConstants.SERVER_CONSOLE_UID)) { this.sendServerWelcomeMessages(player); }